KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > GeneratedFilesHelperTest


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.ruby.spi.project.support.rake;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.net.URL JavaDoc;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.ProjectManager;
27 import org.netbeans.api.project.TestUtil;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.modules.ruby.modules.project.rake.Util;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.util.Utilities;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 /**
38  * Test functionality of GeneratedFilesHelper.
39  * @author Jesse Glick
40  */

41 public class GeneratedFilesHelperTest extends NbTestCase {
42     
43     public GeneratedFilesHelperTest(String JavaDoc name) {
44         super(name);
45     }
46     
47     private FileObject scratch;
48     private FileObject projdir;
49     private ProjectManager pm;
50     private Project p;
51     private RakeProjectHelper h;
52     private GeneratedFilesHelper gfh;
53     
54     protected void setUp() throws Exception JavaDoc {
55         super.setUp();
56         scratch = TestUtil.makeScratchDir(this);
57         projdir = scratch.createFolder("proj");
58         TestUtil.createFileFromContent(GeneratedFilesHelperTest.class.getResource("data/project.xml"), projdir, "nbproject/project.xml");
59         TestUtil.setLookup(new Object JavaDoc[] {
60             RakeBasedTestUtil.testRakeBasedProjectType(),
61         });
62         pm = ProjectManager.getDefault();
63         p = pm.findProject(projdir);
64         h = p.getLookup().lookup(RakeProjectHelper.class);
65         gfh = p.getLookup().lookup(GeneratedFilesHelper.class);
66         assertNotNull(gfh);
67     }
68     
69     /**
70      * Test that creating build-impl.xml from project.xml + build-impl.xsl works.
71      * @throws Exception if anything unexpected happens
72      */

73     public void testGenerateBuildScriptFromStylesheet() throws Exception JavaDoc {
74         // Make sure there is some build-impl.xml.
75
FileObject bi = projdir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
76         assertNull("No build-impl.xml yet", bi);
77         // Modify shared data in a project.
78
Element JavaDoc primdata = h.getPrimaryConfigurationData(true);
79         Element JavaDoc oldDisplayName = Util.findElement(primdata, "display-name", "urn:test:shared");
80         assertNotNull("had a <display-name> before", oldDisplayName);
81         Element JavaDoc displayName = primdata.getOwnerDocument().createElementNS("urn:test:shared", "display-name");
82         displayName.appendChild(primdata.getOwnerDocument().createTextNode("New Name"));
83         primdata.insertBefore(displayName, oldDisplayName);
84         primdata.removeChild(oldDisplayName);
85         h.putPrimaryConfigurationData(primdata, true);
86         assertTrue("project is modified", pm.isModified(p));
87         pm.saveProject(p);
88         // Ensure that build-impl.xml was (correctly) regenerated.
89
FileObject genfiles = projdir.getFileObject(GeneratedFilesHelper.GENFILES_PROPERTIES_PATH);
90         assertNotNull("genfiles.properties exists", genfiles);
91         bi = projdir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
92         assertNotNull("saving the project with a project.xml change regenerates build-impl.xml", bi);
93         Document JavaDoc doc = RakeBasedTestUtil.slurpXml(h, GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
94         Element JavaDoc el = doc.getDocumentElement();
95         assertEquals("build-impl.xml is a <project>", "project", el.getLocalName());
96         assertEquals("<project> has no namespace", null, el.getNamespaceURI());
97         NodeList JavaDoc l = doc.getElementsByTagName("description");
98         assertEquals("one <description> in build-impl.xml", 1, l.getLength());
99         el = (Element JavaDoc)l.item(0);
100         assertEquals("correct description", "New Name", Util.findText(el));
101         // Clear build-impl.xml to test if it is rewritten.
102
bi.delete();
103         // Now make some irrelevant change - e.g. to private.xml - and check that there is no modification.
104
Element JavaDoc data = h.getPrimaryConfigurationData(false);
105         data.setAttribute("someattr", "someval");
106         h.putPrimaryConfigurationData(data, false);
107         assertTrue("project is modified", pm.isModified(p));
108         pm.saveProject(p);
109         bi = projdir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH);
110         assertNull("saving a private.xml change does not regenerate build-impl.xml", bi);
111     }
112     
113     /**
114      * Test that fooling with a build script in various ways is correctly detected.
115      * @throws Exception if anything unexpected happens
116      */

117     public void testGetBuildScriptState() throws Exception JavaDoc {
118         URL JavaDoc xslt = GeneratedFilesHelperTest.class.getResource("data/build.xsl");
119         URL JavaDoc xslt2 = GeneratedFilesHelperTest.class.getResource("data/build2.xsl");
120         String JavaDoc path = GeneratedFilesHelper.BUILD_XML_PATH;
121         assertEquals("initially there is no build.xml",
122             GeneratedFilesHelper.FLAG_MISSING, gfh.getBuildScriptState(path, xslt));
123         assertEquals("stylesheet version ignored for FLAG_MISSING",
124             GeneratedFilesHelper.FLAG_MISSING, gfh.getBuildScriptState(path, xslt2));
125         gfh.generateBuildScriptFromStylesheet(path, xslt);
126         assertEquals("now build.xml is there and clean",
127             0, gfh.getBuildScriptState(path, xslt));
128         assertEquals("build.xml is using first stylesheet",
129             GeneratedFilesHelper.FLAG_OLD_STYLESHEET, gfh.getBuildScriptState(path, xslt2));
130         File JavaDoc buildXml= FileUtil.toFile(projdir.getFileObject("build.xml"));
131         assertEquals("one replacement", 1, RakeBasedTestUtil.replaceInFile(buildXml, "name=\"somename\"", "name=\"someothername\""));
132         assertEquals("now build.xml is modified",
133             GeneratedFilesHelper.FLAG_MODIFIED, gfh.getBuildScriptState(path, xslt));
134         assertEquals("one replacement", 1, RakeBasedTestUtil.replaceInFile(buildXml, "name=\"someothername\"", "name=\"somename\""));
135         assertEquals("now build.xml is clean again",
136             0, gfh.getBuildScriptState(path, xslt));
137         File JavaDoc projectXml= FileUtil.toFile(projdir.getFileObject("nbproject/project.xml"));
138         assertEquals("one replacement", 1, RakeBasedTestUtil.replaceInFile(projectXml, "<name>somename</name>", "<name>newname</name>"));
139         assertEquals("now build.xml is out of date w.r.t. project.xml",
140             GeneratedFilesHelper.FLAG_OLD_PROJECT_XML, gfh.getBuildScriptState(path, xslt));
141         assertEquals("build.xml is out of date w.r.t. project.xml and new XSLT",
142             GeneratedFilesHelper.FLAG_OLD_PROJECT_XML | GeneratedFilesHelper.FLAG_OLD_STYLESHEET, gfh.getBuildScriptState(path, xslt2));
143         assertEquals("one replacement", 1, RakeBasedTestUtil.replaceInFile(buildXml, "name=\"somename\"", "name=\"someothername\""));
144         assertEquals("build.xml is modified and out of date w.r.t. project.xml",
145             GeneratedFilesHelper.FLAG_OLD_PROJECT_XML | GeneratedFilesHelper.FLAG_MODIFIED, gfh.getBuildScriptState(path, xslt));
146         assertEquals("build.xml is modified and out of date w.r.t. project.xml and new XSLT",
147             GeneratedFilesHelper.FLAG_OLD_PROJECT_XML | GeneratedFilesHelper.FLAG_MODIFIED | GeneratedFilesHelper.FLAG_OLD_STYLESHEET, gfh.getBuildScriptState(path, xslt2));
148         gfh.generateBuildScriptFromStylesheet(path, xslt2);
149         assertEquals("now regenerated build.xml is up to date",
150             0, gfh.getBuildScriptState(path, xslt2));
151         // Check newline conventions. First normalize project.xml if running on Windows or Mac.
152
RakeBasedTestUtil.replaceInFile(projectXml, "\r\n", "\n");
153         RakeBasedTestUtil.replaceInFile(projectXml, "\r", "\n");
154         gfh.generateBuildScriptFromStylesheet(path, xslt);
155         assertEquals("build.xml is clean",
156             0, gfh.getBuildScriptState(path, xslt));
157         int count = RakeBasedTestUtil.replaceInFile(projectXml, "\n", "\r\n");
158         assertTrue("Changed newlines", count > 0);
159         assertEquals("build.xml is still clean w.r.t. changed newlines in project.xml",
160             0, gfh.getBuildScriptState(path, xslt));
161         // XXX check also newline changes in stylesheet and build.xml
162
}
163     
164     /**
165      * Test normalization of newlines in CRC-32 computations.
166      * @throws Exception if anything unexpected happens
167      */

168     public void testComputeCrc32() throws Exception JavaDoc {
169         String JavaDoc testDataNl = "hi mom\nhow are you\n";
170         String JavaDoc testDataCrNl = "hi mom\r\nhow are you\r\n";
171         String JavaDoc testDataCr = "hi mom\rhow are you\r";
172         String JavaDoc crcNl = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream JavaDoc(testDataNl.getBytes("UTF-8")));
173         String JavaDoc crcCrNl = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream JavaDoc(testDataCrNl.getBytes("UTF-8")));
174         String JavaDoc crcCr = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream JavaDoc(testDataCr.getBytes("UTF-8")));
175         assertEquals("CRNL normalized -> NL", crcNl, crcCrNl);
176         assertEquals("CR normalized -> NL", crcNl, crcCr);
177     }
178
179     public void testEolOnWindows() throws Exception JavaDoc {
180         if (Utilities.isWindows()) {
181             URL JavaDoc xslt = GeneratedFilesHelperTest.class.getResource("data/build.xsl");
182             String JavaDoc path = GeneratedFilesHelper.BUILD_XML_PATH;
183             assertEquals("initially there is no build.xml",
184                 GeneratedFilesHelper.FLAG_MISSING, gfh.getBuildScriptState(path, xslt));
185             gfh.generateBuildScriptFromStylesheet(path, xslt);
186             assertEquals("now build.xml is there and clean",
187                 0, gfh.getBuildScriptState(path, xslt));
188             File JavaDoc buildXml= FileUtil.toFile(projdir.getFileObject("build.xml"));
189             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(RakeBasedTestUtil.slurpText(h, path));
190             boolean ok = true;
191             for (int i=1; i<sb.length(); i++) {
192                 if (sb.charAt(i) == '\n') {
193                     if (sb.charAt(i-1) != '\r') {
194                         ok = false;
195                         break;
196                     }
197                 }
198             }
199             assertTrue("generated file has platform line endings", ok);
200         }
201     }
202     
203 }
204
Popular Tags