KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > wizards > ImportBlueprintEarWizardIteratorTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject.ui.wizards;
21
22 import java.awt.Dialog JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import org.netbeans.api.java.classpath.ClassPath;
32 import org.netbeans.api.java.platform.JavaPlatform;
33 import org.netbeans.api.java.platform.Specification;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.j2ee.dd.api.application.Application;
37 import org.netbeans.modules.j2ee.dd.api.application.DDProvider;
38 import org.netbeans.modules.j2ee.dd.api.application.Module;
39 import org.netbeans.modules.j2ee.dd.api.application.Web;
40 import org.netbeans.modules.j2ee.earproject.EarProject;
41 import org.netbeans.modules.j2ee.earproject.EarProjectTest;
42 import org.netbeans.modules.j2ee.earproject.ModuleType;
43 import org.netbeans.modules.j2ee.earproject.test.TestUtil;
44 import org.netbeans.modules.java.platform.JavaPlatformProvider;
45 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
46 import org.netbeans.spi.project.support.ant.AntProjectHelper;
47 import org.netbeans.spi.project.support.ant.EditableProperties;
48 import org.openide.DialogDescriptor;
49 import org.openide.DialogDisplayer;
50 import org.openide.NotifyDescriptor;
51 import org.openide.filesystems.FileObject;
52 import org.openide.filesystems.FileUtil;
53 import org.openide.modules.SpecificationVersion;
54
55 /**
56  * @author Martin Krauskopf
57  */

58 public class ImportBlueprintEarWizardIteratorTest extends NbTestCase {
59     
60     private static final String JavaDoc CUSTOM_CONTEXT_ROOT = "/my-context-root";
61     
62     /* Default values. */
63     private String JavaDoc name = "Test EnterpriseApplication";
64     private String JavaDoc j2eeLevel = "1.5";
65     private String JavaDoc warName = "testEA-war";
66     private String JavaDoc jarName = "testEA-ejb";
67     private String JavaDoc carName = "testEA-app-client";
68     private String JavaDoc mainClass = "testEA.app.client.Main";
69     private String JavaDoc platformName = null;
70     private String JavaDoc sourceLevel = "1.5";
71     
72     private String JavaDoc serverInstanceID;
73     private File JavaDoc prjDirF;
74     
75     public ImportBlueprintEarWizardIteratorTest(String JavaDoc testName) {
76         super(testName);
77     }
78     
79     protected void setUp() throws Exception JavaDoc {
80         clearWorkDir();
81         serverInstanceID = TestUtil.registerSunAppServer(
82                 this, new Object JavaDoc[] { new SilentDialogDisplayer(), new SimplePlatformProvider() });
83         assertTrue("wrong dialog displayer", DialogDisplayer.getDefault() instanceof SilentDialogDisplayer);
84         // default project dir
85
prjDirF = new File JavaDoc(getWorkDir(), "testEA");
86     }
87     
88     public void testTestableInstantiateBasics() throws Exception JavaDoc {
89         generateJ2EEApplication(false);
90         File JavaDoc importedDir = new File JavaDoc(getWorkDir(), "testEA-imported");
91         ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel,
92                 j2eeLevel, importedDir, prjDirF, serverInstanceID, name,
93                 Collections.<FileObject, ModuleType>emptyMap(), null);
94         
95         FileObject fo = FileUtil.toFileObject(importedDir);
96         EarProject project = (EarProject) ProjectManager.getDefault().findProject(fo);
97         EditableProperties props = project.getAntProjectHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
98         assertEquals("j2ee.platform was set to 1.5", "1.5", props.getProperty("j2ee.platform")); // #76874
99
}
100     
101     public void testTestableInstantiateWitoutDD() throws Exception JavaDoc {
102         FileObject prjDirFO = generateJ2EEApplication(true);
103         
104         // and Enterprise Application's deployment descriptor
105
prjDirFO.getFileObject("src/conf/application.xml").delete();
106         
107         Map JavaDoc<FileObject, ModuleType> userModules = new HashMap JavaDoc<FileObject, ModuleType>();
108         userModules.put(prjDirFO.getFileObject(warName), ModuleType.WEB);
109         userModules.put(prjDirFO.getFileObject(jarName), ModuleType.EJB);
110         userModules.put(prjDirFO.getFileObject(carName), ModuleType.CLIENT);
111         File JavaDoc importedDir = new File JavaDoc(getWorkDir(), "testEA-imported");
112         ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel,
113                 j2eeLevel, importedDir, prjDirF, serverInstanceID, name, userModules, null);
114         
115         FileObject importedDirFO = FileUtil.toFileObject(importedDir);
116         FileObject ddFO = prjDirFO.getFileObject("src/conf/application.xml");
117         assertNotNull("deployment descriptor was created", ddFO);
118         EarProjectTest.validate(ddFO);
119         EarProject project = (EarProject) ProjectManager.getDefault().findProject(importedDirFO);
120         EarProjectTest.openProject(project);
121         Application app = DDProvider.getDefault().getDDRoot(ddFO);
122         assertSame("three modules", 3, app.getModule().length);
123     }
124     
125     public void testTestableInstantiateWithWebAndEJBAndAC() throws Exception JavaDoc {
126         this.j2eeLevel = "1.4";
127         FileObject prjDirFO = generateJ2EEApplication(true);
128         
129         File JavaDoc importedDir = new File JavaDoc(getWorkDir(), "testEA-imported");
130         ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel,
131                 j2eeLevel, importedDir, prjDirF, serverInstanceID, name,
132                 Collections.<FileObject, ModuleType>emptyMap(), null);
133         
134         assertNotNull("have a backup copy of application.xml", prjDirFO.getFileObject("src/conf/original_application.xml"));
135         assertNotNull("have a backup copy of manifest", prjDirFO.getFileObject("src/conf/original_MANIFEST.MF"));
136         FileObject importedDirFO = FileUtil.toFileObject(importedDir);
137         EarProject project = (EarProject) ProjectManager.getDefault().findProject(importedDirFO);
138         EarProjectTest.openProject(project);
139         
140         FileObject ddFO = project.getAppModule().getDeploymentDescriptor();
141         Application app = DDProvider.getDefault().getDDRoot(ddFO);
142         EarProjectTest.validate(ddFO);
143         assertSame("three modules", 3, app.getModule().length);
144         NewEarProjectWizardIteratorTest.doTestThatEJBWasAddedToWebAndAC( // #66546 and #74123
145
importedDirFO.getFileObject("testEA-war"),
146                 importedDirFO.getFileObject("testEA-app-client"));
147     }
148     
149     // temporarily(?) turned off
150
public void off_testWebContextRootIsSet() throws Exception JavaDoc {
151         this.j2eeLevel = "1.4";
152         generateJ2EEApplicationWithWeb();
153         
154         File JavaDoc importedDir = new File JavaDoc(getWorkDir(), "testEA-imported");
155         ImportBlueprintEarWizardIterator.testableInstantiate(platformName, sourceLevel,
156                 j2eeLevel, importedDir, prjDirF, serverInstanceID, name,
157                 Collections.<FileObject, ModuleType>emptyMap(), null);
158         
159         String JavaDoc importedContextRoot = null;
160         FileObject ddFO = FileUtil.toFileObject(prjDirF).getFileObject("src/conf/application.xml");
161         assertNotNull(ddFO);
162         EarProjectTest.validate(ddFO);
163         Application app = DDProvider.getDefault().getDDRoot(ddFO);
164         assertNotNull(app);
165         for (Module module : app.getModule()) {
166             Web web = module.getWeb();
167             if (web != null) {
168                 importedContextRoot = web.getContextRoot();
169                 break;
170             }
171         }
172         
173         assertNotNull("context-root set", importedContextRoot);
174         assertEquals("context-root successfully imported", CUSTOM_CONTEXT_ROOT, importedContextRoot);
175     }
176     
177     private FileObject generateJ2EEApplication() throws Exception JavaDoc {
178         // creates a project we will use for the import
179
NewEarProjectWizardIteratorTest.generateEARProject(
180                 prjDirF, name, j2eeLevel, serverInstanceID,
181                 warName, jarName, carName, mainClass, platformName, sourceLevel);
182         
183         // Workaround. Set the context root which should be set automatically.
184
// Do not know how to do it. Probably by getting somehow "Sun J2EE DD GUI"
185
// loader into the game.
186
FileObject ddFO = FileUtil.toFileObject(prjDirF).getFileObject("src/conf/application.xml");
187         Application app = DDProvider.getDefault().getDDRoot(ddFO);
188         for (Module module : app.getModule()) {
189             Web web = module.getWeb();
190             if (web != null) {
191                 web.setContextRoot("/my-context-root");
192                 app.write(ddFO);
193                 break;
194             }
195         }
196         
197         // clean-up NB specific metadata
198
FileObject prjDirFO = FileUtil.toFileObject(prjDirF);
199         prjDirFO.getFileObject("nbproject").delete();
200         if (warName != null) {
201             prjDirFO.getFileObject("testEA-war/nbproject").delete();
202         }
203         if (jarName != null) {
204             prjDirFO.getFileObject("testEA-ejb/nbproject").delete();
205         }
206         if (carName != null) {
207             prjDirFO.getFileObject("testEA-app-client/nbproject").delete();
208         }
209         return prjDirFO;
210     }
211     
212     private FileObject generateJ2EEApplication(boolean withSubModules) throws Exception JavaDoc {
213         if (!withSubModules) {
214             this.warName = null;
215             this.jarName = null;
216             this.carName = null;
217             this.mainClass = null;
218         }
219         return generateJ2EEApplication();
220     }
221     
222     private FileObject generateJ2EEApplicationWithWeb() throws Exception JavaDoc {
223         this.jarName = null;
224         this.carName = null;
225         this.mainClass = null;
226         return generateJ2EEApplication();
227     }
228     
229     // This could be probably removed as soon as #66988 is fixed since the
230
// dialog will not be displayed any more.
231
private static final class SilentDialogDisplayer extends DialogDisplayer {
232         
233         public Object JavaDoc notify(NotifyDescriptor descriptor) {
234             return null;
235         }
236         
237         public Dialog JavaDoc createDialog(DialogDescriptor descriptor) {
238             return null;
239         }
240         
241     }
242     
243     private static class SimplePlatformProvider implements JavaPlatformProvider {
244         
245         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
246         }
247         
248         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
249         }
250         
251         public JavaPlatform[] getInstalledPlatforms() {
252             return new JavaPlatform[] {
253                 getDefaultPlatform()
254             };
255         }
256         
257         public JavaPlatform getDefaultPlatform() {
258             return new TestDefaultPlatform();
259         }
260         
261     }
262     
263     private static class TestDefaultPlatform extends JavaPlatform {
264         
265         public FileObject findTool(String JavaDoc toolName) {
266             return null;
267         }
268         
269         public String JavaDoc getDisplayName() {
270             return "Default Platform";
271         }
272         
273         public ClassPath getBootstrapLibraries() {
274             return ClassPathSupport.createClassPath(new URL JavaDoc[0]);
275         }
276         
277         public Collection JavaDoc getInstallFolders() {
278             return null;
279         }
280         
281         public ClassPath getStandardLibraries() {
282             return null;
283         }
284         
285         public String JavaDoc getVendor() {
286             return null;
287         }
288         
289         public Specification getSpecification() {
290             return new Specification("j2se", new SpecificationVersion("1.5"));
291         }
292         
293         public ClassPath getSourceFolders() {
294             return null;
295         }
296         
297         public List JavaDoc getJavadocFolders() {
298             return null;
299         }
300         
301         public Map JavaDoc getProperties() {
302             return Collections.singletonMap("platform.ant.name","default_platform");
303         }
304         
305     }
306     
307 }
308
Popular Tags