1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
36
37
38
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 }