KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.api.java.platform.JavaPlatformManager;
30 import org.netbeans.api.project.Project;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.j2ee.earproject.EarProject;
34 import org.netbeans.modules.j2ee.earproject.EarProjectTest;
35 import org.netbeans.modules.j2ee.earproject.TestPlatformProvider;
36 import org.netbeans.modules.j2ee.earproject.test.TestUtil;
37 import org.netbeans.modules.j2ee.earproject.ui.customizer.EarProjectProperties;
38 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
39 import org.netbeans.spi.project.SubprojectProvider;
40 import org.netbeans.spi.project.support.ant.EditableProperties;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.filesystems.Repository;
44
45 /**
46  * @author Martin Krauskopf
47  */

48 public class NewEarProjectWizardIteratorTest extends NbTestCase {
49     
50     private static final String JavaDoc DEFAULT_PLATFORM_ROOT = "1.5";
51     
52     private String JavaDoc serverInstanceID;
53     
54     /**
55      * Generates a project in the same (or very similar) manner as would be
56      * generated by the user using <em>New Enterprise Application</em> wizard.
57      * May be used for generating project instances in tests.
58      */

59     public static void generateEARProject(
60             File JavaDoc prjDirF, String JavaDoc name, String JavaDoc j2eeLevel,
61             String JavaDoc serverInstanceID, String JavaDoc warName,
62             String JavaDoc jarName, String JavaDoc carName, String JavaDoc mainClass,
63             String JavaDoc platformName, String JavaDoc sourceLevel) throws IOException JavaDoc {
64         NewEarProjectWizardIterator.testableInstantiate(prjDirF, name,
65                 j2eeLevel, serverInstanceID, warName, jarName,
66                 carName, mainClass, platformName, sourceLevel, null);
67     }
68     
69     /**
70      * Generates an empty Enterprise Application project, i.e. without
71      * submodules.
72      *
73      * @see #generateEARProject(File, String, String, String, String, String, String, String, String, String)
74      */

75     public static void generateEARProject(File JavaDoc earDirF, String JavaDoc name,
76             String JavaDoc j2eeLevel, String JavaDoc serverID) throws IOException JavaDoc {
77         generateEARProject(earDirF, name, j2eeLevel, serverID, null, null, null,
78                 null, null, null);
79     }
80     
81     public NewEarProjectWizardIteratorTest(String JavaDoc testName) {
82         super(testName);
83     }
84     
85     protected void setUp() throws Exception JavaDoc {
86         clearWorkDir();
87         FileObject scratch = TestUtil.makeScratchDir(this);
88         FileObject defaultPlatformBootRoot = scratch.createFolder(DEFAULT_PLATFORM_ROOT);
89         ClassPath defBCP = ClassPathSupport.createClassPath(new URL JavaDoc[] { defaultPlatformBootRoot.getURL() });
90         
91         serverInstanceID = TestUtil.registerSunAppServer(
92                 this, new Object JavaDoc[] { new TestPlatformProvider(defBCP, defBCP) });
93         
94         assertEquals("No Java platforms found.", 2, JavaPlatformManager.getDefault().getInstalledPlatforms().length);
95     }
96     
97     public void testTestableInstantiate() throws Exception JavaDoc {
98         File JavaDoc dirF = new File JavaDoc(getWorkDir(), "testEA");
99         String JavaDoc name = "Test EnterpriseApplication";
100         String JavaDoc j2eeLevel = "1.4";
101         String JavaDoc warName = null;
102         String JavaDoc jarName = null;
103         String JavaDoc carName = null;
104         String JavaDoc mainClass = null;
105         String JavaDoc platformName = null;
106         String JavaDoc sourceLevel = null;
107         
108         Set JavaDoc result = NewEarProjectWizardIterator.testableInstantiate(dirF, name,
109                 j2eeLevel, serverInstanceID, warName, jarName,
110                 carName, mainClass, platformName, sourceLevel, null);
111         
112         Set JavaDoc<FileObject> expResult = new HashSet JavaDoc<FileObject>();
113         FileObject testEAFO = FileUtil.toFileObject(new File JavaDoc(getWorkDir(), "testEA"));
114         assertNotNull("testEA directory", testEAFO);
115         expResult = Collections.singleton(testEAFO);
116         assertEquals(expResult, result);
117         
118         EditableProperties ep = TestUtil.loadProjectProperties(testEAFO);
119         assertNull("app.client is not set", ep.getProperty(EarProjectProperties.APPLICATION_CLIENT));
120         assertEquals("client.module.uri is empty", "", ep.getProperty(EarProjectProperties.CLIENT_MODULE_URI));
121     }
122     
123     public void testTestableInstantiateWithAppClient() throws Exception JavaDoc {
124         String JavaDoc resource = "org-netbeans-modules-j2ee-clientproject/application-client-5.xml";
125         assertNotNull("application client registered", Repository.getDefault().getDefaultFileSystem().findResource(resource));
126         File JavaDoc dirF = new File JavaDoc(getWorkDir(), "testEA");
127         String JavaDoc name = "Test EnterpriseApplication";
128         String JavaDoc j2eeLevel = "1.4";
129         String JavaDoc warName = null;
130         String JavaDoc jarName = null;
131         String JavaDoc carName = "testEA-app-client";
132         String JavaDoc mainClass = "testEA.app.client.Main";
133         String JavaDoc platformName = null;
134         String JavaDoc sourceLevel = null;
135         
136         Set JavaDoc result = NewEarProjectWizardIterator.testableInstantiate(dirF, name,
137                 j2eeLevel, serverInstanceID, warName, jarName,
138                 carName, mainClass, platformName, sourceLevel, null);
139         
140         Set JavaDoc<FileObject> expResult = new HashSet JavaDoc<FileObject>();
141         File JavaDoc testEA = new File JavaDoc(getWorkDir(), "testEA");
142         FileObject testEAFO = FileUtil.toFileObject(testEA);
143         assertNotNull("testEA directory", testEAFO);
144         FileObject testEAClientFO = FileUtil.toFileObject(new File JavaDoc(testEA, "testEA-app-client"));
145         assertNotNull("testEA-app-client directory", testEAClientFO);
146         expResult.add(testEAFO);
147         expResult.add(testEAClientFO);
148         assertEquals(expResult, result);
149         
150         EditableProperties ep = TestUtil.loadProjectProperties(testEAFO);
151         assertEquals("app.client set", "testEA-app-client", ep.getProperty(EarProjectProperties.APPLICATION_CLIENT));
152         assertEquals("client.module.uri is set to app. client", "Test EnterpriseApplication/${app.client}", ep.getProperty(EarProjectProperties.CLIENT_MODULE_URI));
153     }
154     
155     public void testTestableInstantiateWithWebAndEJBAppClient() throws Exception JavaDoc {
156         String JavaDoc resource = "org-netbeans-modules-j2ee-clientproject/application-client-5.xml";
157         assertNotNull("application client registered", Repository.getDefault().getDefaultFileSystem().findResource(resource));
158         File JavaDoc dirF = new File JavaDoc(getWorkDir(), "testEA");
159         String JavaDoc name = "Test EnterpriseApplication";
160         String JavaDoc j2eeLevel = "1.4";
161         String JavaDoc warName = "testEA-war";
162         String JavaDoc jarName = "testEA-ejb";
163         String JavaDoc carName = "testEA-app-client";
164         String JavaDoc mainClass = "testEA.app.client.Main";
165         String JavaDoc platformName = null;
166         String JavaDoc sourceLevel = null;
167         
168         Set JavaDoc result = NewEarProjectWizardIterator.testableInstantiate(dirF, name,
169                 j2eeLevel, serverInstanceID, warName, jarName,
170                 carName, mainClass, platformName, sourceLevel, null);
171         
172         Set JavaDoc<FileObject> expResult = new HashSet JavaDoc<FileObject>();
173         File JavaDoc testEA = new File JavaDoc(getWorkDir(), "testEA");
174         FileObject testEAFO = FileUtil.toFileObject(testEA);
175         assertNotNull("testEA directory", testEAFO);
176         FileObject testEAEjbFO = FileUtil.toFileObject(new File JavaDoc(testEA, "testEA-ejb"));
177         assertNotNull("testEA-ejb directory", testEAEjbFO);
178         FileObject testEAClientFO = FileUtil.toFileObject(new File JavaDoc(testEA, "testEA-app-client"));
179         assertNotNull("testEA-app-client directory", testEAClientFO);
180         FileObject testEAWebFO = FileUtil.toFileObject(new File JavaDoc(testEA, "testEA-war"));
181         assertNotNull("testEA-war directory", testEAWebFO);
182         
183         expResult.add(testEAFO);
184         expResult.add(testEAEjbFO);
185         expResult.add(testEAClientFO);
186         expResult.add(testEAWebFO);
187         assertEquals(expResult, result);
188         
189         EditableProperties ep = TestUtil.loadProjectProperties(testEAFO);
190         assertNull("app.client not set", ep.getProperty(EarProjectProperties.APPLICATION_CLIENT));
191         assertEquals("client.module.uri is set to war", "testEA-war.war", ep.getProperty(EarProjectProperties.CLIENT_MODULE_URI));
192         EarProjectTest.openProject((EarProject) ProjectManager.getDefault().findProject(testEAFO));
193         
194         doTestThatEJBWasAddedToWebAndAC(testEAWebFO, testEAClientFO); // #74123
195
}
196     
197     static void doTestThatEJBWasAddedToWebAndAC( // #66546 and #74123
198
final FileObject testEAWebFO, final FileObject testEAClientFO) throws IOException JavaDoc {
199         
200         Project testEAClientProject = ProjectManager.getDefault().findProject(testEAClientFO);
201         SubprojectProvider acSubProjects = (SubprojectProvider)
202                 testEAClientProject.getLookup().lookup(SubprojectProvider.class);
203         assertNotNull("application client has SubprojectProvider in its lookup", acSubProjects);
204         assertEquals("ejb added to application client's", 1, acSubProjects.getSubprojects().size());
205         
206         Project testEAWebProject = ProjectManager.getDefault().findProject(testEAWebFO);
207         SubprojectProvider webSubProjects = (SubprojectProvider)
208                 testEAWebProject.getLookup().lookup(SubprojectProvider.class);
209         assertNotNull("web project has SubprojectProvider in its lookup", webSubProjects);
210         assertEquals("ejb added to web project's", 1, webSubProjects.getSubprojects().size());
211     }
212     
213 }
214
Popular Tags