View Javadoc

1   // Copyright (C) 2006 - 2011 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.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   * Unit test case for {@link ScriptStatisticsImplementation}.
57   *
58   * @author Philip Aston
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      // 1. Null thread context.
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     // 2. No last statistics, no current statistics.
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     // 3. No last statistics, current statistics.
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     // 4. Last statistics, current statistics.
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 }