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.ant.freeform.spi.support;
043:
044: import java.io.ByteArrayOutputStream;
045: import java.io.File;
046: import java.io.InputStream;
047: import javax.xml.parsers.DocumentBuilderFactory;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
051: import org.netbeans.modules.ant.freeform.TestBase;
052: import org.netbeans.spi.project.AuxiliaryConfiguration;
053: import org.netbeans.spi.project.support.ant.AntProjectHelper;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.Utilities;
057: import org.openide.xml.XMLUtil;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.Element;
060:
061: /**
062: * @author David Konecny
063: */
064: public class UtilTest extends TestBase {
065:
066: public UtilTest(String name) {
067: super (name);
068: }
069:
070: protected void setUp() throws Exception {
071: super .setUp();
072: clearWorkDir();
073: }
074:
075: public void testAuxiliaryConfiguration() throws Exception {
076: File proj = new File(getWorkDir(), "aux_proj");
077: proj.mkdir();
078: AntProjectHelper helper = FreeformProjectGenerator
079: .createProject(proj, proj, "proj1", null);
080: FileObject base = helper.getProjectDirectory();
081: Project p = ProjectManager.getDefault().findProject(base);
082: assertNotNull("project was created", p);
083: assertEquals("expected project folder", base, p
084: .getProjectDirectory());
085:
086: AuxiliaryConfiguration au = Util
087: .getAuxiliaryConfiguration(helper);
088: assertNotNull("project has AuxiliaryConfiguration", au);
089: }
090:
091: public void testRelativizeLocation() throws Exception {
092: File srcApp = Utilities.isWindows() ? new File("c:\\src\\app")
093: : new File("/src/app");
094: File srcAppFooBar = new File(srcApp, "foo" + File.separatorChar
095: + "bar");
096: File projApp = Utilities.isWindows() ? new File("c:\\proj\\app")
097: : new File("/proj/app");
098: File otherFooBar = Utilities.isWindows() ? new File(
099: "c:\\other\\foo\\bar") : new File("/other/foo/bar");
100: assertEquals("foo/bar", Util.relativizeLocation(srcApp, srcApp,
101: srcAppFooBar));
102: assertEquals("${project.dir}/foo/bar", Util.relativizeLocation(
103: srcApp, projApp, srcAppFooBar));
104: assertEquals(otherFooBar.getAbsolutePath(), Util
105: .relativizeLocation(srcApp, srcApp, otherFooBar));
106: assertEquals(otherFooBar.getAbsolutePath(), Util
107: .relativizeLocation(srcApp, projApp, otherFooBar));
108: // Mentioned incidentally in #54428:
109: assertEquals(".", Util.relativizeLocation(srcApp, srcApp,
110: srcApp));
111: assertEquals("${project.dir}", Util.relativizeLocation(srcApp,
112: projApp, srcApp));
113: }
114:
115: public void testGetDefaultAntScript() throws Exception {
116: assertNull("no default ant script", Util
117: .getDefaultAntScript(extsrcroot));
118: assertEquals("found build.xml", simple.getProjectDirectory()
119: .getFileObject("build.xml"), Util
120: .getDefaultAntScript(simple));
121: assertEquals("found build.xml", extbuildscript
122: .getProjectDirectory().getFileObject(
123: "scripts/build.xml"), Util
124: .getDefaultAntScript(extbuildscript));
125: }
126:
127: public void testFormatUpgrade() throws Exception {
128: AntProjectHelper helper = FreeformProjectGenerator
129: .createProject(getWorkDir(), getWorkDir(), "prj", null);
130: Project p = ProjectManager.getDefault().findProject(
131: helper.getProjectDirectory());
132: FileObject pxml = helper
133: .resolveFileObject(AntProjectHelper.PROJECT_XML_PATH);
134: // To simplify test, overwrite project.xml with a basic version w/o <properties>, <view>, or <comment>.
135: Element data = helper.getPrimaryConfigurationData(true);
136: data = data.getOwnerDocument().createElementNS(
137: data.getNamespaceURI(), data.getLocalName());
138: data.appendChild(
139: data.getOwnerDocument().createElementNS(
140: data.getNamespaceURI(), "name")).appendChild(
141: data.getOwnerDocument().createTextNode("prj"));
142: helper.putPrimaryConfigurationData(data, true);
143: ProjectManager.getDefault().saveProject(p);
144: // Initial check.
145: assertEquals(
146: "<project/p1><type.../><configuration><general-data/ff1><name>prj</></></></>",
147: xmlSimplified(pxml));
148: data = Util.getPrimaryConfigurationData(helper);
149: assertEquals("<general-data/ff2><name>prj</></>",
150: xmlSimplified(data));
151: // Save something in a /1-compatible format.
152: Element folder = (Element) data.appendChild(
153: data.getOwnerDocument().createElementNS(
154: data.getNamespaceURI(), "folders"))
155: .appendChild(
156: data.getOwnerDocument()
157: .createElementNS(
158: data.getNamespaceURI(),
159: "source-folder"));
160: folder.appendChild(
161: data.getOwnerDocument().createElementNS(
162: data.getNamespaceURI(), "label")).appendChild(
163: data.getOwnerDocument().createTextNode("Sources"));
164: folder.appendChild(
165: data.getOwnerDocument().createElementNS(
166: data.getNamespaceURI(), "location"))
167: .appendChild(
168: data.getOwnerDocument().createTextNode("src"));
169: Util.putPrimaryConfigurationData(helper, data);
170: ProjectManager.getDefault().saveProject(p);
171: assertEquals(
172: "<project/p1><type.../><configuration><general-data/ff1><name>prj</>"
173: + "<folders><source-folder><label>Sources</><location>src</></></></></></>",
174: xmlSimplified(pxml));
175: data = Util.getPrimaryConfigurationData(helper);
176: assertEquals(
177: "<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</></></></>",
178: xmlSimplified(data));
179: // Save something that forces use of the /2 format.
180: data
181: .getElementsByTagName("source-folder")
182: .item(0)
183: .appendChild(
184: data.getOwnerDocument().createElementNS(
185: data.getNamespaceURI(), "excludes"))
186: .appendChild(
187: data.getOwnerDocument().createTextNode("junk/"));
188: Util.putPrimaryConfigurationData(helper, data);
189: ProjectManager.getDefault().saveProject(p);
190: assertEquals(
191: "<project/p1><type.../><configuration><general-data/ff1><name>prj</></><general-data/ff2><name>prj</>"
192: + "<folders><source-folder><label>Sources</><location>src</><excludes>junk/</></></></></></>",
193: xmlSimplified(pxml));
194: data = Util.getPrimaryConfigurationData(helper);
195: assertEquals(
196: "<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</><excludes>junk/</></></></>",
197: xmlSimplified(data));
198: // Save something old again.
199: Element excludes = (Element) data.getElementsByTagName(
200: "excludes").item(0);
201: excludes.getParentNode().removeChild(excludes);
202: Util.putPrimaryConfigurationData(helper, data);
203: ProjectManager.getDefault().saveProject(p);
204: assertEquals(
205: "<project/p1><type.../><configuration><general-data/ff1><name>prj</>"
206: + "<folders><source-folder><label>Sources</><location>src</></></></></></>",
207: xmlSimplified(pxml));
208: data = Util.getPrimaryConfigurationData(helper);
209: assertEquals(
210: "<general-data/ff2><name>prj</><folders><source-folder><label>Sources</><location>src</></></></>",
211: xmlSimplified(data));
212: }
213:
214: private static String xmlSimplified(Element e) throws Exception {
215: Document doc = DocumentBuilderFactory.newInstance()
216: .newDocumentBuilder().newDocument();
217: doc.appendChild(doc.importNode(e, true));
218: ByteArrayOutputStream baos = new ByteArrayOutputStream();
219: XMLUtil.write(doc, baos, "UTF-8");
220: return xmlSimplified(baos.toString("UTF-8"));
221: }
222:
223: private static String xmlSimplified(FileObject f) throws Exception {
224: ByteArrayOutputStream baos = new ByteArrayOutputStream();
225: InputStream is = f.getInputStream();
226: try {
227: FileUtil.copy(is, baos);
228: } finally {
229: is.close();
230: }
231: return xmlSimplified(baos.toString("UTF-8"));
232: }
233:
234: private static String xmlSimplified(String s) throws Exception {
235: return s
236: .replaceFirst("^<\\?xml.+\\?>", "")
237: .replaceAll("(\r|\n|\r\n) *", "")
238: .replace(
239: " xmlns=\"http://www.netbeans.org/ns/project/1\"",
240: "/p1")
241: .replaceAll(
242: " xmlns=\"http://www\\.netbeans\\.org/ns/freeform-project/(\\d+)\"",
243: "/ff$1").replaceAll("</[^>]+>", "</>").replace(
244: "<type>org.netbeans.modules.ant.freeform</>",
245: "<type.../>");
246: }
247:
248: }
|