1 package org.jboss.cache.pojo.integrated; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.jboss.cache.pojo.PojoCache; 9 import org.jboss.cache.pojo.PojoCacheFactory; 10 import org.jboss.cache.pojo.test.NetworkAdmin; 11 import org.jboss.cache.pojo.test.NetworkDomain; 12 import org.jboss.cache.pojo.test.NetworkElement; 13 import org.jboss.cache.pojo.test.NetworkNode; 14 import org.jboss.cache.Fqn; 15 16 import java.util.List ; 17 18 29 30 public class ReplicatedNetworkManagementTest extends TestCase 31 { 32 Log log = LogFactory.getLog(ReplicatedNetworkManagementTest.class); 33 PojoCache cache1; 34 PojoCache cache2; 35 36 public ReplicatedNetworkManagementTest(String name) 37 { 38 super(name); 39 } 40 41 protected void setUp() throws Exception 42 { 43 super.setUp(); 44 log.info("setUp() ...."); 45 cache1 = createCache("CacheGroup"); 46 cache2 = createCache("CacheGroup"); 47 } 48 49 protected void tearDown() throws Exception 50 { 51 super.tearDown(); 52 cache1.getCache().removeNode(Fqn.fromString("/")); 53 cache1.stop(); 54 cache2.stop(); 55 } 56 57 private PojoCache createCache(String name) throws Exception 58 { 59 String configFile = "META-INF/replSync-service.xml"; 60 boolean toStart = false; 61 PojoCache cache = PojoCacheFactory.createCache(configFile, toStart); 62 cache.start(); 63 return cache; 64 } 65 66 69 public void testDomainAndAdmin() throws Exception 70 { 71 NetworkAdmin admin = new NetworkAdmin(); 72 admin.setId("Benny"); 73 74 NetworkDomain tempSensorDomain = new NetworkDomain(); 75 tempSensorDomain.setName("Temperature sensors"); 76 tempSensorDomain.setAdmin(admin); 77 78 79 NetworkDomain vibSensorDomain = new NetworkDomain(); 80 vibSensorDomain.setName("Vibration sensors"); 81 vibSensorDomain.setAdmin(admin); 82 83 cache1.attach("/temp", tempSensorDomain); 84 cache1.attach("/vib", vibSensorDomain); 85 86 NetworkDomain d1 = (NetworkDomain) cache2.find("/temp"); 87 NetworkDomain d2 = (NetworkDomain) cache2.find("/vib"); 88 89 assertEquals("Admin should be the same", d1.getAdmin(), d2.getAdmin()); 90 91 d2.getAdmin().setId("Wen"); 92 assertEquals("New admin id is ", "Wen", d1.getAdmin().getId()); 93 assertEquals("New admin id is ", "Wen", admin.getId()); 94 95 d1 = (NetworkDomain) cache2.find("/vib"); 96 d2 = (NetworkDomain) cache2.find("/temp"); 97 assertEquals("Admin should be the same", d1.getAdmin(), d2.getAdmin()); 98 99 d2.getAdmin().setId("Wen"); 100 assertEquals("New admin id is ", "Wen", d1.getAdmin().getId()); 101 assertEquals("New admin id is ", "Wen", admin.getId()); 102 103 NetworkDomain temp1 = (NetworkDomain) cache1.detach("/temp"); 104 NetworkDomain vib1 = (NetworkDomain) cache1.detach("/vib"); 105 106 assertEquals("Vibration sensor ", vibSensorDomain, vib1); 107 assertEquals("Temperature sensor ", tempSensorDomain, temp1); 108 109 } 110 111 114 public void testNodeAndElement() throws Exception 115 { 116 NetworkNode taipei = new NetworkNode(); 118 taipei.setName("Taipei machine"); 119 taipei.setIpAddress("192.168.1.100"); 120 121 NetworkNode tainan = new NetworkNode(); 122 tainan.setName("Tainan machine"); 123 tainan.setIpAddress("192.168.1.200"); 124 125 NetworkElement vibSensor1 = new NetworkElement(); 127 vibSensor1.setName("Vibration sensor V10"); 128 129 NetworkElement vibSensor2 = new NetworkElement(); 130 vibSensor2.setName("Vibration sensor V20"); 131 132 NetworkElement tempSensor1 = new NetworkElement(); 133 tempSensor1.setName("Temperature sensor T10"); 134 135 NetworkElement tempSensor2 = new NetworkElement(); 136 tempSensor2.setName("Temperature sensor T20"); 137 138 taipei.addElement(vibSensor1); 140 taipei.addElement(tempSensor1); 141 tainan.addElement(vibSensor2); 142 tainan.addElement(tempSensor2); 143 144 assertEquals("Element size ", 2, taipei.getElements().size()); 145 146 cache1.attach("/taipei", taipei); 148 cache1.attach("/tainan", tainan); 149 150 NetworkNode taipei1 = (NetworkNode) cache2.find("/taipei"); 151 NetworkNode tainan1 = (NetworkNode) cache2.find("/tainan"); 152 153 taipei1.setIpAddress("192.168.10.100"); 154 assertEquals("New admin id is ", "192.168.10.100", taipei.getIpAddress()); 155 156 tainan1.setIpAddress("192.168.10.200"); 157 assertEquals("New admin id is ", "192.168.10.200", tainan.getIpAddress()); 158 159 List l2 = taipei1.getElements(); 160 assertEquals("Size is ", 2, l2.size()); 161 NetworkElement vibSens1 = (NetworkElement) l2.get(0); 162 NetworkElement tempSens1 = (NetworkElement) l2.get(1); 163 164 int SUSPENDED = 2; 165 vibSens1.setStatus(SUSPENDED); 166 tempSens1.setStatus(SUSPENDED); 167 168 assertEquals("Status ", vibSensor1.getStatus(), vibSens1.getStatus()); 169 assertEquals("Status ", tempSensor1.getStatus(), tempSens1.getStatus()); 170 } 171 172 175 public void testAll() throws Exception 176 { 177 NetworkAdmin admin = new NetworkAdmin(); 178 admin.setId("Benny"); 179 180 NetworkDomain tempSensorDomain = new NetworkDomain(); 182 tempSensorDomain.setName("Temperature sensors"); 183 tempSensorDomain.setAdmin(admin); 184 185 NetworkDomain vibSensorDomain = new NetworkDomain(); 186 vibSensorDomain.setName("Vibration sensors"); 187 vibSensorDomain.setAdmin(admin); 188 189 NetworkNode taipei = new NetworkNode(); 191 taipei.setName("Taipei machine"); 192 taipei.setIpAddress("192.168.1.100"); 193 194 NetworkNode tainan = new NetworkNode(); 195 tainan.setName("Tainan machine"); 196 tainan.setIpAddress("192.168.1.200"); 197 198 NetworkElement vibSensor1 = new NetworkElement(); 200 vibSensor1.setName("Vibration sensor V10"); 201 202 NetworkElement vibSensor2 = new NetworkElement(); 203 vibSensor2.setName("Vibration sensor V20"); 204 205 NetworkElement tempSensor1 = new NetworkElement(); 206 tempSensor1.setName("Temperature sensor T10"); 207 208 NetworkElement tempSensor2 = new NetworkElement(); 209 tempSensor2.setName("Temperature sensor T20"); 210 211 taipei.addElement(vibSensor1); 213 taipei.addElement(tempSensor1); 214 tainan.addElement(vibSensor2); 215 tainan.addElement(tempSensor2); 216 217 assertEquals("Element size ", 2, taipei.getElements().size()); 218 219 vibSensorDomain.addElement(vibSensor1); 221 vibSensorDomain.addElement(vibSensor2); 222 223 tempSensorDomain.addElement(tempSensor1); 224 tempSensorDomain.addElement(tempSensor2); 225 226 cache1.attach("/temp", tempSensorDomain); 228 cache1.attach("/vib", vibSensorDomain); 229 230 NetworkDomain temp1 = (NetworkDomain) cache2.find("/temp"); 231 NetworkDomain vib1 = (NetworkDomain) cache2.find("/vib"); 232 233 NetworkNode taipei1 = (NetworkNode) temp1.getNodes().get(0); 234 NetworkNode taipei2 = (NetworkNode) vib1.getNodes().get(0); 236 237 List l1 = temp1.getNodes(); 238 assertEquals("Size is ", 2, l1.size()); 239 240 l1 = taipei.getElements(); 241 assertEquals("Size is ", 2, l1.size()); 242 243 assertEquals("IPAddress ", taipei.getIpAddress(), taipei1.getIpAddress()); 244 assertTrue("DataNode should be the same", (taipei1 == taipei2)); 245 246 taipei2.setIpAddress("192.168.10.100"); 247 assertEquals("New admin id is ", "192.168.10.100", taipei.getIpAddress()); 248 249 List l2 = taipei1.getElements(); 250 assertEquals("Size is ", 2, l2.size()); 251 NetworkElement vibSens1 = (NetworkElement) l2.get(0); 252 NetworkElement tempSens1 = (NetworkElement) l2.get(1); 253 254 int SUSPENDED = 2; 255 vibSens1.setStatus(SUSPENDED); 256 tempSens1.setStatus(SUSPENDED); 257 258 assertEquals("Status ", vibSensor1.getStatus(), vibSens1.getStatus()); 259 assertEquals("Status ", tempSensor1.getStatus(), tempSens1.getStatus()); 260 } 261 262 public static Test suite() throws Exception 263 { 264 return new TestSuite(ReplicatedNetworkManagementTest.class); 265 } 266 267 public static void main(String [] args) throws Exception 268 { 269 junit.textui.TestRunner.run(suite()); 270 } 271 272 } 273 274 | Popular Tags |