KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > pojo > observer > LocalTest


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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 JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Set JavaDoc;
45 import java.util.HashSet JavaDoc;
46 import java.lang.reflect.Field JavaDoc;
47
48 /**
49  * This is a test case illustrating how to subscribe to POJO events using Observer.
50  *
51  * @author Ben Wang
52  */

53
54 public class LocalTest extends TestCase
55 {
56    Log log = LogFactory.getLog(LocalTest.class);
57    PojoCache cache_;
58
59    public LocalTest(String JavaDoc name)
60    {
61       super(name);
62    }
63
64    protected void setUp() throws Exception JavaDoc
65    {
66       super.setUp();
67       log.info("setUp() ....");
68       String JavaDoc configFile = "META-INF/local-service.xml";
69       boolean toStart = false;
70       cache_ = PojoCacheFactory.createCache(configFile, toStart);
71       cache_.start();
72    }
73
74    protected void tearDown() throws Exception JavaDoc
75    {
76       super.tearDown();
77       cache_.stop();
78    }
79
80 // public void testDummy() {}
81

82    private Person createPerson(String JavaDoc id, String JavaDoc name, int age)
83    {
84       Person p = new Person();
85       p.setName(name);
86       p.setAge(age);
87       Address add = new Address();
88       add.setZip(95123);
89       add.setCity("San Jose");
90       p.setAddress(add);
91       cache_.attach(id, p);
92       return p;
93    }
94
95    private Student createStudent(String JavaDoc id, String JavaDoc name, int age, String JavaDoc grade)
96    {
97       Student p = new Student();
98       p.setName(name);
99       p.setAge(age);
100       p.setYear(grade);
101       Address add = new Address();
102       add.setZip(95123);
103       add.setCity("San Jose");
104       p.setAddress(add);
105       cache_.attach(id, p);
106       return p;
107    }
108
109    public void testSimple() throws Exception JavaDoc
110    {
111       log.info("testSimple() ....");
112       Person p = createPerson("/person/test1", "Joe Black", 32);
113       assertEquals((Object JavaDoc) "Joe Black", p.getName());
114
115       MyListener listener = new MyListener(p);
116       ((Subject)p).addObserver(listener);
117
118       p.setAge(40);
119       assertTrue("Subject and pojo should be the same ", listener.isSame);
120    }
121
122    public static Test suite() throws Exception JavaDoc
123    {
124       return new TestSuite(LocalTest.class);
125    }
126
127
128    public static void main(String JavaDoc[] args) throws Exception JavaDoc
129    {
130       junit.textui.TestRunner.run(suite());
131    }
132
133    public class MyListener implements Observer
134    {
135       Object JavaDoc pojo;
136       public boolean isSame;
137
138       public MyListener(Object JavaDoc pojo)
139       {
140          this.pojo = pojo;
141          isSame = false;
142       }
143
144       public void fireChange(Subject subject, Field JavaDoc modifiedField, boolean pre)
145       {
146          System.out.println(" Subject: " +subject);
147          isSame = (pojo == subject) ? true: false;
148       }
149    }
150 }
151
Popular Tags