1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.common;
23
24 import junit.framework.TestCase;
25
26
27
28
29
30
31
32 public class TestAbstractTestSemantics extends TestCase {
33
34 public static class ConcreteTest extends AbstractTestSemantics {
35 private final int m_number;
36 private final String m_description;
37
38 public ConcreteTest(int number, String description) {
39 m_number = number;
40 m_description = description;
41 }
42
43 public int getNumber() {
44 return m_number;
45 }
46
47 public String getDescription() {
48 return m_description;
49 }
50 }
51
52 public void testEquals() throws Exception {
53 final Test t1 = new ConcreteTest(10, "foo");
54 final Test t2 = new ConcreteTest(10, "bah");
55 final Test t3 = new ConcreteTest(1, "blah");
56 final Test t4 = new Test() {
57 public int getNumber() { return 1; }
58 public String getDescription() { return null; }
59 public final int compareTo(Test o) { return 0; }
60 };
61
62 assertEquals(t3, t3);
63 assertEquals(t1, t2);
64 assertEquals(t2, t1);
65 assertTrue(!t1.equals(t3));
66 assertTrue(!t1.equals(new Integer(10)));
67 assertEquals(t3, t4);
68 assertTrue(!t2.equals(t4));
69 }
70
71 public void testHashCode() throws Exception {
72 final Test t1 = new ConcreteTest(10, null);
73 final Test t2 = new ConcreteTest(10, "bah");
74 final Test t3 = new ConcreteTest(1, "blah");
75
76 assertEquals(t3.hashCode(), t3.hashCode());
77 assertEquals(t1.hashCode(), t2.hashCode());
78 assertTrue(t1.hashCode() != t3.hashCode());
79 }
80
81 public void testToString() {
82 final Test t1 = new ConcreteTest(10, null);
83 final Test t2 = new ConcreteTest(99, "bah");
84
85 assertTrue(t1.toString().indexOf("10") >= 0);
86 assertTrue(t2.toString().indexOf("99") >= 0);
87 assertTrue(t2.toString().indexOf("bah") >= 0);
88 }
89 }