KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > examples > StudentMaintTest


1 package test.examples;
2
3 import junit.framework.TestCase;
4 import org.jboss.cache.pojo.PojoCache;
5 import org.jboss.cache.factories.XmlConfigurationParser;
6 import org.jboss.cache.config.Configuration;
7 import org.jboss.cache.pojo.PojoCacheFactory;
8 import org.jboss.cache.Fqn;
9 import examples.Address;
10 import examples.Course;
11 //import examples.Person;
12
import examples.Student;
13
14 /**
15  * Driver test to illustrate the sensor netowrk supervising system using pojo cache. By using the cache, it will have:
16  * <ul>
17  * <li>automatic state fail over</li>
18  * <li>fine-grained replication</li>
19  * <li>preservation of object graph relationship</li>
20  * </ul>
21  */

22 public class StudentMaintTest extends TestCase {
23
24    private Student joe_;
25    private Student mary_;
26    private Course foo_;
27    private Course bar_;
28
29    // cache1 and cache2 are in the same clustering group.
30
private PojoCache cache1_;
31    private PojoCache cache2_;
32
33    protected void setUp() throws Exception JavaDoc {
34       cache1_ = createCache("TestCluster");
35       cache2_ = createCache("TestCluster");
36       init();
37    }
38
39    protected void tearDown() throws Exception JavaDoc {
40       cache1_.getCache().removeNode(new Fqn("/"));
41       cache1_.stop();
42       cache2_.stop();
43    }
44
45    private PojoCache createCache(String JavaDoc name) throws Exception JavaDoc {
46       XmlConfigurationParser parser = new XmlConfigurationParser();
47       Configuration conf = parser.parseFile("META-INF/replSync-service.xml");
48       conf.setClusterName(name); // We can set a different cluster group.
49
boolean toStart = true;
50       PojoCache cache = PojoCacheFactory.createCache(conf, toStart);
51       // Or
52
// PojoCache cache = PojoCacheFactory.createCache("META-INF/replSync-service.xml", toStart);
53
return cache;
54    }
55
56    /**
57     * Populate the propagation tree.
58     *
59     * @throws Exception
60     */

61    protected void init() throws Exception JavaDoc {
62
63
64       mary_ = new Student();
65       mary_.setName("Mary Smith");
66
67       Address address = new Address();
68       address.setStreet("456 Oak Drive");
69       address.setCity("Pleasantville, CA");
70       address.setZip(94555);
71
72       mary_.setAddress(address);
73
74
75       joe_ = new Student();
76       joe_.setName("Joe Smith");
77       joe_.setSchool("Engineering");
78
79       // Mary and Joe have the same address
80
joe_.setAddress(address);
81
82       foo_ = new Course();
83       foo_.setTitle("Intro to Foo");
84       foo_.setInstructor("Jones");
85
86       joe_.addCourse(foo_);
87       mary_.addCourse(foo_);
88
89       bar_ = new Course();
90       bar_.setTitle("Advanced Bar");
91       bar_.setInstructor("Woods");
92       bar_.setRoom("104 Encina");
93
94    }
95
96    public void testPropagation() throws Exception JavaDoc {
97
98       // Here we ask the pojo cache to manage mary_ and joe_
99
cache1_.attach("students/54321", mary_);
100       cache1_.attach("students/65432", joe_);
101       cache1_.attach("courses/101", foo_);
102       cache1_.attach("courses/401", bar_);
103
104       // Output
105
printStatus("Initial state for Mary", mary_);
106       printStatus("Initial state for Joe", joe_);
107
108       // Retrieve the pojos from the Server #2
109
Student mary2 = (Student) cache2_.find("students/54321");
110       Student joe2 = (Student) cache2_.find("students/65432");
111       Course foo2 = (Course) cache2_.find("courses/101");
112
113       System.out.println("---------------------------------------------");
114       System.out.println("Modified on Server #1");
115        // Change state in one of the items. This will be fine-grained replicated
116
foo_.setRoom("101 Alvarez"); // Modified state on cache #2
117
printStatus("Course Update: id: 401 room: null->101 Alvarez (retrieved from cache #2)"
118                   , foo2);
119
120       System.out.println("---------------------------------------------");
121       System.out.println("Modified on Server #2");
122        // Change state in one of the items. This will be fine-grained replicated
123
joe2.addCourse(bar_); // Modified state on cache #2
124
printStatus("Student Update: id: 65432 addCourse: Advanced Bar (retrieved from cache #1)"
125                   , joe_);
126
127       System.out.println("---------------------------------------------");
128       System.out.println("Modified on Server #1");
129        // Change state in one of the items. This will be fine-grained replicated.
130
mary_.setSchool("Engineering");
131       printStatus("Student Update: id: 65432 school: null->Engineering (retrieved from cache #2)", mary2);
132
133    }
134
135    private void printStatus(String JavaDoc msg, Object JavaDoc obj) {
136       System.out.println("---------------------------------------------");
137       System.out.println(msg);
138       System.out.println("---------------------------------------------");
139       System.out.println(obj.toString());
140    }
141
142    public static void main(String JavaDoc[] args) throws Exception JavaDoc {
143       StudentMaintTest smTest = new StudentMaintTest();
144       smTest.setUp();
145       smTest.testPropagation();
146       smTest.tearDown();
147    }
148 }
149
Popular Tags