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