1 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 53 54 public class LocalTest extends TestCase 55 { 56 Log log = LogFactory.getLog(LocalTest.class); 57 PojoCache cache_; 58 59 public LocalTest(String name) 60 { 61 super(name); 62 } 63 64 protected void setUp() throws Exception 65 { 66 super.setUp(); 67 log.info("setUp() ...."); 68 String 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 75 { 76 super.tearDown(); 77 cache_.stop(); 78 } 79 80 82 private Person createPerson(String id, String 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 id, String name, int age, String 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 110 { 111 log.info("testSimple() ...."); 112 Person p = createPerson("/person/test1", "Joe Black", 32); 113 assertEquals((Object ) "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 123 { 124 return new TestSuite(LocalTest.class); 125 } 126 127 128 public static void main(String [] args) throws Exception 129 { 130 junit.textui.TestRunner.run(suite()); 131 } 132 133 public class MyListener implements Observer 134 { 135 Object pojo; 136 public boolean isSame; 137 138 public MyListener(Object pojo) 139 { 140 this.pojo = pojo; 141 isSame = false; 142 } 143 144 public void fireChange(Subject subject, Field modifiedField, boolean pre) 145 { 146 System.out.println(" Subject: " +subject); 147 isSame = (pojo == subject) ? true: false; 148 } 149 } 150 } 151 | Popular Tags |