1 19 20 package org.netbeans.spi.project.support.ant; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.File ; 24 import java.net.URL ; 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.project.ant.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 ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.NodeList ; 36 37 41 public class GeneratedFilesHelperTest extends NbTestCase { 42 43 public GeneratedFilesHelperTest(String name) { 44 super(name); 45 } 46 47 private FileObject scratch; 48 private FileObject projdir; 49 private ProjectManager pm; 50 private Project p; 51 private AntProjectHelper h; 52 private GeneratedFilesHelper gfh; 53 54 protected void setUp() throws Exception { 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 [] { 60 AntBasedTestUtil.testAntBasedProjectType(), 61 }); 62 pm = ProjectManager.getDefault(); 63 p = pm.findProject(projdir); 64 h = p.getLookup().lookup(AntProjectHelper.class); 65 gfh = p.getLookup().lookup(GeneratedFilesHelper.class); 66 assertNotNull(gfh); 67 } 68 69 73 public void testGenerateBuildScriptFromStylesheet() throws Exception { 74 FileObject bi = projdir.getFileObject(GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 76 assertNull("No build-impl.xml yet", bi); 77 Element primdata = h.getPrimaryConfigurationData(true); 79 Element oldDisplayName = Util.findElement(primdata, "display-name", "urn:test:shared"); 80 assertNotNull("had a <display-name> before", oldDisplayName); 81 Element 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 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 doc = AntBasedTestUtil.slurpXml(h, GeneratedFilesHelper.BUILD_IMPL_XML_PATH); 94 Element 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 l = doc.getElementsByTagName("description"); 98 assertEquals("one <description> in build-impl.xml", 1, l.getLength()); 99 el = (Element )l.item(0); 100 assertEquals("correct description", "New Name", Util.findText(el)); 101 bi.delete(); 103 Element 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 117 public void testGetBuildScriptState() throws Exception { 118 URL xslt = GeneratedFilesHelperTest.class.getResource("data/build.xsl"); 119 URL xslt2 = GeneratedFilesHelperTest.class.getResource("data/build2.xsl"); 120 String 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 buildXml= FileUtil.toFile(projdir.getFileObject("build.xml")); 131 assertEquals("one replacement", 1, AntBasedTestUtil.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, AntBasedTestUtil.replaceInFile(buildXml, "name=\"someothername\"", "name=\"somename\"")); 135 assertEquals("now build.xml is clean again", 136 0, gfh.getBuildScriptState(path, xslt)); 137 File projectXml= FileUtil.toFile(projdir.getFileObject("nbproject/project.xml")); 138 assertEquals("one replacement", 1, AntBasedTestUtil.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, AntBasedTestUtil.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 AntBasedTestUtil.replaceInFile(projectXml, "\r\n", "\n"); 153 AntBasedTestUtil.replaceInFile(projectXml, "\r", "\n"); 154 gfh.generateBuildScriptFromStylesheet(path, xslt); 155 assertEquals("build.xml is clean", 156 0, gfh.getBuildScriptState(path, xslt)); 157 int count = AntBasedTestUtil.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 } 163 164 168 public void testComputeCrc32() throws Exception { 169 String testDataNl = "hi mom\nhow are you\n"; 170 String testDataCrNl = "hi mom\r\nhow are you\r\n"; 171 String testDataCr = "hi mom\rhow are you\r"; 172 String crcNl = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream (testDataNl.getBytes("UTF-8"))); 173 String crcCrNl = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream (testDataCrNl.getBytes("UTF-8"))); 174 String crcCr = GeneratedFilesHelper.computeCrc32(new ByteArrayInputStream (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 { 180 if (Utilities.isWindows()) { 181 URL xslt = GeneratedFilesHelperTest.class.getResource("data/build.xsl"); 182 String 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 buildXml= FileUtil.toFile(projdir.getFileObject("build.xml")); 189 StringBuffer sb = new StringBuffer (AntBasedTestUtil.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 |