1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.engine.process;
23
24 import junit.framework.TestCase;
25
26 import net.grinder.common.StubTest;
27 import net.grinder.common.Test;
28 import net.grinder.script.InvalidContextException;
29 import net.grinder.script.NoSuchStatisticException;
30 import net.grinder.statistics.StatisticsIndexMap;
31 import net.grinder.statistics.StatisticsServices;
32 import net.grinder.statistics.StatisticsServicesImplementation;
33 import net.grinder.statistics.StatisticsSet;
34 import net.grinder.testutility.RandomStubFactory;
35
36
37
38
39
40
41
42 public class TestStatisticsForTestImplementation extends TestCase {
43
44 final RandomStubFactory<DispatchContext> m_dispatchContextStubFactory =
45 RandomStubFactory.create(DispatchContext.class);
46 final DispatchContext m_dispatchContext =
47 m_dispatchContextStubFactory.getStub();
48
49 public void testStatisticsForTestImplementation() throws Exception {
50 final StatisticsServices statisticsServices =
51 StatisticsServicesImplementation.getInstance();
52 final StatisticsIndexMap statisticsIndexMap =
53 statisticsServices.getStatisticsIndexMap();
54 final TestStatisticsHelper testStatisticsHelper =
55 new TestStatisticsHelperImplementation(
56 statisticsIndexMap);
57
58 final Test test = new StubTest(1, "Hello");
59 m_dispatchContextStubFactory.setResult("getTest", test);
60
61 final StatisticsSet statisticsSet =
62 statisticsServices.getStatisticsSetFactory().create();
63
64 final StatisticsForTestImplementation statisticsForTest =
65 new StatisticsForTestImplementation(
66 m_dispatchContext,
67 testStatisticsHelper,
68 statisticsSet);
69
70 assertEquals(test, statisticsForTest.getTest());
71 assertSame(statisticsSet, statisticsForTest.getStatistics());
72
73 assertTrue(statisticsForTest.getSuccess());
74 assertEquals(0, statisticsForTest.getLong("userLong0"));
75 assertEquals(0, statisticsForTest.getLong("untimedTests"));
76 assertEquals(0.0, statisticsForTest.getDouble("userDouble0"), 0.1);
77
78 statisticsForTest.setLong("userLong0", 10);
79 statisticsForTest.setDouble("userDouble0", 1.1);
80 statisticsForTest.setSuccess(false);
81 assertEquals(10, statisticsForTest.getLong("userLong0"));
82 assertEquals(1.1, statisticsForTest.getDouble("userDouble0"), 0.1);
83 assertFalse(statisticsForTest.getSuccess());
84
85 statisticsForTest.addLong("untimedTests", 5);
86 statisticsForTest.addLong("untimedTests", -2);
87 statisticsForTest.addDouble("userDouble0", -5.4);
88 assertEquals(3, statisticsForTest.getLong("untimedTests"));
89 assertEquals(-4.3, statisticsForTest.getDouble("userDouble0"), 0.1);
90
91 assertEquals(
92 -4.3,
93 statisticsSet.getValue(
94 statisticsIndexMap.getDoubleIndex("userDouble0")),
95 0.1);
96
97 try {
98 statisticsForTest.getLong("userDouble0");
99 fail("Expected NoSuchStatisticException");
100 }
101 catch (NoSuchStatisticException e) {
102 }
103
104 try {
105 statisticsForTest.getDouble("foo");
106 fail("Expected NoSuchStatisticException");
107 }
108 catch (NoSuchStatisticException e) {
109 }
110
111 m_dispatchContextStubFactory.setResult("getElapsedTime", new Long(123));
112 assertEquals(123, statisticsForTest.getTime());
113
114 statisticsForTest.setSuccess(true);
115 testStatisticsHelper.recordTest(statisticsSet, 5555);
116
117 statisticsForTest.freeze();
118
119 assertNull(statisticsForTest.getStatistics());
120 assertEquals(5555, statisticsForTest.getTime());
121
122 try {
123 statisticsForTest.setLong("userLong0", 123);
124 fail("Expected InvalidContextException");
125 }
126 catch (InvalidContextException e) {
127 }
128
129 assertEquals(10, statisticsForTest.getLong("userLong0"));
130 }
131 }