View Javadoc

1   // Copyright (C) 2005 - 2009 Philip Aston
2   // Copyright (C) 2007 Venelin Mitov
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.tools.tcpproxy;
24  
25  import java.awt.Component;
26  import java.awt.event.WindowEvent;
27  import javax.swing.JButton;
28  
29  import net.grinder.testutility.RandomStubFactory;
30  
31  import junit.framework.TestCase;
32  
33  
34  /**
35   * Unit test case for {@link TCPProxyConsole}.
36   *
37   * @author Philip Aston
38   */
39  public class TestTCPProxyConsole extends TestCase {
40  
41    public void testConstructor() throws Exception {
42      final RandomStubFactory<TCPProxyEngine> engineStubFactory =
43        RandomStubFactory.create(TCPProxyEngine.class);
44  
45      final UpdatableCommentSource commentSource =
46        new CommentSourceImplementation();
47  
48      final TCPProxyConsole console =
49        new TCPProxyConsole(engineStubFactory.getStub(), commentSource);
50  
51      console.dispose();
52  
53      engineStubFactory.assertNoMoreCalls();
54    }
55  
56    public void testButton() throws Exception {
57      final RandomStubFactory<TCPProxyEngine> engineStubFactory =
58        RandomStubFactory.create(TCPProxyEngine.class);
59  
60      final UpdatableCommentSource commentSource =
61        new CommentSourceImplementation();
62  
63      final TCPProxyConsole console =
64        new TCPProxyConsole(engineStubFactory.getStub(), commentSource);
65  
66      JButton stopButton = null;
67  
68      final Component[] components = console.getContentPane().getComponents();
69      for (int i=0; i<components.length; ++i) {
70        if (components[i] instanceof JButton) {
71          final JButton b = (JButton)components[i];
72          if ("Stop".equals(b.getText())) {
73            stopButton = (JButton)components[i];
74          }
75        }
76      }
77  
78      assertNotNull(stopButton);
79  
80      if (stopButton != null) { // Shut up eclipse null warning.
81        stopButton.doClick();
82      }
83      engineStubFactory.assertSuccess("stop");
84      engineStubFactory.assertNoMoreCalls();
85  
86      console.dispatchEvent(new WindowEvent(console, WindowEvent.WINDOW_CLOSING));
87      engineStubFactory.assertSuccess("stop");
88      engineStubFactory.assertNoMoreCalls();
89  
90      console.dispose();
91  
92      engineStubFactory.assertNoMoreCalls();
93  
94  
95    }
96  }