View Javadoc

1   // Copyright (C) 2000 - 2012 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.messages.console;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.fail;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.IOException;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  import java.util.Collection;
33  import java.util.HashSet;
34  
35  import net.grinder.common.processidentity.WorkerIdentity;
36  import net.grinder.common.processidentity.ProcessReport.State;
37  import net.grinder.communication.Address;
38  import net.grinder.communication.CommunicationException;
39  import net.grinder.engine.agent.StubAgentIdentity;
40  import net.grinder.messages.agent.CacheHighWaterMark;
41  import net.grinder.messages.agent.StubCacheHighWaterMark;
42  import net.grinder.statistics.ExpressionView;
43  import net.grinder.statistics.StatisticExpressionFactory;
44  import net.grinder.statistics.StatisticsServicesImplementation;
45  import net.grinder.statistics.StatisticsSetFactory;
46  import net.grinder.statistics.TestStatisticsMap;
47  import net.grinder.testutility.Serializer;
48  
49  import org.junit.Test;
50  
51  
52  /**
53   *  Unit test case for console messages.
54   *
55   * @author Philip Aston
56   */
57  public class TestConsoleMessages {
58  
59    @Test public void testRegisterStatisticsViewMessage() throws Exception {
60  
61      final StatisticExpressionFactory statisticExpressionFactory =
62        StatisticsServicesImplementation.getInstance()
63        .getStatisticExpressionFactory();
64  
65      final ExpressionView expressionView =
66        statisticExpressionFactory.createExpressionView(
67          "One", "userLong0", false);
68  
69      final RegisterExpressionViewMessage original =
70        new RegisterExpressionViewMessage(expressionView);
71  
72      final RegisterExpressionViewMessage received =
73        Serializer.serialize(original);
74  
75      assertEquals(original.getExpressionView(),
76                   received.getExpressionView());
77  
78      final ExpressionView view2 =
79        statisticExpressionFactory
80          .createExpressionView("My view2",
81            statisticExpressionFactory.createExpression("userLong0"));
82      try {
83        Serializer.serialize(new RegisterExpressionViewMessage(view2));
84        fail("Expected IOException");
85      }
86      catch (IOException e) {
87      }
88  
89      final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
90      final ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
91      original.writeExternal(objectStream);
92      objectStream.close();
93  
94      // Corrupt the serialised message.
95      final byte[] bytes = byteStream.toByteArray();
96      bytes[bytes.length - 5] = 'X';
97  
98      final RegisterExpressionViewMessage incomingMessage =
99        new RegisterExpressionViewMessage();
100     try {
101       incomingMessage.readExternal(
102         new ObjectInputStream(new ByteArrayInputStream(bytes)));
103 
104       fail("Expected IOException");
105     } catch (IOException e) {
106     }
107   }
108 
109   @Test public void testRegisterTestsMessage() throws Exception {
110 
111     final Collection<net.grinder.common.Test> c =
112       new HashSet<net.grinder.common.Test>();
113 
114     final RegisterTestsMessage original = new RegisterTestsMessage(c);
115 
116     assertEquals(c, original.getTests());
117 
118     final RegisterTestsMessage received = Serializer.serialize(original);
119 
120     assertEquals(original.getTests(), received.getTests());
121   }
122 
123   @Test public void testReportStatisticsMessage() throws Exception {
124 
125     final StatisticsSetFactory statisticsSetFactory =
126       StatisticsServicesImplementation.getInstance().getStatisticsSetFactory();
127     final TestStatisticsMap statisticsDelta =
128       new TestStatisticsMap(statisticsSetFactory);
129 
130     final ReportStatisticsMessage original =
131       new ReportStatisticsMessage(statisticsDelta);
132 
133     assertEquals(statisticsDelta, original.getStatisticsDelta());
134 
135     final ReportStatisticsMessage received = Serializer.serialize(original);
136 
137     assertEquals(original.getStatisticsDelta(), received.getStatisticsDelta());
138   }
139 
140   @Test public void testWorkerReportMessage() throws Exception {
141 
142     final StubAgentIdentity agentIdentity =
143       new StubAgentIdentity("Agent");
144     final WorkerIdentity workerIdentity = agentIdentity.createWorkerIdentity();
145 
146     final WorkerProcessReportMessage original =
147       new WorkerProcessReportMessage(State.RUNNING, (short)2, (short)3);
148 
149     final WorkerAddress address = new WorkerAddress(workerIdentity);
150     original.setAddress(address);
151 
152     assertEquals(workerIdentity, original.getWorkerIdentity());
153     assertEquals(address, original.getProcessAddress());
154     assertEquals(State.RUNNING, original.getState());
155     assertEquals(2, original.getNumberOfRunningThreads());
156     assertEquals(3, original.getMaximumNumberOfThreads());
157 
158     final WorkerProcessReportMessage received = Serializer.serialize(original);
159 
160     assertEquals(workerIdentity, original.getWorkerIdentity());
161     assertEquals(address, original.getProcessAddress());
162     assertEquals(State.RUNNING, received.getState());
163     assertEquals(2, received.getNumberOfRunningThreads());
164     assertEquals(3, received.getMaximumNumberOfThreads());
165   }
166 
167   @Test public void testWorkerReportMessageBadAddress() throws Exception {
168 
169     final WorkerProcessReportMessage message =
170       new WorkerProcessReportMessage(State.RUNNING, (short)2, (short)3);
171 
172     final Address badAddress =
173       new AgentAddress(new StubAgentIdentity("Agent"));
174 
175     try {
176       message.setAddress(badAddress);
177       fail("Expected CommunicationException");
178     }
179     catch (CommunicationException e) {
180     }
181   }
182 
183   @Test public void testAgentReportMessage() throws Exception {
184 
185     final StubAgentIdentity agentIdentity =
186       new StubAgentIdentity("Agent");
187 
188     final CacheHighWaterMark cacheHighWaterMark =
189       new StubCacheHighWaterMark("", 100);
190 
191     final AgentProcessReportMessage original =
192       new AgentProcessReportMessage(State.RUNNING, cacheHighWaterMark);
193 
194     final AgentAddress address = new AgentAddress(agentIdentity);
195     original.setAddress(address);
196 
197     assertEquals(agentIdentity, original.getAgentIdentity());
198     assertEquals(address, original.getProcessAddress());
199     assertEquals(cacheHighWaterMark, original.getCacheHighWaterMark());
200     assertEquals(State.RUNNING, original.getState());
201 
202     final AgentProcessReportMessage received = Serializer.serialize(original);
203 
204     assertEquals(agentIdentity, original.getAgentIdentity());
205     assertEquals(address, original.getProcessAddress());
206     assertEquals(State.RUNNING, received.getState());
207     assertEquals(cacheHighWaterMark, received.getCacheHighWaterMark());
208   }
209 
210   @Test public void testAgentReportMessageBadAddress() throws Exception {
211 
212     final AgentProcessReportMessage message =
213       new AgentProcessReportMessage(State.RUNNING, null);
214 
215     final Address badAddress =
216       new WorkerAddress(new StubAgentIdentity("Agent").createWorkerIdentity());
217 
218     try {
219       message.setAddress(badAddress);
220       fail("Expected CommunicationException");
221     }
222     catch (CommunicationException e) {
223     }
224   }
225 }