1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.grinder.console.model;
24
25 import static net.grinder.console.model.SampleModel.State.Value.IgnoringInitialSamples;
26 import static net.grinder.console.model.SampleModel.State.Value.Recording;
27 import static net.grinder.console.model.SampleModel.State.Value.Stopped;
28 import static net.grinder.console.model.SampleModel.State.Value.WaitingForFirstReport;
29
30 import java.io.File;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Set;
37 import java.util.TimerTask;
38
39 import net.grinder.common.StubTest;
40 import net.grinder.common.Test;
41 import net.grinder.console.common.ErrorHandler;
42 import net.grinder.console.common.Resources;
43 import net.grinder.console.common.StubResources;
44 import net.grinder.console.model.SampleModel.AbstractListener;
45 import net.grinder.console.model.SampleModel.Listener;
46 import net.grinder.console.model.SampleModel.State;
47 import net.grinder.statistics.StatisticExpression;
48 import net.grinder.statistics.StatisticsIndexMap.LongIndex;
49 import net.grinder.statistics.StatisticsServices;
50 import net.grinder.statistics.StatisticsServicesImplementation;
51 import net.grinder.statistics.StatisticsSet;
52 import net.grinder.statistics.TestStatisticsMap;
53 import net.grinder.testutility.AbstractFileTestCase;
54 import net.grinder.testutility.RandomStubFactory;
55 import net.grinder.testutility.StubTimer;
56
57
58
59
60
61
62
63 public class TestSampleModelImplementation extends AbstractFileTestCase {
64
65 private final Resources m_resources = new StubResources<String>(
66 new HashMap<String, String>() {{
67 put("state.ignoring.label", "whatever");
68 put("state.waiting.label", "waiting, waiting, waiting");
69 put("state.stopped.label", "done");
70 put("state.capturing.label", "running");
71 }}
72 );
73
74 private ConsoleProperties m_consoleProperties;
75
76 private final StatisticsServices m_statisticsServices =
77 StatisticsServicesImplementation.getInstance();
78
79 private StubTimer m_timer;
80
81 private final RandomStubFactory<Listener> m_listenerStubFactory =
82 RandomStubFactory.create(SampleModel.Listener.class);
83 private final SampleModel.Listener m_listener =
84 m_listenerStubFactory.getStub();
85
86 final RandomStubFactory<ErrorHandler> m_errorHandlerStubFactory =
87 RandomStubFactory.create(ErrorHandler.class);
88 final ErrorHandler m_errorHandler =
89 m_errorHandlerStubFactory.getStub();
90
91 @Override
92 protected void setUp() throws Exception {
93 super.setUp();
94 m_timer = new StubTimer();
95 m_consoleProperties =
96 new ConsoleProperties(null, new File(getDirectory(), "props"));
97 }
98
99 @Override
100 public void tearDown() throws Exception {
101 super.tearDown();
102 m_timer.cancel();
103 }
104
105 public void testConstruction() throws Exception {
106 final SampleModelImplementation sampleModelImplementation =
107 new SampleModelImplementation(m_consoleProperties,
108 m_statisticsServices,
109 m_timer,
110 m_resources,
111 m_errorHandler);
112
113 final StatisticExpression tpsExpression =
114 sampleModelImplementation.getTPSExpression();
115 assertNotNull(tpsExpression);
116 assertSame(tpsExpression, sampleModelImplementation.getTPSExpression());
117
118 final StatisticExpression peakTPSExpression =
119 sampleModelImplementation.getPeakTPSExpression();
120 assertNotNull(peakTPSExpression);
121 assertSame(
122 peakTPSExpression, sampleModelImplementation.getPeakTPSExpression());
123 assertNotSame(tpsExpression, peakTPSExpression);
124
125 final StatisticsSet totalCumulativeStatistics =
126 sampleModelImplementation.getTotalCumulativeStatistics();
127 assertNotNull(totalCumulativeStatistics);
128 assertSame(totalCumulativeStatistics,
129 sampleModelImplementation.getTotalCumulativeStatistics());
130
131 final StatisticsSet totalLatestStatistics =
132 sampleModelImplementation.getTotalLatestStatistics();
133 assertNotNull(totalLatestStatistics);
134 assertSame(totalLatestStatistics,
135 sampleModelImplementation.getTotalLatestStatistics());
136
137 final State state = sampleModelImplementation.getState();
138 assertEquals(WaitingForFirstReport, state.getValue());
139 assertEquals("waiting, waiting, waiting", state.getDescription());
140 assertNull(m_timer.getLastScheduledTimerTask());
141 }
142
143 @SuppressWarnings("unchecked")
144 public void testRegisterTests() throws Exception {
145 final SampleModelImplementation sampleModelImplementation =
146 new SampleModelImplementation(m_consoleProperties,
147 m_statisticsServices,
148 m_timer,
149 m_resources,
150 m_errorHandler);
151
152 sampleModelImplementation.addModelListener(m_listener);
153
154 final Set<Test> emptySet = Collections.emptySet();
155 sampleModelImplementation.registerTests(emptySet);
156 m_listenerStubFactory.assertNoMoreCalls();
157
158 final Test test1 = new StubTest(1, "test 1");
159 final Test test2 = new StubTest(2, "test 2");
160 final Test test3 = new StubTest(3, "test 3");
161 final Test test4 = new StubTest(4, "test 4");
162
163 final List<Test> testList = new ArrayList<Test>() { {
164 add(test2);
165 add(test1);
166 add(test3);
167 } };
168
169 sampleModelImplementation.registerTests(testList);
170
171 final Object[] callbackParameters = m_listenerStubFactory.assertSuccess(
172 "newTests", Set.class, ModelTestIndex.class).getParameters();
173 m_listenerStubFactory.assertNoMoreCalls();
174
175 Collections.sort(testList);
176
177 final Set<Test> callbackTestSet = (Set<Test>)callbackParameters[0];
178 assertTrue(testList.containsAll(callbackTestSet));
179 assertTrue(callbackTestSet.containsAll(testList));
180
181 final ModelTestIndex modelIndex = (ModelTestIndex)callbackParameters[1];
182 assertEquals(testList.size(), modelIndex.getNumberOfTests());
183 assertEquals(testList.size(), modelIndex.getAccumulatorArray().length);
184
185 for (int i = 0; i < modelIndex.getNumberOfTests(); ++i) {
186 assertEquals(testList.get(i), modelIndex.getTest(i));
187 }
188
189 final List<Test> testList2 = new ArrayList<Test>() { {
190 add(test2);
191 add(test4);
192 } };
193
194 sampleModelImplementation.registerTests(testList2);
195
196 final Object[] callbackParameters2 = m_listenerStubFactory.assertSuccess(
197 "newTests", Set.class, ModelTestIndex.class).getParameters();
198 m_listenerStubFactory.assertNoMoreCalls();
199
200 final Set<Test> expectedNewTests = new HashSet<Test>() { {
201 add(test4);
202 } };
203
204 final Set<Test> callbackTestSet2 = (Set<Test>)callbackParameters2[0];
205 assertTrue(expectedNewTests.containsAll(callbackTestSet2));
206 assertTrue(callbackTestSet2.containsAll(expectedNewTests));
207
208 final ModelTestIndex modelIndex2 = (ModelTestIndex)callbackParameters2[1];
209 assertEquals(4, modelIndex2.getNumberOfTests());
210 assertEquals(4, modelIndex2.getAccumulatorArray().length);
211
212 sampleModelImplementation.registerTests(testList2);
213 m_listenerStubFactory.assertNoMoreCalls();
214 }
215
216 public void testWaitingToStopped() throws Exception {
217 final SampleModelImplementation sampleModelImplementation =
218 new SampleModelImplementation(m_consoleProperties,
219 m_statisticsServices,
220 m_timer,
221 m_resources,
222 m_errorHandler);
223
224 sampleModelImplementation.addModelListener(m_listener);
225
226 final State state = sampleModelImplementation.getState();
227 assertEquals(WaitingForFirstReport, state.getValue());
228 assertEquals("waiting, waiting, waiting", state.getDescription());
229
230 m_listenerStubFactory.assertNoMoreCalls();
231
232
233 sampleModelImplementation.stop();
234
235 m_listenerStubFactory.assertSuccess("stateChanged");
236
237 final State stoppedState = sampleModelImplementation.getState();
238 assertEquals(Stopped, stoppedState.getValue());
239 assertEquals("done", stoppedState.getDescription());
240
241
242 sampleModelImplementation.addTestReport(new TestStatisticsMap());
243
244 final State stoppedState2 = sampleModelImplementation.getState();
245 assertEquals(Stopped, stoppedState2.getValue());
246 assertEquals("done", stoppedState2.getDescription());
247
248
249 assertNull(m_timer.getLastScheduledTimerTask());
250 }
251
252 public void testWaitingToTriggeredToCapturingToStopped() throws Exception {
253 final SampleModelImplementation sampleModelImplementation =
254 new SampleModelImplementation(m_consoleProperties,
255 m_statisticsServices,
256 m_timer,
257 m_resources,
258 m_errorHandler);
259
260 final TestStatisticsMap testStatisticsMap = new TestStatisticsMap();
261
262
263 sampleModelImplementation.addModelListener(m_listener);
264
265 final State waitingState = sampleModelImplementation.getState();
266 assertEquals(WaitingForFirstReport, waitingState.getValue());
267 assertEquals("waiting, waiting, waiting", waitingState.getDescription());
268
269 m_listenerStubFactory.assertNoMoreCalls();
270
271
272 m_consoleProperties.setIgnoreSampleCount(10);
273
274 sampleModelImplementation.addTestReport(testStatisticsMap);
275
276 final State triggeredState = sampleModelImplementation.getState();
277 assertEquals(IgnoringInitialSamples, triggeredState.getValue());
278 assertEquals("whatever 1", triggeredState.getDescription());
279
280
281 final TimerTask triggeredSampleTask = m_timer.getLastScheduledTimerTask();
282 triggeredSampleTask.run();
283
284 assertEquals("whatever 2", triggeredState.getDescription());
285
286
287 sampleModelImplementation.addTestReport(testStatisticsMap);
288 sampleModelImplementation.addTestReport(testStatisticsMap);
289 sampleModelImplementation.addTestReport(testStatisticsMap);
290 triggeredSampleTask.run();
291
292 assertEquals("whatever 3",
293 sampleModelImplementation.getState().getDescription());
294
295
296 for (int i = 0; i < 4; ++i) {
297 triggeredSampleTask.run();
298 }
299
300 assertEquals("whatever 7",
301 sampleModelImplementation.getState().getDescription());
302
303
304 triggeredSampleTask.run();
305
306 assertEquals("whatever 8",
307 sampleModelImplementation.getState().getDescription());
308 assertEquals(IgnoringInitialSamples, sampleModelImplementation.getState().getValue());
309
310 for (int i = 0; i < 3; ++i) {
311 sampleModelImplementation.addTestReport(testStatisticsMap);
312 triggeredSampleTask.run();
313 }
314
315 final State capturingState = sampleModelImplementation.getState();
316 assertEquals(Recording, capturingState.getValue());
317 assertEquals("running 1", capturingState.getDescription());
318
319
320 sampleModelImplementation.addTestReport(testStatisticsMap);
321
322 assertEquals("running 1",
323 sampleModelImplementation.getState().getDescription());
324
325
326 final TimerTask capturingSampleTask = m_timer.getLastScheduledTimerTask();
327 assertNotSame(triggeredSampleTask, capturingSampleTask);
328 capturingSampleTask.run();
329
330 assertEquals("running 2",
331 sampleModelImplementation.getState().getDescription());
332
333
334 sampleModelImplementation.addTestReport(testStatisticsMap);
335 capturingSampleTask.run();
336
337 assertEquals("running 3",
338 sampleModelImplementation.getState().getDescription());
339
340
341 m_consoleProperties.setCollectSampleCount(2);
342 capturingSampleTask.run();
343
344 assertEquals("done", sampleModelImplementation.getState().getDescription());
345
346
347 capturingSampleTask.run();
348 assertEquals("done", sampleModelImplementation.getState().getDescription());
349 }
350
351 public void testReset() throws Exception {
352 final SampleModelImplementation sampleModelImplementation =
353 new SampleModelImplementation(m_consoleProperties,
354 m_statisticsServices,
355 m_timer,
356 m_resources,
357 m_errorHandler);
358
359 sampleModelImplementation.addModelListener(m_listener);
360 sampleModelImplementation.reset();
361
362 m_listenerStubFactory.assertSuccess("resetTests");
363 }
364
365 public void testSampleListeners() throws Exception {
366 final SampleModelImplementation sampleModelImplementation =
367 new SampleModelImplementation(m_consoleProperties,
368 m_statisticsServices,
369 m_timer,
370 m_resources,
371 m_errorHandler);
372
373 final RandomStubFactory<SampleListener> totalSampleListenerStubFactory =
374 RandomStubFactory.create(SampleListener.class);
375 sampleModelImplementation.addTotalSampleListener(
376 totalSampleListenerStubFactory.getStub());
377
378 final Test test1 = new StubTest(1, "test 1");
379 final Test test2 = new StubTest(2, "test 2");
380 final Test test3 = new StubTest(3, "test 3");
381 final Test test4 = new StubTest(4, "test 4");
382
383 final RandomStubFactory<SampleListener> sampleListenerStubFactory =
384 RandomStubFactory.create(SampleListener.class);
385 final SampleListener sampleListener =
386 sampleListenerStubFactory.getStub();
387
388
389 sampleModelImplementation.addSampleListener(test1, sampleListener);
390
391
392 final Set<Test> testSet = new HashSet<Test>() { {
393 add(test2);
394 add(test4);
395 } };
396
397 sampleModelImplementation.registerTests(testSet);
398 sampleModelImplementation.addSampleListener(test2, sampleListener);
399
400
401 sampleModelImplementation.reset();
402 sampleListenerStubFactory.assertNoMoreCalls();
403
404
405 final TestStatisticsMap testReports = new TestStatisticsMap();
406 final StatisticsSet statistics1 =
407 m_statisticsServices.getStatisticsSetFactory().create();
408 final LongIndex userLong0 =
409 m_statisticsServices.getStatisticsIndexMap().getLongIndex("userLong0");
410 final StatisticsSet statistics2 =
411 m_statisticsServices.getStatisticsSetFactory().create();
412 statistics1.setValue(userLong0, 99);
413 statistics2.setValue(userLong0, 1);
414 statistics2.setIsComposite();
415 testReports.put(test2, statistics1);
416 testReports.put(test3, statistics2);
417 testReports.put(test4, statistics2);
418
419
420 sampleModelImplementation.registerTests(testSet);
421 sampleModelImplementation.addSampleListener(test2, sampleListener);
422 sampleModelImplementation.addTestReport(testReports);
423
424 totalSampleListenerStubFactory.assertNoMoreCalls();
425 sampleListenerStubFactory.assertNoMoreCalls();
426
427
428 final TimerTask capturingTask = m_timer.getLastScheduledTimerTask();
429 capturingTask.run();
430
431 final Object[] updateParameters =
432 sampleListenerStubFactory.assertSuccess(
433 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
434 sampleListenerStubFactory.assertNoMoreCalls();
435
436 assertEquals(99, ((StatisticsSet)updateParameters[0]).getValue(userLong0));
437 assertEquals(99, ((StatisticsSet)updateParameters[1]).getValue(userLong0));
438
439 final Object[] totalParameters =
440 totalSampleListenerStubFactory.assertSuccess(
441 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
442 totalSampleListenerStubFactory.assertNoMoreCalls();
443
444 assertEquals(99, ((StatisticsSet)totalParameters[0]).getValue(userLong0));
445 assertEquals(99, ((StatisticsSet)totalParameters[1]).getValue(userLong0));
446
447
448 capturingTask.run();
449
450 final Object[] updateParameters2 =
451 sampleListenerStubFactory.assertSuccess(
452 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
453 sampleListenerStubFactory.assertNoMoreCalls();
454
455 assertEquals(0, ((StatisticsSet)updateParameters2[0]).getValue(userLong0));
456 assertEquals(99, ((StatisticsSet)updateParameters2[1]).getValue(userLong0));
457
458 final Object[] totalParameters2 =
459 totalSampleListenerStubFactory.assertSuccess(
460 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
461 totalSampleListenerStubFactory.assertNoMoreCalls();
462
463 assertEquals(0, ((StatisticsSet)totalParameters2[0]).getValue(userLong0));
464 assertEquals(99, ((StatisticsSet)totalParameters2[1]).getValue(userLong0));
465
466
467
468 sampleModelImplementation.start();
469 m_consoleProperties.setIgnoreSampleCount(10);
470 statistics1.setValue(userLong0, 3);
471
472 sampleModelImplementation.addTestReport(testReports);
473
474 final TimerTask triggeredTask = m_timer.getLastScheduledTimerTask();
475 triggeredTask.run();
476
477 final Object[] updateParameters3 =
478 sampleListenerStubFactory.assertSuccess(
479 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
480 sampleListenerStubFactory.assertNoMoreCalls();
481
482 assertEquals(3, ((StatisticsSet)updateParameters3[0]).getValue(userLong0));
483 assertEquals(0, ((StatisticsSet)updateParameters3[1]).getValue(userLong0));
484
485 final Object[] totalParameters3 =
486 totalSampleListenerStubFactory.assertSuccess(
487 "update", StatisticsSet.class, StatisticsSet.class).getParameters();
488 totalSampleListenerStubFactory.assertNoMoreCalls();
489
490 assertEquals(3, ((StatisticsSet)totalParameters3[0]).getValue(userLong0));
491 assertEquals(0, ((StatisticsSet)totalParameters3[1]).getValue(userLong0));
492 }
493
494 public void testAbstractListener() {
495
496 final AbstractListener listener = new AbstractListener() {};
497
498 listener.newTests(null, null);
499 listener.resetTests();
500 listener.newSample();
501 listener.stateChanged();
502 }
503 }