1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.util.Collections ; 23 import java.util.Properties ; 24 import org.netbeans.api.project.Project; 25 import org.netbeans.api.project.ProjectManager; 26 import org.netbeans.api.project.ProjectUtils; 27 import org.netbeans.api.project.TestUtil; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.modules.ruby.modules.project.rake.RakeBasedProjectFactorySingleton; 30 import org.netbeans.modules.ruby.modules.project.rake.Util; 31 import org.netbeans.api.project.ProjectInformation; 32 import org.openide.filesystems.FileObject; 33 import org.openide.util.Lookup; 34 import org.openide.util.Mutex; 35 import org.openide.util.MutexException; 36 import org.openide.util.lookup.Lookups; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 41 45 public class ProjectGeneratorTest extends NbTestCase { 46 47 51 public ProjectGeneratorTest(String name) { 52 super(name); 53 } 54 55 private FileObject scratch; 56 private FileObject projdir; 57 58 protected void setUp() throws Exception { 59 super.setUp(); 60 scratch = TestUtil.makeScratchDir(this); 61 projdir = scratch.createFolder("proj"); 62 TestUtil.setLookup(Lookups.fixed(new Object [] { 63 new RakeBasedProjectFactorySingleton(), 64 RakeBasedTestUtil.testRakeBasedProjectType(), 65 })); 66 } 67 68 protected void tearDown() throws Exception { 69 scratch = null; 70 projdir = null; 71 TestUtil.setLookup(Lookup.EMPTY); 72 super.tearDown(); 73 } 74 75 79 public void testCreateNewProject() throws Exception { 80 try { 81 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 82 public Void run() throws Exception { 83 RakeProjectHelper h = ProjectGenerator.createProject(projdir, "test"); 85 assertNotNull("Returned some project helper", h); 86 Project p = ProjectManager.getDefault().findProject(projdir); 87 assertNotNull("Project exists", p); 88 ProjectInformation pi = ProjectUtils.getInformation(p); 90 assertEquals("correct directory", projdir, p.getProjectDirectory()); 91 assertTrue("already modified", ProjectManager.getDefault().isModified(p)); 92 Element data = h.getPrimaryConfigurationData(true); 94 assertEquals("correct namespace for shared data", "urn:test:shared", data.getNamespaceURI()); 95 assertEquals("empty initial shared data", 0, data.getChildNodes().getLength()); 96 Element stuff = data.getOwnerDocument().createElementNS("urn:test:shared", "shared-stuff"); 97 data.appendChild(stuff); 98 h.putPrimaryConfigurationData(data, true); 99 data = h.getPrimaryConfigurationData(false); 100 assertEquals("correct namespace for private data", "urn:test:private", data.getNamespaceURI()); 101 assertEquals("empty initial private data", 0, data.getChildNodes().getLength()); 102 stuff = data.getOwnerDocument().createElementNS("urn:test:private", "private-stuff"); 103 data.appendChild(stuff); 104 h.putPrimaryConfigurationData(data, false); 105 EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 106 assertEquals("empty initial project.properties", 0, ep.size()); 107 ep.setProperty("shared.prop", "val1"); 108 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep); 109 ep = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH); 110 assertEquals("empty initial private.properties", 0, ep.size()); 111 ep.setProperty("private.prop", "val2"); 112 h.putProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH, ep); 113 ProjectManager.getDefault().saveProject(p); 115 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 117 NodeList l = doc.getElementsByTagNameNS(RakeProjectHelper.PROJECT_NS, "type"); 118 assertEquals("one <type>", 1, l.getLength()); 119 Element el = (Element )l.item(0); 120 assertEquals("correct saved type", "test", Util.findText(el)); 121 l = doc.getElementsByTagNameNS("urn:test:shared", "shared-stuff"); 122 assertEquals("one <shared-stuff>", 1, l.getLength()); 123 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PRIVATE_XML_PATH); 124 l = doc.getElementsByTagNameNS("urn:test:private", "private-stuff"); 125 assertEquals("one <private-stuff>", 1, l.getLength()); 126 Properties props = RakeBasedTestUtil.slurpProperties(h, RakeProjectHelper.PROJECT_PROPERTIES_PATH); 127 assertEquals("correct project.properties", Collections.singletonMap("shared.prop", "val1"), props); 128 props = RakeBasedTestUtil.slurpProperties(h, RakeProjectHelper.PRIVATE_PROPERTIES_PATH); 129 assertEquals("correct project.properties", Collections.singletonMap("private.prop", "val2"), props); 130 doc = RakeBasedTestUtil.slurpXml(h, "nbproject/build-impl.xml"); 131 el = doc.getDocumentElement(); 132 assertEquals("build-impl.xml is a <project>", "project", el.getLocalName()); 133 assertEquals("<project> has no namespace", null, el.getNamespaceURI()); 134 l = doc.getElementsByTagName("target"); 135 assertEquals("two targets in build-impl.xml", 2, l.getLength()); 136 el = (Element )l.item(1); 137 assertEquals("second target is \"x\"", "x", el.getAttribute("name")); 138 new GeneratedFilesHelper(h).generateBuildScriptFromStylesheet( 139 GeneratedFilesHelper.BUILD_XML_PATH, RakeBasedTestUtil.testBuildXmlStylesheet()); 140 doc = RakeBasedTestUtil.slurpXml(h, "build.xml"); 141 el = doc.getDocumentElement(); 142 assertEquals("build.xml is a <project>", "project", el.getLocalName()); 143 assertEquals("<project> has no namespace", null, el.getNamespaceURI()); 144 l = doc.getElementsByTagName("target"); 145 assertEquals("one target in build.xml", 1, l.getLength()); 146 el = (Element )l.item(0); 147 assertEquals("target is \"all\"", "all", el.getAttribute("name")); 148 return null; 149 } 150 }); 151 } catch (MutexException e) { 152 throw e.getException(); 153 } 154 } 155 156 } 157 | Popular Tags |