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.spi.project.support.ant;
043:
044: import java.util.Collections;
045: import java.util.Properties;
046: import org.netbeans.api.project.Project;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.api.project.ProjectUtils;
049: import org.netbeans.api.project.TestUtil;
050: import org.netbeans.junit.NbTestCase;
051: import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
052: import org.netbeans.modules.project.ant.Util;
053: import org.netbeans.api.project.ProjectInformation;
054: import org.openide.filesystems.FileObject;
055: import org.openide.util.Lookup;
056: import org.openide.util.Mutex;
057: import org.openide.util.MutexException;
058: import org.openide.util.lookup.Lookups;
059: import org.w3c.dom.Document;
060: import org.w3c.dom.Element;
061: import org.w3c.dom.NodeList;
062:
063: /**
064: * Test functionality of ProjectGenerator.
065: * @author Jesse Glick
066: */
067: public class ProjectGeneratorTest extends NbTestCase {
068:
069: /**
070: * Create the test case.
071: * @param name the test name
072: */
073: public ProjectGeneratorTest(String name) {
074: super (name);
075: }
076:
077: private FileObject scratch;
078: private FileObject projdir;
079:
080: protected void setUp() throws Exception {
081: super .setUp();
082: scratch = TestUtil.makeScratchDir(this );
083: projdir = scratch.createFolder("proj");
084: TestUtil.setLookup(Lookups.fixed(new Object[] {
085: new AntBasedProjectFactorySingleton(),
086: AntBasedTestUtil.testAntBasedProjectType(), }));
087: }
088:
089: protected void tearDown() throws Exception {
090: scratch = null;
091: projdir = null;
092: TestUtil.setLookup(Lookup.EMPTY);
093: super .tearDown();
094: }
095:
096: /**
097: * Check that it is possible to create a complete new Ant-based project from scratch.
098: * @throws Exception if anything fails unexpectedly
099: */
100: public void testCreateNewProject() throws Exception {
101: try {
102: ProjectManager.mutex().writeAccess(
103: new Mutex.ExceptionAction<Void>() {
104: public Void run() throws Exception {
105: // Create the new project.
106: AntProjectHelper h = ProjectGenerator
107: .createProject(projdir, "test");
108: assertNotNull(
109: "Returned some project helper", h);
110: Project p = ProjectManager.getDefault()
111: .findProject(projdir);
112: assertNotNull("Project exists", p);
113: // Check that basic characteristics are correct.
114: ProjectInformation pi = ProjectUtils
115: .getInformation(p);
116: assertEquals("correct directory", projdir,
117: p.getProjectDirectory());
118: assertTrue("already modified",
119: ProjectManager.getDefault()
120: .isModified(p));
121: // Configure it.
122: Element data = h
123: .getPrimaryConfigurationData(true);
124: assertEquals(
125: "correct namespace for shared data",
126: "urn:test:shared", data
127: .getNamespaceURI());
128: assertEquals("empty initial shared data",
129: 0, data.getChildNodes().getLength());
130: Element stuff = data.getOwnerDocument()
131: .createElementNS("urn:test:shared",
132: "shared-stuff");
133: data.appendChild(stuff);
134: h.putPrimaryConfigurationData(data, true);
135: data = h.getPrimaryConfigurationData(false);
136: assertEquals(
137: "correct namespace for private data",
138: "urn:test:private", data
139: .getNamespaceURI());
140: assertEquals("empty initial private data",
141: 0, data.getChildNodes().getLength());
142: stuff = data.getOwnerDocument()
143: .createElementNS(
144: "urn:test:private",
145: "private-stuff");
146: data.appendChild(stuff);
147: h.putPrimaryConfigurationData(data, false);
148: EditableProperties ep = h
149: .getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
150: assertEquals(
151: "empty initial project.properties",
152: 0, ep.size());
153: ep.setProperty("shared.prop", "val1");
154: h
155: .putProperties(
156: AntProjectHelper.PROJECT_PROPERTIES_PATH,
157: ep);
158: ep = h
159: .getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
160: assertEquals(
161: "empty initial private.properties",
162: 0, ep.size());
163: ep.setProperty("private.prop", "val2");
164: h
165: .putProperties(
166: AntProjectHelper.PRIVATE_PROPERTIES_PATH,
167: ep);
168: // Save it.
169: ProjectManager.getDefault().saveProject(p);
170: // Check that everything is OK on disk.
171: Document doc = AntBasedTestUtil.slurpXml(h,
172: AntProjectHelper.PROJECT_XML_PATH);
173: NodeList l = doc
174: .getElementsByTagNameNS(
175: AntProjectHelper.PROJECT_NS,
176: "type");
177: assertEquals("one <type>", 1, l.getLength());
178: Element el = (Element) l.item(0);
179: assertEquals("correct saved type", "test",
180: Util.findText(el));
181: l = doc.getElementsByTagNameNS(
182: "urn:test:shared", "shared-stuff");
183: assertEquals("one <shared-stuff>", 1, l
184: .getLength());
185: doc = AntBasedTestUtil.slurpXml(h,
186: AntProjectHelper.PRIVATE_XML_PATH);
187: l = doc
188: .getElementsByTagNameNS(
189: "urn:test:private",
190: "private-stuff");
191: assertEquals("one <private-stuff>", 1, l
192: .getLength());
193: Properties props = AntBasedTestUtil
194: .slurpProperties(
195: h,
196: AntProjectHelper.PROJECT_PROPERTIES_PATH);
197: assertEquals("correct project.properties",
198: Collections.singletonMap(
199: "shared.prop", "val1"),
200: props);
201: props = AntBasedTestUtil
202: .slurpProperties(
203: h,
204: AntProjectHelper.PRIVATE_PROPERTIES_PATH);
205: assertEquals("correct project.properties",
206: Collections.singletonMap(
207: "private.prop", "val2"),
208: props);
209: doc = AntBasedTestUtil.slurpXml(h,
210: "nbproject/build-impl.xml");
211: el = doc.getDocumentElement();
212: assertEquals(
213: "build-impl.xml is a <project>",
214: "project", el.getLocalName());
215: assertEquals("<project> has no namespace",
216: null, el.getNamespaceURI());
217: l = doc.getElementsByTagName("target");
218: assertEquals(
219: "two targets in build-impl.xml", 2,
220: l.getLength());
221: el = (Element) l.item(1);
222: assertEquals("second target is \"x\"", "x",
223: el.getAttribute("name"));
224: new GeneratedFilesHelper(h)
225: .generateBuildScriptFromStylesheet(
226: GeneratedFilesHelper.BUILD_XML_PATH,
227: AntBasedTestUtil
228: .testBuildXmlStylesheet());
229: doc = AntBasedTestUtil.slurpXml(h,
230: "build.xml");
231: el = doc.getDocumentElement();
232: assertEquals("build.xml is a <project>",
233: "project", el.getLocalName());
234: assertEquals("<project> has no namespace",
235: null, el.getNamespaceURI());
236: l = doc.getElementsByTagName("target");
237: assertEquals("one target in build.xml", 1,
238: l.getLength());
239: el = (Element) l.item(0);
240: assertEquals("target is \"all\"", "all", el
241: .getAttribute("name"));
242: return null;
243: }
244: });
245: } catch (MutexException e) {
246: throw e.getException();
247: }
248: }
249:
250: }
|