1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.notification; |
9 |
| |
10 |
| import junit.framework.Test; |
11 |
| import junit.framework.TestCase; |
12 |
| import junit.framework.TestSuite; |
13 |
| |
14 |
| import org.jboss.cache.pojo.PojoCache; |
15 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
16 |
| import org.jboss.cache.pojo.notification.event.AttachedEvent; |
17 |
| import org.jboss.cache.pojo.notification.event.DetachedEvent; |
18 |
| import org.jboss.cache.pojo.notification.event.FieldModifiedEvent; |
19 |
| import org.jboss.cache.pojo.notification.event.Event; |
20 |
| import org.jboss.cache.pojo.test.Address; |
21 |
| import org.jboss.cache.pojo.test.Person; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| public class ObjectTest extends TestCase |
31 |
| { |
32 |
| private PojoCache cache; |
33 |
| private Listener listener; |
34 |
| |
35 |
4
| public ObjectTest(String name)
|
36 |
| { |
37 |
4
| super(name);
|
38 |
| } |
39 |
| |
40 |
4
| protected void setUp() throws Exception
|
41 |
| { |
42 |
4
| super.setUp();
|
43 |
4
| String configFile = "META-INF/local-service.xml";
|
44 |
4
| boolean toStart = false;
|
45 |
4
| cache = PojoCacheFactory.createCache(configFile, toStart);
|
46 |
4
| cache.start();
|
47 |
| |
48 |
4
| listener = new Listener();
|
49 |
4
| cache.addListener(listener);
|
50 |
| } |
51 |
| |
52 |
9
| private <T extends Event> T takeNotification(Class<T> clazz)
|
53 |
| { |
54 |
9
| T notification = listener.take(clazz);
|
55 |
9
| verifyNotification(notification);
|
56 |
| |
57 |
9
| return notification;
|
58 |
| } |
59 |
| |
60 |
9
| protected void verifyNotification(Event notification)
|
61 |
| { |
62 |
9
| assertSame(cache, notification.getContext().getPojoCache());
|
63 |
9
| assertEquals(true, notification.isLocal());
|
64 |
| } |
65 |
| |
66 |
1
| public static Test suite() throws Exception
|
67 |
| { |
68 |
1
| return new TestSuite(ObjectTest.class);
|
69 |
| } |
70 |
| |
71 |
0
| public static void main(String[] args) throws Exception
|
72 |
| { |
73 |
0
| junit.textui.TestRunner.run(ObjectTest.suite());
|
74 |
| } |
75 |
| |
76 |
4
| protected void tearDown() throws Exception
|
77 |
| { |
78 |
4
| super.tearDown();
|
79 |
4
| cache.stop();
|
80 |
| } |
81 |
| |
82 |
1
| public void testAttachNotification() throws Exception
|
83 |
| { |
84 |
1
| Person test = new Person();
|
85 |
1
| test.setName("Ben");
|
86 |
1
| test.setAge(10);
|
87 |
1
| cache.attach("/a", test);
|
88 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
89 |
1
| assertEquals(test, attach.getSource());
|
90 |
| } |
91 |
| |
92 |
1
| public void testAttachNotification2() throws Exception
|
93 |
| { |
94 |
1
| Person test = new Person();
|
95 |
1
| test.setName("Ben");
|
96 |
1
| test.setAge(10);
|
97 |
1
| Address addr = new Address();
|
98 |
1
| test.setAddress(addr);
|
99 |
1
| cache.attach("/a", test);
|
100 |
| |
101 |
| |
102 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
103 |
1
| assertEquals(addr, attach.getSource());
|
104 |
| |
105 |
| |
106 |
1
| attach = takeNotification(AttachedEvent.class);
|
107 |
1
| assertEquals(test, attach.getSource());
|
108 |
| } |
109 |
| |
110 |
1
| public void testDetachNotification() throws Exception
|
111 |
| { |
112 |
1
| Person test = new Person();
|
113 |
1
| test.setName("Ben");
|
114 |
1
| test.setAge(10);
|
115 |
1
| cache.attach("/a", test);
|
116 |
| |
117 |
| |
118 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
119 |
1
| assertEquals(test, attach.getSource());
|
120 |
| |
121 |
1
| cache.detach("/a");
|
122 |
| |
123 |
| |
124 |
1
| DetachedEvent detach = takeNotification(DetachedEvent.class);
|
125 |
1
| assertEquals(test, detach.getSource());
|
126 |
| } |
127 |
| |
128 |
1
| public void testFieldNotification() throws Exception
|
129 |
| { |
130 |
1
| Person test = new Person();
|
131 |
1
| test.setName("Ben");
|
132 |
1
| test.setAge(10);
|
133 |
1
| cache.attach("/a", test);
|
134 |
| |
135 |
| |
136 |
1
| AttachedEvent attach = takeNotification(AttachedEvent.class);
|
137 |
1
| assertEquals(test, attach.getSource());
|
138 |
| |
139 |
| |
140 |
1
| test.setAge(20);
|
141 |
1
| FieldModifiedEvent modify = takeNotification(FieldModifiedEvent.class);
|
142 |
1
| assertEquals(test, modify.getSource());
|
143 |
1
| assertEquals(test.getClass().getDeclaredField("age"), modify.getField());
|
144 |
1
| assertEquals(20, modify.getValue());
|
145 |
| |
146 |
| |
147 |
1
| Address addr = new Address();
|
148 |
1
| addr.setCity("Madison");
|
149 |
1
| addr.setStreet("State St.");
|
150 |
1
| addr.setZip(53703);
|
151 |
1
| test.setAddress(addr);
|
152 |
| |
153 |
| |
154 |
1
| attach = takeNotification(AttachedEvent.class);
|
155 |
1
| assertEquals(addr, attach.getSource());
|
156 |
| |
157 |
| |
158 |
1
| modify = takeNotification(FieldModifiedEvent.class);
|
159 |
1
| assertEquals(test, modify.getSource());
|
160 |
1
| assertEquals(test.getClass().getDeclaredField("address"), modify.getField());
|
161 |
1
| assertEquals(addr, modify.getValue());
|
162 |
| } |
163 |
| } |