View Javadoc

1   // Copyright (C) 2006 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 net.grinder.common.StubTest;
25  import net.grinder.statistics.StatisticsIndexMap;
26  import net.grinder.statistics.StatisticsServices;
27  import net.grinder.statistics.StatisticsServicesImplementation;
28  import net.grinder.statistics.StatisticsSet;
29  import net.grinder.statistics.TestStatisticsMap;
30  import junit.framework.TestCase;
31  
32  
33  /**
34   * Unit test case for <code>TestStatisticsHelperImplementation</code>.
35   *
36   * @author Philip Aston
37   */
38  public class TestTestStatisticsHelperImplementation extends TestCase {
39  
40    private final StatisticsServices m_statisticsServices =
41      StatisticsServicesImplementation.getInstance();
42  
43    private final StatisticsIndexMap m_indexMap =
44      m_statisticsServices.getStatisticsIndexMap();
45  
46    private final StatisticsIndexMap.LongIndex m_errorsIndex =
47      m_indexMap.getLongIndex("errors");
48  
49    private final StatisticsIndexMap.LongSampleIndex m_timedTestsIndex =
50      m_indexMap.getLongSampleIndex("timedTests");
51  
52    private final StatisticsIndexMap.LongIndex m_untimedTestsIndex =
53      m_indexMap.getLongIndex("untimedTests");
54  
55  
56    public void testSuccessMethods() throws Exception {
57      final TestStatisticsHelper helper =
58        new TestStatisticsHelperImplementation(m_indexMap);
59  
60      assertSame(m_indexMap, helper.getStatisticsIndexMap());
61  
62      final StatisticsSet statistics =
63        m_statisticsServices.getStatisticsSetFactory().create();
64  
65      assertTrue(helper.getSuccess(statistics));
66  
67      statistics.setValue(m_errorsIndex, 1);
68      assertFalse(helper.getSuccess(statistics));
69  
70      statistics.setValue(m_errorsIndex, 2);
71      assertFalse(helper.getSuccess(statistics));
72  
73      statistics.setValue(m_errorsIndex, 0);
74      assertTrue(helper.getSuccess(statistics));
75  
76      helper.setSuccess(statistics, false);
77      assertFalse(helper.getSuccess(statistics));
78  
79      helper.setSuccess(statistics, true);
80      assertTrue(helper.getSuccess(statistics));
81  
82      assertEquals(0, statistics.getValue(m_errorsIndex));
83    }
84  
85    public void testRecordTest() throws Exception {
86  
87      final StatisticsSet statistics =
88        m_statisticsServices.getStatisticsSetFactory().create();
89  
90      final TestStatisticsHelper helper =
91        new TestStatisticsHelperImplementation(m_indexMap);
92  
93      helper.recordTest(statistics, 1234);
94      assertEquals(0, statistics.getValue(m_errorsIndex));
95      assertEquals(1234, statistics.getSum(m_timedTestsIndex));
96  
97      statistics.setValue(m_untimedTestsIndex, 1);
98      helper.recordTest(statistics, 999);
99      assertEquals(0, statistics.getValue(m_errorsIndex));
100     assertEquals(999, statistics.getSum(m_timedTestsIndex));
101     assertEquals(0, statistics.getValue(m_untimedTestsIndex));
102 
103     statistics.setValue(m_errorsIndex, 2);
104     helper.recordTest(statistics, 1234);
105     assertEquals(1, statistics.getValue(m_errorsIndex));
106     assertEquals(1234, statistics.getSum(m_timedTestsIndex));
107     assertEquals(0, statistics.getValue(m_untimedTestsIndex));
108   }
109 
110   public void testRemoveTestTimeFromSample() throws Exception {
111 
112     final TestStatisticsHelper helper =
113       new TestStatisticsHelperImplementation(m_indexMap);
114 
115     final StatisticsSet statistics1 =
116       m_statisticsServices.getStatisticsSetFactory().create();
117     statistics1.addSample(m_timedTestsIndex, 21321);
118     statistics1.addSample(m_timedTestsIndex, 1231);
119 
120     assertEquals(21321 + 1231, helper.getTestTime(statistics1));
121 
122     final StatisticsSet statistics2 =
123       m_statisticsServices.getStatisticsSetFactory().create();
124     statistics2.setValue(m_untimedTestsIndex, 1);
125     statistics2.addSample(m_timedTestsIndex, 18782);
126     statistics2.setValue(m_errorsIndex, 1);
127 
128     assertEquals(18782, helper.getTestTime(statistics2));
129 
130     final TestStatisticsMap sample = new TestStatisticsMap();
131     sample.put(new StubTest(1, ""), statistics1);
132     sample.put(new StubTest(2, ""), statistics2);
133 
134     helper.removeTestTimeFromSample(sample);
135 
136     assertEquals(0, statistics1.getValue(m_errorsIndex));
137     assertEquals(2, statistics1.getValue(m_untimedTestsIndex));
138     assertEquals(0, statistics1.getCount(m_timedTestsIndex));
139     assertEquals(0, statistics1.getSum(m_timedTestsIndex));
140     assertEquals(0, helper.getTestTime(statistics1));
141 
142     assertEquals(1, statistics2.getValue(m_errorsIndex));
143     assertEquals(2, statistics2.getValue(m_untimedTestsIndex));
144     assertEquals(0, statistics2.getCount(m_timedTestsIndex));
145     assertEquals(0, statistics2.getSum(m_timedTestsIndex));
146     assertEquals(0, helper.getTestTime(statistics2));
147   }
148 
149   public void testIncrementErrors() throws Exception {
150 
151     final StatisticsSet statistics =
152       m_statisticsServices.getStatisticsSetFactory().create();
153 
154     final TestStatisticsHelper helper =
155       new TestStatisticsHelperImplementation(m_indexMap);
156 
157     helper.incrementErrors(statistics);
158     helper.incrementErrors(statistics);
159     helper.incrementErrors(statistics);
160     assertEquals(3, statistics.getValue(m_errorsIndex));
161   }
162 }