KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > EarProjectGeneratorTest


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;
21
22 import java.io.File JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import org.netbeans.api.project.ProjectManager;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
29 import org.netbeans.modules.j2ee.earproject.test.TestUtil;
30 import org.netbeans.spi.project.support.ant.AntProjectHelper;
31 import org.netbeans.spi.project.support.ant.EditableProperties;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Mutex;
35 import org.openide.xml.XMLUtil;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39
40 /**
41  * @author vkraemer
42  */

43 public class EarProjectGeneratorTest extends NbTestCase {
44     
45     private String JavaDoc serverID;
46     
47     private static final String JavaDoc[] CREATED_FILES = {
48         "build.xml",
49         "nbproject/build-impl.xml",
50         "nbproject/genfiles.properties",
51         "nbproject/project.xml",
52         "nbproject/project.properties",
53         "nbproject/private/private.properties",
54         "src/conf/application.xml"
55     };
56     
57     private static final String JavaDoc[] CREATED_FILES_EXT_SOURCES = {
58         "build.xml",
59         "nbproject/build-impl.xml",
60         "nbproject/genfiles.properties",
61         "nbproject/project.xml",
62         "nbproject/project.properties",
63         "nbproject/private/private.properties",
64     };
65     
66     private static final String JavaDoc[] CREATED_PROPERTIES = {
67         "build.classes.excludes",
68         "build.dir",
69         "build.generated.dir",
70         "client.module.uri",
71         "client.urlPart",
72         "debug.classpath",
73         "display.browser",
74         "dist.dir",
75         "dist.jar",
76         "j2ee.appclient.mainclass.args",
77         "j2ee.platform",
78         "j2ee.server.type",
79         "jar.compress",
80         "jar.content.additional",
81         "jar.name",
82         "javac.debug",
83         "javac.deprecation",
84         "javac.source",
85         "javac.target",
86         "meta.inf",
87         "no.dependencies",
88         "platform.active",
89         "resource.dir",
90         "source.root",
91     };
92     
93     private static final String JavaDoc[] CREATED_PROPERTIES_EXT_SOURCES = {
94         "build.classes.excludes",
95         "build.dir",
96         "build.generated.dir",
97         "client.module.uri",
98         "client.urlPart",
99         "debug.classpath",
100         "display.browser",
101         "dist.dir",
102         "dist.jar",
103         "j2ee.appclient.mainclass.args",
104         "j2ee.platform",
105         "j2ee.server.type",
106         "jar.compress",
107         "jar.content.additional",
108         "jar.name",
109         "javac.debug",
110         "javac.deprecation",
111         "javac.source",
112         "javac.target",
113         "meta.inf",
114         "no.dependencies",
115         "platform.active",
116         //"resource.dir", -XXX- this is not found in project.props
117
// when the project is created from ex. sources. Bug or not???
118
"source.root",
119     };
120     
121     public EarProjectGeneratorTest(String JavaDoc name) {
122         super(name);
123     }
124     
125     protected void setUp() throws Exception JavaDoc {
126         super.setUp();
127         TestUtil.makeScratchDir(this);
128         serverID = TestUtil.registerSunAppServer(this);
129     }
130     
131     public void testCreateProject() throws Exception JavaDoc {
132         File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "EARProject");
133         AntProjectHelper aph = EarProjectGenerator.createProject(prjDirF, "test-project",
134                 J2eeModule.JAVA_EE_5, serverID, "1.5");
135         assertNotNull(aph);
136         FileObject prjDirFO = aph.getProjectDirectory();
137         for (String JavaDoc file : CREATED_FILES) {
138             assertNotNull(file + " file/folder cannot be found", prjDirFO.getFileObject(file));
139         }
140         EditableProperties props = aph.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
141         @SuppressWarnings JavaDoc("unchecked")
142         List JavaDoc createdProperties = new ArrayList JavaDoc(props.keySet());
143         for (String JavaDoc property : CREATED_PROPERTIES) {
144             assertNotNull(property + " property cannot be found in project.properties", props.getProperty(property));
145             createdProperties.remove(property);
146         }
147         assertEquals("Found unexpected property: " + createdProperties,
148                 CREATED_PROPERTIES.length, props.keySet().size());
149     }
150     
151     public void testImportProject() throws Exception JavaDoc {
152         File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "EARProject");
153         AntProjectHelper helper = EarProjectGenerator.importProject(prjDirF, prjDirF,
154                 "test-project-ext-src", J2eeModule.JAVA_EE_5, serverID, null,
155                 "1.5", Collections.<FileObject, ModuleType>emptyMap());
156         assertNotNull(helper);
157         FileObject prjDirFO = FileUtil.toFileObject(prjDirF);
158         for (String JavaDoc createdFile : CREATED_FILES_EXT_SOURCES) {
159             assertNotNull(createdFile + " file/folder cannot be found", prjDirFO.getFileObject(createdFile));
160         }
161         EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
162         @SuppressWarnings JavaDoc("unchecked")
163         List JavaDoc createdProperties = new ArrayList JavaDoc(props.keySet());
164         int extFileRefCount = 0;
165         for (String JavaDoc propName : CREATED_PROPERTIES_EXT_SOURCES) {
166             String JavaDoc propValue = props.getProperty(propName);
167             assertNotNull(propName+" property cannot be found in project.properties", propValue);
168             createdProperties.remove(propName);
169             if ("manifest.file".equals(propName)) {
170                 assertEquals("Invalid value of manifest.file property.", "manifest.mf", propValue);
171             }
172         }
173         assertEquals("Found unexpected property: " + createdProperties,
174                 CREATED_PROPERTIES_EXT_SOURCES.length, props.keySet().size() - extFileRefCount);
175     }
176     
177     public void testProjectNameIsSet() throws Exception JavaDoc { // #73930
178
File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "EARProject");
179         EarProjectGenerator.createProject(prjDirF, "test-project",
180                 J2eeModule.JAVA_EE_5, serverID, "1.5");
181         // test also build
182
final File JavaDoc buildXML = new File JavaDoc(prjDirF, "build.xml");
183         String JavaDoc projectName = (String JavaDoc) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() {
184             public Object JavaDoc run() throws Exception JavaDoc {
185                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(buildXML.toURI().toString()),
186                         false, true, null, null);
187                 Element JavaDoc project = doc.getDocumentElement();
188                 return project.getAttribute("name");
189             }
190         });
191         assertEquals("project name is set in the build.xml", "test-project", projectName);
192     }
193     
194     public void testProjectNameIsEscaped() throws Exception JavaDoc {
195         final File JavaDoc prjDirF = new File JavaDoc(getWorkDir(), "EARProject");
196         EarProjectGenerator.createProject(prjDirF, "test project",
197                 J2eeModule.JAVA_EE_5, serverID, "1.5");
198         // test build.xml
199
String JavaDoc buildXmlProjectName = (String JavaDoc) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() {
200             public Object JavaDoc run() throws Exception JavaDoc {
201                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(new File JavaDoc(prjDirF, "build.xml").toURI().toString()),
202                         false, true, null, null);
203                 Element JavaDoc project = doc.getDocumentElement();
204                 return project.getAttribute("name");
205             }
206         });
207         assertEquals("project name is escaped in build.xml", "test_project", buildXmlProjectName);
208         // test build-impl.xml
209
String JavaDoc buildImplXmlProjectName = (String JavaDoc) ProjectManager.mutex().readAccess(new Mutex.ExceptionAction() {
210             public Object JavaDoc run() throws Exception JavaDoc {
211                 Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(new File JavaDoc(prjDirF, "nbproject/build-impl.xml").toURI().toString()),
212                         false, true, null, null);
213                 Element JavaDoc project = doc.getDocumentElement();
214                 return project.getAttribute("name");
215             }
216         });
217         assertEquals("project name is escaped in build-impl.xml", "test_project-impl", buildImplXmlProjectName);
218     }
219     
220 }
221
Popular Tags