1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import org.netbeans.api.project.Project; 31 import org.netbeans.api.project.ProjectManager; 32 import org.netbeans.api.project.ProjectUtils; 33 import org.netbeans.api.project.TestUtil; 34 import org.netbeans.junit.NbTestCase; 35 import org.netbeans.modules.ruby.modules.project.rake.Util; 36 import org.netbeans.modules.ruby.spi.project.AuxiliaryConfiguration; 37 import org.netbeans.modules.ruby.spi.project.CacheDirectoryProvider; 38 import org.openide.filesystems.FileLock; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.openide.util.Mutex; 42 import org.openide.xml.XMLUtil; 43 import org.w3c.dom.Document ; 44 import org.w3c.dom.Element ; 45 import org.w3c.dom.Node ; 46 import org.w3c.dom.NodeList ; 47 48 63 64 68 public class RakeProjectHelperTest extends NbTestCase { 69 70 74 public RakeProjectHelperTest(String name) { 75 super(name); 76 } 77 78 private FileObject scratch; 79 private FileObject projdir; 80 private ProjectManager pm; 81 private Project p; 82 private RakeProjectHelper h; 83 private RakeBasedTestUtil.TestListener l; 84 85 protected void setUp() throws Exception { 86 super.setUp(); 87 scratch = TestUtil.makeScratchDir(this); 88 projdir = scratch.createFolder("proj"); 89 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/project.xml"), projdir, "nbproject/project.xml"); 90 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/private.xml"), projdir, "nbproject/private/private.xml"); 91 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/project.properties"), projdir, "nbproject/project.properties"); 92 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/private.properties"), projdir, "nbproject/private/private.properties"); 93 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/global.properties"), scratch, "userdir/build.properties"); 94 TestUtil.setLookup(new Object [] { 95 RakeBasedTestUtil.testRakeBasedProjectType(), 96 }); 97 pm = ProjectManager.getDefault(); 98 p = pm.findProject(projdir); 99 h = p.getLookup().lookup(RakeProjectHelper.class); 100 l = new RakeBasedTestUtil.TestListener(); 101 } 102 103 107 public void testBasicRecognition() throws Exception { 108 assertNotNull("recognized the project", p); 109 assertEquals("correct project directory", projdir, p.getProjectDirectory()); 110 assertEquals("found something in project lookup", "hello", p.getLookup().lookup(String .class)); 111 } 112 113 117 public void testGetPrimaryConfigurationData() throws Exception { 118 assertNotNull("Had helper in lookup", h); 119 Element data = h.getPrimaryConfigurationData(true); 120 assertEquals("correct element name", "data", data.getLocalName()); 121 assertEquals("correct element namespace", "urn:test:shared", data.getNamespaceURI()); 122 Element stuff = Util.findElement(data, "shared-stuff", "urn:test:shared"); 123 assertNotNull("had nested stuff in it", stuff); 124 data = h.getPrimaryConfigurationData(false); 125 assertEquals("correct element name", "data", data.getLocalName()); 126 assertEquals("correct element namespace", "urn:test:private", data.getNamespaceURI()); 127 stuff = Util.findElement(data, "private-stuff", "urn:test:private"); 128 assertNotNull("had nested stuff in it", stuff); 129 } 130 131 135 public void testBrokenPrimaryConfigurationData() throws Exception { 136 TestUtil.createFileFromContent(null, projdir, RakeProjectHelper.PROJECT_XML_PATH); 138 RakeProjectHelper.QUIETLY_SWALLOW_XML_LOAD_ERRORS = true; 139 Element data; 140 try { 141 data = h.getPrimaryConfigurationData(true); 142 } finally { 143 RakeProjectHelper.QUIETLY_SWALLOW_XML_LOAD_ERRORS = false; 144 } 145 assertEquals("correct element name", "data", data.getLocalName()); 146 assertEquals("correct element namespace", "urn:test:shared", data.getNamespaceURI()); 147 Element stuff = Util.findElement(data, "shared-stuff", "urn:test:shared"); 148 assertNull("had no stuff in it", stuff); 149 data = XMLUtil.createDocument("whatever", "urn:test:shared", null, null).createElementNS("urn:test:shared", "data"); 151 data.appendChild(data.getOwnerDocument().createElementNS("urn:test:shared", "details")); 152 h.putPrimaryConfigurationData(data, true); 153 pm.saveProject(p); 154 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 155 Element root = doc.getDocumentElement(); 156 Element type = Util.findElement(root, "type", RakeProjectHelper.PROJECT_NS); 157 assertEquals("correct restored type", "test", Util.findText(type)); 158 Element config = Util.findElement(root, "configuration", RakeProjectHelper.PROJECT_NS); 159 assertNotNull("have <configuration>", config); 160 data = Util.findElement(config, "data", "urn:test:shared"); 161 assertNotNull("have <data>", data); 162 Element details = Util.findElement(data, "details", "urn:test:shared"); 163 assertNotNull("have <details>", details); 164 } 165 166 170 public void testImmutabilityOfGottenConfigurationData() throws Exception { 171 Element data = h.getPrimaryConfigurationData(true); 172 assertNull("no parent for data", data.getParentNode()); 173 } 176 177 181 public void testGetProperties() throws Exception { 182 EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 183 assertNotNull("getProperties should never return null", ep); 184 assertEquals("three properties defined", 3, ep.size()); 185 assertEquals("shared.prop correct", "value1", ep.get("shared.prop")); 186 assertEquals("overridden.prop correct", "value3", ep.get("overridden.prop")); 187 ep = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH); 188 assertNotNull("getProperties should never return null", ep); 189 assertEquals("four properties defined", 4, ep.size()); 190 assertEquals("private.prop correct", "value2", ep.get("private.prop")); 191 assertEquals("overridden.prop correct", "value4", ep.get("overridden.prop")); 192 ep = h.getProperties("bogus/path.properties"); 193 assertNotNull("getProperties should never return null", ep); 194 assertEquals("no properties defined", 0, ep.size()); 195 } 196 197 201 public void testStandardPropertyEvaluator() throws Exception { 202 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 204 public Void run() throws Exception { 205 PropertyEvaluator pev = h.getStandardPropertyEvaluator(); 206 assertEquals("shared.prop correct", "value1", pev.getProperty("shared.prop")); 207 assertEquals("private.prop correct", "value2", pev.getProperty("private.prop")); 208 assertEquals("overridden.prop correct", "value4", pev.getProperty("overridden.prop")); 209 assertEquals("derived.prop correct", "value2:value1:${undefined.prop}", pev.getProperty("derived.prop")); 210 assertEquals("tempdir correct", System.getProperty("java.io.tmpdir") + "/foo", pev.getProperty("tempdir")); 211 assertEquals("global.prop correct", "value5", pev.getProperty("global.prop")); 212 assertEquals("does not have other defs", null, pev.getProperty("bogus.prop")); 213 Map m = pev.getProperties(); 214 assertEquals("shared.prop correct", "value1", m.get("shared.prop")); 215 assertEquals("private.prop correct", "value2", m.get("private.prop")); 216 assertEquals("overridden.prop correct", "value4", m.get("overridden.prop")); 217 assertEquals("derived.prop correct", "value2:value1:${undefined.prop}", m.get("derived.prop")); 218 assertEquals("tempdir correct", System.getProperty("java.io.tmpdir") + "/foo", m.get("tempdir")); 219 assertEquals("global.prop correct", "value5", m.get("global.prop")); 220 assertEquals("does not have other defs", null, m.get("bogus.prop")); 221 assertEquals("correct evaluateString", "value1:value2", 222 pev.evaluate("${shared.prop}:${private.prop}")); 223 RakeBasedTestUtil.TestPCL l = new RakeBasedTestUtil.TestPCL(); 225 pev.addPropertyChangeListener(l); 226 FileObject buildProperties = scratch.getFileObject("userdir/build.properties"); 227 assertNotNull("have build.properties", buildProperties); 228 FileLock lock = buildProperties.lock(); 229 OutputStream os = buildProperties.getOutputStream(lock); 230 Properties p = new Properties (); 231 p.setProperty("global.prop", "value5a"); 232 p.setProperty("global.prop.2", "globalvalue2"); 233 p.store(os, null); 234 os.close(); 235 lock.releaseLock(); 236 assertEquals("two properties fired", new HashSet <String >(Arrays.asList(new String [] {"global.prop", "global.prop.2"})), l.changed); 237 l.reset(); 238 assertEquals("global.prop is correct", "value5a", pev.getProperty("global.prop")); 239 assertEquals("global.prop.2 is correct", "globalvalue2", pev.getProperty("global.prop.2")); 240 FileObject projectProperties = projdir.getFileObject("nbproject/project.properties"); 242 assertNotNull("have project.properties", projectProperties); 243 lock = projectProperties.lock(); 244 os = projectProperties.getOutputStream(lock); 245 p = new Properties (); 246 p.setProperty("overridden.prop", "value3a"); p.setProperty("shared.prop", "value1a"); p.setProperty("derived.prop", "${private.prop}:${shared.prop}:${undefined.prop}"); p.store(os, null); 250 os.close(); 251 lock.releaseLock(); 252 assertEquals("two properties fired", new HashSet <String >(Arrays.asList(new String [] {"shared.prop", "derived.prop"})), l.changed); 253 l.reset(); 254 assertEquals("shared.prop is correct", "value1a", pev.getProperty("shared.prop")); 255 assertEquals("derived.prop correct", "value2:value1a:${undefined.prop}", pev.getProperty("derived.prop")); 256 FileObject privateProperties = projdir.getFileObject("nbproject/private/private.properties"); 257 assertNotNull("have private.properties", privateProperties); 258 lock = privateProperties.lock(); 259 os = privateProperties.getOutputStream(lock); 260 p = new Properties (); 261 p.setProperty("private.prop", "value2a"); p.setProperty("overridden.prop", "value4"); p.setProperty("tempdir", "${java.io.tmpdir}/foo"); p.setProperty("user.properties.file", "../userdir/build.properties"); p.store(os, null); 266 os.close(); 267 lock.releaseLock(); 268 assertEquals("two properties fired", new HashSet <String >(Arrays.asList(new String [] {"private.prop", "derived.prop"})), l.changed); 269 l.reset(); 270 assertEquals("private.prop is correct", "value2a", pev.getProperty("private.prop")); 271 assertEquals("derived.prop correct", "value2a:value1a:${undefined.prop}", pev.getProperty("derived.prop")); 272 projectProperties.delete(); 274 assertEquals("two properties fired", new HashSet <String >(Arrays.asList(new String [] {"shared.prop", "derived.prop"})), l.changed); 275 l.reset(); 276 assertEquals("shared.prop is gone", null, pev.getProperty("shared.prop")); 277 assertEquals("derived.prop is gone", null, pev.getProperty("derived.prop")); 278 projectProperties = projdir.getFileObject("nbproject").createData("project.properties"); 280 lock = projectProperties.lock(); 281 os = projectProperties.getOutputStream(lock); 282 p = new Properties (); 283 p.setProperty("derived.prop", "${private.prop}:${shared.prop}:${undefined.prop.2}"); p.store(os, null); 285 os.close(); 286 lock.releaseLock(); 287 assertEquals("one property fired", Collections.singleton("derived.prop"), l.changed); 288 l.reset(); 289 assertEquals("derived.prop is back", "value2a:${shared.prop}:${undefined.prop.2}", pev.getProperty("derived.prop")); 290 FileObject buildProperties2 = scratch.getFileObject("userdir").createData("build2.properties"); 292 lock = buildProperties2.lock(); 293 os = buildProperties2.getOutputStream(lock); 294 p = new Properties (); 295 p.setProperty("global.prop", "value5b"); p.setProperty("global.prop.2", "globalvalue2"); p.store(os, null); 298 os.close(); 299 lock.releaseLock(); 300 lock = privateProperties.lock(); 301 os = privateProperties.getOutputStream(lock); 302 p = new Properties (); 303 p.setProperty("private.prop", "value2a"); p.setProperty("overridden.prop", "value4"); p.setProperty("tempdir", "${java.io.tmpdir}/foo"); p.setProperty("user.properties.file", "../userdir/build2.properties"); p.store(os, null); 308 os.close(); 309 lock.releaseLock(); 310 assertEquals("two properties fired", new HashSet <String >(Arrays.asList(new String [] {"user.properties.file", "global.prop"})), l.changed); 311 l.reset(); 312 assertEquals("user.properties.file is correct", "../userdir/build2.properties", pev.getProperty("user.properties.file")); 313 assertEquals("global.prop is correct", "value5b", pev.getProperty("global.prop")); 314 lock = buildProperties2.lock(); 315 os = buildProperties2.getOutputStream(lock); 316 p = new Properties (); 317 p.setProperty("global.prop", "value5b"); p.store(os, null); 320 os.close(); 321 lock.releaseLock(); 322 assertEquals("one property fired", Collections.singleton("global.prop.2"), l.changed); 323 l.reset(); 324 assertEquals("global.prop.2 is gone", null, pev.getProperty("global.prop.2")); 325 return null; 327 } 328 }); 329 } 330 331 335 public void testResolveFile() throws Exception { 336 File scratchF = FileUtil.toFile(scratch); 338 assertNotNull("scratch directory exists on disk", scratchF); 339 File projdirF = FileUtil.toFile(projdir); 340 assertNotNull("project directory exists on disk", projdirF); 341 assertEquals(". resolves to project basedir", projdirF, h.resolveFile(".")); 342 assertEquals(". resolves to project basedir", projdir, h.resolveFileObject(".")); 343 assertEquals("simple relative path resolves", new File (projdirF, "foo"), h.resolveFile("foo")); 344 assertEquals("simple relative path resolves (but there is no such file object)", null, h.resolveFileObject("foo")); 345 assertEquals("Unix-style ./ resolves", new File (projdirF, "foo"), h.resolveFile("./foo")); 346 assertEquals("DOS-style .\\ resolves", new File (projdirF, "foo"), h.resolveFile(".\\foo")); 347 assertEquals("Unix-style ./ resolves (but there is no such file object)", null, h.resolveFileObject("./foo")); 348 assertEquals("DOS-style ./ resolves (but there is no such file object)", null, h.resolveFileObject(".\\foo")); 349 assertEquals(".. resolves up a dir", scratchF, h.resolveFile("..")); 350 assertEquals(".. resolves up a dir", scratch, h.resolveFileObject("..")); 351 assertEquals("Unix-style ../ resolves up and down", projdirF, h.resolveFile("../proj")); 352 assertEquals("DOS-style ..\\ resolves up and down", projdirF, h.resolveFile("..\\proj")); 353 assertEquals("Unix-style ../ resolves up and down", projdir, h.resolveFileObject("../proj")); 354 assertEquals("DOS-style ..\\ resolves up and down", projdir, h.resolveFileObject("..\\proj")); 355 assertEquals("absolute path is left alone", scratchF, h.resolveFile(scratchF.getAbsolutePath())); 356 assertEquals("absolute path is left alone", scratch, h.resolveFileObject(scratchF.getAbsolutePath())); 357 File somethingElseF = new File (scratchF.getParentFile(), "nonexistent-file-path"); 358 assertEquals("absolute (nonexistent) path is left alone", somethingElseF, h.resolveFile(somethingElseF.getAbsolutePath())); 359 assertEquals("absolute (nonexistent) path has no file object", null, h.resolveFileObject(somethingElseF.getAbsolutePath())); 360 } 361 362 368 public void testResolvePath() throws Exception { 369 File scratchF = FileUtil.toFile(scratch); 371 assertNotNull("scratch dir exists on disk", scratchF); 372 String scratchS = scratchF.getAbsolutePath(); 373 File projdirF = FileUtil.toFile(projdir); 374 assertNotNull("project dir exists on disk", projdirF); 375 String projdirS = projdirF.getAbsolutePath(); 376 assertEquals("empty path doesn't need to resolve", "", h.resolvePath("")); 377 assertEquals(". resolves", projdirS, h.resolvePath(".")); 378 assertEquals(".. resolves", scratchS, h.resolvePath("..")); 379 assertEquals("Unix-style ../ resolves", projdirS, h.resolvePath("../proj")); 380 assertEquals("DOS-style ..\\ resolves", projdirS, h.resolvePath("..\\proj")); 381 String longpath = projdirS + File.pathSeparator + scratchS + File.pathSeparator + projdirS + File.pathSeparator + projdirS; 382 assertEquals("mixed Unix-style path resolves", longpath, h.resolvePath(".:..:../proj:..\\proj")); 383 assertEquals("mixed DOS-style path resolves", longpath, h.resolvePath(".;..;../proj;..\\proj")); 384 assertEquals("absolute path resolves to itself", scratchS, h.resolvePath(scratchS)); 385 } 388 389 393 public void testPutProperties() throws Exception { 394 h.addRakeProjectListener(l); 395 EditableProperties ep = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 396 assertNotNull("getProperties should never return null", ep); 397 assertEquals("three properties defined", 3, ep.size()); 398 ep.put("testprop", "testval"); 399 assertTrue("uncommitted changes do not modify project", !pm.isModified(p)); 400 assertEquals("uncommitted changes not yet in project properties", null, h.getStandardPropertyEvaluator().getProperty("testprop")); 401 assertEquals("uncommitted changes fire no events", 0, l.events().length); 402 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, ep); 403 assertTrue("committed changes do modify project", pm.isModified(p)); 404 RakeProjectEvent[] evs = l.events(); 405 assertEquals("putProperties fires one event", 1, evs.length); 406 assertEquals("correct helper", h, evs[0].getHelper()); 407 assertEquals("correct path", RakeProjectHelper.PROJECT_PROPERTIES_PATH, evs[0].getPath()); 408 assertTrue("expected change", evs[0].isExpected()); 409 assertEquals("committed changes are in project properties", "testval", h.getStandardPropertyEvaluator().getProperty("testprop")); 410 Properties props = RakeBasedTestUtil.slurpProperties(h, RakeProjectHelper.PROJECT_PROPERTIES_PATH); 411 assertNotNull("project.properties already exists", props); 412 assertEquals("project.properties does not yet contain testprop", null, props.getProperty("testprop")); 413 pm.saveProject(p); 414 assertTrue("project is now saved", !pm.isModified(p)); 415 assertEquals("saving changes fires no new events", 0, l.events().length); 416 assertEquals("committed & saved changes are in project properties", "testval", h.getStandardPropertyEvaluator().getProperty("testprop")); 417 props = RakeBasedTestUtil.slurpProperties(h, RakeProjectHelper.PROJECT_PROPERTIES_PATH); 418 assertNotNull("project.properties still exists", props); 419 assertEquals("project.properties now contains testprop", "testval", props.getProperty("testprop")); 420 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 422 public Void run() throws Exception { 423 TestUtil.createFileFromContent(null, h.getProjectDirectory(), RakeProjectHelper.PROJECT_PROPERTIES_PATH); 424 return null; 425 } 426 }); 427 evs = l.events(); 428 assertEquals("touching project.properties fires one event", 1, evs.length); 429 assertEquals("correct helper", h, evs[0].getHelper()); 430 assertEquals("correct path", RakeProjectHelper.PROJECT_PROPERTIES_PATH, evs[0].getPath()); 431 assertFalse("unexpected change", evs[0].isExpected()); 432 assertEquals("empty file now", Collections.EMPTY_MAP, h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH)); 433 } 438 439 443 public void testPutPrimaryConfigurationData() throws Exception { 444 h.addRakeProjectListener(l); 445 Element data = h.getPrimaryConfigurationData(true); 446 assertNotNull("<shared-stuff/> is there to start", Util.findElement(data, "shared-stuff", "urn:test:shared")); 447 assertTrue("project is not initially modified", !pm.isModified(p)); 448 assertEquals("gPCD fires no events", 0, l.events().length); 449 assertNotNull("config data has an owner document", data.getOwnerDocument()); 450 Element nue = data.getOwnerDocument().createElementNS("urn:test:shared", "misc"); 451 data.appendChild(nue); 452 assertTrue("project is not modified after uncommitted change", !pm.isModified(p)); 453 assertEquals("no events fired after uncommitted change", 0, l.events().length); 454 assertEquals("after uncommitted change gPCD does not yet have new <misc/>", null, Util.findElement(h.getPrimaryConfigurationData(true), "misc", "urn:test:shared")); 455 h.putPrimaryConfigurationData(data, true); 456 assertTrue("project is modified after committed change", pm.isModified(p)); 457 RakeProjectEvent[] evs = l.events(); 458 assertEquals("putPCD fires one event", 1, evs.length); 459 assertEquals("correct helper", h, evs[0].getHelper()); 460 assertEquals("correct path", RakeProjectHelper.PROJECT_XML_PATH, evs[0].getPath()); 461 assertTrue("expected change", evs[0].isExpected()); 462 nue = Util.findElement(h.getPrimaryConfigurationData(true), "misc", "urn:test:shared"); 463 assertNotNull("after committed change gPCD has new <misc/>", nue); 464 assertEquals("new element name is correct", "misc", nue.getLocalName()); 465 assertEquals("new element namespace is correct", "urn:test:shared", nue.getNamespaceURI()); 466 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 467 Element configuration = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 468 assertNotNull("still has <configuration> on disk", configuration); 469 data = Util.findElement(configuration, "data", "urn:test:shared"); 470 assertNotNull("still has <data> on disk", data); 471 nue = Util.findElement(data, "misc", "urn:test:shared"); 472 assertEquals("<misc/> not yet on disk", null, nue); 473 pm.saveProject(p); 474 assertTrue("project is not modified after save", !pm.isModified(p)); 475 assertEquals("saving changes fires no new events", 0, l.events().length); 476 nue = Util.findElement(h.getPrimaryConfigurationData(true), "misc", "urn:test:shared"); 477 assertNotNull("after save gPCD still has new <misc/>", nue); 478 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 479 configuration = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 480 assertNotNull("still has <configuration> on disk", configuration); 481 data = Util.findElement(configuration, "data", "urn:test:shared"); 482 assertNotNull("still has <data> on disk", data); 483 nue = Util.findElement(data, "misc", "urn:test:shared"); 484 assertNotNull("<misc/> now on disk", nue); 485 ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void >() { 487 public Void run() throws Exception { 488 TestUtil.createFileFromContent(RakeProjectHelperTest.class.getResource("data/project-modified.xml"), projdir, RakeProjectHelper.PROJECT_XML_PATH); 489 return null; 490 } 491 }); 492 evs = l.events(); 493 assertEquals("writing project.xml on disk fires one event", 1, evs.length); 494 assertEquals("correct helper", h, evs[0].getHelper()); 495 assertEquals("correct path", RakeProjectHelper.PROJECT_XML_PATH, evs[0].getPath()); 496 assertFalse("unexpected change", evs[0].isExpected()); 497 assertEquals("correct new display name", "Some New Name", ProjectUtils.getInformation(p).getDisplayName()); 498 data = h.getPrimaryConfigurationData(true); 499 Element stuff = Util.findElement(data, "other-shared-stuff", "urn:test:shared"); 500 assertNotNull("have <other-shared-stuff/> now", stuff); 501 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 502 data = aux.getConfigurationFragment("data", "urn:test:shared-aux", true); 503 assertNotNull("have aux <data>", data); 504 stuff = Util.findElement(data, "other-aux-shared-stuff", "urn:test:shared-aux"); 505 assertNotNull("have <other-aux-shared-stuff/> now", stuff); 506 } 511 512 516 public void testExtensibleMetadataProviderImpl() throws Exception { 517 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 518 assertNotNull("AuxiliaryConfiguration present", aux); 519 CacheDirectoryProvider cdp = p.getLookup().lookup(CacheDirectoryProvider.class); 520 assertNotNull("CacheDirectoryProvider present", cdp); 521 FileObject cache = cdp.getCacheDirectory(); 523 assertNotNull("has a cache dir", cache); 524 assertTrue("cache dir is a folder", cache.isFolder()); 525 assertEquals("cache dir is empty", Collections.EMPTY_LIST, Arrays.asList(cache.getChildren())); 526 cache.createData("foo"); 527 cache = cdp.getCacheDirectory(); 528 assertEquals("cache contents still there", 1, cache.getChildren().length); 529 h.addRakeProjectListener(l); 531 Element data = aux.getConfigurationFragment("data", "urn:test:shared-aux", true); 532 assertNotNull("found shared <data>", data); 533 assertEquals("correct name", "data", data.getLocalName()); 534 assertEquals("correct namespace", "urn:test:shared-aux", data.getNamespaceURI()); 535 Element stuff = Util.findElement(data, "aux-shared-stuff", "urn:test:shared-aux"); 536 assertNotNull("found <aux-shared-stuff/>", stuff); 537 assertEquals("gCF fires no changes", 0, l.events().length); 538 stuff.setAttribute("attr", "val"); 540 assertFalse("project not modified by local change", pm.isModified(p)); 541 aux.putConfigurationFragment(data, true); 542 assertTrue("now project is modified", pm.isModified(p)); 543 RakeProjectEvent[] evs = l.events(); 544 assertEquals("pCF fires one event", 1, evs.length); 545 assertEquals("correct helper", h, evs[0].getHelper()); 546 assertEquals("correct path", RakeProjectHelper.PROJECT_XML_PATH, evs[0].getPath()); 547 assertTrue("expected change", evs[0].isExpected()); 548 pm.saveProject(p); 549 assertEquals("saving project fires no new changes", 0, l.events().length); 550 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 551 Element config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 552 assertNotNull("<configuration> still exists", config); 553 data = Util.findElement(config, "data", "urn:test:shared-aux"); 554 assertNotNull("<data> still exists", data); 555 stuff = Util.findElement(data, "aux-shared-stuff", "urn:test:shared-aux"); 556 assertNotNull("still have <aux-shared-stuff/>", stuff); 557 assertEquals("attr written correctly", "val", stuff.getAttribute("attr")); 558 data = aux.getConfigurationFragment("data", "urn:test:private-aux", false); 560 assertNotNull("found shared <data>", data); 561 assertEquals("correct name", "data", data.getLocalName()); 562 assertEquals("correct namespace", "urn:test:private-aux", data.getNamespaceURI()); 563 stuff = Util.findElement(data, "aux-private-stuff", "urn:test:private-aux"); 564 assertNotNull("found <aux-private-stuff/>", stuff); 565 assertEquals("gCF fires no changes", 0, l.events().length); 566 stuff.setAttribute("attr", "val"); 568 assertFalse("project not modified by local change", pm.isModified(p)); 569 aux.putConfigurationFragment(data, false); 570 assertTrue("now project is modified", pm.isModified(p)); 571 evs = l.events(); 572 assertEquals("pCF fires one event", 1, evs.length); 573 assertEquals("correct helper", h, evs[0].getHelper()); 574 assertEquals("correct path", RakeProjectHelper.PRIVATE_XML_PATH, evs[0].getPath()); 575 assertTrue("expected change", evs[0].isExpected()); 576 pm.saveProject(p); 577 assertEquals("saving project fires no new changes", 0, l.events().length); 578 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PRIVATE_XML_PATH); 579 config = doc.getDocumentElement(); 580 data = Util.findElement(config, "data", "urn:test:private-aux"); 581 assertNotNull("<data> still exists", data); 582 stuff = Util.findElement(data, "aux-private-stuff", "urn:test:private-aux"); 583 assertNotNull("still have <aux-private-stuff/>", stuff); 584 assertEquals("attr written correctly", "val", stuff.getAttribute("attr")); 585 Element bogus = aux.getConfigurationFragment("doesn't exist", "bogus", true); 587 assertNull("no such fragment - wrong name/ns", bogus); 588 bogus = aux.getConfigurationFragment("data", "bogus", true); 589 assertNull("no such fragment - wrong ns", bogus); 590 bogus = aux.getConfigurationFragment("doesn't exist", "urn:test:shared-aux", true); 591 assertNull("no such fragment - wrong name", bogus); 592 bogus = aux.getConfigurationFragment("data", "urn:test:shared-aux", false); 593 assertNull("no such fragment - wrong file", bogus); 594 Document temp = XMLUtil.createDocument("whatever", null, null, null); 596 data = temp.createElementNS("urn:test:whatever", "hello"); 597 data.appendChild(temp.createTextNode("stuff")); 598 assertFalse("project currently unmodified", pm.isModified(p)); 599 aux.putConfigurationFragment(data, true); 600 assertTrue("adding frag modified project", pm.isModified(p)); 601 evs = l.events(); 602 assertEquals("pCF fires one event", 1, evs.length); 603 assertEquals("correct path", RakeProjectHelper.PROJECT_XML_PATH, evs[0].getPath()); 604 pm.saveProject(p); 605 assertEquals("saving project fires no new changes", 0, l.events().length); 606 data = aux.getConfigurationFragment("hello", "urn:test:whatever", true); 607 assertNotNull("can retrieve new frag", data); 608 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 609 config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 610 assertNotNull("<configuration> still exists", config); 611 data = Util.findElement(config, "hello", "urn:test:whatever"); 612 assertNotNull("<hello> still exists", data); 613 assertEquals("correct nested contents too", "stuff", Util.findText(data)); 614 assertFalse("project is unmodified", pm.isModified(p)); 616 assertTrue("can remove new frag", aux.removeConfigurationFragment("hello", "urn:test:whatever", true)); 617 assertTrue("project is now modified", pm.isModified(p)); 618 assertNull("now frag is gone", aux.getConfigurationFragment("hello", "urn:test:whatever", true)); 619 pm.saveProject(p); 620 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 621 config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 622 assertNotNull("<configuration> still exists", config); 623 data = Util.findElement(config, "hello", "urn:test:whatever"); 624 assertNull("now <hello> is gone", data); 625 assertFalse("cannot remove a frag that is not there", aux.removeConfigurationFragment("hello", "urn:test:whatever", true)); 626 assertFalse("trying to remove a nonexistent frag does not modify project", pm.isModified(p)); 627 628 data = temp.createElementNS("namespace", "ccc"); 630 aux.putConfigurationFragment(data, true); 631 data = temp.createElementNS("namespace", "bbb"); 632 aux.putConfigurationFragment(data, true); 633 data = temp.createElementNS("namespace", "aaa"); 634 aux.putConfigurationFragment(data, true); 635 data = temp.createElementNS("namespace-1", "bbb"); 636 aux.putConfigurationFragment(data, true); 637 data = temp.createElementNS("name-sp", "bbb"); 638 aux.putConfigurationFragment(data, true); 639 data = temp.createElementNS("namespace", "aaaa"); 640 aux.putConfigurationFragment(data, true); 641 pm.saveProject(p); 642 doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 643 config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 644 String [] names = new String []{"aaa-namespace", "aaaa-namespace", "bbb-name-sp", "bbb-namespace", "bbb-namespace-1", "ccc-namespace", "data-urn:test:shared", "data-urn:test:shared-aux"}; 645 int count = 0; 646 NodeList list = config.getChildNodes(); 647 for (int i=0; i<list.getLength(); i++) { 648 Node n = list.item(i); 649 if (n.getNodeType() != Node.ELEMENT_NODE) { 650 continue; 651 } 652 assertEquals(names[count], n.getNodeName()+"-"+n.getNamespaceURI()); 653 count++; 654 } 655 assertEquals("Elements count does not match", names.length, count); 656 657 } 661 662 public void test68872() throws Exception { 663 AuxiliaryConfiguration aux = p.getLookup().lookup(AuxiliaryConfiguration.class); 664 assertNotNull("AuxiliaryConfiguration present", aux); 665 666 Element data = aux.getConfigurationFragment("data", "urn:test:private-aux", false); 667 Element stuff = Util.findElement(data, "aux-private-stuff", "urn:test:private-aux"); 668 assertNotNull("found <aux-private-stuff/>", stuff); 669 stuff.setAttribute("attr", "val"); 671 aux.putConfigurationFragment(data, false); 672 assertTrue("now project is modified", pm.isModified(p)); 673 674 FileObject privateXMLFO = p.getProjectDirectory().getFileObject("nbproject/private/private.xml"); 675 676 assertNotNull(privateXMLFO); 677 678 File privateXML = FileUtil.toFile(privateXMLFO); 679 680 privateXML.delete(); 681 682 privateXMLFO.refresh(); 683 privateXMLFO.getParent().refresh(); 684 685 pm.saveProject(p); 686 687 assertTrue(privateXML.exists()); 689 690 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PRIVATE_XML_PATH); 692 Element config = doc.getDocumentElement(); 693 data = Util.findElement(config, "data", "urn:test:private-aux"); 694 assertNotNull("<data> still exists", data); 695 stuff = Util.findElement(data, "aux-private-stuff", "urn:test:private-aux"); 696 assertNotNull("still have <aux-private-stuff/>", stuff); 697 assertEquals("attr written correctly", "val", stuff.getAttribute("attr")); 698 699 privateXML.delete(); 701 702 privateXMLFO.refresh(); 703 privateXMLFO.getParent().refresh(); 704 705 assertNull(aux.getConfigurationFragment("data", "urn:test:private-aux", false)); 706 } 707 708 public void testCreatePropertyProvider() throws Exception { 709 PropertyProvider pp = h.getPropertyProvider(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 710 Map defs = pp.getProperties(); 711 assertEquals("correct number of defs", 3, defs.size()); 712 assertEquals("correct value", "value1", defs.get("shared.prop")); 713 RakeBasedTestUtil.TestCL l = new RakeBasedTestUtil.TestCL(); 715 pp.addChangeListener(l); 716 EditableProperties p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 717 p.setProperty("foo", "bar"); 718 assertFalse("no events from uncommitted changes", l.expect()); 719 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p); 720 assertTrue("got a change from setting a property", l.expect()); 721 defs = pp.getProperties(); 722 assertEquals("correct new size", 4, defs.size()); 723 assertEquals("correct new value", "bar", defs.get("foo")); 724 p = p.cloneProperties(); 726 p.setProperty("foo", "bar2"); 727 p.setProperty("foo", "bar"); 728 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p); 729 assertFalse("no events from no-op changes", l.expect()); 730 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, null); 732 assertTrue("got a change from removing a property file", l.expect()); 733 assertEquals("now have no definitions", Collections.EMPTY_MAP, pp.getProperties()); 734 String path = "foo.properties"; 736 pp = h.getPropertyProvider(path); 737 pp.addChangeListener(l); 738 assertEquals("no defs initially", Collections.EMPTY_MAP, pp.getProperties()); 739 assertNull("no file made yet", h.getProjectDirectory().getFileObject(path)); 740 p = new EditableProperties(); 741 p.setProperty("one", "1"); 742 p.setProperty("two", "2"); 743 h.putProperties(path, p); 744 assertTrue("making the file fired a change", l.expect()); 745 defs = pp.getProperties(); 746 assertEquals("two defs", 2, defs.size()); 747 assertEquals("right value #1", "1", defs.get("one")); 748 assertEquals("right value #2", "2", defs.get("two")); 749 assertNull("no file yet saved to disk", h.getProjectDirectory().getFileObject(path)); 750 p.setProperty("three", "3"); 751 assertFalse("no events from uncomm. change", l.expect()); 752 h.putProperties(path, p); 753 assertTrue("now have changed new file", l.expect()); 754 defs = pp.getProperties(); 755 assertEquals("three defs", 3, defs.size()); 756 } 759 760 792 793 } 794 | Popular Tags |