1 17 package org.apache.geronimo.kernel.config; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashSet ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.SortedSet ; 31 import java.util.TreeSet ; 32 import junit.framework.TestCase; 33 import org.apache.geronimo.gbean.AbstractName; 34 import org.apache.geronimo.gbean.AbstractNameQuery; 35 import org.apache.geronimo.gbean.GBeanData; 36 import org.apache.geronimo.gbean.GBeanInfo; 37 import org.apache.geronimo.gbean.GBeanInfoBuilder; 38 import org.apache.geronimo.kernel.GBeanNotFoundException; 39 import org.apache.geronimo.kernel.Kernel; 40 import org.apache.geronimo.kernel.KernelFactory; 41 import org.apache.geronimo.kernel.management.State; 42 import org.apache.geronimo.kernel.repository.Artifact; 43 import org.apache.geronimo.kernel.repository.ArtifactManager; 44 import org.apache.geronimo.kernel.repository.DefaultArtifactManager; 45 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 46 import org.apache.geronimo.kernel.repository.Environment; 47 import org.apache.geronimo.kernel.repository.ImportType; 48 import org.apache.geronimo.kernel.repository.ListableRepository; 49 import org.apache.geronimo.kernel.repository.Version; 50 51 54 public class ConfigurationManagerTest extends TestCase { 55 private static Kernel kernel; 56 private Artifact artifact1; 57 private Artifact artifact2; 58 private Artifact artifact3; 59 private Artifact artifact3NoVersion; 60 private Map configurations = new HashMap (); 61 private ConfigurationManager configurationManager; 62 private AbstractName gbean1; 63 private AbstractName gbean2; 64 private AbstractName gbean3; 65 private AbstractName gbean3newer; 66 private TestConfigStore configStore; 67 68 public void testLoad() throws Exception { 69 configurationManager.loadConfiguration(artifact3); 70 assertTrue(configurationManager.isLoaded(artifact3)); 71 assertTrue(configurationManager.isLoaded(artifact2)); 72 assertTrue(configurationManager.isLoaded(artifact1)); 73 assertFalse(configurationManager.isRunning(artifact3)); 74 assertFalse(configurationManager.isRunning(artifact2)); 75 assertFalse(configurationManager.isRunning(artifact1)); 76 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 77 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 78 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 79 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 80 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 81 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 82 assertFalse(kernel.isLoaded(gbean1)); 83 assertFalse(kernel.isLoaded(gbean2)); 84 assertFalse(kernel.isLoaded(gbean3)); 85 86 87 configurationManager.unloadConfiguration(artifact3); 88 assertFalse(configurationManager.isLoaded(artifact3)); 89 assertFalse(configurationManager.isLoaded(artifact2)); 90 assertFalse(configurationManager.isLoaded(artifact1)); 91 assertFalse(configurationManager.isRunning(artifact3)); 92 assertFalse(configurationManager.isRunning(artifact2)); 93 assertFalse(configurationManager.isRunning(artifact1)); 94 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 95 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 96 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 97 } 98 99 public void testStart() throws Exception { 100 configurationManager.loadConfiguration(artifact3); 101 assertTrue(configurationManager.isLoaded(artifact3)); 102 assertTrue(configurationManager.isLoaded(artifact2)); 103 assertTrue(configurationManager.isLoaded(artifact1)); 104 assertFalse(configurationManager.isRunning(artifact3)); 105 assertFalse(configurationManager.isRunning(artifact2)); 106 assertFalse(configurationManager.isRunning(artifact1)); 107 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 108 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 109 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 110 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 111 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 112 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 113 assertFalse(kernel.isLoaded(gbean1)); 114 assertFalse(kernel.isLoaded(gbean2)); 115 assertFalse(kernel.isLoaded(gbean3)); 116 117 configurationManager.startConfiguration(artifact3); 118 assertTrue(configurationManager.isRunning(artifact3)); 119 assertTrue(configurationManager.isRunning(artifact2)); 120 assertTrue(configurationManager.isRunning(artifact1)); 121 assertTrue(kernel.isLoaded(gbean1)); 122 assertTrue(kernel.isLoaded(gbean2)); 123 assertTrue(kernel.isLoaded(gbean3)); 124 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 125 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 126 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3)) ; 127 128 129 configurationManager.stopConfiguration(artifact3); 130 assertTrue(configurationManager.isLoaded(artifact3)); 131 assertTrue(configurationManager.isLoaded(artifact2)); 132 assertTrue(configurationManager.isLoaded(artifact1)); 133 assertFalse(configurationManager.isRunning(artifact3)); 134 assertFalse(configurationManager.isRunning(artifact2)); 135 assertFalse(configurationManager.isRunning(artifact1)); 136 assertFalse(kernel.isLoaded(gbean1)); 137 assertFalse(kernel.isLoaded(gbean2)); 138 assertFalse(kernel.isLoaded(gbean3)); 139 140 configurationManager.unloadConfiguration(artifact3); 141 assertFalse(configurationManager.isLoaded(artifact3)); 142 assertFalse(configurationManager.isLoaded(artifact2)); 143 assertFalse(configurationManager.isRunning(artifact3)); 144 assertFalse(configurationManager.isRunning(artifact2)); 145 assertFalse(configurationManager.isRunning(artifact1)); 146 assertFalse(configurationManager.isLoaded(artifact1)); 147 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 148 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 149 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 150 } 151 152 public void testRestart() throws Exception { 153 configurationManager.loadConfiguration(artifact3); 154 configurationManager.startConfiguration(artifact3); 155 Object g1 = kernel.getGBean(gbean1); 156 Object g2 = kernel.getGBean(gbean2); 157 Object g3 = kernel.getGBean(gbean3); 158 assertSame(g1, kernel.getGBean(gbean1)); 159 assertSame(g2, kernel.getGBean(gbean2)); 160 assertSame(g3, kernel.getGBean(gbean3)); 161 162 LifecycleResults results = configurationManager.restartConfiguration(artifact1); 166 167 assertTrue(results.wasStopped(artifact1)); 169 assertTrue(results.wasStopped(artifact2)); 170 assertTrue(results.wasStopped(artifact3)); 171 assertTrue(results.wasStarted(artifact1)); 172 assertTrue(results.wasStarted(artifact2)); 173 assertTrue(results.wasStarted(artifact3)); 174 175 assertFalse(results.wasUnloaded(artifact1)); 177 assertFalse(results.wasUnloaded(artifact2)); 178 assertFalse(results.wasUnloaded(artifact3)); 179 assertFalse(results.wasLoaded(artifact1)); 180 assertFalse(results.wasLoaded(artifact2)); 181 assertFalse(results.wasLoaded(artifact3)); 182 assertFalse(results.wasFailed(artifact1)); 183 assertFalse(results.wasFailed(artifact2)); 184 assertFalse(results.wasFailed(artifact3)); 185 186 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 188 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 189 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 190 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 191 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 192 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 193 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 194 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 195 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3)) ; 196 assertNotSame(g1, kernel.getGBean(gbean1)); 197 assertNotSame(g2, kernel.getGBean(gbean2)); 198 assertNotSame(g3, kernel.getGBean(gbean3)); 199 200 configurationManager.stopConfiguration(artifact3); 201 assertFalse(kernel.isLoaded(gbean2)); 202 assertFalse(kernel.isLoaded(gbean3)); 203 204 assertTrue(kernel.isLoaded(gbean1)); 206 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 207 208 configurationManager.unloadConfiguration(artifact3); 209 assertFalse(configurationManager.isLoaded(artifact3)); 210 assertFalse(configurationManager.isLoaded(artifact2)); 211 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 212 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 213 214 assertTrue(configurationManager.isLoaded(artifact1)); 216 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 217 assertTrue(kernel.isLoaded(gbean1)); 218 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)); 219 220 configurationManager.unloadConfiguration(artifact1); 221 assertFalse(configurationManager.isLoaded(artifact1)); 222 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 223 assertFalse(kernel.isLoaded(gbean1)); 224 225 } 226 227 public void testRestartException() throws Exception { 228 configurationManager.loadConfiguration(artifact3); 229 configurationManager.startConfiguration(artifact3); 230 Object g1 = kernel.getGBean(gbean1); 231 Object g2 = kernel.getGBean(gbean2); 232 kernel.getGBean(gbean3); 233 234 shouldFail.add(gbean3.getObjectName().getCanonicalName()); 236 LifecycleResults results = configurationManager.restartConfiguration(artifact1); 237 238 assertTrue(results.wasStopped(artifact3)); 240 assertTrue(results.wasFailed(artifact3)); 241 assertFalse(results.wasStarted(artifact3)); 242 243 assertTrue(results.wasStopped(artifact1)); 245 assertTrue(results.wasStopped(artifact2)); 246 assertTrue(results.wasStarted(artifact1)); 247 assertTrue(results.wasStarted(artifact2)); 248 assertFalse(results.wasFailed(artifact1)); 249 assertFalse(results.wasFailed(artifact2)); 250 251 assertFalse(results.wasUnloaded(artifact1)); 253 assertFalse(results.wasUnloaded(artifact2)); 254 assertFalse(results.wasUnloaded(artifact3)); 255 assertFalse(results.wasLoaded(artifact1)); 256 assertFalse(results.wasLoaded(artifact2)); 257 assertFalse(results.wasLoaded(artifact3)); 258 259 assertTrue(configurationManager.isLoaded(artifact3)); 261 assertTrue(configurationManager.isLoaded(artifact2)); 262 assertTrue(configurationManager.isLoaded(artifact1)); 263 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 264 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 265 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 266 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 267 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 268 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 269 270 assertTrue(configurationManager.isRunning(artifact1)); 272 assertTrue(configurationManager.isRunning(artifact2)); 273 assertFalse(configurationManager.isRunning(artifact3)); 274 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 275 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 276 assertFalse(kernel.isLoaded(gbean3)); 277 278 assertNotSame(g1, kernel.getGBean(gbean1)); 280 assertNotSame(g2, kernel.getGBean(gbean2)); 281 282 configurationManager.unloadConfiguration(artifact1); 283 assertFalse(configurationManager.isLoaded(artifact3)); 284 assertFalse(configurationManager.isLoaded(artifact2)); 285 assertFalse(configurationManager.isLoaded(artifact1)); 286 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 287 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 288 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 289 } 290 291 public void testReload() throws Exception { 292 configurationManager.loadConfiguration(artifact3); 293 configurationManager.startConfiguration(artifact3); 294 Object g1 = kernel.getGBean(gbean1); 295 Object g2 = kernel.getGBean(gbean2); 296 Object g3 = kernel.getGBean(gbean3); 297 assertSame(g1, kernel.getGBean(gbean1)); 298 assertSame(g2, kernel.getGBean(gbean2)); 299 assertSame(g3, kernel.getGBean(gbean3)); 300 Configuration configuration1 = configurationManager.getConfiguration(artifact1); 301 Configuration configuration2 = configurationManager.getConfiguration(artifact2); 302 Configuration configuration3 = configurationManager.getConfiguration(artifact3); 303 304 LifecycleResults results = configurationManager.reloadConfiguration(artifact1); 305 306 assertTrue(results.wasStopped(artifact1)); 309 assertTrue(results.wasStopped(artifact2)); 310 assertTrue(results.wasStopped(artifact3)); 311 assertTrue(results.wasUnloaded(artifact1)); 312 assertTrue(results.wasUnloaded(artifact2)); 313 assertTrue(results.wasUnloaded(artifact3)); 314 assertTrue(results.wasLoaded(artifact1)); 315 assertTrue(results.wasLoaded(artifact2)); 316 assertTrue(results.wasLoaded(artifact3)); 317 assertTrue(results.wasStarted(artifact1)); 318 assertTrue(results.wasStarted(artifact2)); 319 assertTrue(results.wasStarted(artifact3)); 320 321 assertFalse(results.wasFailed(artifact1)); 323 assertFalse(results.wasFailed(artifact2)); 324 assertFalse(results.wasFailed(artifact3)); 325 326 assertTrue(configurationManager.isLoaded(artifact1)); 328 assertTrue(configurationManager.isLoaded(artifact2)); 329 assertTrue(configurationManager.isLoaded(artifact3)); 330 assertTrue(configurationManager.isRunning(artifact1)); 331 assertTrue(configurationManager.isRunning(artifact2)); 332 assertTrue(configurationManager.isRunning(artifact3)); 333 assertNotSame(configuration1, configurationManager.getConfiguration(artifact1)); 334 assertNotSame(configuration2, configurationManager.getConfiguration(artifact2)); 335 assertNotSame(configuration3, configurationManager.getConfiguration(artifact3)); 336 337 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 339 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 340 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 341 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3))) ; 342 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact2))) ; 343 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact1))) ; 344 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 345 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 346 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 347 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 348 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 349 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3)) ; 350 assertNotSame(g1, kernel.getGBean(gbean1)); 351 assertNotSame(g2, kernel.getGBean(gbean2)); 352 assertNotSame(g3, kernel.getGBean(gbean3)); 353 354 configurationManager.stopConfiguration(artifact3); 355 assertFalse(kernel.isLoaded(gbean1)); 356 assertFalse(kernel.isLoaded(gbean2)); 357 assertFalse(kernel.isLoaded(gbean3)); 358 359 configurationManager.unloadConfiguration(artifact3); 360 assertFalse(configurationManager.isLoaded(artifact3)); 361 assertFalse(configurationManager.isLoaded(artifact2)); 362 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 363 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 364 365 assertTrue(configurationManager.isLoaded(artifact1)); 367 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 368 369 configurationManager.unloadConfiguration(artifact1); 370 assertFalse(configurationManager.isLoaded(artifact1)); 371 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 372 } 373 374 public void testReloadException() throws Exception { 375 configurationManager.loadConfiguration(artifact3); 376 configurationManager.startConfiguration(artifact3); 377 Object g1 = kernel.getGBean(gbean1); 378 Object g2 = kernel.getGBean(gbean2); 379 kernel.getGBean(gbean3); 380 381 shouldFail.add(gbean3.getObjectName().getCanonicalName()); 383 LifecycleResults results = configurationManager.reloadConfiguration(artifact1); 384 385 387 assertTrue(results.wasStopped(artifact3)); 389 assertTrue(results.wasUnloaded(artifact3)); 390 assertTrue(results.wasFailed(artifact3)); 391 assertFalse(results.wasLoaded(artifact3)); 392 assertFalse(results.wasStarted(artifact3)); 393 394 assertTrue(results.wasStopped(artifact1)); 396 assertTrue(results.wasStopped(artifact2)); 397 assertTrue(results.wasUnloaded(artifact1)); 398 assertTrue(results.wasUnloaded(artifact2)); 399 assertTrue(results.wasLoaded(artifact1)); 400 assertTrue(results.wasLoaded(artifact2)); 401 assertTrue(results.wasStarted(artifact1)); 402 assertTrue(results.wasStarted(artifact2)); 403 404 assertFalse(configurationManager.isLoaded(artifact3)); 406 assertTrue(configurationManager.isLoaded(artifact2)); 407 assertTrue(configurationManager.isLoaded(artifact1)); 408 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 409 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 410 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 411 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 412 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 413 414 assertTrue(configurationManager.isRunning(artifact1)); 416 assertTrue(configurationManager.isRunning(artifact2)); 417 assertFalse(configurationManager.isRunning(artifact3)); 418 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 419 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 420 assertFalse(kernel.isLoaded(gbean3)); 421 422 assertNotSame(g1, kernel.getGBean(gbean1)); 424 assertNotSame(g2, kernel.getGBean(gbean2)); 425 426 configurationManager.unloadConfiguration(artifact1); 427 assertFalse(configurationManager.isLoaded(artifact3)); 428 assertFalse(configurationManager.isLoaded(artifact2)); 429 assertFalse(configurationManager.isLoaded(artifact1)); 430 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 431 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 432 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 433 } 434 435 public void testReloadFallback() throws Exception { 436 configurationManager.loadConfiguration(artifact3); 437 configurationManager.startConfiguration(artifact3); 438 Object g1 = kernel.getGBean(gbean1); 439 Object g2 = kernel.getGBean(gbean2); 440 Object g3 = kernel.getGBean(gbean3); 441 assertSame(g1, kernel.getGBean(gbean1)); 442 assertSame(g2, kernel.getGBean(gbean2)); 443 assertSame(g3, kernel.getGBean(gbean3)); 444 Configuration configuration1 = configurationManager.getConfiguration(artifact1); 445 Configuration configuration2 = configurationManager.getConfiguration(artifact2); 446 Configuration configuration3 = configurationManager.getConfiguration(artifact3); 447 448 Environment environment = new Environment(); 449 environment.setConfigId(artifact1); 450 ConfigurationData configurationData1 = new ConfigurationData(environment, kernel.getNaming()); 451 configurationData1.setConfigurationStore(configStore); 452 GBeanData gbeanData = configurationData1.addGBean("gbean1", TestBean.getGBeanInfo()); 453 gbeanData.setReferencePattern("nonExistantReference", new AbstractNameQuery("some.non.existant.Clazz")); 454 configurations.put(artifact1, configurationData1); 455 456 LifecycleResults results = null; 457 try { 458 configurationManager.reloadConfiguration(artifact1); 459 fail("Expected LifecycleException"); 460 } catch (LifecycleException expected) { 461 results = expected.getLifecycleResults(); 462 } 463 464 466 assertTrue(results.wasFailed(artifact1)); 468 469 assertTrue(results.wasStopped(artifact1)); 471 assertTrue(results.wasStopped(artifact2)); 472 assertTrue(results.wasStopped(artifact3)); 473 assertTrue(results.wasUnloaded(artifact1)); 474 assertTrue(results.wasUnloaded(artifact2)); 475 assertTrue(results.wasUnloaded(artifact3)); 476 assertTrue(results.wasLoaded(artifact1)); 477 assertTrue(results.wasLoaded(artifact2)); 478 assertTrue(results.wasLoaded(artifact3)); 479 assertTrue(results.wasStarted(artifact1)); 480 assertTrue(results.wasStarted(artifact2)); 481 assertTrue(results.wasStarted(artifact3)); 482 483 assertTrue(configurationManager.isLoaded(artifact1)); 485 assertTrue(configurationManager.isLoaded(artifact2)); 486 assertTrue(configurationManager.isLoaded(artifact3)); 487 assertTrue(configurationManager.isRunning(artifact1)); 488 assertTrue(configurationManager.isRunning(artifact2)); 489 assertTrue(configurationManager.isRunning(artifact3)); 490 assertNotSame(configuration1, configurationManager.getConfiguration(artifact1)); 491 assertNotSame(configuration2, configurationManager.getConfiguration(artifact2)); 492 assertNotSame(configuration3, configurationManager.getConfiguration(artifact3)); 493 494 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 496 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 497 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 498 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3))) ; 499 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact2))) ; 500 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact1))) ; 501 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 502 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 503 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 504 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 505 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 506 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3)) ; 507 assertNotSame(g1, kernel.getGBean(gbean1)); 508 assertNotSame(g2, kernel.getGBean(gbean2)); 509 assertNotSame(g3, kernel.getGBean(gbean3)); 510 511 configurationManager.stopConfiguration(artifact3); 512 assertFalse(kernel.isLoaded(gbean1)); 513 assertFalse(kernel.isLoaded(gbean2)); 514 assertFalse(kernel.isLoaded(gbean3)); 515 516 configurationManager.unloadConfiguration(artifact3); 517 assertFalse(configurationManager.isLoaded(artifact3)); 518 assertFalse(configurationManager.isLoaded(artifact2)); 519 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 520 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 521 522 assertTrue(configurationManager.isLoaded(artifact1)); 524 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 525 526 configurationManager.unloadConfiguration(artifact1); 527 assertFalse(configurationManager.isLoaded(artifact1)); 528 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 529 } 530 531 public void testReloadNewerConfiguration() throws Exception { 532 configurationManager.loadConfiguration(artifact3); 533 configurationManager.startConfiguration(artifact3); 534 Object g1 = kernel.getGBean(gbean1); 535 Object g2 = kernel.getGBean(gbean2); 536 Object g3 = kernel.getGBean(gbean3); 537 try { 538 kernel.getGBean(gbean3newer); 539 fail("Should not have found the newer GBean yet"); 540 } catch (GBeanNotFoundException e) {} 541 assertSame(g1, kernel.getGBean(gbean1)); 542 assertSame(g2, kernel.getGBean(gbean2)); 543 assertSame(g3, kernel.getGBean(gbean3)); 544 Configuration configuration1 = configurationManager.getConfiguration(artifact1); 545 Configuration configuration2 = configurationManager.getConfiguration(artifact2); 546 Configuration configuration3 = configurationManager.getConfiguration(artifact3); 547 assertNull(configurationManager.getConfiguration(artifact3NoVersion)); 548 549 LifecycleResults results = configurationManager.reloadConfiguration(artifact1); 550 551 assertTrue(results.wasStopped(artifact1)); 553 assertTrue(results.wasStopped(artifact2)); 554 assertTrue(results.wasStopped(artifact3)); 555 assertTrue(results.wasUnloaded(artifact1)); 556 assertTrue(results.wasUnloaded(artifact2)); 557 assertTrue(results.wasUnloaded(artifact3)); 558 assertTrue(results.wasLoaded(artifact1)); 559 assertTrue(results.wasLoaded(artifact2)); 560 assertTrue(results.wasLoaded(artifact3)); 561 assertTrue(results.wasStarted(artifact1)); 562 assertTrue(results.wasStarted(artifact2)); 563 assertTrue(results.wasStarted(artifact3)); 564 assertFalse(results.wasFailed(artifact3NoVersion)); 565 assertFalse(results.wasLoaded(artifact3NoVersion)); 566 assertFalse(results.wasLoaded(artifact3NoVersion)); 567 assertFalse(results.wasStarted(artifact3NoVersion)); 568 assertFalse(results.wasStarted(artifact3NoVersion)); 569 assertFalse(results.wasStopped(artifact3NoVersion)); 570 assertFalse(results.wasUnloaded(artifact3NoVersion)); 571 572 assertTrue(configurationManager.isLoaded(artifact1)); 574 assertTrue(configurationManager.isLoaded(artifact2)); 575 assertTrue(configurationManager.isLoaded(artifact3)); 576 assertFalse(configurationManager.isLoaded(artifact3NoVersion)); 577 assertTrue(configurationManager.isRunning(artifact1)); 578 assertTrue(configurationManager.isRunning(artifact2)); 579 assertTrue(configurationManager.isRunning(artifact3)); 580 assertFalse(configurationManager.isRunning(artifact3NoVersion)); 581 assertNotSame(configuration1, configurationManager.getConfiguration(artifact1)); 582 assertNotSame(configuration2, configurationManager.getConfiguration(artifact2)); 583 assertNotSame(configuration3, configurationManager.getConfiguration(artifact3)); 584 585 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))) ; 587 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 588 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 589 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3NoVersion))); 590 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3))) ; 591 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact2))) ; 592 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact1))) ; 593 assertFalse(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3NoVersion))); 594 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3))) ; 595 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 596 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 597 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 598 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 599 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3)) ; 600 assertNotSame(g1, kernel.getGBean(gbean1)); 601 assertNotSame(g2, kernel.getGBean(gbean2)); 602 assertNotSame(g3, kernel.getGBean(gbean3)); 603 604 results = configurationManager.reloadConfiguration(artifact3, artifact3NoVersion.getVersion()); 608 609 assertTrue(results.wasStopped(artifact3)); 611 assertTrue(results.wasUnloaded(artifact3)); 612 assertTrue(results.wasLoaded(artifact3NoVersion)); 613 assertTrue(results.wasStarted(artifact3NoVersion)); 614 assertFalse(results.wasLoaded(artifact3)); 615 assertFalse(results.wasStarted(artifact3)); 616 617 assertFalse(results.wasStopped(artifact1)); 619 assertFalse(results.wasStopped(artifact2)); 620 assertFalse(results.wasUnloaded(artifact1)); 621 assertFalse(results.wasUnloaded(artifact2)); 622 assertFalse(results.wasLoaded(artifact1)); 623 assertFalse(results.wasLoaded(artifact2)); 624 assertFalse(results.wasStarted(artifact1)); 625 assertFalse(results.wasStarted(artifact2)); 626 627 assertFalse(results.wasFailed(artifact1)); 629 assertFalse(results.wasFailed(artifact2)); 630 assertFalse(results.wasFailed(artifact3)); 631 assertFalse(results.wasFailed(artifact3NoVersion)); 632 633 assertTrue(configurationManager.isLoaded(artifact1)); 635 assertTrue(configurationManager.isLoaded(artifact2)); 636 assertFalse(configurationManager.isLoaded(artifact3)); 637 assertTrue(configurationManager.isLoaded(artifact3NoVersion)); 638 assertTrue(configurationManager.isRunning(artifact1)); 639 assertTrue(configurationManager.isRunning(artifact2)); 640 assertFalse(configurationManager.isRunning(artifact3)); 641 assertTrue(configurationManager.isRunning(artifact3NoVersion)); 642 643 assertFalse(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3))); 645 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact2))) ; 646 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact1))) ; 647 assertTrue(kernel.isLoaded(Configuration.getConfigurationAbstractName(artifact3NoVersion))); 648 assertFalse(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3))); 649 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact2))) ; 650 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact1))) ; 651 assertTrue(kernel.isRunning(Configuration.getConfigurationAbstractName(artifact3NoVersion))); 652 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact3NoVersion))) ; 653 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact2))) ; 654 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(Configuration.getConfigurationAbstractName(artifact1))) ; 655 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean1)) ; 656 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean2)) ; 657 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(gbean3newer)) ; 658 } 659 660 private static final Set shouldFail = new HashSet (); 661 private static void checkFail(String objectName) { 662 if (shouldFail.contains(objectName)) { 663 throw new RuntimeException ("FAILING"); 664 } 665 } 666 667 protected void setUp() throws Exception { 668 super.setUp(); 669 670 shouldFail.clear(); 671 672 kernel = KernelFactory.newInstance().createKernel("test"); 673 kernel.boot(); 674 675 GBeanData artifactManagerData = buildGBeanData("name", "ArtifactManager", DefaultArtifactManager.GBEAN_INFO); 676 kernel.loadGBean(artifactManagerData, getClass().getClassLoader()); 677 kernel.startGBean(artifactManagerData.getAbstractName()); 678 assertEquals(State.RUNNING_INDEX, kernel.getGBeanState(artifactManagerData.getAbstractName())); 679 ArtifactManager artifactManager = (ArtifactManager) kernel.getGBean(artifactManagerData.getAbstractName()); 680 681 configStore = new TestConfigStore(); 682 TestRepository testRepository = new TestRepository(); 683 DefaultArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, testRepository); 684 685 artifact1 = new Artifact("test", "1", "1.1", "bar"); 686 artifact2 = new Artifact("test", "2", "2.2", "bar"); 687 artifact3 = new Artifact("test", "3", "3.3", "bar"); 688 artifact3NoVersion = new Artifact(artifact3.getGroupId(), artifact3.getArtifactId(), new Version(Long.toString(System.currentTimeMillis())), artifact3.getType()); 690 691 Environment e1 = new Environment(); 692 e1.setConfigId(artifact1); 693 ConfigurationData configurationData1 = new ConfigurationData(e1, kernel.getNaming()); 694 configurationData1.setConfigurationStore(configStore); 695 gbean1 = configurationData1.addGBean("gbean1", TestBean.getGBeanInfo()).getAbstractName(); 696 configurations.put(artifact1, configurationData1); 697 698 Environment e2 = new Environment(); 699 e2.setConfigId(artifact2); 700 e2.addDependency(new Artifact("test", "1", (Version) null, "bar"), ImportType.ALL); 701 ConfigurationData configurationData2 = new ConfigurationData(e2, kernel.getNaming()); 702 gbean2 = configurationData2.addGBean("gbean2", TestBean.getGBeanInfo()).getAbstractName(); 703 configurationData2.setConfigurationStore(configStore); 704 configurations.put(artifact2, configurationData2); 705 706 { Environment e3 = new Environment(); 708 e3.setConfigId(artifact3); 709 e3.addDependency(new Artifact("test", "2", (Version) null, "bar"), ImportType.ALL); 710 ConfigurationData configurationData3 = new ConfigurationData(e3, kernel.getNaming()); 711 gbean3 = configurationData3.addGBean("gbean3", TestBean.getGBeanInfo()).getAbstractName(); 712 configurationData3.setConfigurationStore(configStore); 713 configurations.put(artifact3, configurationData3); 714 } 715 716 { 717 Environment e3newer = new Environment(); 718 e3newer.setConfigId(artifact3NoVersion); 719 e3newer.addDependency(new Artifact("test", "2", (Version) null, "bar"), ImportType.ALL); 720 ConfigurationData configurationData3newer = new ConfigurationData(e3newer, kernel.getNaming()); 721 gbean3newer = configurationData3newer.addGBean("gbean3", TestBean.getGBeanInfo()).getAbstractName(); 722 configurationData3newer.setConfigurationStore(configStore); 723 configurations.put(artifact3NoVersion, configurationData3newer); 724 } 725 726 727 configurationManager = new KernelConfigurationManager(kernel, 728 Collections.singleton(configStore), 729 null, 730 null, 731 artifactManager, 732 artifactResolver, 733 Collections.singleton(testRepository), 734 Collections.EMPTY_SET, 735 KernelConfigurationManager.class.getClassLoader()); 736 } 737 738 protected void tearDown() throws Exception { 739 kernel.shutdown(); 740 super.tearDown(); 741 } 742 743 private class TestConfigStore extends NullConfigurationStore { 744 public ConfigurationData loadConfiguration(Artifact configId) throws IOException , InvalidConfigException, NoSuchConfigException { 745 return (ConfigurationData) configurations.get(configId); 746 } 747 748 public boolean containsConfiguration(Artifact configId) { 749 return configurations.containsKey(configId); 750 } 751 752 public String getObjectName() { 753 throw new UnsupportedOperationException (); 754 } 755 756 public AbstractName getAbstractName() { 757 throw new UnsupportedOperationException (); 758 } 759 760 public List listConfigurations() { 761 throw new UnsupportedOperationException (); 762 } 763 764 public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException { 765 throw new UnsupportedOperationException (); 766 } 767 768 public Set resolve(Artifact configId, String moduleName, String pattern) throws NoSuchConfigException, MalformedURLException { 769 throw new UnsupportedOperationException (); 770 } 771 } 772 773 private GBeanData buildGBeanData(String key, String value, GBeanInfo info) { 774 AbstractName abstractName = kernel.getNaming().createRootName(new Artifact("test", "foo", "1", "car"), value, key); 775 return new GBeanData(abstractName, info); 776 } 777 778 private class TestRepository implements ListableRepository { 779 public SortedSet list() { 780 return new TreeSet (configurations.keySet()); 781 } 782 783 public SortedSet list(Artifact query) { 784 TreeSet artifacts = new TreeSet (); 785 for (Iterator iterator = configurations.keySet().iterator(); iterator.hasNext();) { 786 Artifact artifact = (Artifact) iterator.next(); 787 if (query.matches(artifact)) { 788 artifacts.add(artifact); 789 } 790 } 791 return artifacts; 792 } 793 794 public boolean contains(Artifact artifact) { 795 return configurations.containsKey(artifact); 796 } 797 798 public File getLocation(Artifact artifact) { 799 throw new UnsupportedOperationException (); 800 } 801 802 public LinkedHashSet getDependencies(Artifact artifact) { 803 return new LinkedHashSet (); 804 } 805 } 806 807 public static class TestBean { 808 public TestBean(String objectName) { 809 checkFail(objectName); 810 } 811 812 private static final GBeanInfo GBEAN_INFO; 813 static { 814 GBeanInfoBuilder builder = GBeanInfoBuilder.createStatic(TestBean.class); 815 builder.addAttribute("objectName", String .class, false); 816 builder.setConstructor(new String [] {"objectName"}); 817 GBEAN_INFO = builder.getBeanInfo(); 818 } 819 820 public static GBeanInfo getGBeanInfo() { 821 return GBEAN_INFO; 822 } 823 } 824 } 825 | Popular Tags |