View Javadoc

1   // Copyright (C) 2000 Paco Gomez
2   // Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Philip Aston
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 junit.framework.TestCase;
26  
27  import java.io.ByteArrayInputStream;
28  import java.io.ByteArrayOutputStream;
29  import java.io.ObjectInputStream;
30  import java.io.ObjectOutputStream;
31  import java.util.Random;
32  
33  
34  /**
35   * Unit test case for <code>StatisticsSetFactory</code>.
36   *
37   * @author Philip Aston
38   * @see StatisticsSet
39   */
40  public class TestStatisticsSetFactory extends TestCase {
41  
42    public TestStatisticsSetFactory(String name) {
43      super(name);
44    }
45  
46    private final StatisticsServices m_statisticsServices =
47      StatisticsServicesImplementation.getInstance();
48  
49    public void testCreation() throws Exception {
50      final StatisticsSetFactory factory =
51        m_statisticsServices.getStatisticsSetFactory();
52  
53      assertSame(factory, m_statisticsServices.getStatisticsSetFactory());
54    }
55  
56    public void testFactory() throws Exception {
57      final StatisticsSetFactory factory =
58        m_statisticsServices.getStatisticsSetFactory();
59  
60      final StatisticsSet statistics = factory.create();
61      assertTrue(statistics instanceof StatisticsSetImplementation);
62    }
63  
64    public void testSerialisation() throws Exception {
65      final StatisticsSetFactory factory =
66        m_statisticsServices.getStatisticsSetFactory();
67  
68      final Random random = new Random();
69  
70      final StatisticsIndexMap indexMap =
71        m_statisticsServices.getStatisticsIndexMap();
72  
73      final StatisticsIndexMap.LongIndex aIndex = indexMap
74          .getLongIndex("userLong0");
75      final StatisticsIndexMap.LongIndex bIndex = indexMap
76          .getLongIndex("userLong1");
77      final StatisticsIndexMap.LongIndex cIndex = indexMap
78          .getLongIndex("userLong2");
79  
80      final StatisticsSet original0 = factory.create();
81      original0.addValue(aIndex, Math.abs(random.nextLong()));
82      original0.addValue(bIndex, Math.abs(random.nextLong()));
83      original0.addValue(cIndex, Math.abs(random.nextLong()));
84  
85      final StatisticsSet original1 = factory.create();
86  
87      final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
88  
89      final ObjectOutputStream objectOutputStream = new ObjectOutputStream(
90        byteOutputStream);
91  
92      factory.writeStatisticsExternal(objectOutputStream,
93        (StatisticsSetImplementation) original0);
94      factory.writeStatisticsExternal(objectOutputStream,
95        (StatisticsSetImplementation) original1);
96  
97      objectOutputStream.close();
98  
99      final ObjectInputStream objectInputStream = new ObjectInputStream(
100       new ByteArrayInputStream(byteOutputStream.toByteArray()));
101 
102     final StatisticsSet received0 = factory
103         .readStatisticsExternal(objectInputStream);
104 
105     final StatisticsSet received1 = factory
106         .readStatisticsExternal(objectInputStream);
107 
108     assertEquals(original0, received0);
109     assertEquals(original1, received1);
110   }
111 }