001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.deployment;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.io.OutputStream;
021: import java.net.MalformedURLException;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.List;
026: import java.util.Set;
027: import java.util.LinkedHashSet;
028: import java.util.jar.JarFile;
029:
030: import junit.framework.TestCase;
031: import org.apache.geronimo.common.DeploymentException;
032: import org.apache.geronimo.gbean.AbstractName;
033: import org.apache.geronimo.kernel.Jsr77Naming;
034: import org.apache.geronimo.kernel.mock.MockConfigStore;
035: import org.apache.geronimo.kernel.config.Configuration;
036: import org.apache.geronimo.kernel.config.ConfigurationAlreadyExistsException;
037: import org.apache.geronimo.kernel.config.ConfigurationData;
038: import org.apache.geronimo.kernel.config.ConfigurationManager;
039: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
040: import org.apache.geronimo.kernel.config.ConfigurationResolver;
041: import org.apache.geronimo.kernel.config.ConfigurationStore;
042: import org.apache.geronimo.kernel.config.InvalidConfigException;
043: import org.apache.geronimo.kernel.config.LifecycleException;
044: import org.apache.geronimo.kernel.config.LifecycleMonitor;
045: import org.apache.geronimo.kernel.config.LifecycleResults;
046: import org.apache.geronimo.kernel.config.NoSuchConfigException;
047: import org.apache.geronimo.kernel.config.NoSuchStoreException;
048: import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
049: import org.apache.geronimo.kernel.config.ConfigurationInfo;
050: import org.apache.geronimo.kernel.repository.Artifact;
051: import org.apache.geronimo.kernel.repository.ArtifactResolver;
052: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
053: import org.apache.geronimo.kernel.repository.Environment;
054: import org.apache.geronimo.kernel.repository.Version;
055: import org.apache.geronimo.kernel.repository.Repository;
056: import org.apache.geronimo.kernel.repository.MissingDependencyException;
057:
058: /**
059: * @version $Rev: 620870 $ $Date: 2008-02-12 09:24:48 -0800 (Tue, 12 Feb 2008) $
060: */
061: public class SingleFileHotDeployerTest extends TestCase {
062: private static final long NOW = System.currentTimeMillis();
063: private static final long PAST = NOW - 1000;
064:
065: private final Artifact NEW_ID = new Artifact("new", "new", "new",
066: "new");
067: private final Artifact OLD_VERSION_ID = new Artifact("new", "new",
068: "old", "new");
069: private final Artifact DIFFERENT_ID = new Artifact("different",
070: "different", "different", "different");
071:
072: private File basedir = new File(System.getProperty("basedir"));
073:
074: private File dir;
075: private String[] watchPaths;
076: private MockConfigurationBuilder builder;
077: private MockConfigStore store;
078: private MockConfigurationManager configurationManager;
079:
080: private ArtifactResolver artifactResolver = new DefaultArtifactResolver(
081: null, null);
082: private ArrayList existingConfigurationInfos = new ArrayList();
083:
084: private boolean shouldUninstall;
085: private boolean shouldUnload;
086: private boolean shouldLoad;
087: private boolean shouldStart;
088: private boolean isConfigurationAlreadyLoaded;
089: private boolean isConfigurationInstalled;
090:
091: private File watchFile1;
092: private File watchFile2;
093:
094: protected void setUp() throws Exception {
095: super .setUp();
096:
097: dir = new File(basedir, "target/deployTest");
098: dir.mkdirs();
099:
100: File someFile = new File(dir, "someFile");
101: someFile.createNewFile();
102:
103: String watch1 = "watch1";
104: String watch2 = "watch2";
105: watchPaths = new String[] { watch1, watch2 };
106:
107: watchFile1 = new File(dir, watch1);
108: watchFile2 = new File(dir, watch2);
109:
110: builder = new MockConfigurationBuilder();
111: store = new MockConfigStore();
112: configurationManager = new MockConfigurationManager();
113: }
114:
115: private void touch(File file, long lastModified) throws IOException {
116: file.createNewFile();
117: file.setLastModified(lastModified);
118: }
119:
120: public void testDeploy() throws Exception {
121: shouldUninstall = false;
122: shouldUnload = false;
123: shouldLoad = true;
124: shouldStart = true;
125: isConfigurationAlreadyLoaded = true;
126: isConfigurationInstalled = false;
127:
128: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
129: dir, watchPaths, Collections.singleton(builder), store,
130: configurationManager, false);
131: assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId());
132: assertEquals(dir, singleFileHotDeployer.getDir());
133: assertTrue(singleFileHotDeployer.wasDeployed());
134: assertFalse(singleFileHotDeployer.isForceDeploy());
135: }
136:
137: public void testRedeploySame() throws Exception {
138: shouldUninstall = true;
139: shouldUnload = true;
140: shouldLoad = true;
141: shouldStart = true;
142: isConfigurationAlreadyLoaded = true;
143: isConfigurationInstalled = false;
144:
145: touch(watchFile1, NOW);
146: touch(watchFile2, NOW);
147:
148: existingConfigurationInfos.add(new ConfigurationInfo(null,
149: NEW_ID, ConfigurationModuleType.CAR, PAST, null, null,
150: dir));
151:
152: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
153: dir, watchPaths, Collections.singleton(builder), store,
154: configurationManager, false);
155: assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId());
156: assertEquals(dir, singleFileHotDeployer.getDir());
157: assertTrue(singleFileHotDeployer.wasDeployed());
158: assertFalse(singleFileHotDeployer.isForceDeploy());
159: }
160:
161: public void testRedeployCompletelyNew() throws Exception {
162: shouldUninstall = true;
163: shouldUnload = true;
164: shouldLoad = true;
165: shouldStart = true;
166: isConfigurationAlreadyLoaded = true;
167: isConfigurationInstalled = false;
168:
169: touch(watchFile1, NOW);
170: touch(watchFile2, NOW);
171:
172: existingConfigurationInfos.add(new ConfigurationInfo(null,
173: DIFFERENT_ID, ConfigurationModuleType.CAR, PAST, null,
174: null, dir));
175:
176: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
177: dir, watchPaths, Collections.singleton(builder), store,
178: configurationManager, false);
179: assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId());
180: assertEquals(dir, singleFileHotDeployer.getDir());
181: assertTrue(singleFileHotDeployer.wasDeployed());
182: assertFalse(singleFileHotDeployer.isForceDeploy());
183: }
184:
185: public void testRedeployNewVersion() throws Exception {
186: shouldUninstall = true;
187: shouldUnload = true;
188: shouldLoad = true;
189: shouldStart = true;
190: isConfigurationAlreadyLoaded = true;
191: isConfigurationInstalled = false;
192:
193: touch(watchFile1, NOW);
194: touch(watchFile2, NOW);
195:
196: existingConfigurationInfos.add(new ConfigurationInfo(null,
197: OLD_VERSION_ID, ConfigurationModuleType.CAR, PAST,
198: null, null, dir));
199:
200: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
201: dir, watchPaths, Collections.singleton(builder), store,
202: configurationManager, 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,
221: NEW_ID, ConfigurationModuleType.CAR, NOW, null, null,
222: dir));
223:
224: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
225: dir, watchPaths, Collections.singleton(builder), store,
226: configurationManager, false);
227: assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId());
228: assertEquals(dir, singleFileHotDeployer.getDir());
229: assertFalse(singleFileHotDeployer.wasDeployed());
230: assertFalse(singleFileHotDeployer.isForceDeploy());
231: }
232:
233: public void testForceRedeploy() throws Exception {
234: shouldUninstall = true;
235: shouldUnload = true;
236: shouldLoad = true;
237: shouldStart = true;
238: isConfigurationAlreadyLoaded = true;
239: isConfigurationInstalled = false;
240:
241: touch(watchFile1, PAST);
242: touch(watchFile2, PAST);
243:
244: existingConfigurationInfos.add(new ConfigurationInfo(null,
245: OLD_VERSION_ID, ConfigurationModuleType.CAR, NOW, null,
246: null, dir));
247:
248: SingleFileHotDeployer singleFileHotDeployer = new SingleFileHotDeployer(
249: dir, watchPaths, Collections.singleton(builder), store,
250: configurationManager, true);
251: assertEquals(NEW_ID, singleFileHotDeployer.getConfigurationId());
252: assertEquals(dir, singleFileHotDeployer.getDir());
253: assertTrue(singleFileHotDeployer.wasDeployed());
254: assertTrue(singleFileHotDeployer.isForceDeploy());
255: }
256:
257: private class MockConfigurationBuilder implements
258: ConfigurationBuilder {
259: public Object getDeploymentPlan(File planFile, JarFile module,
260: ModuleIDBuilder idBuilder) throws DeploymentException {
261: return new Object();
262: }
263:
264: public Artifact getConfigurationID(Object plan, JarFile module,
265: ModuleIDBuilder idBuilder) throws IOException,
266: DeploymentException {
267: return NEW_ID;
268: }
269:
270: public DeploymentContext buildConfiguration(
271: boolean inPlaceDeployment, Artifact configId,
272: Object plan, JarFile module,
273: Collection configurationStores,
274: ArtifactResolver artifactResolver,
275: ConfigurationStore targetConfigurationStore)
276: throws IOException, DeploymentException {
277: return new DeploymentContext(dir, dir, new Environment(
278: configId), null, ConfigurationModuleType.CAR,
279: new Jsr77Naming(), new SimpleConfigurationManager(
280: Collections.singletonList(store),
281: artifactResolver, Collections.EMPTY_SET));
282: }
283: }
284:
285: /*
286: private static class MockConfigurationStore implements ConfigurationStore {
287: public boolean isInPlaceConfiguration(Artifact configId) throws NoSuchConfigException, IOException {
288: throw new UnsupportedOperationException();
289: }
290:
291: public void install(ConfigurationData configurationData) throws IOException, InvalidConfigException {
292: }
293:
294: public void uninstall(Artifact configId) throws NoSuchConfigException, IOException {
295: throw new UnsupportedOperationException();
296: }
297:
298: public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException, InvalidConfigException {
299: throw new UnsupportedOperationException();
300: }
301:
302: public boolean containsConfiguration(Artifact configId) {
303: throw new UnsupportedOperationException();
304: }
305:
306: public String getObjectName() {
307: throw new UnsupportedOperationException();
308: }
309:
310: public AbstractName getAbstractName() {
311: throw new UnsupportedOperationException();
312: }
313:
314: public List listConfigurations() {
315: throw new UnsupportedOperationException();
316: }
317:
318: public File createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
319: throw new UnsupportedOperationException();
320: }
321:
322: public Set resolve(Artifact configId, String moduleName, String path) throws NoSuchConfigException, MalformedURLException {
323: throw new UnsupportedOperationException();
324: }
325:
326: public void exportConfiguration(Artifact configId, OutputStream output) throws IOException, NoSuchConfigException {
327: throw new UnsupportedOperationException();
328: }
329: }
330: */
331:
332: private class MockConfigurationManager extends
333: org.apache.geronimo.kernel.mock.MockConfigurationManager {
334: private ConfigurationData loadedConfigurationData;
335:
336: public boolean isInstalled(Artifact configurationId) {
337: return isConfigurationInstalled;
338: }
339:
340: public boolean isLoaded(Artifact configurationId) {
341: return isConfigurationAlreadyLoaded;
342: }
343:
344: public List listConfigurations() {
345: return existingConfigurationInfos;
346: }
347:
348: public ConfigurationStore[] getStores() {
349: return new ConfigurationStore[] { store };
350: }
351:
352: public Configuration getConfiguration(Artifact configurationId) {
353: try {
354: return new Configuration(
355: Collections.EMPTY_SET,
356: loadedConfigurationData,
357: new ConfigurationResolver(configurationId, dir),
358: null);
359: } catch (Exception e) {
360: throw new RuntimeException(e);
361: }
362: }
363:
364: public LifecycleResults loadConfiguration(
365: Artifact configurationId) throws NoSuchConfigException,
366: LifecycleException {
367: assertTrue("Did not expect configuration to be loaded "
368: + configurationId, shouldLoad);
369: return null;
370: }
371:
372: public LifecycleResults loadConfiguration(
373: ConfigurationData configurationData)
374: throws NoSuchConfigException, LifecycleException {
375: loadedConfigurationData = configurationData;
376: return null;
377: }
378:
379: public LifecycleResults unloadConfiguration(
380: Artifact configurationId) throws NoSuchConfigException {
381: assertTrue("Did not expect configuration to be unloaded "
382: + configurationId, shouldUnload);
383: return null;
384: }
385:
386: public LifecycleResults startConfiguration(
387: Artifact configurationId) throws NoSuchConfigException,
388: LifecycleException {
389: assertTrue("Did not expect configuration to be started "
390: + configurationId, shouldStart);
391: return null;
392: }
393:
394: public void uninstallConfiguration(Artifact configurationId)
395: throws IOException, NoSuchConfigException {
396: assertTrue(
397: "Did not expect configuration to be uninstalled "
398: + configurationId, shouldUninstall);
399: }
400:
401: public ArtifactResolver getArtifactResolver() {
402: return artifactResolver;
403: }
404:
405: }
406: }
|