View Javadoc

1   // Copyright (C) 2006 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.client;
23  
24  import java.io.Serializable;
25  
26  import net.grinder.common.GrinderProperties;
27  import net.grinder.communication.BlockingSender;
28  import net.grinder.communication.CommunicationException;
29  import net.grinder.console.communication.server.messages.GetNumberOfAgentsMessage;
30  import net.grinder.console.communication.server.messages.ResetRecordingMessage;
31  import net.grinder.console.communication.server.messages.ResetWorkerProcessesMessage;
32  import net.grinder.console.communication.server.messages.ResultMessage;
33  import net.grinder.console.communication.server.messages.StartRecordingMessage;
34  import net.grinder.console.communication.server.messages.StartWorkerProcessesMessage;
35  import net.grinder.console.communication.server.messages.StopAgentAndWorkerProcessesMessage;
36  import net.grinder.console.communication.server.messages.StopRecordingMessage;
37  import net.grinder.testutility.CallData;
38  import net.grinder.testutility.RandomStubFactory;
39  import junit.framework.TestCase;
40  
41  
42  /**
43   * Unit tests for ConsoleConnectionImplementation.
44   *
45   * @author Philip Aston
46   */
47  public class TestConsoleConnectionImplementation extends TestCase {
48  
49    private final RandomStubFactory<BlockingSender> m_senderStubFactory =
50      RandomStubFactory.create(BlockingSender.class);
51    private final BlockingSender m_sender = m_senderStubFactory.getStub();
52  
53    public void testRecordingControls() throws Exception {
54      final ConsoleConnection consoleConnection =
55        new ConsoleConnectionImplementation(m_sender);
56  
57      consoleConnection.startRecording();
58      m_senderStubFactory.assertSuccess("blockingSend",
59                                        StartRecordingMessage.class);
60      m_senderStubFactory.assertNoMoreCalls();
61  
62      consoleConnection.stopRecording();
63      m_senderStubFactory.assertSuccess("blockingSend",
64                                        StopRecordingMessage.class);
65      m_senderStubFactory.assertNoMoreCalls();
66  
67      consoleConnection.resetRecording();
68      m_senderStubFactory.assertSuccess("blockingSend",
69                                        ResetRecordingMessage.class);
70      m_senderStubFactory.assertNoMoreCalls();
71  
72      final CommunicationException communicationException =
73        new CommunicationException("");
74      m_senderStubFactory.setThrows("blockingSend", communicationException);
75  
76      try {
77        consoleConnection.resetRecording();
78        fail("Expected ConsoleConnectionException");
79      }
80      catch (ConsoleConnectionException e) {
81        assertSame(communicationException, e.getCause());
82      }
83  
84      try {
85        consoleConnection.stopRecording();
86        fail("Expected ConsoleConnectionException");
87      }
88      catch (ConsoleConnectionException e) {
89        assertSame(communicationException, e.getCause());
90      }
91  
92      try {
93        consoleConnection.startRecording();
94        fail("Expected ConsoleConnectionException");
95      }
96      catch (ConsoleConnectionException e) {
97        assertSame(communicationException, e.getCause());
98      }
99  
100     consoleConnection.close();
101   }
102 
103   public void testProcessMessages() throws Exception {
104     final ConsoleConnection consoleConnection =
105       new ConsoleConnectionImplementation(m_sender);
106 
107     m_senderStubFactory.setResult(
108       "blockingSend", new ResultMessage(new Integer(10)));
109     assertEquals(10, consoleConnection.getNumberOfAgents());
110     m_senderStubFactory.assertSuccess("blockingSend",
111       GetNumberOfAgentsMessage.class);
112     m_senderStubFactory.assertNoMoreCalls();
113 
114     final GrinderProperties properties = new GrinderProperties();
115     consoleConnection.startWorkerProcesses(properties);
116     final CallData data =
117       m_senderStubFactory.assertSuccess(
118         "blockingSend", StartWorkerProcessesMessage.class);
119     assertSame(properties,
120       ((StartWorkerProcessesMessage)data.getParameters()[0]).getProperties());
121     m_senderStubFactory.assertNoMoreCalls();
122 
123     consoleConnection.resetWorkerProcesses();
124     m_senderStubFactory.assertSuccess(
125       "blockingSend", ResetWorkerProcessesMessage.class);
126     m_senderStubFactory.assertNoMoreCalls();
127 
128     consoleConnection.stopAgents();
129     m_senderStubFactory.assertSuccess(
130       "blockingSend", StopAgentAndWorkerProcessesMessage.class);
131     m_senderStubFactory.assertNoMoreCalls();
132 
133     m_senderStubFactory.setResult("blockingSend", null);
134 
135     try {
136       consoleConnection.getNumberOfAgents();
137       fail("Expected ConsoleConnectionException");
138     }
139     catch (ConsoleConnectionException e) {
140     }
141 
142     m_senderStubFactory.setResult(
143       "blockingSend", new ResultMessage(new Serializable() {}));
144 
145     try {
146       consoleConnection.getNumberOfAgents();
147       fail("Expected ConsoleConnectionException");
148     }
149     catch (ConsoleConnectionException e) {
150     }
151 
152     final CommunicationException communicationException =
153       new CommunicationException("");
154     m_senderStubFactory.setThrows("blockingSend", communicationException);
155 
156     try {
157       consoleConnection.getNumberOfAgents();
158       fail("Expected ConsoleConnectionException");
159     }
160     catch (ConsoleConnectionException e) {
161       assertSame(communicationException, e.getCause());
162     }
163 
164     try {
165       consoleConnection.startWorkerProcesses(new GrinderProperties());
166       fail("Expected ConsoleConnectionException");
167     }
168     catch (ConsoleConnectionException e) {
169       assertSame(communicationException, e.getCause());
170     }
171 
172     try {
173       consoleConnection.resetWorkerProcesses();
174       fail("Expected ConsoleConnectionException");
175     }
176     catch (ConsoleConnectionException e) {
177       assertSame(communicationException, e.getCause());
178     }
179 
180     try {
181       consoleConnection.stopAgents();
182       fail("Expected ConsoleConnectionException");
183     }
184     catch (ConsoleConnectionException e) {
185       assertSame(communicationException, e.getCause());
186     }
187 
188     consoleConnection.close();
189   }
190 }