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.Student; 13 14 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 private PojoCache cache1_; 31 private PojoCache cache2_; 32 33 protected void setUp() throws Exception { 34 cache1_ = createCache("TestCluster"); 35 cache2_ = createCache("TestCluster"); 36 init(); 37 } 38 39 protected void tearDown() throws Exception { 40 cache1_.getCache().removeNode(new Fqn("/")); 41 cache1_.stop(); 42 cache2_.stop(); 43 } 44 45 private PojoCache createCache(String name) throws Exception { 46 XmlConfigurationParser parser = new XmlConfigurationParser(); 47 Configuration conf = parser.parseFile("META-INF/replSync-service.xml"); 48 conf.setClusterName(name); boolean toStart = true; 50 PojoCache cache = PojoCacheFactory.createCache(conf, toStart); 51 return cache; 54 } 55 56 61 protected void init() throws Exception { 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 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 { 97 98 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 printStatus("Initial state for Mary", mary_); 106 printStatus("Initial state for Joe", joe_); 107 108 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 foo_.setRoom("101 Alvarez"); 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 joe2.addCourse(bar_); 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 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 msg, Object 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 [] args) throws Exception { 143 StudentMaintTest smTest = new StudentMaintTest(); 144 smTest.setUp(); 145 smTest.testPropagation(); 146 smTest.tearDown(); 147 } 148 } 149 | Popular Tags |