View Javadoc

1   // Copyright (C) 2001 - 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.statistics;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.ByteArrayOutputStream;
26  import java.io.ObjectInputStream;
27  import java.io.ObjectOutputStream;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import junit.framework.TestCase;
32  import net.grinder.common.StubTest;
33  import net.grinder.common.Test;
34  import net.grinder.testutility.RandomStubFactory;
35  
36  
37  /**
38   * Unit test case for <code>TestStatisticsMap</code>.
39   *
40   * @author Philip Aston
41   * @see TestStatisticsMap
42   */
43  public class TestTestStatisticsMap extends TestCase {
44  
45    private final Test m_test0 = new StubTest(0, "foo");
46    private final Test m_test1 = new StubTest(1, "");
47    private final Test m_test2 = new StubTest(2, "");
48    private StatisticsSet m_statistics0;
49    private StatisticsSet m_statistics1;
50    private StatisticsIndexMap.LongIndex m_index;
51  
52    private final StatisticsServices m_statisticsServices =
53      StatisticsServicesImplementation.getInstance();
54  
55    protected void setUp() throws Exception {
56      final StatisticsSetFactory factory =
57        m_statisticsServices.getStatisticsSetFactory();
58  
59      m_statistics0 = factory.create();
60      m_statistics1 = factory.create();
61  
62      m_index =
63        m_statisticsServices.getStatisticsIndexMap().getLongIndex("userLong0");
64  
65      m_statistics0.addValue(m_index, 10);
66    }
67  
68    public void testPut() throws Exception {
69  
70      final TestStatisticsMap map =
71        new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
72  
73      assertEquals(0, map.size());
74  
75      map.put(m_test0, m_statistics0);
76      assertEquals(1, map.size());
77  
78      map.put(m_test0, m_statistics1);
79      assertEquals(1, map.size());
80  
81      map.put(m_test1, m_statistics1);
82      assertEquals(2, map.size());
83  
84      final StatisticsSet bogusStatisticsSetImplementation =
85        RandomStubFactory.create(StatisticsSet.class).getStub();
86  
87      try {
88        map.put(m_test1, bogusStatisticsSetImplementation);
89        fail("Expected AssertionError");
90      }
91      catch (AssertionError e) {
92      }
93    }
94  
95    public void testEqualsAndHashCode() throws Exception {
96  
97      final TestStatisticsMap map0 =
98        new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
99      final TestStatisticsMap map1 =
100       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
101 
102     assertEquals(map0, map0);
103     assertEquals(map0, map1);
104 
105     map0.put(m_test0, m_statistics0);
106     assertFalse(map0.equals(map1));
107 
108     map1.put(m_test1, m_statistics0);
109     assertFalse(map0.equals(map1));
110 
111     map0.put(m_test1, m_statistics0);
112     map1.put(m_test0, m_statistics0);
113     assertEquals(map0, map0);
114     assertEquals(map0, map1);
115 
116     map1.put(m_test0, m_statistics1);
117     assertFalse(map0.equals(map1));
118 
119     assertFalse(map0.equals(this));
120     assertEquals(map0.hashCode(), map0.hashCode());
121     assertFalse(map0.hashCode() == map1.hashCode());
122   }
123 
124   public void testAdd() throws Exception {
125     final TestStatisticsMap map0 =
126       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
127     final TestStatisticsMap map1 =
128       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
129 
130     assertEquals(map0, map1);
131 
132     map0.put(m_test0, m_statistics0);
133     assertFalse(map0.equals(map1));
134 
135     map1.add(map0);
136 
137     assertEquals(map0, map1);
138 
139     map1.add(map0);
140 
141     assertEquals(1, map1.size());
142 
143     final Pair content = extract(map1).get(0);
144     assertEquals(m_test0, content.getTest());
145     assertEquals(20, content.getStatisticsSet().getValue(m_index));
146   }
147 
148   public void testReset() throws Exception {
149     final TestStatisticsMap map =
150       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
151     assertEquals(0, map.size());
152 
153     final TestStatisticsMap snapshot0 = map.reset();
154     assertEquals(map, snapshot0);
155 
156     map.put(m_test0, m_statistics0);
157     assertFalse(map.equals(snapshot0));
158 
159     final TestStatisticsMap snapshot1 = map.reset();
160     assertFalse(snapshot1.equals(snapshot0));
161     assertFalse(snapshot1.equals(map));
162     assertEquals(1, snapshot1.size());
163     assertEquals(1, map.size());
164 
165     final Pair content = extract(map).get(0);
166     assertEquals(m_test0, content.getTest());
167     assertEquals(0, content.getStatisticsSet().getValue(m_index));
168 
169     final Pair snapShotContent = extract(snapshot1).get(0);
170     assertEquals(m_test0, snapShotContent.getTest());
171     assertEquals(10, snapShotContent.getStatisticsSet().getValue(m_index));
172 
173     // The map was reset, so the statistics will be zero and reset()
174     // will not copy them through.
175     final TestStatisticsMap snapshot2 = map.reset();
176     assertFalse(snapshot2.equals(snapshot1));
177     assertFalse(snapshot2.equals(map));
178     assertEquals(0, snapshot2.size());
179     assertEquals(1, map.size());
180   }
181 
182   public void testToString() throws Exception {
183     final TestStatisticsMap map =
184       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
185     final String s0 = map.toString();
186     assertEquals("TestStatisticsMap = {}", s0);
187 
188     map.put(m_test0, m_statistics0);
189     final String s1 = map.toString();
190     assertTrue(s1.indexOf("(") > 0);
191     assertTrue(s1.indexOf("10") > 0);
192   }
193 
194   public void testSerialisation() throws Exception {
195 
196     final TestStatisticsMap original0 =
197       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
198     original0.put(m_test0, m_statistics0);
199     original0.put(m_test1, m_statistics0);
200 
201     final TestStatisticsMap original1 =
202       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
203 
204     final ByteArrayOutputStream byteOutputStream =
205       new ByteArrayOutputStream();
206 
207     final ObjectOutputStream objectOutputStream =
208       new ObjectOutputStream(byteOutputStream);
209 
210     objectOutputStream.writeObject(original0);
211     objectOutputStream.writeObject(original1);
212 
213     objectOutputStream.close();
214 
215     final ObjectInputStream objectInputStream =
216       new ObjectInputStream(
217         new ByteArrayInputStream(byteOutputStream.toByteArray()));
218 
219     final TestStatisticsMap received0 =
220       (TestStatisticsMap)objectInputStream.readObject();
221 
222     final TestStatisticsMap received1 =
223       (TestStatisticsMap)objectInputStream.readObject();
224 
225     assertEquals(original0, received0);
226     assertEquals(original1, received1);
227 
228     assertEquals(2, received0.size());
229 
230     for (Pair pair : extract(received0)) {
231       assertEquals(m_statistics0, pair.getStatisticsSet());
232       // We get a simplified test object.
233       assertEquals("", pair.getTest().getDescription());
234     }
235   }
236 
237   public void testTotalsMethods() throws Exception {
238     final TestStatisticsMap map =
239       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
240     map.put(m_test0, m_statistics0);
241     map.put(m_test1, m_statistics0);
242     map.put(m_test2, m_statistics1);
243 
244     m_statistics1.setValue(m_index, 3);
245     m_statistics1.setIsComposite();
246 
247     final StatisticsSet compositeTotals = map.compositeStatisticsTotals();
248     assertEquals(3, compositeTotals.getValue(m_index));
249 
250     final StatisticsSet nonCompositeTotals = map.nonCompositeStatisticsTotals();
251     assertEquals(20, nonCompositeTotals.getValue(m_index));
252   }
253 
254   public void testSynchronisation() throws Exception {
255     final TestStatisticsMap map =
256       new TestStatisticsMap(m_statisticsServices.getStatisticsSetFactory());
257 
258     final Thread[] threads = new Thread[10];
259     final Boolean[] start = { Boolean.FALSE };
260     final Boolean[] sucess = { Boolean.TRUE };
261 
262     for (int i=0; i<threads.length; ++i) {
263       final int n = i;
264 
265       threads[i] = new Thread() {
266         public void run() {
267           synchronized (start) {
268             while (!start[0].booleanValue()) {
269               try {
270                 start.wait();
271               }
272               catch (InterruptedException e) {
273                 Thread.interrupted();
274               }
275             }
276           }
277 
278           try {
279             for (int j=0; j<100; ++j) {
280               map.reset();
281               map.put(new StubTest(n*100 + j, ""), m_statistics0);
282             }
283           }
284           catch (Exception e) {
285             e.printStackTrace();
286             synchronized (sucess) {
287               sucess[0] = Boolean.FALSE;
288             }
289           }
290         }
291       };
292     }
293 
294     for (int i=0; i<threads.length; ++i) {
295       threads[i].start();
296     }
297 
298     synchronized (start) {
299       start[0] = Boolean.TRUE;
300       start.notifyAll();
301     }
302 
303     for (int i=0; i<threads.length; ++i) {
304       threads[i].join();
305     }
306 
307     synchronized (sucess) {
308       assertTrue(sucess[0].booleanValue());
309     }
310 
311     assertEquals(1000, map.size());
312   }
313 
314   private static final class Pair {
315     private final Test m_test;
316     private final StatisticsSet m_statisticsSet;
317 
318     public Pair(Test test, StatisticsSet statisticsSet) {
319       m_test = test;
320       m_statisticsSet = statisticsSet;
321     }
322 
323     public Test getTest() { return m_test; }
324     public StatisticsSet getStatisticsSet() { return m_statisticsSet; }
325   }
326 
327   private static final List<Pair> extract(final TestStatisticsMap map) {
328 
329     final List<Pair> result = new ArrayList<Pair>();
330 
331     map.new ForEach() {
332       @Override
333       protected void next(Test test, StatisticsSet statistics) {
334         result.add(new Pair(test, statistics));
335       }
336     }.iterate();
337 
338     return result;
339   }
340 }