KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > deployer > NanoContainerDeployerTestCase


1 package org.nanocontainer.deployer;
2
3 import org.apache.commons.vfs.FileObject;
4 import org.apache.commons.vfs.FileSystemException;
5 import org.apache.commons.vfs.impl.DefaultFileSystemManager;
6 import org.apache.commons.vfs.impl.VFSClassLoader;
7 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
8 import org.apache.commons.vfs.provider.zip.ZipFileProvider;
9 import org.nanocontainer.deployer.Deployer;
10 import org.nanocontainer.deployer.NanoContainerDeployer;
11 import org.picocontainer.PicoContainer;
12 import org.picocontainer.defaults.ObjectReference;
13 import junit.framework.TestCase;
14
15 import java.net.MalformedURLException JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.io.File JavaDoc;
18
19 /**
20  * @author Aslak Hellesøy
21  * @version $Revision: 3157 $
22  */

23 public class NanoContainerDeployerTestCase extends TestCase {
24
25     private String JavaDoc jarsDir = "target/deployer/apps";
26     private String JavaDoc folderPath = "src/deploytest";
27
28     public void testZipWithDeploymentScriptAndClassesCanBeDeployed() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc, InstantiationException JavaDoc {
29         DefaultFileSystemManager manager = new DefaultFileSystemManager();
30         FileObject applicationArchive = getApplicationArchive(manager, jarsDir + "/successful-deploy.jar");
31
32         Deployer deployer = new NanoContainerDeployer(manager);
33         ObjectReference containerRef = deployer.deploy(applicationArchive, getClass().getClassLoader(), null);
34         PicoContainer pico = (PicoContainer) containerRef.get();
35         Object JavaDoc zap = pico.getComponentInstance("zap");
36         assertEquals("Groovy Started", zap.toString());
37     }
38
39     public void testZipWithBadScriptNameThrowsFileSystemException() throws ClassNotFoundException JavaDoc, FileSystemException {
40
41       DefaultFileSystemManager manager = new DefaultFileSystemManager();
42       FileObject applicationFolder = getApplicationArchive(manager, jarsDir + "/badscript-deploy.jar");
43
44       try {
45         Deployer deployer = new NanoContainerDeployer(manager);
46         ObjectReference containerRef= deployer.deploy(applicationFolder, getClass().getClassLoader(), null,null);
47         fail("Deployment should have thrown FileSystemException for bad script file name. Instead got:" + containerRef.toString() + " built.");
48       }
49       catch (FileSystemException ex) {
50         //a-ok
51
}
52     }
53
54     public void testMalformedDeployerArchiveThrowsFileSystemException() throws ClassNotFoundException JavaDoc, FileSystemException {
55       DefaultFileSystemManager manager = new DefaultFileSystemManager();
56       FileObject applicationFolder = getApplicationArchive(manager, jarsDir + "/malformed-deploy.jar");
57
58       try {
59         Deployer deployer = new NanoContainerDeployer(manager);
60         ObjectReference containerRef= deployer.deploy(applicationFolder, getClass().getClassLoader(), null,null);
61         fail("Deployment should have thrown FileSystemException for badly formed archive. Instead got:" + containerRef.toString() + " built.");
62       }
63       catch (FileSystemException ex) {
64         //a-ok
65
}
66     }
67
68     public void testFolderWithDeploymentScriptAndClassesCanBeDeployed() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, NoSuchMethodException JavaDoc, InvocationTargetException JavaDoc, InstantiationException JavaDoc {
69         DefaultFileSystemManager manager = new DefaultFileSystemManager();
70         FileObject applicationFolder = getApplicationFolder(manager, folderPath);
71
72         try {
73             Deployer deployer = null;
74             deployer = new NanoContainerDeployer(manager);
75             ObjectReference containerRef = deployer.deploy(applicationFolder, getClass().getClassLoader(), null,null);
76             PicoContainer pico = (PicoContainer) containerRef.get();
77             Object JavaDoc zap = pico.getComponentInstance("zap");
78             assertEquals("Groovy Started", zap.toString());
79
80         } catch (Exception JavaDoc e) {
81             e.printStackTrace();
82         }
83     }
84
85     public void testZapClassCanBeLoadedByVFSClassLoader() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
86         DefaultFileSystemManager manager = new DefaultFileSystemManager();
87         FileObject applicationFolder = getApplicationFolder(manager, folderPath);
88         ClassLoader JavaDoc applicationClassLoader = new VFSClassLoader(applicationFolder, manager, getClass().getClassLoader());
89         applicationClassLoader.loadClass("foo.bar.Zap");
90     }
91
92     public void testSettingDifferentBaseNameWillResultInChangeForWhatBuilderLooksFor() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
93         DefaultFileSystemManager manager = new DefaultFileSystemManager();
94         FileObject applicationFolder = getApplicationFolder(manager, folderPath);
95         NanoContainerDeployer deployer = new NanoContainerDeployer(manager);
96         assertEquals("nanocontainer", deployer.getFileBasename());
97
98         deployer = new NanoContainerDeployer(manager,"foo");
99         assertEquals("foo", deployer.getFileBasename());
100
101         try {
102             ObjectReference containerRef = deployer.deploy(applicationFolder, getClass().getClassLoader(), null);
103             fail("Deployer should have now thrown an exception after changing the base name. Instead got: " + containerRef.toString());
104         }
105         catch (FileSystemException ex) {
106             //a-ok
107
}
108
109     }
110
111
112     public void testParentClassLoadersArePropertyPropagated() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
113         DefaultFileSystemManager manager = new DefaultFileSystemManager();
114         FileObject applicationFolder = getApplicationFolder(manager, folderPath);
115         NanoContainerDeployer deployer = new NanoContainerDeployer(manager);
116         FileObject badArchive = getApplicationArchive(manager, jarsDir + "/successful-deploy.jar");
117         VFSClassLoader classLoader = new VFSClassLoader(new FileObject[] {badArchive}, manager, getClass().getClassLoader());
118
119         ObjectReference containerRef = deployer.deploy(applicationFolder, classLoader, null, null);
120
121     }
122
123     public void testAssemblyScope() throws FileSystemException, MalformedURLException JavaDoc, ClassNotFoundException JavaDoc {
124         DefaultFileSystemManager manager = new DefaultFileSystemManager();
125         FileObject applicationArchive = getApplicationArchive(manager, jarsDir + "/successful-deploy.jar");
126
127         Deployer deployer = new NanoContainerDeployer(manager);
128
129         ObjectReference containerRef = deployer.deploy(applicationArchive, getClass().getClassLoader(), null, "Test");
130         PicoContainer pico = (PicoContainer)containerRef.get();
131         assertEquals("Assembly Scope Test", pico.getComponentInstance(String JavaDoc.class));
132         assertNull(pico.getComponentInstance("zap"));
133     }
134
135
136     private FileObject getApplicationFolder(final DefaultFileSystemManager manager, String JavaDoc folderPath) throws FileSystemException, MalformedURLException JavaDoc {
137         manager.setDefaultProvider(new DefaultLocalFileProvider());
138         manager.init();
139         File JavaDoc testapp = new File JavaDoc(folderPath);
140         String JavaDoc url = testapp.toURL().toExternalForm();
141         if (url.endsWith("/")) {
142             url = url.substring(0, url.length() - 1);
143         }
144         FileObject applicationFolder = manager.resolveFile(url);
145         return applicationFolder;
146     }
147
148     private FileObject getApplicationArchive(final DefaultFileSystemManager manager, final String JavaDoc jarPath) throws FileSystemException {
149         manager.addProvider("file", new DefaultLocalFileProvider());
150         manager.addProvider("zip", new ZipFileProvider());
151         manager.init();
152         File JavaDoc src = new File JavaDoc(jarPath);
153         FileObject applicationFolder = manager.resolveFile("zip:/" + src.getAbsolutePath());
154         return applicationFolder;
155     }
156
157
158
159
160
161 }
162
Popular Tags