View Javadoc

1   // Copyright (C) 2008 - 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.console.swingui;
23  
24  import java.awt.Color;
25  import java.io.File;
26  import java.io.StringWriter;
27  import java.text.DecimalFormat;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Timer;
31  
32  import net.grinder.common.StubTest;
33  import net.grinder.common.Test;
34  import net.grinder.console.common.StubResources;
35  import net.grinder.console.model.ConsoleProperties;
36  import net.grinder.console.model.ModelTestIndex;
37  import net.grinder.console.model.SampleModel;
38  import net.grinder.console.model.SampleModelImplementation;
39  import net.grinder.console.model.SampleModelViews;
40  import net.grinder.statistics.StatisticsServices;
41  import net.grinder.statistics.StatisticsServicesTestFactory;
42  import net.grinder.statistics.StatisticsSet;
43  import net.grinder.statistics.TestStatisticsMap;
44  import net.grinder.statistics.TestStatisticsQueries;
45  import net.grinder.testutility.AbstractFileTestCase;
46  import net.grinder.testutility.DelegatingStubFactory;
47  import net.grinder.testutility.RandomStubFactory;
48  import net.grinder.testutility.StubTimer;
49  
50  /**
51   * Unit tests for {@link CumulativeStatisticsTableModel}.
52   *
53   * @author Philip Aston
54   */
55  public class TestCumulativeStatisticsTableModel extends AbstractFileTestCase {
56  
57    private File m_file;
58  
59    protected void setUp() throws Exception {
60      super.setUp();
61      m_file = new File(getDirectory(), "properties");
62    }
63  
64    public static class NullSwingDispatcherFactory
65      implements SwingDispatcherFactory {
66  
67      @Override
68      public <T> T create(Class<T> clazz, T delegate) {
69        return delegate;
70      }
71    }
72  
73    private final SwingDispatcherFactory m_swingDispatcherFactoryDelegate =
74      new NullSwingDispatcherFactory();
75  
76    private final StubResources<String> m_resources =
77      new StubResources<String>(
78        new HashMap<String, String>() { {
79          put("table.test.label", "t3st");
80          put("table.testColumn.label", "Test Column");
81          put("table.descriptionColumn.label", "Test Description Column");
82          put("table.total.label", "Total Label");
83        } }
84      );
85  
86    private final DelegatingStubFactory<SwingDispatcherFactory>
87      m_swingDispatcherFactoryStubFactory =
88        DelegatingStubFactory.create(m_swingDispatcherFactoryDelegate);
89    private final SwingDispatcherFactory m_swingDispatcherFactory =
90      m_swingDispatcherFactoryStubFactory.getStub();
91  
92    private final RandomStubFactory<SampleModel> m_sampleModelStubFactory =
93      RandomStubFactory.create(SampleModel.class);
94    private final SampleModel m_sampleModel = m_sampleModelStubFactory.getStub();
95  
96    private final RandomStubFactory<SampleModelViews>
97      m_sampleModelViewsStubFactory =
98        RandomStubFactory.create(SampleModelViews.class);
99    private final SampleModelViews m_sampleModelViews =
100     m_sampleModelViewsStubFactory.getStub();
101 
102   private final StatisticsServices m_statisticsServices =
103     StatisticsServicesTestFactory.createTestInstance();
104 
105   private final TestStatisticsQueries m_testStatisticsQueries =
106     m_statisticsServices.getTestStatisticsQueries();
107 
108   {
109     m_sampleModelViewsStubFactory.setResult("getCumulativeStatisticsView",
110       m_statisticsServices.getSummaryStatisticsView());
111     m_sampleModelViewsStubFactory.setResult("getTestStatisticsQueries",
112                                             m_testStatisticsQueries);
113     m_sampleModelViewsStubFactory.setResult("getNumberFormat",
114       new DecimalFormat("0.0"));
115 
116     m_sampleModelStubFactory.setResult("getTotalCumulativeStatistics",
117       m_statisticsServices.getStatisticsSetFactory().create());
118   }
119 
120   public void testConstruction() throws Exception {
121     final CumulativeStatisticsTableModel model =
122       new CumulativeStatisticsTableModel(m_sampleModel,
123                                          m_sampleModelViews,
124                                          m_resources,
125                                          m_swingDispatcherFactory);
126 
127     // The dispatcher factory is used a couple of times to wrap
128     // listeners.
129     m_swingDispatcherFactoryStubFactory.assertSuccess("create",
130                                                       Class.class,
131                                                       Object.class);
132 
133     m_swingDispatcherFactoryStubFactory.assertSuccess("create",
134                                                       Class.class,
135                                                       Object.class);
136 
137     m_swingDispatcherFactoryStubFactory.assertNoMoreCalls();
138 
139     assertSame(m_sampleModel, model.getModel());
140     assertSame(m_sampleModelViews, model.getModelViews());
141 
142     assertEquals(7, model.getColumnCount());
143     assertEquals(1, model.getRowCount());
144     assertEquals(0, model.getLastModelTestIndex().getNumberOfTests());
145 
146     assertEquals("Test Column", model.getColumnName(0));
147     assertEquals("Test Description Column", model.getColumnName(1));
148     assertEquals("Errors", model.getColumnName(3));
149 
150     assertEquals("Total Label", model.getValueAt(0, 0));
151     assertEquals("", model.getValueAt(0, 1));
152     assertEquals("0 tests", "0", model.getValueAt(0, 2));
153     assertEquals("", model.getValueAt(0, 4));
154     assertEquals("SD is 0", "0.0", model.getValueAt(0, 5));
155     assertEquals("?", model.getValueAt(0, 10));
156 
157     assertTrue(model.isBold(0, 1));
158     assertTrue(model.isBold(0, 2));
159     assertTrue(model.isBold(0, 3));
160 
161     assertNull(model.getForeground(0, 0));
162     assertNull(model.getForeground(0, 1));
163     assertNull(model.getForeground(0, 3));
164     assertNull(model.getBackground(0, 0));
165     assertNull(model.getBackground(0, 1));
166     assertNull(model.getBackground(0, 3));
167   }
168 
169   public void testDefaultWrite() throws Exception {
170     final CumulativeStatisticsTableModel model =
171       new CumulativeStatisticsTableModel(m_sampleModel,
172                                          m_sampleModelViews,
173                                          m_resources,
174                                          m_swingDispatcherFactory);
175 
176     final StringWriter writer = new StringWriter();
177 
178     model.write(writer, "::", "**");
179 
180     assertEquals("Test Column::Test Description Column::Tests::Errors::Mean Test Time (ms)::Test Time Standard Deviation (ms)::TPS::**Total Label::::0::0::::0.0::::**",
181                  writer.toString());
182   }
183 
184   public void testWriteWithoutTotals() throws Exception {
185     final CumulativeStatisticsTableModel model =
186       new CumulativeStatisticsTableModel(m_sampleModel,
187                                          m_sampleModelViews,
188                                          m_resources,
189                                          m_swingDispatcherFactory);
190 
191     final StringWriter writer = new StringWriter();
192 
193     model.writeWithoutTotals(writer, "::", "**");
194 
195     assertEquals("Test Column::Test Description Column::Tests::Errors::Mean Test Time (ms)::Test Time Standard Deviation (ms)::TPS::**",
196                  writer.toString());
197   }
198 
199   public void testAddColumns() throws Exception {
200     final CumulativeStatisticsTableModel model =
201       new CumulativeStatisticsTableModel(m_sampleModel,
202                                          m_sampleModelViews,
203                                          m_resources,
204                                          m_swingDispatcherFactory);
205 
206     m_resources.put("statistic.Errors", "Blah");
207     m_resources.put("statistic.Mean_Test_Time_(ms)", "meantime");
208 
209     assertEquals(7, model.getColumnCount());
210 
211     model.addColumns(m_statisticsServices.getSummaryStatisticsView());
212 
213     // Adding same columns again is a no-op.
214     assertEquals(7, model.getColumnCount());
215     assertEquals("Tests", model.getColumnName(2));
216     assertEquals("Errors", model.getColumnName(3));
217     assertEquals("Mean Test Time (ms)", model.getColumnName(4));
218 
219     model.addColumns(m_statisticsServices.getDetailStatisticsView());
220 
221     assertEquals(8, model.getColumnCount());
222     assertEquals("Test time", model.getColumnName(2));
223     assertEquals("Blah", model.getColumnName(4));
224     assertEquals("meantime", model.getColumnName(5));
225   }
226 
227   public void testWithData() throws Exception {
228     final Timer timer = new StubTimer();
229 
230     final SampleModelImplementation sampleModelImplementation =
231       new SampleModelImplementation(new ConsoleProperties(m_resources, m_file),
232                                     m_statisticsServices,
233                                     timer,
234                                     m_resources,
235                                     null);
236 
237     final CumulativeStatisticsTableModel model =
238       new CumulativeStatisticsTableModel(sampleModelImplementation,
239                                          m_sampleModelViews,
240                                          m_resources,
241                                          m_swingDispatcherFactory);
242 
243     model.newTests(null, new ModelTestIndex());
244 
245     assertEquals(1, model.getRowCount());
246     assertNull(model.getForeground(0, 0));
247     assertNull(model.getBackground(0, 0));
248 
249     final Test[] tests = {
250         new StubTest(1, "test 1"),
251         new StubTest(2, "test 2"),
252     };
253 
254     sampleModelImplementation.registerTests(Arrays.asList(tests));
255 
256     assertEquals(3, model.getRowCount());
257     assertNull(model.getForeground(0, 0));
258     assertNull(model.getBackground(0, 0));
259     assertEquals("t3st 1", model.getValueAt(0, 0));
260     assertEquals("test 1", model.getValueAt(0, 1));
261     assertEquals("0", model.getValueAt(0, 3));
262     assertNull(model.getForeground(0, 3));
263     assertNull(model.getForeground(2, 3));
264 
265     final StatisticsSet statistics =
266       m_statisticsServices.getStatisticsSetFactory().create();
267     statistics.addValue(
268       m_statisticsServices.getStatisticsIndexMap().getLongIndex("errors"), 1);
269 
270     final TestStatisticsMap testStatisticsMap = new TestStatisticsMap();
271     testStatisticsMap.put(tests[0], statistics);
272     sampleModelImplementation.addTestReport(testStatisticsMap);
273 
274     assertEquals("1", model.getValueAt(0, 3));
275     assertEquals(Color.RED, model.getForeground(0, 3));
276     assertNull(model.getForeground(0, 2));
277     assertEquals(Color.RED, model.getForeground(2, 3));
278   }
279 }