KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > ProjectGeneratorTest


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.spi.project.support.ant;
21
22 import java.util.Collections JavaDoc;
23 import java.util.Properties JavaDoc;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.api.project.ProjectManager;
26 import org.netbeans.api.project.ProjectUtils;
27 import org.netbeans.api.project.TestUtil;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
30 import org.netbeans.modules.project.ant.Util;
31 import org.netbeans.api.project.ProjectInformation;
32 import org.openide.filesystems.FileObject;
33 import org.openide.util.Lookup;
34 import org.openide.util.Mutex;
35 import org.openide.util.MutexException;
36 import org.openide.util.lookup.Lookups;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.NodeList JavaDoc;
40
41 /**
42  * Test functionality of ProjectGenerator.
43  * @author Jesse Glick
44  */

45 public class ProjectGeneratorTest extends NbTestCase {
46     
47     /**
48      * Create the test case.
49      * @param name the test name
50      */

51     public ProjectGeneratorTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     private FileObject scratch;
56     private FileObject projdir;
57     
58     protected void setUp() throws Exception JavaDoc {
59         super.setUp();
60         scratch = TestUtil.makeScratchDir(this);
61         projdir = scratch.createFolder("proj");
62         TestUtil.setLookup(Lookups.fixed(new Object JavaDoc[] {
63             new AntBasedProjectFactorySingleton(),
64             AntBasedTestUtil.testAntBasedProjectType(),
65         }));
66     }
67     
68     protected void tearDown() throws Exception JavaDoc {
69         scratch = null;
70         projdir = null;
71         TestUtil.setLookup(Lookup.EMPTY);
72         super.tearDown();
73     }
74     
75     /**
76      * Check that it is possible to create a complete new Ant-based project from scratch.
77      * @throws Exception if anything fails unexpectedly
78      */

79     public void testCreateNewProject() throws Exception JavaDoc {
80         try {
81             ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
82                 public Void JavaDoc run() throws Exception JavaDoc {
83                     // Create the new project.
84
AntProjectHelper h = ProjectGenerator.createProject(projdir, "test");
85                     assertNotNull("Returned some project helper", h);
86                     Project p = ProjectManager.getDefault().findProject(projdir);
87                     assertNotNull("Project exists", p);
88                     // Check that basic characteristics are correct.
89
ProjectInformation pi = ProjectUtils.getInformation(p);
90                     assertEquals("correct directory", projdir, p.getProjectDirectory());
91                     assertTrue("already modified", ProjectManager.getDefault().isModified(p));
92                     // Configure it.
93
Element JavaDoc data = h.getPrimaryConfigurationData(true);
94                     assertEquals("correct namespace for shared data", "urn:test:shared", data.getNamespaceURI());
95                     assertEquals("empty initial shared data", 0, data.getChildNodes().getLength());
96                     Element JavaDoc stuff = data.getOwnerDocument().createElementNS("urn:test:shared", "shared-stuff");
97                     data.appendChild(stuff);
98                     h.putPrimaryConfigurationData(data, true);
99                     data = h.getPrimaryConfigurationData(false);
100                     assertEquals("correct namespace for private data", "urn:test:private", data.getNamespaceURI());
101                     assertEquals("empty initial private data", 0, data.getChildNodes().getLength());
102                     stuff = data.getOwnerDocument().createElementNS("urn:test:private", "private-stuff");
103                     data.appendChild(stuff);
104                     h.putPrimaryConfigurationData(data, false);
105                     EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
106                     assertEquals("empty initial project.properties", 0, ep.size());
107                     ep.setProperty("shared.prop", "val1");
108                     h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
109                     ep = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
110                     assertEquals("empty initial private.properties", 0, ep.size());
111                     ep.setProperty("private.prop", "val2");
112                     h.putProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH, ep);
113                     // Save it.
114
ProjectManager.getDefault().saveProject(p);
115                     // Check that everything is OK on disk.
116
Document JavaDoc doc = AntBasedTestUtil.slurpXml(h, AntProjectHelper.PROJECT_XML_PATH);
117                     NodeList JavaDoc l = doc.getElementsByTagNameNS(AntProjectHelper.PROJECT_NS, "type");
118                     assertEquals("one <type>", 1, l.getLength());
119                     Element JavaDoc el = (Element JavaDoc)l.item(0);
120                     assertEquals("correct saved type", "test", Util.findText(el));
121                     l = doc.getElementsByTagNameNS("urn:test:shared", "shared-stuff");
122                     assertEquals("one <shared-stuff>", 1, l.getLength());
123                     doc = AntBasedTestUtil.slurpXml(h, AntProjectHelper.PRIVATE_XML_PATH);
124                     l = doc.getElementsByTagNameNS("urn:test:private", "private-stuff");
125                     assertEquals("one <private-stuff>", 1, l.getLength());
126                     Properties JavaDoc props = AntBasedTestUtil.slurpProperties(h, AntProjectHelper.PROJECT_PROPERTIES_PATH);
127                     assertEquals("correct project.properties", Collections.singletonMap("shared.prop", "val1"), props);
128                     props = AntBasedTestUtil.slurpProperties(h, AntProjectHelper.PRIVATE_PROPERTIES_PATH);
129                     assertEquals("correct project.properties", Collections.singletonMap("private.prop", "val2"), props);
130                     doc = AntBasedTestUtil.slurpXml(h, "nbproject/build-impl.xml");
131                     el = doc.getDocumentElement();
132                     assertEquals("build-impl.xml is a <project>", "project", el.getLocalName());
133                     assertEquals("<project> has no namespace", null, el.getNamespaceURI());
134                     l = doc.getElementsByTagName("target");
135                     assertEquals("two targets in build-impl.xml", 2, l.getLength());
136                     el = (Element JavaDoc)l.item(1);
137                     assertEquals("second target is \"x\"", "x", el.getAttribute("name"));
138                     new GeneratedFilesHelper(h).generateBuildScriptFromStylesheet(
139                         GeneratedFilesHelper.BUILD_XML_PATH, AntBasedTestUtil.testBuildXmlStylesheet());
140                     doc = AntBasedTestUtil.slurpXml(h, "build.xml");
141                     el = doc.getDocumentElement();
142                     assertEquals("build.xml is a <project>", "project", el.getLocalName());
143                     assertEquals("<project> has no namespace", null, el.getNamespaceURI());
144                     l = doc.getElementsByTagName("target");
145                     assertEquals("one target in build.xml", 1, l.getLength());
146                     el = (Element JavaDoc)l.item(0);
147                     assertEquals("target is \"all\"", "all", el.getAttribute("name"));
148                     return null;
149                 }
150             });
151         } catch (MutexException e) {
152             throw e.getException();
153         }
154     }
155     
156 }
157
Popular Tags