View Javadoc

1   // Copyright (C) 2000 - 2012 Philip Aston
2   // Copyright (C) 2005 Martin Wagner.
3   // All rights reserved.
4   //
5   // This file is part of The Grinder software distribution. Refer to
6   // the file LICENSE which is part of The Grinder distribution for
7   // licensing details. The Grinder distribution is available on the
8   // Internet at http://grinder.sourceforge.net/
9   //
10  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
11  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
12  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
14  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
15  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
20  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
21  // OF THE POSSIBILITY OF SUCH DAMAGE.
22  
23  package net.grinder.statistics;
24  
25  import java.io.PrintWriter;
26  import java.io.StringWriter;
27  import java.util.Locale;
28  
29  import net.grinder.common.StubTest;
30  import net.grinder.testutility.AssertUtilities;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  
37  /**
38   * Unit test case for {@link StatisticsTable}.
39   *
40   * @author Philip Aston
41   * @see StatisticsSet
42   */
43  public class TestStatisticsTable  {
44  
45    private TestStatisticsMap m_testStatisticsMap;
46    private StatisticsIndexMap m_indexMap;
47  
48    private StatisticsView m_statisticsView;
49  
50    private final StatisticsServices m_statisticsServices =
51      StatisticsServicesImplementation.getInstance();
52  
53    private final Locale m_originalDefaultLocale = Locale.getDefault();
54  
55    @Before public void setUp() throws Exception {
56      Locale.setDefault(Locale.US);
57  
58      m_testStatisticsMap =
59        new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
60  
61      m_indexMap = m_statisticsServices.getStatisticsIndexMap();
62  
63      final StatisticsIndexMap.LongIndex aIndex =
64        m_indexMap.getLongIndex("userLong0");
65      final StatisticsIndexMap.LongIndex bIndex =
66        m_indexMap.getLongIndex("userLong1");
67  
68      final StatisticExpressionFactory statisticExpressionFactory =
69        StatisticsServicesImplementation.getInstance()
70        .getStatisticExpressionFactory();
71  
72      final ExpressionView[] expressionViews = {
73          statisticExpressionFactory.createExpressionView("A", "userLong0", true),
74          statisticExpressionFactory.createExpressionView("B", "userLong1", false),
75          statisticExpressionFactory.createExpressionView("A plus B", "(+ userLong0 userLong1)", false),
76          statisticExpressionFactory.createExpressionView("A divided by B", "(/ userLong0 userLong1)", false),
77      };
78  
79      m_statisticsView = new StatisticsView();
80  
81      for (final ExpressionView expressionView : expressionViews) {
82        m_statisticsView.add(expressionView);
83      }
84  
85      final net.grinder.common.Test[] tests = {
86        new StubTest(9, "Test 9"),
87        new StubTest(3, null),
88        new StubTest(113, "Another test"),
89        new StubTest(12345678, "A test with a long name"),
90      };
91  
92      final StatisticsSet[] statistics = new StatisticsSet[tests.length];
93  
94      final StatisticsSetFactory factory =
95        m_statisticsServices.getStatisticsSetFactory();
96  
97      for (int i = 0; i < tests.length; ++i) {
98        statistics[i] =
99          new StatisticsSetImplementation(
100           m_statisticsServices.getStatisticsIndexMap());
101       if (i != 2) {
102         statistics[i].addValue(aIndex, i);
103         statistics[i].addValue(bIndex, i + 1);
104       }
105 
106       final StatisticsSet statistics2 = factory.create();
107       statistics2.add(statistics[i]);
108 
109       m_testStatisticsMap.put(tests[i], statistics2);
110     }
111   }
112 
113   @After public void tearDown() throws Exception {
114     Locale.setDefault(m_originalDefaultLocale);
115   }
116 
117   @Test public void testStatisticsTable() throws Exception {
118     final StringWriter expected = new StringWriter();
119     final PrintWriter in = new PrintWriter(expected);
120 
121     in.println("             A            B            A plus B     A divided by ");
122     in.println("                                                    B            ");
123     in.println();
124     in.println("Test 3       1            2            3            0.50         ");
125     in.println("Test 9       0            1            1            0.00          \"Test 9\"");
126     in.println("Test 113     0            0            0            -             \"Another test\"");
127     in.println("Test 12345678 3            4            7            0.75          \"A test with a long name\"");
128     in.println();
129     in.println("Totals       4            7            11           0.57         ");
130     in.close();
131 
132     final StatisticsTable table =
133       new StatisticsTable(m_statisticsView, m_indexMap, m_testStatisticsMap);
134 
135     final StringWriter output = new StringWriter();
136     final PrintWriter out = new PrintWriter(output);
137     table.print(out, 1234);
138     out.close();
139 
140     AssertUtilities.assertContains(
141       output.getBuffer().toString(),
142       expected.getBuffer().toString());
143   }
144 
145   @Test public void testStatisticsTableWithCompositeTests() throws Exception {
146     final StatisticsSet statistics =
147       m_statisticsServices.getStatisticsSetFactory().create();
148     statistics.setValue(
149       m_statisticsServices.getStatisticsIndexMap().getLongIndex("userLong1"), 1);
150     statistics.setIsComposite();
151     m_testStatisticsMap.put(new StubTest(4, "T4"), statistics);
152 
153     final StringWriter expected = new StringWriter();
154     final PrintWriter in = new PrintWriter(expected);
155 
156     in.println("             A            B            A plus B     A divided by ");
157     in.println("                                                    B            ");
158     in.println();
159     in.println("Test 3       1            2            3            0.50         ");
160     in.println("(Test 4      0            1            1            0.00)         \"T4\"");
161     in.println("Test 9       0            1            1            0.00          \"Test 9\"");
162     in.println("Test 113     0            0            0            -             \"Another test\"");
163     in.println("Test 12345678 3            4            7            0.75          \"A test with a long name\"");
164     in.println();
165     in.println("Totals       4            7            11           0.57         ");
166     in.println("             (0)                                                 ");
167     in.close();
168 
169     final StatisticsTable table =
170       new StatisticsTable(m_statisticsView, m_indexMap, m_testStatisticsMap);
171 
172     final StringWriter output = new StringWriter();
173     final PrintWriter out = new PrintWriter(output);
174     table.print(out, 1234);
175     out.close();
176 
177     AssertUtilities.assertContains(
178       output.getBuffer().toString(),
179       expected.getBuffer().toString());
180   }
181 }