1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| package org.jboss.cache.pojo.observer; |
24 |
| |
25 |
| import junit.framework.TestCase; |
26 |
| import junit.framework.Test; |
27 |
| import junit.framework.TestSuite; |
28 |
| import org.apache.commons.logging.Log; |
29 |
| import org.apache.commons.logging.LogFactory; |
30 |
| import org.jboss.cache.pojo.PojoCache; |
31 |
| import org.jboss.cache.pojo.PojoCacheFactory; |
32 |
| import org.jboss.cache.pojo.PojoCacheListener; |
33 |
| import org.jboss.cache.pojo.observable.Observer; |
34 |
| import org.jboss.cache.pojo.observable.Subject; |
35 |
| import org.jboss.cache.pojo.test.Person; |
36 |
| import org.jboss.cache.pojo.test.Address; |
37 |
| import org.jboss.cache.pojo.test.Student; |
38 |
| import org.jboss.aop.proxy.ClassProxy; |
39 |
| |
40 |
| import java.util.List; |
41 |
| import java.util.Map; |
42 |
| import java.util.HashMap; |
43 |
| import java.util.ArrayList; |
44 |
| import java.util.Set; |
45 |
| import java.util.HashSet; |
46 |
| import java.lang.reflect.Field; |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public class LocalTest extends TestCase |
55 |
| { |
56 |
| Log log = LogFactory.getLog(LocalTest.class); |
57 |
| PojoCache cache_; |
58 |
| |
59 |
2
| public LocalTest(String name)
|
60 |
| { |
61 |
2
| super(name);
|
62 |
| } |
63 |
| |
64 |
2
| protected void setUp() throws Exception
|
65 |
| { |
66 |
2
| super.setUp();
|
67 |
2
| log.info("setUp() ....");
|
68 |
2
| String configFile = "META-INF/local-service.xml";
|
69 |
2
| boolean toStart = false;
|
70 |
2
| cache_ = PojoCacheFactory.createCache(configFile, toStart);
|
71 |
2
| cache_.start();
|
72 |
| } |
73 |
| |
74 |
2
| protected void tearDown() throws Exception
|
75 |
| { |
76 |
2
| super.tearDown();
|
77 |
2
| cache_.stop();
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
2
| private Person createPerson(String id, String name, int age)
|
83 |
| { |
84 |
2
| Person p = new Person();
|
85 |
2
| p.setName(name);
|
86 |
2
| p.setAge(age);
|
87 |
2
| Address add = new Address();
|
88 |
2
| add.setZip(95123);
|
89 |
2
| add.setCity("San Jose");
|
90 |
2
| p.setAddress(add);
|
91 |
2
| cache_.attach(id, p);
|
92 |
2
| return p;
|
93 |
| } |
94 |
| |
95 |
0
| private Student createStudent(String id, String name, int age, String grade)
|
96 |
| { |
97 |
0
| Student p = new Student();
|
98 |
0
| p.setName(name);
|
99 |
0
| p.setAge(age);
|
100 |
0
| p.setYear(grade);
|
101 |
0
| Address add = new Address();
|
102 |
0
| add.setZip(95123);
|
103 |
0
| add.setCity("San Jose");
|
104 |
0
| p.setAddress(add);
|
105 |
0
| cache_.attach(id, p);
|
106 |
0
| return p;
|
107 |
| } |
108 |
| |
109 |
2
| public void testSimple() throws Exception
|
110 |
| { |
111 |
2
| log.info("testSimple() ....");
|
112 |
2
| Person p = createPerson("/person/test1", "Joe Black", 32);
|
113 |
2
| assertEquals((Object) "Joe Black", p.getName());
|
114 |
| |
115 |
2
| MyListener listener = new MyListener(p);
|
116 |
2
| ((Subject)p).addObserver(listener);
|
117 |
| |
118 |
2
| p.setAge(40);
|
119 |
2
| assertTrue("Subject and pojo should be the same ", listener.isSame);
|
120 |
| } |
121 |
| |
122 |
2
| public static Test suite() throws Exception
|
123 |
| { |
124 |
2
| return new TestSuite(LocalTest.class);
|
125 |
| } |
126 |
| |
127 |
| |
128 |
0
| public static void main(String[] args) throws Exception
|
129 |
| { |
130 |
0
| junit.textui.TestRunner.run(suite());
|
131 |
| } |
132 |
| |
133 |
| public class MyListener implements Observer |
134 |
| { |
135 |
| Object pojo; |
136 |
| public boolean isSame; |
137 |
| |
138 |
2
| public MyListener(Object pojo)
|
139 |
| { |
140 |
2
| this.pojo = pojo;
|
141 |
2
| isSame = false;
|
142 |
| } |
143 |
| |
144 |
4
| public void fireChange(Subject subject, Field modifiedField, boolean pre)
|
145 |
| { |
146 |
4
| System.out.println(" Subject: " +subject);
|
147 |
4
| isSame = (pojo == subject) ? true: false;
|
148 |
| } |
149 |
| } |
150 |
| } |