1 17 package org.apache.geronimo.deployment; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.net.MalformedURLException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.Set ; 28 import java.util.jar.JarFile ; 29 30 import junit.framework.TestCase; 31 import org.apache.geronimo.common.DeploymentException; 32 import org.apache.geronimo.gbean.AbstractName; 33 import org.apache.geronimo.kernel.Jsr77Naming; 34 import org.apache.geronimo.kernel.config.Configuration; 35 import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException; 36 import org.apache.geronimo.kernel.config.ConfigurationData; 37 import org.apache.geronimo.kernel.config.ConfigurationManager; 38 import org.apache.geronimo.kernel.config.ConfigurationModuleType; 39 import org.apache.geronimo.kernel.config.ConfigurationResolver; 40 import org.apache.geronimo.kernel.config.ConfigurationStore; 41 import org.apache.geronimo.kernel.config.InvalidConfigException; 42 import org.apache.geronimo.kernel.config.LifecycleException; 43 import org.apache.geronimo.kernel.config.LifecycleMonitor; 44 import org.apache.geronimo.kernel.config.LifecycleResults; 45 import org.apache.geronimo.kernel.config.NoSuchConfigException; 46 import org.apache.geronimo.kernel.config.NoSuchStoreException; 47 import org.apache.geronimo.kernel.config.SimpleConfigurationManager; 48 import org.apache.geronimo.kernel.config.ConfigurationInfo; 49 import org.apache.geronimo.kernel.repository.Artifact; 50 import org.apache.geronimo.kernel.repository.ArtifactResolver; 51 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver; 52 import org.apache.geronimo.kernel.repository.Environment; 53 import org.apache.geronimo.kernel.repository.Version; 54 55 56 59 public class SingleFileHotDeployerTest extends TestCase { 60 private static final long NOW = System.currentTimeMillis(); 61 private static final long PAST = NOW - 1000; 62 63 private final Artifact NEW_ID = new Artifact("new", "new", "new", "new"); 64 private final Artifact OLD_VERSION_ID = new Artifact("new", "new", "old", "new"); 65 private final Artifact DIFFERENT_ID = new Artifact("different", "different", "different", "different"); 66 67 private File basedir = new File (System.getProperty("basedir")); 68 69 private File dir; 70 private String [] watchPaths; 71 private MockConfigurationBuilder builder; 72 private MockConfigurationStore store; 73 private MockConfigurationManager configurationManager; 74 75 private ArtifactResolver artifactResolver = new DefaultArtifactResolver(null, null); 76 private ArrayList existingConfigurationInfos = new ArrayList (); 77 78 private boolean shouldUninstall; 79 private boolean shouldUnload; 80 private boolean shouldLoad; 81 private boolean shouldStart; 82 private boolean isConfigurationAlreadyLoaded; 83 private boolean isConfigurationInstalled; 84 85 private File watchFile1; 86 private File watchFile2; 87 88 protected void setUp() throws Exception { 89 super.setUp(); 90 91 dir = new File (basedir, "target/deployTest"); 92 dir.mkdirs(); 93 94 File someFile = new File (dir, "someFile"); 95 someFile.createNewFile(); 96 97 String watch1 = "watch1"; 98 String watch2 = "watch2"; 99 watchPaths = new String []{watch1, watch2}; 100 101 watchFile1 = new File (dir, watch1); 102 watchFile2 = new File (dir, watch2); 103 104 builder = new MockConfigurationBuilder(); 105 store = new MockConfigurationStore(); 106 configurationManager = new MockConfigurationManager(); 107 } 108 109 private void touch(File file, long lastModified) throws IOException { 110 file.createNewFile(); 111 file.setLastModified(lastModified); 112 } 113 114 public void testDeploy() throws Exception { 115 shouldUninstall = false; 116 shouldUnload = false; 117 shouldLoad = true; 118 shouldStart = true; 119 isConfigurationAlreadyLoaded = true; 120 isConfigurationInstalled = false; 121 122 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 123 watchPaths, 124 Collections.singleton(builder), 125 store, 126 configurationManager, 127 false); 128 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 129 assertEquals(dir, singleFileHotDeployer.getDir()); 130 assertTrue(singleFileHotDeployer.wasDeployed()); 131 assertFalse(singleFileHotDeployer.isForceDeploy()); 132 } 133 134 public void testRedeploySame() throws Exception { 135 shouldUninstall = true; 136 shouldUnload = true; 137 shouldLoad = true; 138 shouldStart = true; 139 isConfigurationAlreadyLoaded = true; 140 isConfigurationInstalled = false; 141 142 touch(watchFile1, NOW); 143 touch(watchFile2, NOW); 144 145 existingConfigurationInfos.add(new ConfigurationInfo(null, NEW_ID, ConfigurationModuleType.CAR, PAST, null, null, dir)); 146 147 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 148 watchPaths, 149 Collections.singleton(builder), 150 store, 151 configurationManager, 152 false); 153 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 154 assertEquals(dir, singleFileHotDeployer.getDir()); 155 assertTrue(singleFileHotDeployer.wasDeployed()); 156 assertFalse(singleFileHotDeployer.isForceDeploy()); 157 } 158 159 public void testRedeployCompletelyNew() throws Exception { 160 shouldUninstall = true; 161 shouldUnload = true; 162 shouldLoad = true; 163 shouldStart = true; 164 isConfigurationAlreadyLoaded = true; 165 isConfigurationInstalled = false; 166 167 touch(watchFile1, NOW); 168 touch(watchFile2, NOW); 169 170 existingConfigurationInfos.add(new ConfigurationInfo(null, DIFFERENT_ID, ConfigurationModuleType.CAR, PAST, null, null, dir)); 171 172 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 173 watchPaths, 174 Collections.singleton(builder), 175 store, 176 configurationManager, 177 false); 178 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 179 assertEquals(dir, singleFileHotDeployer.getDir()); 180 assertTrue(singleFileHotDeployer.wasDeployed()); 181 assertFalse(singleFileHotDeployer.isForceDeploy()); 182 } 183 184 public void testRedeployNewVersion() throws Exception { 185 shouldUninstall = true; 186 shouldUnload = true; 187 shouldLoad = true; 188 shouldStart = true; 189 isConfigurationAlreadyLoaded = true; 190 isConfigurationInstalled = false; 191 192 touch(watchFile1, NOW); 193 touch(watchFile2, NOW); 194 195 existingConfigurationInfos.add(new ConfigurationInfo(null, OLD_VERSION_ID, ConfigurationModuleType.CAR, PAST, null, null, dir)); 196 197 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 198 watchPaths, 199 Collections.singleton(builder), 200 store, 201 configurationManager, 202 false); 203 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 204 assertEquals(dir, singleFileHotDeployer.getDir()); 205 assertTrue(singleFileHotDeployer.wasDeployed()); 206 assertFalse(singleFileHotDeployer.isForceDeploy()); 207 } 208 209 public void testNoRedeploy() throws Exception { 210 shouldUninstall = false; 211 shouldUnload = false; 212 shouldLoad = true; 213 shouldStart = true; 214 isConfigurationAlreadyLoaded = true; 215 isConfigurationInstalled = false; 216 217 touch(watchFile1, PAST); 218 touch(watchFile2, PAST); 219 220 existingConfigurationInfos.add(new ConfigurationInfo(null, NEW_ID, ConfigurationModuleType.CAR, NOW, null, null, dir)); 221 222 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 223 watchPaths, 224 Collections.singleton(builder), 225 store, 226 configurationManager, 227 false); 228 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 229 assertEquals(dir, singleFileHotDeployer.getDir()); 230 assertFalse(singleFileHotDeployer.wasDeployed()); 231 assertFalse(singleFileHotDeployer.isForceDeploy()); 232 } 233 234 public void testForceRedeploy() throws Exception { 235 shouldUninstall = true; 236 shouldUnload = true; 237 shouldLoad = true; 238 shouldStart = true; 239 isConfigurationAlreadyLoaded = true; 240 isConfigurationInstalled = false; 241 242 touch(watchFile1, PAST); 243 touch(watchFile2, PAST); 244 245 existingConfigurationInfos.add(new ConfigurationInfo(null, OLD_VERSION_ID, ConfigurationModuleType.CAR, NOW, null, null, dir)); 246 247 SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(dir, 248 watchPaths, 249 Collections.singleton(builder), 250 store, 251 configurationManager, 252 true); 253 assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId()); 254 assertEquals(dir, singleFileHotDeployer.getDir()); 255 assertTrue(singleFileHotDeployer.wasDeployed()); 256 assertTrue(singleFileHotDeployer.isForceDeploy()); 257 } 258 259 private class MockConfigurationBuilder implements ConfigurationBuilder { 260 public Object getDeploymentPlan(File planFile, JarFile module, ModuleIDBuilder idBuilder) throws DeploymentException { 261 return new Object (); 262 } 263 264 public Artifact getConfigurationID(Object plan, JarFile module, ModuleIDBuilder idBuilder) throws IOException , DeploymentException { 265 return NEW_ID; 266 } 267 268 public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object plan, JarFile module, Collection configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException , DeploymentException { 269 return new DeploymentContext(dir, 270 dir, 271 new Environment(configId), 272 null, 273 ConfigurationModuleType.CAR, 274 new Jsr77Naming(), 275 new SimpleConfigurationManager(Collections.singletonList(store), artifactResolver, Collections.EMPTY_SET)); 276 } 277 } 278 279 private class MockConfigurationStore implements ConfigurationStore { 280 public boolean isInPlaceConfiguration(Artifact configId) throws NoSuchConfigException, IOException { 281 throw new UnsupportedOperationException (); 282 } 283 284 public void install(ConfigurationData configurationData) throws IOException , InvalidConfigException { 285 } 286 287 public void uninstall(Artifact configId) throws NoSuchConfigException, IOException { 288 throw new UnsupportedOperationException (); 289 } 290 291 public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException , InvalidConfigException { 292 throw new UnsupportedOperationException (); 293 } 294 295 public boolean containsConfiguration(Artifact configId) { 296 throw new UnsupportedOperationException (); 297 } 298 299 public String getObjectName() { 300 throw new UnsupportedOperationException (); 301 } 302 303 public AbstractName getAbstractName() { 304 throw new UnsupportedOperationException (); 305 } 306 307 public List listConfigurations() { 308 throw new UnsupportedOperationException (); 309 } 310 311 public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException { 312 throw new UnsupportedOperationException (); 313 } 314 315 public Set resolve(Artifact configId, String moduleName, String path) throws NoSuchConfigException, MalformedURLException { 316 throw new UnsupportedOperationException (); 317 } 318 319 public void exportConfiguration(Artifact configId, OutputStream output) throws IOException , NoSuchConfigException { 320 throw new UnsupportedOperationException (); 321 } 322 } 323 324 private class MockConfigurationManager implements ConfigurationManager { 325 private ConfigurationData loadedConfigurationData; 326 327 public boolean isInstalled(Artifact configurationId) { 328 return isConfigurationInstalled; 329 } 330 331 public boolean isLoaded(Artifact configurationId) { 332 return isConfigurationAlreadyLoaded; 333 } 334 335 public boolean isRunning(Artifact configurationId) { 336 throw new UnsupportedOperationException (); 337 } 338 339 public Artifact[] getInstalled(Artifact query) { 340 throw new UnsupportedOperationException (); 341 } 342 343 public Artifact[] getLoaded(Artifact query) { 344 throw new UnsupportedOperationException (); 345 } 346 347 public Artifact[] getRunning(Artifact query) { 348 throw new UnsupportedOperationException (); 349 } 350 351 public List listConfigurations() { 352 return existingConfigurationInfos; 353 } 354 355 public List listStores() { 356 throw new UnsupportedOperationException (); 357 } 358 359 public ConfigurationStore[] getStores() { 360 return new ConfigurationStore[]{store}; 361 } 362 363 public ConfigurationStore getStoreForConfiguration(Artifact configuration) { 364 throw new UnsupportedOperationException (); 365 } 366 367 public List listConfigurations(AbstractName store) throws NoSuchStoreException { 368 throw new UnsupportedOperationException (); 369 } 370 371 public boolean isConfiguration(Artifact artifact) { 372 throw new UnsupportedOperationException (); 373 } 374 375 public Configuration getConfiguration(Artifact configurationId) { 376 try { 377 return new Configuration(Collections.EMPTY_SET, loadedConfigurationData, new ConfigurationResolver(configurationId, dir), null); 378 } catch (Exception e) { 379 throw new RuntimeException (e); 380 } 381 } 382 383 public LifecycleResults loadConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException { 384 assertTrue("Did not expect configuration to be loaded " + configurationId, shouldLoad); 385 return null; 386 } 387 388 public LifecycleResults loadConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, LifecycleException { 389 loadedConfigurationData = configurationData; 390 return null; 391 } 392 393 public LifecycleResults loadConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 394 throw new UnsupportedOperationException (); 395 } 396 397 public LifecycleResults loadConfiguration(ConfigurationData configurationData, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 398 throw new UnsupportedOperationException (); 399 } 400 401 public LifecycleResults unloadConfiguration(Artifact configurationId) throws NoSuchConfigException { 402 assertTrue("Did not expect configuration to be unloaded " + configurationId, shouldUnload); 403 return null; 404 } 405 406 public LifecycleResults unloadConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException { 407 throw new UnsupportedOperationException (); 408 } 409 410 public LifecycleResults startConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException { 411 assertTrue("Did not expect configuration to be started " + configurationId, shouldStart); 412 return null; 413 } 414 415 public LifecycleResults startConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 416 throw new UnsupportedOperationException (); 417 } 418 419 public LifecycleResults stopConfiguration(Artifact configurationId) throws NoSuchConfigException { 420 throw new UnsupportedOperationException (); 421 } 422 423 public LifecycleResults stopConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException { 424 throw new UnsupportedOperationException (); 425 } 426 427 public LifecycleResults restartConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException { 428 throw new UnsupportedOperationException (); 429 } 430 431 public LifecycleResults restartConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 432 throw new UnsupportedOperationException (); 433 } 434 435 public LifecycleResults reloadConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException { 436 throw new UnsupportedOperationException (); 437 } 438 439 public LifecycleResults reloadConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 440 throw new UnsupportedOperationException (); 441 } 442 443 public LifecycleResults reloadConfiguration(Artifact configurationId, Version version) throws NoSuchConfigException, LifecycleException { 444 throw new UnsupportedOperationException (); 445 } 446 447 public LifecycleResults reloadConfiguration(Artifact configurationId, Version version, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 448 throw new UnsupportedOperationException (); 449 } 450 451 public LifecycleResults reloadConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, LifecycleException { 452 throw new UnsupportedOperationException (); 453 } 454 455 public LifecycleResults reloadConfiguration(ConfigurationData configurationData, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException { 456 throw new UnsupportedOperationException (); 457 } 458 459 public void uninstallConfiguration(Artifact configurationId) throws IOException , NoSuchConfigException { 460 assertTrue("Did not expect configuration to be uninstalled " + configurationId, shouldUninstall); 461 } 462 463 public ArtifactResolver getArtifactResolver() { 464 return artifactResolver; 465 } 466 467 public boolean isOnline() { 468 return true; 469 } 470 471 public void setOnline(boolean online) { 472 } 473 } 474 } 475 | Popular Tags |