1 22 package org.objectweb.petals.jbi.management.systemstate; 23 24 import java.io.File ; 25 import java.util.List ; 26 27 import org.apache.commons.io.FileUtils; 28 29 import junit.framework.TestCase; 30 31 37 public class StateStorageTest extends TestCase { 38 39 42 private String baseDir; 43 44 private StateStorage stateStorage; 45 46 private ComponentState c = new ComponentState("c", "iURL", "zURL", "iS", 47 "lS");; 48 49 private ServiceAssemblyState sa = new ServiceAssemblyState("sa", "iURL", 50 "zURL", "lS");; 51 52 private SharedLibraryState sl = new SharedLibraryState("sl", "iURL", "zURL"); 53 54 private File stateFile; 55 56 59 protected void setUp() throws Exception { 60 super.setUp(); 61 baseDir = this.getClass().getResource(".").toString(); 62 baseDir = baseDir.substring(0, baseDir.indexOf("target")); 63 baseDir = baseDir.substring(baseDir.indexOf(":") + 1); 64 65 System.setProperty("petals.home", new File (baseDir + "target") 66 .getAbsolutePath()); 67 new File (baseDir + "target" + File.separator + "lib").mkdir(); 68 new File (baseDir + "target" + File.separator + "lib" + File.separator 69 + "petals-1.0-SNAPSHOT.jar").mkdir(); 70 new File (baseDir + "target" + File.separator + "conf").mkdir(); 71 72 stateFile = new File (baseDir + "target" + File.separator 73 + "systemstate.xml"); 74 stateFile.createNewFile(); 75 File xsdFile = new File (baseDir.replace("container", "assembly") 76 + "src" + File.separator + StateStorage.SCHEMA_FILE_PATH); 77 78 stateStorage = new StateStorage(); 79 stateStorage.file = stateFile; 80 stateStorage.schemaFile = xsdFile; 81 } 82 83 public void tearDown() { 84 System.clearProperty("petals.home"); 85 stateFile.delete(); 86 stateFile = null; 87 } 88 89 93 public void testInit() { 94 stateStorage.file = null; 96 97 boolean exception = true; 98 try { 99 stateStorage.init(); 100 exception = false; 101 } catch (Exception e) { 102 } 104 assertTrue(exception); 105 } 106 107 111 public void testInit2() { 112 stateStorage.file = new File (baseDir + File.separator + "target" 114 + File.separator + "test-classes" + File.separator 115 + "systemstate" + File.separator + "wrongSystemState.xml"); 116 assert (stateStorage.file.exists()); 117 118 boolean exception = true; 119 try { 120 stateStorage.init(); 121 exception = false; 122 } catch (Exception e) { 123 } 125 assertTrue(exception); 126 127 stateStorage.file = new File (baseDir + File.separator + "target" 128 + File.separator + "test-classes" + File.separator 129 + "systemstate" + File.separator + "wrongSystemState.xml"); 130 assert (stateStorage.file.exists()); 131 } 132 133 137 public void testInit3() { 138 stateStorage.file = new File (baseDir + File.separator + "target" 140 + File.separator + "test-classes" + File.separator 141 + "systemstate" + File.separator + "wrongSystemState.xml"); 142 assert (stateStorage.file.exists()); 143 144 boolean exception = true; 145 try { 146 stateStorage.init(); 147 exception = false; 148 } catch (Exception e) { 149 } 151 assertTrue(exception); 152 } 153 154 158 public void testInit4() throws Exception { 159 stateStorage.file = new File (baseDir + "src" + File.separator 161 + "test-data" + File.separator + "systemstate" + File.separator 162 + "goodSystemState.xml"); 163 assertTrue(stateStorage.file.exists()); 164 stateStorage.init(); 165 } 166 167 172 public void testCreateAndRetreiveComponent() throws Exception { 173 stateStorage.init(); 174 175 ComponentState cc = stateStorage.createComponentStateHolder( 176 c.getName(), c.getInstallURL(), c.getArchiveURL(), c 177 .getInstallState(), c.getLifecycleState()); 178 179 assertTrue(c.equals(cc)); 181 182 stateStorage.document = null; 185 stateStorage.file = stateFile; 186 187 stateStorage.init(); 188 List <ComponentState> l = stateStorage.retrieveAllComponentStates(); 189 assertTrue(l != null); 190 assertTrue(l.size() == 1); 191 assertTrue(c.equals(l.get(0))); 192 } 193 194 199 public void testCreateAndRetreiveServiceAssembly() throws Exception { 200 stateStorage.init(); 201 202 ServiceAssemblyState cc = stateStorage 203 .createServiceAssemblyStateHolder(sa.getName(), sa 204 .getInstallURL(), sa.getArchiveURL(), sa 205 .getLifecycleState()); 206 207 assertTrue(sa.equals(cc)); 209 210 stateStorage.document = null; 214 stateStorage.file = stateFile; 215 216 stateStorage.loadStateStorageDocument(); 217 List <ServiceAssemblyState> l = stateStorage 218 .retrieveAllServiceAssemblyStates(); 219 assertTrue(l != null); 220 assertTrue(l.size() == 1); 221 assertTrue(sa.equals(l.get(0))); 222 } 223 224 229 public void testCreateAndRetreiveSharedLibrary() throws Exception { 230 stateStorage.init(); 231 232 SharedLibraryState cc = stateStorage.createSharedLibraryStateHolder(sl 233 .getName(), sl.getInstallURL(), sl.getArchiveURL()); 234 235 assertTrue(sl.equals(cc)); 237 238 stateStorage.document = null; 242 stateStorage.file = stateFile; 243 244 stateStorage.init(); 245 List <SharedLibraryState> l = stateStorage 246 .retrieveAllSharedLibraryStates(); 247 assertTrue(l != null); 248 assertTrue(l.size() == 1); 249 assertTrue(sl.equals(l.get(0))); 250 } 251 252 257 public void testUpdateComponentInstallationState() throws Exception { 258 new File (baseDir + "target" + File.separator 259 + "updatecompoinst.xml").delete(); 260 stateStorage.file = new File (baseDir + "target" + File.separator 261 + "updatecompoinst.xml"); 262 263 stateStorage.init(); 264 265 stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(), 266 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState()); 267 268 stateStorage.updateComponentInstallationState(c.getName(), 269 "uninstalled"); 270 271 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 273 + File.separator + "test-data" + File.separator + "systemstate" 274 + File.separator + "updatecompoinst.xml"), "UTF-8"); 275 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 276 + File.separator + "updatecompoinst.xml"), "UTF-8"); 277 assertEquals(xmlComp, xmlRef); 278 } 279 280 285 public void testUpdateComponentLifeCycleState() throws Exception { 286 new File (baseDir + "target" + File.separator 287 + "updatecompolife.xml").delete(); 288 stateStorage.file = new File (baseDir + "target" + File.separator 289 + "updatecompolife.xml"); 290 291 stateStorage.init(); 292 293 stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(), 294 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState()); 295 296 stateStorage.updateComponentLifeCycleState(c.getName(), "stopped"); 297 298 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 300 + File.separator + "test-data" + File.separator + "systemstate" 301 + File.separator + "updatecompolife.xml"), "UTF-8"); 302 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 303 + File.separator + "updatecompolife.xml"), "UTF-8"); 304 assertEquals(xmlComp, xmlRef); 305 } 306 307 312 public void testUpdateServiceAssemblyState() throws Exception { 313 new File (baseDir + "target" + File.separator 314 + "updatesa.xml").delete(); 315 stateStorage.file = new File (baseDir + "target" + File.separator 316 + "updatesa.xml"); 317 318 stateStorage.init(); 319 320 stateStorage.createServiceAssemblyStateHolder(c.getName(), c 321 .getInstallURL(), c.getArchiveURL(), c.getLifecycleState()); 322 323 stateStorage.updateServiceAssemblyState(c.getName(), "stopped"); 324 325 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 327 + File.separator + "test-data" + File.separator + "systemstate" 328 + File.separator + "updatesa.xml"), "UTF-8"); 329 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 330 + File.separator + "updatesa.xml"), "UTF-8"); 331 assertEquals(xmlComp, xmlRef); 332 } 333 334 338 public void testDeleteComponentStateHolder() throws Exception { 339 new File (baseDir + "target" + File.separator 340 + "deletecompo.xml").delete(); 341 stateStorage.file = new File (baseDir + "target" + File.separator 342 + "deletecompo.xml"); 343 344 stateStorage.init(); 345 346 stateStorage.createComponentStateHolder(c.getName(), c.getInstallURL(), 347 c.getArchiveURL(), c.getInstallState(), c.getLifecycleState()); 348 349 stateStorage.deleteComponentStateHolder(c.getName()); 350 351 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 353 + File.separator + "test-data" + File.separator + "systemstate" 354 + File.separator + "deletecompo.xml"), "UTF-8"); 355 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 356 + File.separator + "deletecompo.xml"), "UTF-8"); 357 assertEquals(xmlComp, xmlRef); 358 } 359 360 364 public void testDeleteServiceAssemblyStateHolder() throws Exception { 365 new File (baseDir + "target" + File.separator 366 + "deletesa.xml").delete(); 367 stateStorage.file = new File (baseDir + "target" + File.separator 368 + "deletesa.xml"); 369 370 stateStorage.init(); 371 372 stateStorage.createServiceAssemblyStateHolder(c.getName(), c 373 .getInstallURL(), c.getArchiveURL(), c.getLifecycleState()); 374 375 stateStorage.deleteServiceAssemblyStateHolder(c.getName()); 376 377 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 379 + File.separator + "test-data" + File.separator + "systemstate" 380 + File.separator + "deletesa.xml"), "UTF-8"); 381 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 382 + File.separator + "deletesa.xml"), "UTF-8"); 383 assertEquals(xmlComp, xmlRef); 384 } 385 386 390 public void testDeleteSharedLibraryStateHolder() throws Exception { 391 new File (baseDir + "target" + File.separator 392 + "deletesl.xml").delete(); 393 stateStorage.file = new File (baseDir + "target" + File.separator 394 + "deletesl.xml"); 395 396 stateStorage.init(); 397 398 stateStorage.createSharedLibraryStateHolder(c.getName(), c 399 .getInstallURL(), c.getArchiveURL()); 400 401 stateStorage.deleteSharedLibraryStateHolder(c.getName()); 402 403 String xmlRef = FileUtils.readFileToString(new File (baseDir + "src" 405 + File.separator + "test-data" + File.separator + "systemstate" 406 + File.separator + "deletesl.xml"), "UTF-8"); 407 String xmlComp = FileUtils.readFileToString(new File (baseDir + "target" 408 + File.separator + "deletesl.xml"), "UTF-8"); 409 assertEquals(xmlComp, xmlRef); 410 } 411 412 } 413 | Popular Tags |