1
2
3
4
5
6
7
8
9
10 package nl.tudelft.simulation.jstats.statistics;
11
12 import junit.framework.Assert;
13 import junit.framework.TestCase;
14 import nl.tudelft.simulation.event.Event;
15 import nl.tudelft.simulation.event.EventInterface;
16 import nl.tudelft.simulation.event.EventListenerInterface;
17
18 /***
19 * The counterTest test the counter.
20 * <p>
21 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
22 * University of Technology </a>, the Netherlands. <br>
23 * See for project information <a
24 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
25 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
26 * License (GPL) </a>, no warranty <br>
27 *
28 * @author <a href="http://www.simulation.tudelft.nl/people/jacobs.html">Peter
29 * Jacobs </a>
30 * @version 1.0, 2004-03-18
31 * @since 1.2
32 */
33 public class CounterTest extends TestCase
34 {
35 /*** TEST_METHOD reflects the method which is invoked */
36 public static final String TEST_METHOD = "test";
37
38 /***
39 * constructs a new CounterTest
40 */
41 public CounterTest()
42 {
43 this(TEST_METHOD);
44 }
45
46 /***
47 * constructs a new CounterTest.
48 *
49 * @param arg0 the name of the test method
50 */
51 public CounterTest(final String arg0)
52 {
53 super(arg0);
54 }
55
56 /***
57 * tests the counter
58 */
59 public void test()
60 {
61 String description = "counter description";
62 Counter counter = new Counter(description);
63 Assert.assertEquals(counter.toString(), description);
64 Assert.assertEquals(counter.getDescription(), description);
65
66 Assert.assertTrue(counter.getN() == Long.MIN_VALUE);
67 Assert.assertTrue(counter.getCount() == Long.MIN_VALUE);
68
69 counter.initialize();
70
71 counter.addListener(new EventListenerInterface()
72 {
73 public void notify(final EventInterface event)
74 {
75 Assert.assertTrue(event.getType().equals(Counter.COUNT_EVENT));
76 Assert.assertTrue(event.getContent().getClass().equals(
77 Long.class));
78 }
79 }, Counter.COUNT_EVENT);
80
81 try
82 {
83 counter.notify(new Event(Counter.COUNT_EVENT, this, "errror"));
84 Assert.fail("counter should have reacted on non-number event");
85 } catch (Exception exception)
86 {
87 Assert.assertTrue(exception.getClass().equals(
88 IllegalArgumentException.class));
89 }
90
91 long value = 0;
92 for (int i = 0; i < 100; i++)
93 {
94 counter
95 .notify(new Event(Counter.COUNT_EVENT, this,
96 new Long(2 * i)));
97 value = value + 2 * i;
98 }
99 Assert.assertTrue(counter.getN() == 100);
100 Assert.assertTrue(counter.getCount() == value);
101 }
102 }