View Javadoc

1   // Copyright (C) 2006 - 2009 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 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   * Unit test for {@link TestStatisticsForTestImplementation}.
39   *
40   * @author Philip Aston
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 }