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 static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyNoMoreInteractions;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35
36 import net.grinder.communication.Sender;
37 import net.grinder.messages.console.RegisterExpressionViewMessage;
38 import net.grinder.script.InvalidContextException;
39 import net.grinder.script.Statistics;
40 import net.grinder.script.Statistics.StatisticsForTest;
41 import net.grinder.statistics.ExpressionView;
42 import net.grinder.statistics.StatisticsServices;
43 import net.grinder.statistics.StatisticsServicesTestFactory;
44 import net.grinder.statistics.StatisticsView;
45 import net.grinder.testutility.AssertUtilities;
46
47 import org.junit.Before;
48 import org.junit.Test;
49 import org.mockito.ArgumentCaptor;
50 import org.mockito.Captor;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53
54
55
56
57
58
59
60 public class TestScriptStatisticsImplementation {
61
62 @Mock private ThreadContext m_threadContext;
63 @Mock private Sender m_sender;
64
65 @Captor ArgumentCaptor<RegisterExpressionViewMessage> m_messageCaptor;
66
67 private final StubThreadContextLocator m_threadContextLocator =
68 new StubThreadContextLocator();
69
70 private final StatisticsServices m_statisticsServices =
71 StatisticsServicesTestFactory.createTestInstance();
72
73 @Before public void setup() {
74 MockitoAnnotations.initMocks(this);
75 }
76
77 @Test public void testContextChecks() throws Exception {
78
79 final ScriptStatisticsImplementation scriptStatistics =
80 new ScriptStatisticsImplementation(
81 m_threadContextLocator,
82 m_statisticsServices,
83 m_sender);
84
85
86 assertFalse(scriptStatistics.isTestInProgress());
87
88 try {
89 scriptStatistics.getForCurrentTest();
90 fail("Expected InvalidContextException");
91 }
92 catch (InvalidContextException e) {
93 AssertUtilities.assertContains(e.getMessage(), "worker threads");
94 }
95
96 try {
97 scriptStatistics.getForLastTest();
98 fail("Expected InvalidContextException");
99 }
100 catch (InvalidContextException e) {
101 AssertUtilities.assertContains(e.getMessage(), "worker threads");
102 }
103
104 try {
105 scriptStatistics.report();
106 fail("Expected InvalidContextException");
107 }
108 catch (InvalidContextException e) {
109 AssertUtilities.assertContains(e.getMessage(), "worker threads");
110 }
111
112 try {
113 scriptStatistics.setDelayReports(false);
114 fail("Expected InvalidContextException");
115 }
116 catch (InvalidContextException e) {
117 AssertUtilities.assertContains(e.getMessage(), "worker threads");
118 }
119
120
121 m_threadContextLocator.set(m_threadContext);
122 when(m_threadContext.getStatisticsForCurrentTest()).thenReturn(null);
123 when(m_threadContext.getStatisticsForLastTest()).thenReturn(null);
124 assertFalse(scriptStatistics.isTestInProgress());
125
126 scriptStatistics.setDelayReports(false);
127
128 try {
129 scriptStatistics.getForCurrentTest();
130 fail("Expected InvalidContextException");
131 }
132 catch (InvalidContextException e) {
133 AssertUtilities.assertContains(e.getMessage(), "no test");
134 }
135
136 try {
137 scriptStatistics.getForLastTest();
138 fail("Expected InvalidContextException");
139 }
140 catch (InvalidContextException e) {
141 AssertUtilities.assertContains(e.getMessage(), "No tests");
142 }
143
144
145 final StatisticsForTest statisticsForTest1 = mock(StatisticsForTest.class);
146
147 when(m_threadContext.getStatisticsForCurrentTest())
148 .thenReturn(statisticsForTest1);
149
150 assertTrue(scriptStatistics.isTestInProgress());
151 assertSame(statisticsForTest1, scriptStatistics.getForCurrentTest());
152
153 try {
154 scriptStatistics.getForLastTest();
155 fail("Expected InvalidContextException");
156 }
157 catch (InvalidContextException e) {
158 AssertUtilities.assertContains(e.getMessage(), "No tests");
159 }
160
161
162 final StatisticsForTest statisticsForTest2 = mock(StatisticsForTest.class);
163
164 when(m_threadContext.getStatisticsForLastTest())
165 .thenReturn(statisticsForTest2);
166
167 assertTrue(scriptStatistics.isTestInProgress());
168 assertSame(statisticsForTest1, scriptStatistics.getForCurrentTest());
169 assertSame(statisticsForTest2, scriptStatistics.getForLastTest());
170
171 verifyNoMoreInteractions(m_sender);
172 }
173
174 @Test public void testRegisterStatisticsViews() throws Exception {
175
176 final StubThreadContextLocator threadContextLocator =
177 new StubThreadContextLocator();
178 threadContextLocator.set(m_threadContext);
179
180 final Statistics scriptStatistics =
181 new ScriptStatisticsImplementation(
182 threadContextLocator,
183 m_statisticsServices,
184 m_sender);
185
186 final ExpressionView expressionView =
187 m_statisticsServices.getStatisticExpressionFactory()
188 .createExpressionView("display", "errors", false);
189 scriptStatistics.registerSummaryExpression("display", "errors");
190
191 verify(m_sender).send(m_messageCaptor.capture());
192
193 final RegisterExpressionViewMessage message =
194 m_messageCaptor.getValue();
195 assertEquals("display", message.getExpressionView().getDisplayName());
196 assertEquals("errors", message.getExpressionView().getExpressionString());
197
198 verifyNoMoreInteractions(m_sender);
199
200 final StatisticsView summaryStatisticsView =
201 m_statisticsServices.getSummaryStatisticsView();
202
203 final ExpressionView[] summaryExpressionViews =
204 summaryStatisticsView.getExpressionViews();
205 assertTrue(Arrays.asList(summaryExpressionViews).contains(expressionView));
206
207 try {
208 scriptStatistics.registerDataLogExpression("display2", "untimedTests");
209 fail("Expected InvalidContextException");
210 }
211 catch (InvalidContextException e) {
212 }
213
214 threadContextLocator.set(null);
215
216 scriptStatistics.registerDataLogExpression("display2", "untimedTests");
217
218 final StatisticsView detailStatisticsView =
219 m_statisticsServices.getDetailStatisticsView();
220
221 final ExpressionView[] detailExpressionViews =
222 detailStatisticsView.getExpressionViews();
223 assertTrue(Arrays.asList(detailExpressionViews).contains(
224 m_statisticsServices.getStatisticExpressionFactory()
225 .createExpressionView("display2", "untimedTests", false)));
226 }
227 }