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 NetworkManagementTest extends TestCase 31 { 32 Log log = LogFactory.getLog(NetworkManagementTest.class); 33 PojoCache cache1; 34 35 public NetworkManagementTest(String name) 36 { 37 super(name); 38 } 39 40 protected void setUp() throws Exception 41 { 42 super.setUp(); 43 log.info("setUp() ...."); 44 cache1 = createCache("CacheGroup"); 45 } 46 47 protected void tearDown() throws Exception 48 { 49 super.tearDown(); 50 cache1.getCache().removeNode(Fqn.fromString("/")); 51 cache1.stop(); 52 } 53 54 private PojoCache createCache(String name) throws Exception 55 { 56 String configFile = "META-INF/replSync-service.xml"; 57 boolean toStart = false; 58 PojoCache cache = PojoCacheFactory.createCache(configFile, toStart); 59 cache.start(); 60 return cache; 61 } 62 63 66 public void testDomainAndAdmin() throws Exception 67 { 68 NetworkAdmin admin = new NetworkAdmin(); 69 admin.setId("Benny"); 70 71 NetworkDomain tempSensorDomain = new NetworkDomain(); 72 tempSensorDomain.setName("Temperature sensors"); 73 tempSensorDomain.setAdmin(admin); 74 75 76 NetworkDomain vibSensorDomain = new NetworkDomain(); 77 vibSensorDomain.setName("Vibration sensors"); 78 vibSensorDomain.setAdmin(admin); 79 80 cache1.attach("/temp", tempSensorDomain); 81 cache1.attach("/vib", vibSensorDomain); 82 83 NetworkDomain d1 = (NetworkDomain) cache1.find("/temp"); 84 NetworkDomain d2 = (NetworkDomain) cache1.find("/vib"); 85 86 assertEquals("Admin should be the same", d1.getAdmin(), d2.getAdmin()); 87 88 d2.getAdmin().setId("Wen"); 89 assertEquals("New admin id is ", "Wen", d1.getAdmin().getId()); 90 91 assertEquals("New admin id is ", "Wen", admin.getId()); 92 93 NetworkDomain temp1 = (NetworkDomain) cache1.detach("/temp"); 94 NetworkDomain vib1 = (NetworkDomain) cache1.detach("/vib"); 95 96 assertEquals("Vibration sensor ", vibSensorDomain, vib1); 97 assertEquals("Temperature sensor ", tempSensorDomain, temp1); 98 } 99 100 103 public void testNodeAndElement() throws Exception 104 { 105 NetworkNode taipei = new NetworkNode(); 107 taipei.setName("Taipei machine"); 108 taipei.setIpAddress("192.168.1.100"); 109 110 NetworkNode tainan = new NetworkNode(); 111 tainan.setName("Tainan machine"); 112 tainan.setIpAddress("192.168.1.200"); 113 114 NetworkElement vibSensor1 = new NetworkElement(); 116 vibSensor1.setName("Vibration sensor V10"); 117 118 NetworkElement vibSensor2 = new NetworkElement(); 119 vibSensor2.setName("Vibration sensor V20"); 120 121 NetworkElement tempSensor1 = new NetworkElement(); 122 tempSensor1.setName("Temperature sensor T10"); 123 124 NetworkElement tempSensor2 = new NetworkElement(); 125 tempSensor2.setName("Temperature sensor T20"); 126 127 taipei.addElement(vibSensor1); 129 taipei.addElement(tempSensor1); 130 tainan.addElement(vibSensor2); 131 tainan.addElement(tempSensor2); 132 133 assertEquals("Element size ", 2, taipei.getElements().size()); 134 135 cache1.attach("/taipei", taipei); 137 cache1.attach("/tainan", tainan); 138 139 NetworkNode taipei1 = (NetworkNode) cache1.find("/taipei"); 140 NetworkNode tainan1 = (NetworkNode) cache1.find("/tainan"); 141 142 taipei1.setIpAddress("192.168.10.100"); 143 assertEquals("New admin id is ", "192.168.10.100", taipei.getIpAddress()); 144 145 List l2 = taipei1.getElements(); 146 assertEquals("Size is ", 2, l2.size()); 147 NetworkElement vibSens1 = (NetworkElement) l2.get(0); 148 NetworkElement tempSens1 = (NetworkElement) l2.get(1); 149 150 int SUSPENDED = 2; 151 vibSens1.setStatus(SUSPENDED); 152 tempSens1.setStatus(SUSPENDED); 153 154 assertEquals("Status ", vibSensor1.getStatus(), vibSens1.getStatus()); 155 assertEquals("Status ", tempSensor1.getStatus(), tempSens1.getStatus()); 156 } 157 158 161 public void testSharedNodeAndElement() throws Exception 162 { 163 NetworkNode taipei = new NetworkNode(); 165 taipei.setName("Taipei machine"); 166 taipei.setIpAddress("192.168.1.100"); 167 168 NetworkNode tainan = new NetworkNode(); 169 tainan.setName("Tainan machine"); 170 tainan.setIpAddress("192.168.1.200"); 171 172 NetworkElement vibSensor1 = new NetworkElement(); 174 vibSensor1.setName("Vibration sensor V10"); 175 176 NetworkElement vibSensor2 = new NetworkElement(); 177 vibSensor2.setName("Vibration sensor V20"); 178 179 NetworkElement tempSensor1 = new NetworkElement(); 180 tempSensor1.setName("Temperature sensor T10"); 181 182 NetworkElement tempSensor2 = new NetworkElement(); 183 tempSensor2.setName("Temperature sensor T20"); 184 185 taipei.addElement(vibSensor1); 187 taipei.addElement(tempSensor1); 188 tainan.addElement(vibSensor2); 189 tainan.addElement(tempSensor2); 190 191 assertEquals("Element size ", 2, taipei.getElements().size()); 192 193 cache1.attach("/taipei", taipei); 195 cache1.attach("/alias", taipei); 196 197 NetworkNode taipei1 = (NetworkNode) cache1.find("/taipei"); 198 NetworkNode taipei2 = (NetworkNode) cache1.find("/alias"); 199 200 taipei1.setIpAddress("192.168.10.100"); 201 assertEquals("New admin id is ", "192.168.10.100", taipei.getIpAddress()); 202 203 List l2 = taipei2.getElements(); 204 assertEquals("Size is ", 2, l2.size()); 205 NetworkElement vibSens1 = (NetworkElement) l2.get(0); 206 NetworkElement tempSens1 = (NetworkElement) l2.get(1); 207 208 int SUSPENDED = 2; 209 vibSens1.setStatus(SUSPENDED); 210 tempSens1.setStatus(SUSPENDED); 211 212 assertEquals("Status ", vibSensor1.getStatus(), vibSens1.getStatus()); 213 assertEquals("Status ", tempSensor1.getStatus(), tempSens1.getStatus()); 214 } 215 216 217 220 public void testRecursive() throws Exception 221 { 222 NetworkAdmin admin = new NetworkAdmin(); 223 admin.setId("Benny"); 224 225 NetworkDomain tempSensorDomain = new NetworkDomain(); 227 tempSensorDomain.setName("Temperature sensors"); 228 tempSensorDomain.setAdmin(admin); 229 230 NetworkDomain vibSensorDomain = new NetworkDomain(); 231 vibSensorDomain.setName("Vibration sensors"); 232 vibSensorDomain.setAdmin(admin); 233 234 NetworkNode taipei = new NetworkNode(); 236 taipei.setName("Taipei machine"); 237 taipei.setIpAddress("192.168.1.100"); 238 239 NetworkNode tainan = new NetworkNode(); 240 tainan.setName("Tainan machine"); 241 tainan.setIpAddress("192.168.1.200"); 242 243 NetworkElement vibSensor1 = new NetworkElement(); 245 vibSensor1.setName("Vibration sensor V10"); 246 247 NetworkElement vibSensor2 = new NetworkElement(); 248 vibSensor2.setName("Vibration sensor V20"); 249 250 NetworkElement tempSensor1 = new NetworkElement(); 251 tempSensor1.setName("Temperature sensor T10"); 252 253 NetworkElement tempSensor2 = new NetworkElement(); 254 tempSensor2.setName("Temperature sensor T20"); 255 256 taipei.addElement(vibSensor1); 258 taipei.addElement(tempSensor1); 259 tainan.addElement(vibSensor2); 260 tainan.addElement(tempSensor2); 261 262 assertEquals("Element size ", 2, taipei.getElements().size()); 263 264 vibSensorDomain.addElement(vibSensor1); 266 vibSensorDomain.addElement(vibSensor2); 267 268 tempSensorDomain.addElement(tempSensor1); 269 tempSensorDomain.addElement(tempSensor2); 270 271 cache1.attach("/vib", vibSensorDomain); 273 274 } 276 277 280 public void testAll() throws Exception 281 { 282 NetworkDomain tempSensorDomain = new NetworkDomain(); 284 tempSensorDomain.setName("Temperature sensors"); 285 286 NetworkDomain vibSensorDomain = new NetworkDomain(); 287 vibSensorDomain.setName("Vibration sensors"); 288 289 NetworkNode taipei = new NetworkNode(); 291 taipei.setName("Taipei machine"); 292 taipei.setIpAddress("192.168.1.100"); 293 294 NetworkNode tainan = new NetworkNode(); 295 tainan.setName("Tainan machine"); 296 tainan.setIpAddress("192.168.1.200"); 297 298 NetworkElement vibSensor1 = new NetworkElement(); 300 vibSensor1.setName("Vibration sensor V10"); 301 302 NetworkElement vibSensor2 = new NetworkElement(); 303 vibSensor2.setName("Vibration sensor V20"); 304 305 NetworkElement tempSensor1 = new NetworkElement(); 306 tempSensor1.setName("Temperature sensor T10"); 307 308 NetworkElement tempSensor2 = new NetworkElement(); 309 tempSensor2.setName("Temperature sensor T20"); 310 311 taipei.addElement(vibSensor1); 313 taipei.addElement(tempSensor1); 314 tainan.addElement(vibSensor2); 315 tainan.addElement(tempSensor2); 316 317 assertEquals("Element size ", 2, taipei.getElements().size()); 318 319 vibSensorDomain.addElement(vibSensor1); 321 vibSensorDomain.addElement(vibSensor2); 322 323 tempSensorDomain.addElement(tempSensor1); 324 tempSensorDomain.addElement(tempSensor2); 325 326 cache1.attach("/vib", vibSensorDomain); 328 cache1.attach("/temp", tempSensorDomain); 329 330 NetworkDomain temp1 = (NetworkDomain) cache1.find("/temp"); 331 NetworkDomain vib1 = (NetworkDomain) cache1.find("/vib"); 332 333 NetworkNode taipei1 = (NetworkNode) temp1.getNodes().get(0); 334 NetworkNode taipei2 = (NetworkNode) vib1.getNodes().get(0); 336 337 List l1 = temp1.getNodes(); 338 assertEquals("Size is ", 2, l1.size()); 339 340 l1 = taipei.getElements(); 341 assertEquals("Size is ", 2, l1.size()); 342 343 assertEquals("IPAddress ", taipei.getIpAddress(), taipei1.getIpAddress()); 344 assertTrue("DataNode should be the same", (taipei1 == taipei2)); 345 346 taipei2.setIpAddress("192.168.10.100"); 347 assertEquals("New admin id is ", "192.168.10.100", taipei.getIpAddress()); 348 349 List l2 = taipei1.getElements(); 350 assertEquals("Size is ", 2, l2.size()); 351 NetworkElement vibSens1 = (NetworkElement) l2.get(0); 352 NetworkElement tempSens1 = (NetworkElement) l2.get(1); 353 354 int SUSPENDED = 2; 355 vibSens1.setStatus(SUSPENDED); 356 tempSens1.setStatus(SUSPENDED); 357 358 assertEquals("Status ", vibSensor1.getStatus(), vibSens1.getStatus()); 359 assertEquals("Status ", tempSensor1.getStatus(), tempSens1.getStatus()); 360 } 361 362 public static Test suite() throws Exception 363 { 364 return new TestSuite(NetworkManagementTest.class); 365 } 366 367 public static void main(String [] args) throws Exception 368 { 369 junit.textui.TestRunner.run(suite()); 370 } 371 372 } 373 374 | Popular Tags |