1 19 20 package org.netbeans.modules.ant.freeform.spi.support; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.InputStream ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 37 import org.w3c.dom.Element ; 38 39 42 public class UtilTest extends TestBase { 43 44 public UtilTest(String name) { 45 super(name); 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 clearWorkDir(); 51 } 52 53 public void testAuxiliaryConfiguration() throws Exception { 54 File proj = new File (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 { 67 File srcApp = Utilities.isWindows() ? new File ("c:\\src\\app") : new File ("/src/app"); 68 File srcAppFooBar = new File (srcApp, "foo" + File.separatorChar + "bar"); 69 File projApp = Utilities.isWindows() ? new File ("c:\\proj\\app") : new File ("/proj/app"); 70 File otherFooBar = Utilities.isWindows() ? new File ("c:\\other\\foo\\bar") : new File ("/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 assertEquals(".", Util.relativizeLocation(srcApp, srcApp, srcApp)); 77 assertEquals("${project.dir}", Util.relativizeLocation(srcApp, projApp, srcApp)); 78 } 79 80 public void testGetDefaultAntScript() throws Exception { 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 { 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 Element 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 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 Element folder = (Element ) 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 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 Element excludes = (Element ) 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 xmlSimplified(Element e) throws Exception { 132 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); 133 doc.appendChild(doc.importNode(e, true)); 134 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 135 XMLUtil.write(doc, baos, "UTF-8"); 136 return xmlSimplified(baos.toString("UTF-8")); 137 } 138 private static String xmlSimplified(FileObject f) throws Exception { 139 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 140 InputStream 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 xmlSimplified(String s) throws Exception { 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 |