001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.j2ee.earproject;
043:
044: import java.io.File;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.List;
048: import org.netbeans.api.project.ProjectManager;
049: import org.netbeans.junit.NbTestCase;
050: import org.netbeans.modules.j2ee.deployment.devmodules.api.J2eeModule;
051: import org.netbeans.modules.j2ee.earproject.test.TestUtil;
052: import org.netbeans.spi.project.support.ant.AntProjectHelper;
053: import org.netbeans.spi.project.support.ant.EditableProperties;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.Mutex;
057: import org.openide.xml.XMLUtil;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.Element;
060: import org.xml.sax.InputSource;
061:
062: /**
063: * @author vkraemer
064: */
065: public class EarProjectGeneratorTest extends NbTestCase {
066:
067: private String serverID;
068:
069: private static final String[] CREATED_FILES = { "build.xml",
070: "nbproject/build-impl.xml",
071: "nbproject/genfiles.properties", "nbproject/project.xml",
072: "nbproject/project.properties",
073: "nbproject/private/private.properties",
074: "src/conf/application.xml" };
075:
076: private static final String[] CREATED_FILES_EXT_SOURCES = {
077: "build.xml", "nbproject/build-impl.xml",
078: "nbproject/genfiles.properties", "nbproject/project.xml",
079: "nbproject/project.properties",
080: "nbproject/private/private.properties", };
081:
082: private static final String[] CREATED_PROPERTIES = {
083: "build.classes.excludes", "build.dir",
084: "build.generated.dir", "client.module.uri",
085: "client.urlPart", "debug.classpath", "display.browser",
086: "dist.dir", "dist.jar", "j2ee.appclient.mainclass.args",
087: "j2ee.platform", "j2ee.server.type", "jar.compress",
088: "jar.content.additional", "jar.name", "javac.debug",
089: "javac.deprecation", "javac.source", "javac.target",
090: "meta.inf", "no.dependencies", "platform.active",
091: "resource.dir", "source.root", };
092:
093: private static final String[] CREATED_PROPERTIES_EXT_SOURCES = {
094: "build.classes.excludes", "build.dir",
095: "build.generated.dir", "client.module.uri",
096: "client.urlPart", "debug.classpath", "display.browser",
097: "dist.dir", "dist.jar", "j2ee.appclient.mainclass.args",
098: "j2ee.platform", "j2ee.server.type", "jar.compress",
099: "jar.content.additional", "jar.name", "javac.debug",
100: "javac.deprecation", "javac.source", "javac.target",
101: "meta.inf", "no.dependencies", "platform.active",
102: //"resource.dir", -XXX- this is not found in project.props
103: // when the project is created from ex. sources. Bug or not???
104: "source.root", };
105:
106: public EarProjectGeneratorTest(String name) {
107: super (name);
108: }
109:
110: protected void setUp() throws Exception {
111: super .setUp();
112: TestUtil.makeScratchDir(this );
113: serverID = TestUtil.registerSunAppServer(this );
114: }
115:
116: public void testCreateProjectJavaEE5() throws Exception {
117:
118: // #102486 - test broken for long time; commenting out
119: if (true)
120: return;
121:
122: File prjDirF = new File(getWorkDir(), "EARProject");
123: AntProjectHelper aph = EarProjectGenerator.createProject(
124: prjDirF, "test-project", J2eeModule.JAVA_EE_5,
125: serverID, "1.5", null, null);
126: assertNotNull(aph);
127: FileObject prjDirFO = aph.getProjectDirectory();
128: for (String file : CREATED_FILES) {
129: FileObject fo = prjDirFO.getFileObject(file);
130: if ("src/conf/application.xml".equals(file)) {
131: // deployment descriptor should not exist
132: assertNull(file + " file/folder should not exist", fo);
133: } else {
134: assertNotNull(file + " file/folder should exist", fo);
135: }
136: }
137: EditableProperties props = aph
138: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
139: @SuppressWarnings("unchecked")
140: List createdProperties = new ArrayList(props.keySet());
141: for (String property : CREATED_PROPERTIES) {
142: assertNotNull(
143: property
144: + " property cannot be found in project.properties",
145: props.getProperty(property));
146: createdProperties.remove(property);
147: }
148: assertEquals("Found unexpected property: " + createdProperties,
149: CREATED_PROPERTIES.length, props.keySet().size());
150: }
151:
152: public void testCreateProjectJ2EE14() throws Exception {
153:
154: // #102486 - test broken for long time; commenting out
155: if (true)
156: return;
157:
158: File prjDirF = new File(getWorkDir(), "EARProject");
159: AntProjectHelper aph = EarProjectGenerator.createProject(
160: prjDirF, "test-project", J2eeModule.J2EE_14, serverID,
161: "1.4", null, null);
162: assertNotNull(aph);
163: FileObject prjDirFO = aph.getProjectDirectory();
164: for (String file : CREATED_FILES) {
165: assertNotNull(file + " file/folder cannot be found",
166: prjDirFO.getFileObject(file));
167: }
168: EditableProperties props = aph
169: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
170: @SuppressWarnings("unchecked")
171: List createdProperties = new ArrayList(props.keySet());
172: for (String property : CREATED_PROPERTIES) {
173: assertNotNull(
174: property
175: + " property cannot be found in project.properties",
176: props.getProperty(property));
177: createdProperties.remove(property);
178: }
179: assertEquals("Found unexpected property: " + createdProperties,
180: CREATED_PROPERTIES.length, props.keySet().size());
181: }
182:
183: public void testImportProject() throws Exception {
184:
185: // #102486 - test broken for long time; commenting out
186: if (true)
187: return;
188:
189: File prjDirF = new File(getWorkDir(), "EARProject");
190: AntProjectHelper helper = EarProjectGenerator.importProject(
191: prjDirF, prjDirF, "test-project-ext-src",
192: J2eeModule.JAVA_EE_5, serverID, null, "1.5",
193: Collections.<FileObject, ModuleType> emptyMap(), null,
194: null);
195: assertNotNull(helper);
196: FileObject prjDirFO = FileUtil.toFileObject(prjDirF);
197: for (String createdFile : CREATED_FILES_EXT_SOURCES) {
198: assertNotNull(createdFile + " file/folder cannot be found",
199: prjDirFO.getFileObject(createdFile));
200: }
201: EditableProperties props = helper
202: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
203: @SuppressWarnings("unchecked")
204: List createdProperties = new ArrayList(props.keySet());
205: int extFileRefCount = 0;
206: for (String propName : CREATED_PROPERTIES_EXT_SOURCES) {
207: String propValue = props.getProperty(propName);
208: assertNotNull(
209: propName
210: + " property cannot be found in project.properties",
211: propValue);
212: createdProperties.remove(propName);
213: if ("manifest.file".equals(propName)) {
214: assertEquals(
215: "Invalid value of manifest.file property.",
216: "manifest.mf", propValue);
217: }
218: }
219: assertEquals("Found unexpected property: " + createdProperties,
220: CREATED_PROPERTIES_EXT_SOURCES.length, props.keySet()
221: .size()
222: - extFileRefCount);
223: }
224:
225: public void testProjectNameIsSet() throws Exception { // #73930
226: File prjDirF = new File(getWorkDir(), "EARProject");
227: EarProjectGenerator.createProject(prjDirF, "test-project",
228: J2eeModule.JAVA_EE_5, serverID, "1.5", null, null);
229: // test also build
230: final File buildXML = new File(prjDirF, "build.xml");
231: String projectName = ProjectManager.mutex().readAccess(
232: new Mutex.ExceptionAction<String>() {
233: public String run() throws Exception {
234: Document doc = XMLUtil.parse(new InputSource(
235: buildXML.toURI().toString()), false,
236: true, null, null);
237: Element project = doc.getDocumentElement();
238: return project.getAttribute("name");
239: }
240: });
241: assertEquals("project name is set in the build.xml",
242: "test-project", projectName);
243: }
244:
245: public void testProjectNameIsEscaped() throws Exception {
246: final File prjDirF = new File(getWorkDir(), "EARProject");
247: EarProjectGenerator.createProject(prjDirF, "test project",
248: J2eeModule.JAVA_EE_5, serverID, "1.5", null, null);
249: // test build.xml
250: String buildXmlProjectName = ProjectManager.mutex().readAccess(
251: new Mutex.ExceptionAction<String>() {
252: public String run() throws Exception {
253: Document doc = XMLUtil.parse(new InputSource(
254: new File(prjDirF, "build.xml").toURI()
255: .toString()), false, true,
256: null, null);
257: Element project = doc.getDocumentElement();
258: return project.getAttribute("name");
259: }
260: });
261: assertEquals("project name is escaped in build.xml",
262: "test_project", buildXmlProjectName);
263: // test build-impl.xml
264: String buildImplXmlProjectName = ProjectManager.mutex()
265: .readAccess(new Mutex.ExceptionAction<String>() {
266: public String run() throws Exception {
267: Document doc = XMLUtil.parse(new InputSource(
268: new File(prjDirF,
269: "nbproject/build-impl.xml")
270: .toURI().toString()), false,
271: true, null, null);
272: Element project = doc.getDocumentElement();
273: return project.getAttribute("name");
274: }
275: });
276: assertEquals("project name is escaped in build-impl.xml",
277: "test_project-impl", buildImplXmlProjectName);
278: }
279:
280: }
|