KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > spi > support > UtilTest


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.ant.freeform.spi.support;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.api.project.ProjectManager;
28 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
29 import org.netbeans.modules.ant.freeform.TestBase;
30 import org.netbeans.spi.project.AuxiliaryConfiguration;
31 import org.netbeans.spi.project.support.ant.AntProjectHelper;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.util.Utilities;
35 import org.openide.xml.XMLUtil;
36 import org.w3c.dom.Document JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38
39 /**
40  * @author David Konecny
41  */

42 public class UtilTest extends TestBase {
43     
44     public UtilTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50         clearWorkDir();
51     }
52     
53     public void testAuxiliaryConfiguration() throws Exception JavaDoc {
54         File JavaDoc proj = new File JavaDoc(getWorkDir(), "aux_proj");
55         proj.mkdir();
56         AntProjectHelper helper = FreeformProjectGenerator.createProject(proj, proj, "proj1", null);
57         FileObject base = helper.getProjectDirectory();
58         Project p = ProjectManager.getDefault().findProject(base);
59         assertNotNull("project was created", p);
60         assertEquals("expected project folder", base, p.getProjectDirectory());
61         
62         AuxiliaryConfiguration au = Util.getAuxiliaryConfiguration(helper);
63         assertNotNull("project has AuxiliaryConfiguration", au);
64     }
65     
66     public void testRelativizeLocation() throws Exception JavaDoc {
67         File JavaDoc srcApp = Utilities.isWindows() ? new File JavaDoc("c:\\src\\app") : new File JavaDoc("/src/app");
68         File JavaDoc srcAppFooBar = new File JavaDoc(srcApp, "foo" + File.separatorChar + "bar");
69         File JavaDoc projApp = Utilities.isWindows() ? new File JavaDoc("c:\\proj\\app") : new File JavaDoc("/proj/app");
70         File JavaDoc otherFooBar = Utilities.isWindows() ? new File JavaDoc("c:\\other\\foo\\bar") : new File JavaDoc("/other/foo/bar");
71         assertEquals("foo/bar", Util.relativizeLocation(srcApp, srcApp, srcAppFooBar));
72         assertEquals("${project.dir}/foo/bar", Util.relativizeLocation(srcApp, projApp, srcAppFooBar));
73         assertEquals(otherFooBar.getAbsolutePath(), Util.relativizeLocation(srcApp, srcApp, otherFooBar));
74         assertEquals(otherFooBar.getAbsolutePath(), Util.relativizeLocation(srcApp, projApp, otherFooBar));
75         // Mentioned incidentally in #54428:
76
assertEquals(".", Util.relativizeLocation(srcApp, srcApp, srcApp));
77         assertEquals("${project.dir}", Util.relativizeLocation(srcApp, projApp, srcApp));
78     }
79
80     public void testGetDefaultAntScript() throws Exception JavaDoc {
81         assertNull("no default ant script", Util.getDefaultAntScript(extsrcroot));
82         assertEquals("found build.xml", simple.getProjectDirectory().getFileObject("build.xml"), Util.getDefaultAntScript(simple));
83         assertEquals("found build.xml", extbuildscript.getProjectDirectory().getFileObject("scripts/build.xml"), Util.getDefaultAntScript(extbuildscript));
84     }
85
86     public void testFormatUpgrade() throws Exception JavaDoc {
87         AntProjectHelper helper = FreeformProjectGenerator.createProject(getWorkDir(), getWorkDir(), "prj", null);
88         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
89         FileObject pxml = helper.resolveFileObject(AntProjectHelper.PROJECT_XML_PATH);
90         // To simplify test, overwrite project.xml with a basic version w/o <properties>, <view>, or <comment>.
91
Element JavaDoc data = helper.getPrimaryConfigurationData(true);
92         data = data.getOwnerDocument().createElementNS(data.getNamespaceURI(), data.getLocalName());
93         data.appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "name")).appendChild(data.getOwnerDocument().createTextNode("prj"));
94         helper.putPrimaryConfigurationData(data, true);
95         ProjectManager.getDefault().saveProject(p);
96         // Initial check.
97
assertEquals("<project/p1><type.../><configuration><general-data/ff1><name>prj</></></></>", xmlSimplified(pxml));
98         data = Util.getPrimaryConfigurationData(helper);
99         assertEquals("<general-data/ff2><name>prj</></>", xmlSimplified(data));
100         // Save something in a /1-compatible format.
101
Element JavaDoc folder = (Element JavaDoc) data.appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "folders")).
102                 appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "source-folder"));
103         folder.appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "label")).appendChild(data.getOwnerDocument().createTextNode("Sources"));
104         folder.appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "location")).appendChild(data.getOwnerDocument().createTextNode("src"));
105         Util.putPrimaryConfigurationData(helper, data);
106         ProjectManager.getDefault().saveProject(p);
107         assertEquals("<project/p1><type.../><configuration><general-data/ff1><name>prj</>" +
108                 "<folders><source-folder><label>Sources</><location>src</></></></></></>", xmlSimplified(pxml));
109         data = Util.getPrimaryConfigurationData(helper);
110         assertEquals("<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</></></></>", xmlSimplified(data));
111         // Save something that forces use of the /2 format.
112
data.getElementsByTagName("source-folder").item(0).
113                 appendChild(data.getOwnerDocument().createElementNS(data.getNamespaceURI(), "excludes")).
114                 appendChild(data.getOwnerDocument().createTextNode("junk/"));
115         Util.putPrimaryConfigurationData(helper, data);
116         ProjectManager.getDefault().saveProject(p);
117         assertEquals("<project/p1><type.../><configuration><general-data/ff1><name>prj</></><general-data/ff2><name>prj</>" +
118                 "<folders><source-folder><label>Sources</><location>src</><excludes>junk/</></></></></></>", xmlSimplified(pxml));
119         data = Util.getPrimaryConfigurationData(helper);
120         assertEquals("<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</><excludes>junk/</></></></>", xmlSimplified(data));
121         // Save something old again.
122
Element JavaDoc excludes = (Element JavaDoc) data.getElementsByTagName("excludes").item(0);
123         excludes.getParentNode().removeChild(excludes);
124         Util.putPrimaryConfigurationData(helper, data);
125         ProjectManager.getDefault().saveProject(p);
126         assertEquals("<project/p1><type.../><configuration><general-data/ff1><name>prj</>" +
127                 "<folders><source-folder><label>Sources</><location>src</></></></></></>", xmlSimplified(pxml));
128         data = Util.getPrimaryConfigurationData(helper);
129         assertEquals("<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</></></></>", xmlSimplified(data));
130     }
131     private static String JavaDoc xmlSimplified(Element JavaDoc e) throws Exception JavaDoc {
132         Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
133         doc.appendChild(doc.importNode(e, true));
134         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
135         XMLUtil.write(doc, baos, "UTF-8");
136         return xmlSimplified(baos.toString("UTF-8"));
137     }
138     private static String JavaDoc xmlSimplified(FileObject f) throws Exception JavaDoc {
139         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
140         InputStream JavaDoc is = f.getInputStream();
141         try {
142             FileUtil.copy(is, baos);
143         } finally {
144             is.close();
145         }
146         return xmlSimplified(baos.toString("UTF-8"));
147     }
148     private static String JavaDoc xmlSimplified(String JavaDoc s) throws Exception JavaDoc {
149         return s.replaceFirst("^<\\?xml.+\\?>", "").
150                 replaceAll("(\r|\n|\r\n) *", "").
151                 replace(" xmlns=\"http://www.netbeans.org/ns/project/1\"", "/p1").
152                 replaceAll(" xmlns=\"http://www\\.netbeans\\.org/ns/freeform-project/(\\d+)\"", "/ff$1").
153                 replaceAll("</[^>]+>", "</>").
154                 replace("<type>org.netbeans.modules.ant.freeform</>", "<type.../>");
155     }
156     
157 }
158
Popular Tags