KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > SingleFileHotDeployerTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.deployment;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
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 /**
57  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
58  */

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 JavaDoc basedir = new File JavaDoc(System.getProperty("basedir"));
68     
69     private File JavaDoc dir;
70     private String JavaDoc[] 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 JavaDoc existingConfigurationInfos = new ArrayList JavaDoc();
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 JavaDoc watchFile1;
86     private File JavaDoc watchFile2;
87
88     protected void setUp() throws Exception JavaDoc {
89         super.setUp();
90
91         dir = new File JavaDoc(basedir, "target/deployTest");
92         dir.mkdirs();
93
94         File JavaDoc someFile = new File JavaDoc(dir, "someFile");
95         someFile.createNewFile();
96
97         String JavaDoc watch1 = "watch1";
98         String JavaDoc watch2 = "watch2";
99         watchPaths = new String JavaDoc[]{watch1, watch2};
100
101         watchFile1 = new File JavaDoc(dir, watch1);
102         watchFile2 = new File JavaDoc(dir, watch2);
103
104         builder = new MockConfigurationBuilder();
105         store = new MockConfigurationStore();
106         configurationManager = new MockConfigurationManager();
107     }
108
109     private void touch(File JavaDoc file, long lastModified) throws IOException JavaDoc {
110         file.createNewFile();
111         file.setLastModified(lastModified);
112     }
113
114     public void testDeploy() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc getDeploymentPlan(File JavaDoc planFile, JarFile JavaDoc module, ModuleIDBuilder idBuilder) throws DeploymentException {
261             return new Object JavaDoc();
262         }
263
264         public Artifact getConfigurationID(Object JavaDoc plan, JarFile JavaDoc module, ModuleIDBuilder idBuilder) throws IOException JavaDoc, DeploymentException {
265             return NEW_ID;
266         }
267
268         public DeploymentContext buildConfiguration(boolean inPlaceDeployment, Artifact configId, Object JavaDoc plan, JarFile JavaDoc module, Collection JavaDoc configurationStores, ArtifactResolver artifactResolver, ConfigurationStore targetConfigurationStore) throws IOException JavaDoc, 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 JavaDoc {
281             throw new UnsupportedOperationException JavaDoc();
282         }
283
284         public void install(ConfigurationData configurationData) throws IOException JavaDoc, InvalidConfigException {
285         }
286
287         public void uninstall(Artifact configId) throws NoSuchConfigException, IOException JavaDoc {
288             throw new UnsupportedOperationException JavaDoc();
289         }
290
291         public ConfigurationData loadConfiguration(Artifact configId) throws NoSuchConfigException, IOException JavaDoc, InvalidConfigException {
292             throw new UnsupportedOperationException JavaDoc();
293         }
294
295         public boolean containsConfiguration(Artifact configId) {
296             throw new UnsupportedOperationException JavaDoc();
297         }
298
299         public String JavaDoc getObjectName() {
300             throw new UnsupportedOperationException JavaDoc();
301         }
302
303         public AbstractName getAbstractName() {
304             throw new UnsupportedOperationException JavaDoc();
305         }
306
307         public List JavaDoc listConfigurations() {
308             throw new UnsupportedOperationException JavaDoc();
309         }
310
311         public File JavaDoc createNewConfigurationDir(Artifact configId) throws ConfigurationAlreadyExistsException {
312             throw new UnsupportedOperationException JavaDoc();
313         }
314
315         public Set JavaDoc resolve(Artifact configId, String JavaDoc moduleName, String JavaDoc path) throws NoSuchConfigException, MalformedURLException JavaDoc {
316             throw new UnsupportedOperationException JavaDoc();
317         }
318
319         public void exportConfiguration(Artifact configId, OutputStream JavaDoc output) throws IOException JavaDoc, NoSuchConfigException {
320             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc();
337         }
338
339         public Artifact[] getInstalled(Artifact query) {
340             throw new UnsupportedOperationException JavaDoc();
341         }
342
343         public Artifact[] getLoaded(Artifact query) {
344             throw new UnsupportedOperationException JavaDoc();
345         }
346
347         public Artifact[] getRunning(Artifact query) {
348             throw new UnsupportedOperationException JavaDoc();
349         }
350
351         public List JavaDoc listConfigurations() {
352             return existingConfigurationInfos;
353         }
354
355         public List JavaDoc listStores() {
356             throw new UnsupportedOperationException JavaDoc();
357         }
358
359         public ConfigurationStore[] getStores() {
360             return new ConfigurationStore[]{store};
361         }
362
363         public ConfigurationStore getStoreForConfiguration(Artifact configuration) {
364             throw new UnsupportedOperationException JavaDoc();
365         }
366
367         public List JavaDoc listConfigurations(AbstractName store) throws NoSuchStoreException {
368             throw new UnsupportedOperationException JavaDoc();
369         }
370
371         public boolean isConfiguration(Artifact artifact) {
372             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc e) {
379                 throw new RuntimeException JavaDoc(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 JavaDoc();
395         }
396
397         public LifecycleResults loadConfiguration(ConfigurationData configurationData, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException {
398             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc();
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 JavaDoc();
417         }
418
419         public LifecycleResults stopConfiguration(Artifact configurationId) throws NoSuchConfigException {
420             throw new UnsupportedOperationException JavaDoc();
421         }
422
423         public LifecycleResults stopConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException {
424             throw new UnsupportedOperationException JavaDoc();
425         }
426
427         public LifecycleResults restartConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException {
428             throw new UnsupportedOperationException JavaDoc();
429         }
430
431         public LifecycleResults restartConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException {
432             throw new UnsupportedOperationException JavaDoc();
433         }
434
435         public LifecycleResults reloadConfiguration(Artifact configurationId) throws NoSuchConfigException, LifecycleException {
436             throw new UnsupportedOperationException JavaDoc();
437         }
438
439         public LifecycleResults reloadConfiguration(Artifact configurationId, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException {
440             throw new UnsupportedOperationException JavaDoc();
441         }
442
443         public LifecycleResults reloadConfiguration(Artifact configurationId, Version version) throws NoSuchConfigException, LifecycleException {
444             throw new UnsupportedOperationException JavaDoc();
445         }
446
447         public LifecycleResults reloadConfiguration(Artifact configurationId, Version version, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException {
448             throw new UnsupportedOperationException JavaDoc();
449         }
450
451         public LifecycleResults reloadConfiguration(ConfigurationData configurationData) throws NoSuchConfigException, LifecycleException {
452             throw new UnsupportedOperationException JavaDoc();
453         }
454
455         public LifecycleResults reloadConfiguration(ConfigurationData configurationData, LifecycleMonitor monitor) throws NoSuchConfigException, LifecycleException {
456             throw new UnsupportedOperationException JavaDoc();
457         }
458
459         public void uninstallConfiguration(Artifact configurationId) throws IOException JavaDoc, 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