1 19 20 package org.netbeans.modules.ruby.spi.project.support.rake; 21 22 import java.io.File ; 23 import java.io.OutputStream ; 24 import java.net.URI ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.List ; 29 import java.util.Set ; 30 import java.util.TreeSet ; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.api.project.ProjectManager; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.netbeans.api.project.TestUtil; 35 import org.netbeans.modules.ruby.api.project.rake.RakeArtifact; 36 import org.netbeans.modules.ruby.api.project.rake.RakeArtifactQuery; 37 import org.netbeans.api.queries.CollocationQuery; 38 import org.netbeans.junit.NbTestCase; 39 import org.netbeans.modules.ruby.modules.project.rake.Util; 40 import org.netbeans.modules.queries.AlwaysRelativeCollocationQuery; 41 import org.netbeans.modules.ruby.spi.project.SubprojectProvider; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 import org.openide.util.Lookup; 46 import org.openide.util.lookup.Lookups; 47 import org.openide.util.lookup.ProxyLookup; 48 import org.w3c.dom.Document ; 49 import org.w3c.dom.Element ; 50 import org.w3c.dom.NodeList ; 51 52 65 66 70 public class ReferenceHelperTest extends NbTestCase { 71 72 76 public ReferenceHelperTest(String name) { 77 super(name); 78 } 79 80 81 private FileObject scratch; 82 83 private FileObject projdir; 84 90 private FileObject sisterprojdir; 91 95 private FileObject sisterprojdir2; 96 100 private FileObject sepprojdir; 101 102 private ProjectManager pm; 103 104 private Project p; 105 106 private RakeProjectHelper h; 107 108 private RakeProjectHelper sisterh; 109 110 private RakeProjectHelper sisterh2; 111 112 private RakeProjectHelper seph; 113 114 private ReferenceHelper r; 115 private PropertyEvaluator pev; 117 118 private static void setCodeNameOfTestProject(RakeProjectHelper helper, String name) { 119 Element data = helper.getPrimaryConfigurationData(true); 120 Element nameEl = data.getOwnerDocument().createElementNS("urn:test:shared", "name"); 121 nameEl.appendChild(data.getOwnerDocument().createTextNode(name)); 122 data.appendChild(nameEl); 123 helper.putPrimaryConfigurationData(data, true); 124 } 125 126 protected void setUp() throws Exception { 127 super.setUp(); 128 Object [] instances = new Object [] { 129 RakeBasedTestUtil.testRakeBasedProjectType(), 130 RakeBasedTestUtil.testCollocationQueryImplementation(getWorkDir()), 131 }; 132 ClassLoader l = ReferenceHelper.class.getClassLoader(); 133 TestUtil.setLookup (new ProxyLookup (new Lookup[] { 134 Lookups.fixed(instances), 135 Lookups.singleton(l), 136 Lookups.exclude(Lookups.metaInfServices(l), new Class [] {AlwaysRelativeCollocationQuery.class}) 137 })); 138 scratch = TestUtil.makeScratchDir(this); 139 projdir = scratch.createFolder("proj"); 140 TestUtil.createFileFromContent(ReferenceHelperTest.class.getResource("data/project.xml"), projdir, "nbproject/project.xml"); 141 pm = ProjectManager.getDefault(); 142 p = pm.findProject(projdir); 143 assertNotNull("found project in " + projdir, p); 144 h = p.getLookup().lookup(RakeProjectHelper.class); 145 assertNotNull("found helper for " + p, h); 146 r = p.getLookup().lookup(ReferenceHelper.class); 147 assertNotNull("found ref helper for " + p, r); 148 sisterprojdir = FileUtil.createFolder(scratch, "proj2"); 149 assertTrue("projdir and sisterprojdir collocated", 150 CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sisterprojdir))); 151 sisterh = ProjectGenerator.createProject(sisterprojdir, "test"); 152 setCodeNameOfTestProject(sisterh, "proj2"); 153 EditableProperties props = sisterh.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 154 props.setProperty("build.jar", "dist/proj2.jar"); 155 props.setProperty("build.javadoc", "build/javadoc"); 156 props.setProperty("build.javadoc.complete", "build/complete-javadoc"); 157 sisterh.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 158 160 sisterprojdir2 = FileUtil.createFolder(scratch, "proj2-copy"); 161 assertTrue("projdir and sisterprojdir2 collocated", 162 CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sisterprojdir2))); 163 sisterh2 = ProjectGenerator.createProject(sisterprojdir2, "test"); 164 setCodeNameOfTestProject(sisterh2, "proj2"); 165 props = sisterh2.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 166 props.setProperty("build.jar", "dist/proj2.jar"); 167 props.setProperty("build.javadoc", "build/javadoc"); 168 props.setProperty("build.javadoc.complete", "build/complete-javadoc"); 169 sisterh2.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 170 171 sepprojdir = FileUtil.createFolder(scratch, "separate/proj3"); 172 assertFalse("" + projdir + " and " + sepprojdir + " cannot be collocated", 177 CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sepprojdir))); 178 seph = ProjectGenerator.createProject(sepprojdir, "test"); 179 setCodeNameOfTestProject(seph, "proj3"); 180 props = seph.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 181 props.setProperty("build.jar", "d i s t/p r o j 3.jar"); 182 seph.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 183 185 pev = h.getStandardPropertyEvaluator(); 186 } 187 188 protected void tearDown() throws Exception { 189 scratch = null; 190 projdir = null; 191 sisterprojdir = null; 192 sisterh = null; 193 pm = null; 195 p = null; 196 h = null; 197 TestUtil.setLookup(Lookup.EMPTY); 199 super.tearDown(); 200 } 201 202 206 @SuppressWarnings ("deprecation") 207 public void testRawReferenceManipulation() throws Exception { 208 assertEquals("starting with no raw references", Collections.EMPTY_LIST, Arrays.asList(r.getRawReferences())); 209 ReferenceHelper.RawReference ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID"); 211 assertTrue("successfully added a raw ref to otherproj.dojar", r.addRawReference(ref)); 212 assertNull("project.properties not changed", pev.getProperty("project.otherproj")); 213 assertTrue("project is modified", pm.isModified(p)); 214 ref = r.getRawReference("otherproj", "dojarID"); 215 assertNotNull("found otherproj.dojar", ref); 216 assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName()); 217 assertEquals("correct artifact type", "jar", ref.getArtifactType()); 218 assertEquals("correct script location", URI.create("build.xml"), ref.getScriptLocation()); 219 assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue()); 220 assertEquals("correct target name", "dojar", ref.getTargetName()); 221 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 222 assertEquals("correct ID name", "dojarID", ref.getID()); 223 ref = r.getRawReference("otherproj2", "dojarID"); 225 assertNull("no such ref otherproj2.dojar", ref); 226 ref = r.getRawReference("otherproj", "dojar2"); 227 assertNull("no such ref otherproj.dojar2", ref); 228 ref = r.getRawReference("otherproj2", "dojar2"); 229 assertNull("no such ref otherproj2.dojar2", ref); 230 ReferenceHelper.RawReference[] refs = r.getRawReferences(); 232 assertEquals("one reference here", 1, refs.length); 233 ref = refs[0]; 234 assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName()); 235 assertEquals("correct artifact type", "jar", ref.getArtifactType()); 236 assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue()); 237 assertEquals("correct target name", "dojar", ref.getTargetName()); 238 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 239 assertEquals("correct ID name", "dojarID", ref.getID()); 240 assertTrue("successfully removed otherproj.dojar", r.removeRawReference("otherproj", "dojarID")); 242 refs = r.getRawReferences(); 243 assertEquals("no references here", 0, refs.length); 244 ref = r.getRawReference("otherproj", "dojar"); 245 assertNull("otherproj.dojar is gone", ref); 246 ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID"); 248 assertTrue("added ref to otherproj.dojar", r.addRawReference(ref)); 249 ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar2", "clean", "dojar2ID"); 250 assertTrue("added ref to otherproj.dojar2", r.addRawReference(ref)); 251 ref = new ReferenceHelper.RawReference("otherproj2", "ear", URI.create("build.xml"), "dojar", "clean", "dojarID"); 252 assertTrue("added ref to otherproj2.dojar", r.addRawReference(ref)); 253 assertEquals("have three refs", 3, r.getRawReferences().length); 254 pm.saveProject(p); 256 assertFalse("project is saved", pm.isModified(p)); 257 ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID"); 258 assertFalse("already had ref to otherproj.dojar", r.addRawReference(ref)); 259 assertFalse("project is not modified by no-op add", pm.isModified(p)); 260 assertEquals("still have three refs", 3, r.getRawReferences().length); 261 assertFalse("did not have ref to foo.bar", r.removeRawReference("foo", "bar")); 262 assertFalse("project is not modified by no-op remove", pm.isModified(p)); 263 assertEquals("still have three refs", 3, r.getRawReferences().length); 264 ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build.xml"), "dojar", "clean", "dojarID"); 266 assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref)); 267 assertTrue("project is modified by changed ref", pm.isModified(p)); 268 assertEquals("still have three refs", 3, r.getRawReferences().length); 269 ref = r.getRawReference("otherproj", "dojarID"); 270 assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName()); 271 assertEquals("correct modified artifact type", "war", ref.getArtifactType()); 272 assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue()); 273 assertEquals("correct target name", "dojar", ref.getTargetName()); 274 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 275 assertEquals("correct ID name", "dojarID", ref.getID()); 276 ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build2.xml"), "dojar", "clean", "dojarID"); 277 assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref)); 278 ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build2.xml"), "dojar", "clean2", "dojarID"); 279 assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref)); 280 ref = r.getRawReference("otherproj", "dojarID"); 281 assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName()); 282 assertEquals("correct modified artifact type", "war", ref.getArtifactType()); 283 assertEquals("correct script location", "${project.otherproj}/build2.xml", ref.getScriptLocationValue()); 284 assertEquals("correct target name", "dojar", ref.getTargetName()); 285 assertEquals("correct clean target name", "clean2", ref.getCleanTargetName()); 286 assertEquals("correct ID name", "dojarID", ref.getID()); 287 assertEquals("still have three refs", 3, r.getRawReferences().length); 288 assertTrue("now removing otherproj.dojar2", r.removeRawReference("otherproj", "dojar2ID")); 290 assertNull("otherproj.dojar2 is gone", r.getRawReference("otherproj", "dojar2ID")); 291 assertNotNull("otherproj.jar is still there", r.getRawReference("otherproj", "dojarID")); 292 assertNotNull("otherproj2.dojar is still there", r.getRawReference("otherproj2", "dojarID")); 293 assertEquals("down to two refs", 2, r.getRawReferences().length); 294 ref = new ReferenceHelper.RawReference("aardvark", "jar", URI.create("build.xml"), "jar", "clean", "jarID"); 295 assertTrue("added ref to aardvark.jar", r.addRawReference(ref)); 296 refs = r.getRawReferences(); 298 assertEquals("back to three refs", 3, refs.length); 299 ref = refs[0]; 301 assertEquals("correct foreign project name", "aardvark", ref.getForeignProjectName()); 302 assertEquals("correct modified artifact type", "jar", ref.getArtifactType()); 303 assertEquals("correct script location", "${project.aardvark}/build.xml", ref.getScriptLocationValue()); 304 assertEquals("correct target name", "jar", ref.getTargetName()); 305 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 306 assertEquals("correct ID name", "jarID", ref.getID()); 307 ref = refs[1]; 308 assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName()); 309 assertEquals("correct modified artifact type", "war", ref.getArtifactType()); 310 assertEquals("correct script location", "${project.otherproj}/build2.xml", ref.getScriptLocationValue()); 311 assertEquals("correct target name", "dojar", ref.getTargetName()); 312 assertEquals("correct clean target name", "clean2", ref.getCleanTargetName()); 313 assertEquals("correct ID name", "dojarID", ref.getID()); 314 ref = refs[2]; 315 assertEquals("correct foreign project name", "otherproj2", ref.getForeignProjectName()); 316 assertEquals("correct modified artifact type", "ear", ref.getArtifactType()); 317 assertEquals("correct script location", "${project.otherproj2}/build.xml", ref.getScriptLocationValue()); 318 assertEquals("correct target name", "dojar", ref.getTargetName()); 319 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 320 assertEquals("correct ID name", "dojarID", ref.getID()); 321 assertTrue("Project is still modified", pm.isModified(p)); 323 pm.saveProject(p); 324 Document doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH); 325 Element config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS); 326 assertNotNull("have <configuration>", config); 327 Element references = Util.findElement(config, ReferenceHelper.REFS_NAME, ReferenceHelper.REFS_NS); 328 assertNotNull("have <references>", references); 329 NodeList nl = references.getElementsByTagNameNS(ReferenceHelper.REFS_NS, "reference"); 330 assertEquals("have three <reference>s", 3, nl.getLength()); 331 String [] elementNames = { 332 "foreign-project", 333 "artifact-type", 334 "script", 335 "target", 336 "clean-target", 337 "id", 338 }; 339 String [][] values = { 340 { 341 "aardvark", 342 "jar", 343 "build.xml", 344 "jar", 345 "clean", 346 "jarID", 347 }, 348 { 349 "otherproj", 350 "war", 351 "build2.xml", 352 "dojar", 353 "clean2", 354 "dojarID", 355 }, 356 { 357 "otherproj2", 358 "ear", 359 "build.xml", 360 "dojar", 361 "clean", 362 "dojarID", 363 }, 364 }; 365 for (int i = 0; i < 3; i++) { 366 Element reference = (Element )nl.item(i); 367 for (int j = 0; j < 6; j++) { 368 String elementName = elementNames[j]; 369 Element element = Util.findElement(reference, elementName, ReferenceHelper.REFS_NS); 370 assertNotNull("had element " + elementName + " in ref #" + i, element); 371 assertEquals("correct text in " + elementName + " in ref #" + i, values[i][j], Util.findText(element)); 372 } 373 } 374 } 375 376 380 @SuppressWarnings ("deprecation") 381 public void testAddRemoveArtifact() throws Exception { 382 assertFalse("project not initially modified", pm.isModified(p)); 384 RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean"); 385 assertFalse("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 386 assertTrue("added a ref to proj2.dojar", r.addReference(art)); 387 assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 388 assertTrue("project now modified", pm.isModified(p)); 389 ReferenceHelper.RawReference[] refs = r.getRawReferences(); 390 assertEquals("one ref now", 1, refs.length); 391 ReferenceHelper.RawReference ref = refs[0]; 392 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 393 assertEquals("correct artifact type", "jar", ref.getArtifactType()); 394 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 395 assertEquals("correct target name", "dojar", ref.getTargetName()); 396 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 397 EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 399 assertEquals("correct ${project.proj2}", "../proj2", 400 props.getProperty("project.proj2")); 401 assertEquals("correct evaluated ${project.proj2}", 402 FileUtil.toFile(sisterprojdir), 403 h.resolveFile(pev.getProperty("project.proj2"))); 404 assertEquals("correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar", 405 props.getProperty("reference.proj2.dojar")); 406 assertEquals("correct evaluated ${reference.proj2.dojar}", 407 new File (new File (FileUtil.toFile(sisterprojdir), "dist"), "proj2.jar"), 408 h.resolveFile(pev.getProperty("reference.proj2.dojar"))); 409 pm.saveProject(p); 411 assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 412 assertFalse("no-op add", r.addReference(art)); 413 assertFalse("project not modified by no-op add", pm.isModified(p)); 414 art = sisterh.createSimpleRakeArtifact("javadoc", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean"); 416 assertFalse("reference does not exist", r.isReferenced(art, art.getArtifactLocations()[0])); 417 assertNotNull("added a ref to proj2.dojavadoc", r.addReference(art, art.getArtifactLocations()[0])); 418 assertTrue("project now modified", pm.isModified(p)); 419 refs = r.getRawReferences(); 420 assertEquals("two refs now", 2, refs.length); 421 ref = refs[0]; 422 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 423 assertEquals("correct target name", "dojar", ref.getTargetName()); 424 ref = refs[1]; 425 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 426 assertEquals("correct artifact type", "javadoc", ref.getArtifactType()); 427 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 428 assertEquals("correct target name", "dojavadoc", ref.getTargetName()); 429 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 430 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 431 assertEquals("correct ${project.proj2}", "../proj2", 432 props.getProperty("project.proj2")); 433 assertEquals("still correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar", 434 props.getProperty("reference.proj2.dojar")); 435 assertEquals("correct ${reference.proj2.dojavadoc}", 436 "${project.proj2}/build/javadoc", 437 props.getProperty("reference.proj2.dojavadoc")); 438 assertEquals("correct evaluated ${reference.proj2.dojavadoc}", 439 new File (new File (FileUtil.toFile(sisterprojdir), "build"), "javadoc"), 440 h.resolveFile(pev.getProperty("reference.proj2.dojavadoc"))); 441 pm.saveProject(p); 442 assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 443 r.addReference(art, art.getArtifactLocations()[0]); 444 assertFalse("project not modified by no-op add", pm.isModified(p)); 445 art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean"); 448 assertFalse("reference exist but needs to be updated", r.isReferenced(art, art.getArtifactLocations()[0])); 449 r.addReference(art, art.getArtifactLocations()[0]); 450 assertTrue("project modified by ref mod", pm.isModified(p)); 451 refs = r.getRawReferences(); 452 assertEquals("still two refs", 2, refs.length); 453 ref = refs[1]; 454 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 455 assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType()); 456 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 457 assertEquals("correct target name", "dojavadoc", ref.getTargetName()); 458 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 459 art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "realclean"); 460 r.addReference(art, art.getArtifactLocations()[0]); 461 pm.saveProject(p); 462 art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc.complete", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "realclean"); 463 r.addReference(art, art.getArtifactLocations()[0]); 464 assertTrue("project modified by ref mod", pm.isModified(p)); 465 refs = r.getRawReferences(); 466 assertEquals("still two refs", 2, refs.length); 467 ref = refs[1]; 468 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 469 assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType()); 470 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 471 assertEquals("correct target name", "dojavadoc", ref.getTargetName()); 472 assertEquals("correct modified clean target name", "realclean", ref.getCleanTargetName()); 473 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 475 assertEquals("correct ${project.proj2}", "../proj2", 476 props.getProperty("project.proj2")); 477 assertEquals("still correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar", 478 props.getProperty("reference.proj2.dojar")); 479 assertEquals("correct ${reference.proj2.dojavadoc}", 480 "${project.proj2}/build/complete-javadoc", 481 props.getProperty("reference.proj2.dojavadoc")); 482 assertEquals("correct evaluated ${reference.proj2.dojavadoc}", 483 new File (new File (FileUtil.toFile(sisterprojdir), "build"), "complete-javadoc"), 484 h.resolveFile(pev.getProperty("reference.proj2.dojavadoc"))); 485 pm.saveProject(p); 488 assertTrue("reference exist but needs to be updated", r.isReferenced(art, art.getArtifactLocations()[0])); 489 r.addReference(art, art.getArtifactLocations()[0]); 490 assertFalse("project not modified by no-op add", pm.isModified(p)); 491 props = sisterh.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 492 props.setProperty("build.javadoc.complete", "build/total-javadoc"); 493 sisterh.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 494 r.addReference(art, art.getArtifactLocations()[0]); 495 assertTrue("project modified by new ${reference.proj2.dojavadoc}", pm.isModified(p)); 496 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 497 assertEquals("correct ${reference.proj2.dojavadoc}", 498 "${project.proj2}/build/total-javadoc", 499 props.getProperty("reference.proj2.dojavadoc")); 500 assertEquals("correct evaluated ${reference.proj2.dojavadoc}", 501 new File (new File (FileUtil.toFile(sisterprojdir), "build"), "total-javadoc"), 502 h.resolveFile(pev.getProperty("reference.proj2.dojavadoc"))); 503 pm.saveProject(p); 505 assertTrue("remove proj2.dojar succeeded", r.destroyReference("${reference.proj2.dojar}")); 506 assertTrue("remove ref modified project", pm.isModified(p)); 507 refs = r.getRawReferences(); 508 assertEquals("now have just one ref", 1, refs.length); 509 ref = refs[0]; 510 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 511 assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType()); 512 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 513 assertEquals("correct target name", "dojavadoc", ref.getTargetName()); 514 assertEquals("correct modified clean target name", "realclean", ref.getCleanTargetName()); 515 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 516 assertEquals("correct ${project.proj2}", "../proj2", 517 props.getProperty("project.proj2")); 518 assertEquals("no more ${reference.proj2.dojar}", null, 519 props.getProperty("reference.proj2.dojar")); 520 assertEquals("still correct ${reference.proj2.dojavadoc}", 521 "${project.proj2}/build/total-javadoc", 522 props.getProperty("reference.proj2.dojavadoc")); 523 pm.saveProject(p); 524 assertFalse("no-op remove proj2.dojar failed", r.destroyReference("${reference.proj2.dojar}")); 525 assertFalse("no-op remove did not modify project", pm.isModified(p)); 526 assertTrue("remove proj2.dojavadoc succeeded", r.destroyReference("${reference.proj2.dojavadoc}")); 528 assertTrue("remove ref modified project", pm.isModified(p)); 529 refs = r.getRawReferences(); 530 assertEquals("now have no refs", 0, refs.length); 531 props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 532 assertEquals("no more ${project.proj2}", null, 533 props.getProperty("project.proj2")); 534 assertEquals("no more ${reference.proj2.dojar}", null, 535 props.getProperty("reference.proj2.dojar")); 536 assertEquals("no more ${reference.proj2.dojavadoc}", null, 537 props.getProperty("reference.proj2.dojavadoc")); 538 540 RakeArtifact aa = new RakeBasedTestUtil.TestRakeArtifact(new URI []{new URI ("dist/foo.jar"), new URI ("dist/bar.jar")}, sisterh); 542 String ref1 = r.addReference(aa, aa.getArtifactLocations()[0]); 543 String ref2 = r.addReference(aa, aa.getArtifactLocations()[1]); 544 assertEquals("correct evaluated ref1", 545 new File (new File (FileUtil.toFile(sisterprojdir), "dist"), "foo.jar"), 546 h.resolveFile(pev.evaluate(ref1))); 547 assertEquals("correct evaluated ref2", 548 new File (new File (FileUtil.toFile(sisterprojdir), "dist"), "bar.jar"), 549 h.resolveFile(pev.evaluate(ref2))); 550 r.destroyReference(ref1); 551 assertEquals("correct evaluated ref2", 552 new File (new File (FileUtil.toFile(sisterprojdir), "dist"), "bar.jar"), 553 h.resolveFile(pev.evaluate(ref2))); 554 assertEquals("ref1 does not exist", ref1, pev.evaluate(ref1)); 555 r.destroyReference(ref2); 556 assertEquals("ref1 does not exist", ref1, pev.evaluate(ref1)); 557 assertEquals("ref2 does not exist", ref2, pev.evaluate(ref2)); 558 pm.saveProject(p); 559 } 560 561 @SuppressWarnings ("deprecation") 562 public void testReferenceEscaping() throws Exception { 563 FileObject proj4Folder = FileUtil.createFolder(scratch, "proj4"); 566 RakeProjectHelper proj4Helper = ProjectGenerator.createProject(proj4Folder, "test"); 567 setCodeNameOfTestProject(proj4Helper, "pro-ject.4"); 568 Project p = pm.findProject(projdir); 569 ReferenceHelper referenceHelperProj4 = p.getLookup().lookup(ReferenceHelper.class); 570 RakeArtifact art = proj4Helper.createSimpleRakeArtifact("jar", "build.jar", proj4Helper.getStandardPropertyEvaluator(), "do.jar", "clean"); 571 String ref = referenceHelperProj4.addReference(art, art.getArtifactLocations()[0]); 572 assertEquals("Project reference was not correctly escaped", "${reference.pro-ject_4.do_jar}", ref); 573 574 ReferenceHelper.RawReference rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", false); 576 assertNull("Cannot be found because it was escaped", rr); 577 rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", true); 578 assertNotNull("Created reference was not created", rr); 579 assertEquals("do.jar", rr.getID()); 580 assertEquals("pro-ject_4", rr.getForeignProjectName()); 581 582 referenceHelperProj4.destroyForeignFileReference(ref); 584 rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", true); 585 assertNull("Reference was not deleted", rr); 586 } 587 588 public void testArtifactProperties() throws Exception { 589 assertFalse("project not initially modified", pm.isModified(p)); 590 RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean"); 591 art.getProperties().setProperty("configuration", "debug"); 592 art.getProperties().setProperty("empty", ""); 593 assertFalse("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 594 assertEquals("added a ref to proj2.dojar", "${reference.proj2.dojar}", r.addReference(art, art.getArtifactLocations()[0])); 595 assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0])); 596 assertTrue("project now modified", pm.isModified(p)); 597 ProjectManager.getDefault().saveAllProjects(); 598 ReferenceHelper.RawReference[] refs = r.getRawReferences(); 599 assertEquals("one ref now", 1, refs.length); 600 ReferenceHelper.RawReference ref = refs[0]; 601 assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName()); 602 assertEquals("correct artifact type", "jar", ref.getArtifactType()); 603 assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue()); 604 assertEquals("correct target name", "dojar", ref.getTargetName()); 605 assertEquals("correct clean target name", "clean", ref.getCleanTargetName()); 606 assertEquals("correct property keys", new TreeSet <String >(Arrays.asList(new String []{"configuration", "empty"})), ref.getProperties().keySet()); 607 assertEquals("correct property values", new TreeSet <String >(Arrays.asList(new String []{"debug", ""})), 608 new TreeSet <Object >(ref.getProperties().values())); 609 } 610 611 615 public void testSubprojectProviderImpl() throws Exception { 616 RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean"); 617 assertNotNull("added a ref to proj2.dojar", r.addReference(art, art.getArtifactLocations()[0])); 618 art = sisterh.createSimpleRakeArtifact("javadoc", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean"); 619 assertNotNull("added a ref to proj2.dojavadoc", r.addReference(art, art.getArtifactLocations()[0])); 620 art = seph.createSimpleRakeArtifact("jar", "build.jar", seph.getStandardPropertyEvaluator(), "dojar", "clean"); 621 assertNotNull("added a ref to proj3.dojar", r.addReference(art, art.getArtifactLocations()[0])); 622 SubprojectProvider sp = r.createSubprojectProvider(); 623 Set <? extends Project> subprojs = sp.getSubprojects(); 624 assertEquals("two subprojects", 2, subprojs.size()); 625 Project[] subprojsA = subprojs.toArray(new Project[2]); 626 Project proj2, proj3; 627 if (ProjectUtils.getInformation(subprojsA[0]).getName().equals("proj2")) { 628 proj2 = subprojsA[0]; 629 proj3 = subprojsA[1]; 630 } else { 631 proj2 = subprojsA[1]; 632 proj3 = subprojsA[0]; 633 } 634 assertEquals("proj2 was found correctly", pm.findProject(sisterprojdir), proj2); 635 assertEquals("proj3 was found correctly", pm.findProject(sepprojdir), proj3); 636 } 637 638 642 @SuppressWarnings ("deprecation") 643 public void testForeignFileReferences() throws Exception { 644 File f = new File (new File (FileUtil.toFile(sisterprojdir), "dist"), "proj2.jar"); 646 assertEquals("can add a ref to an artifact", "${reference.proj2.dojar}", r.createForeignFileReference(f, "jar")); 647 assertEquals("creating reference second time must return already existing ID", "${reference.proj2.dojar}", r.createForeignFileReference(f, "jar")); 648 assertNotNull("ref added", r.getRawReference("proj2", "dojar")); 649 EditableProperties privateProps = h.getProperties (RakeProjectHelper.PRIVATE_PROPERTIES_PATH); 650 String refval = privateProps.getProperty("project.proj2"); 651 assertEquals("reference correctly stored into private.properties", FileUtil.toFile(sisterprojdir).getAbsolutePath(), refval); 652 EditableProperties projectProps = h.getProperties (RakeProjectHelper.PROJECT_PROPERTIES_PATH); 653 refval = projectProps.getProperty("project.proj2"); 654 assertEquals("reference correctly stored into project.properties", "../proj2", refval); 655 refval = pev.getProperty("reference.proj2.dojar"); 656 assertNotNull("reference correctly evaluated", refval); 657 assertEquals("reference correctly evaluated", f, h.resolveFile(refval)); 658 RakeArtifact art = r.getForeignFileReferenceAsArtifact("${reference.proj2.dojar}"); 659 assertNotNull("got the reference back", art); 660 assertEquals("correct project", sisterprojdir, art.getProject().getProjectDirectory()); 661 assertEquals("correct target name", "dojar", art.getTargetName()); 662 assertEquals("correct type", "jar", art.getType()); 663 assertEquals("correct artifact location", URI.create("dist/proj2.jar"), art.getArtifactLocations()[0]); 664 art = (RakeArtifact)r.findArtifactAndLocation("reference.proj2.dojar")[0]; 665 assertNull("bad format", art); 666 art = (RakeArtifact)r.findArtifactAndLocation("${reference.proj2.doojar}")[0]; 667 assertNull("wrong target name", art); 668 File f2 = new File (new File (FileUtil.toFile(sisterprojdir2), "dist"), "proj2.jar"); 669 assertEquals("reference ID must be unique", "${reference.proj2-1.dojar}", r.createForeignFileReference(f2, "jar")); 670 assertEquals("creating reference second time must return already existing ID", "${reference.proj2-1.dojar}", r.createForeignFileReference(f2, "jar")); 671 r.destroyForeignFileReference("${reference.proj2-1.dojar}"); 672 assertNull("ref removed", r.getRawReference("proj2-1", "dojar")); 673 r.destroyForeignFileReference("${reference.proj2.dojar}"); 674 assertNull("ref removed", r.getRawReference("proj2", "dojar")); 675 assertNull("project ref property removed", pev.getProperty("reference.proj2.dojar")); 676 assertEquals("no refs remaining", 0, r.getRawReferences().length); 677 678 FileObject nonCollocatedProjectLib = scratch.getFileObject("separate/proj3").createFolder("d i s t").createData("p r o j 3.jar"); 680 f = FileUtil.toFile(nonCollocatedProjectLib); 681 art = RakeArtifactQuery.findArtifactByID(pm.findProject(sepprojdir), "dojar"); 682 assertNotNull("have an artifact proj3.dojar", art); 683 assertEquals("can add a reference to a direct artifact", "${reference.proj3.dojar}", r.createForeignFileReference(art)); 684 assertEquals("creating reference second time must return already existing ID", "${reference.proj3.dojar}", r.createForeignFileReference(art)); 685 assertNotNull("ref added", r.getRawReference("proj3", "dojar")); 686 refval = pev.getProperty("reference.proj3.dojar"); 687 String val = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty("reference.proj3.dojar"); 688 assertEquals("reference was not correctly set", "${project.proj3}/d i s t/p r o j 3.jar", val); 689 assertEquals("reference correctly evaluated", f, h.resolveFile(refval)); 690 val = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty("project.proj3"); 691 assertEquals("reference correctly evaluated", FileUtil.toFile(sepprojdir).getAbsolutePath(), val); 692 art = (RakeArtifact)r.findArtifactAndLocation("${reference.proj3.dojar}")[0]; 693 assertNotNull("got the reference back", art); 694 assertEquals("correct project", sepprojdir, art.getProject().getProjectDirectory()); 695 assertEquals("correct target name", "dojar", art.getTargetName()); 696 r.destroyForeignFileReference("${reference.proj3.dojar}"); 697 assertNull("ref removed", r.getRawReference("proj3", "dojar")); 698 assertNull("project ref property removed", pev.getProperty("reference.proj3.dojar")); 699 assertEquals("no refs remaining", 0, r.getRawReferences().length); 700 701 FileObject collocatedLib = scratch.createFolder("j a r s").createData("m y l i b.jar"); 703 f = FileUtil.toFile(collocatedLib); 704 String ref = r.createForeignFileReference(f, "jar"); 705 String ref2 = r.createForeignFileReference(f, "jar"); 706 assertEquals("Duplicate reference created", ref, ref2); 707 assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar}", ref); 708 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 709 assertEquals("Reference was not correctly evaluated from project.properties", "../j a r s/m y l i b.jar", refval); 710 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 711 assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval); 712 713 assertEquals("Reference was not correctly evaluated", f, h.resolveFile(refval)); 714 collocatedLib = scratch.createFolder("jars2").createData("m y l i b.jar"); 715 f = FileUtil.toFile(collocatedLib); 716 ref = r.createForeignFileReference(f, "jar"); 717 ref2 = r.createForeignFileReference(f, "jar"); 718 assertEquals("Duplicate reference created", ref, ref2); 719 assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar-1}", ref); 720 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 721 assertEquals("Reference was not correctly evaluated form project.properties", "../jars2/m y l i b.jar", refval); 722 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 723 assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval); 724 collocatedLib = scratch.createFolder("jars3").createData("m y l i b.jar"); 725 f = FileUtil.toFile(collocatedLib); 726 ref = r.createForeignFileReference(f, "jar"); 727 ref2 = r.createForeignFileReference(f, "jar"); 728 assertEquals("Duplicate reference created", ref, ref2); 729 assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar-2}", ref); 730 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 731 assertEquals("Reference was not correctly evaluated from project.properties", "../jars3/m y l i b.jar", refval); 732 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 733 assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval); 734 735 assertTrue("Reference was not removed", r.destroyReference(ref)); 736 assertFalse("There should not be any reference", r.destroyReference(ref)); 737 refval = pev.evaluate(ref); 738 assertEquals("Reference was not removed", ref, refval); 739 740 FileObject nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars").createData("mylib2.jar"); 742 f = FileUtil.toFile(nonCollocatedLib); 743 ref = r.createForeignFileReference(f, "jar"); 744 ref2 = r.createForeignFileReference(f, "jar"); 745 assertEquals("Duplicate reference created", ref, ref2); 746 assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar}", ref); 747 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 748 assertNull ("Foreign file reference is stored into project.properties",refval); 749 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 750 assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval); 751 assertEquals("Reference was not correctly evaluated", f, h.resolveFile(refval)); 752 nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars2").createData("mylib2.jar"); 753 f = FileUtil.toFile(nonCollocatedLib); 754 ref = r.createForeignFileReference(f, "jar"); 755 ref2 = r.createForeignFileReference(f, "jar"); 756 assertEquals("Duplicate reference created", ref, ref2); 757 assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar-1}", ref); 758 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 759 assertNull ("Foreign file reference is stored into project.properties",refval); 760 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 761 assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval); 762 nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars3").createData("mylib2.jar"); 763 f = FileUtil.toFile(nonCollocatedLib); 764 ref = r.createForeignFileReference(f, "jar"); 765 ref2 = r.createForeignFileReference(f, "jar"); 766 assertEquals("Duplicate reference created", ref, ref2); 767 assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar-2}", ref); 768 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 769 assertNull ("Foreign file reference is stored into project.properties",refval); 770 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 771 assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval); 772 r.destroyForeignFileReference(ref); 773 refval = pev.evaluate(ref); 774 assertEquals("Reference was not removed", ref, refval); 775 776 } 777 778 public void testToRakeArtifact() throws Exception { 779 ReferenceHelper.RawReference ref = new ReferenceHelper.RawReference( 780 "proj2", "irrelevant", new URI ("also-irrelevant"), "dojar", "totally-irrelevant", "dojar"); 781 RakeArtifact art = ref.toRakeArtifact(r); 782 assertNull("${project.proj2} not set, will not be found", art); 783 EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 784 props.setProperty("project.proj2", "../proj2"); 785 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 786 art = ref.toRakeArtifact(r); 787 assertNotNull("now artifact will be found", art); 788 assertEquals("correct directory", sisterprojdir, art.getProject().getProjectDirectory()); 789 assertEquals("correct artifact location", URI.create("dist/proj2.jar"), art.getArtifactLocations()[0]); 790 assertEquals("correct script location", new File (FileUtil.toFile(sisterprojdir), "build.xml"), art.getScriptLocation()); 791 assertEquals("correct target name", "dojar", art.getTargetName()); 792 assertEquals("correct clean target name", "clean", art.getCleanTargetName()); 793 ref = new ReferenceHelper.RawReference( 794 "proj2", "irrelevant", new URI ("also-irrelevant"), "doojar", "totally-irrelevant", "doojar"); 795 art = ref.toRakeArtifact(r); 796 assertNull("wrong target name, will not be found", art); 797 } 798 799 public void testAddRemoveExtraBaseDirectory() throws Exception { 800 FileObject nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars").createData("mylib.jar"); 802 File f = FileUtil.toFile(nonCollocatedLib); 803 EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH); 804 props.setProperty("externalSourceRoot", "../separate"); 805 h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props); 806 r.addExtraBaseDirectory("externalSourceRoot"); 807 String ref = r.createForeignFileReference(f, "jar"); 808 assertEquals("foreign file reference created", "${file.reference.mylib.jar}", ref); 809 String refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 810 assertEquals("reference is using extra base folder", "${externalSourceRoot}/jars/mylib.jar", refval); 811 assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval))); 812 r.removeExtraBaseDirectory("externalSourceRoot"); 814 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 815 assertEquals("reference does not contain extra base folder", "../separate/jars/mylib.jar", refval); 816 assertEquals("reference is correctly evaluated", f, h.resolveFile(refval)); 817 818 nonCollocatedLib = scratch.createFolder("separate2").createFolder("jars").createData("mylib2.jar"); 820 f = FileUtil.toFile(nonCollocatedLib); 821 props = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH); 822 String absolutePath = FileUtil.toFile(scratch.getFileObject("separate2")).getAbsolutePath(); 823 props.setProperty("externalSourceRootAbsolute", absolutePath); 824 h.putProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH, props); 825 r.addExtraBaseDirectory("externalSourceRootAbsolute"); 826 ref = r.createForeignFileReference(f, "jar"); 827 assertEquals("foreign file reference created", "${file.reference.mylib2.jar}", ref); 828 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 829 assertEquals("reference is using extra base folder", "${externalSourceRootAbsolute}/jars/mylib2.jar", refval); 830 assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval))); 831 r.removeExtraBaseDirectory("externalSourceRootAbsolute"); 832 refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 833 assertNull("reference was removed from PROJECT_PROPERTIES_PATH", refval); 834 refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1)); 835 assertNotNull("reference was moved to PRIVATE_PROPERTIES_PATH", refval); 836 assertEquals("reference does not contain extra base folder", absolutePath+"/jars/mylib2.jar", refval); 837 assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval))); 838 } 839 840 844 public void testDeletionOfArtifactLocations() throws Exception { 845 final URI [] locations = { 846 URI.create("dist/output1.jar"), 847 URI.create("dist/output2.jar"), 848 URI.create("dist/output3.jar"), 849 }; 850 final boolean[] includeLocations = { 851 true, 852 false, 853 true, 854 }; 855 class MultiRakeArtifact extends RakeArtifact { 856 public String getType() { 857 return "jar"; 858 } 859 public String getTargetName() { 860 return "build"; 861 } 862 public String getCleanTargetName() { 863 return "clean"; 864 } 865 public File getScriptLocation() { 866 return sisterh.resolveFile("build.xml"); 867 } 868 public URI [] getArtifactLocations() { 869 List <URI > locs = new ArrayList <URI >(); 870 for (int i = 0; i < locations.length; i++) { 871 if (includeLocations[i]) { 872 locs.add(locations[i]); 873 } 874 } 875 return locs.toArray(new URI [locs.size()]); 876 } 877 } 878 RakeArtifact art = new MultiRakeArtifact(); 879 assertFalse("project not initially modified", pm.isModified(p)); 880 assertFalse("no refs yet", r.isReferenced(art, locations[0])); 881 assertEquals("added a ref to output1.jar", "${reference.proj2.build}", r.addReference(art, locations[0])); 882 assertEquals("added a ref to output3.jar", "${reference.proj2.build.1}", r.addReference(art, locations[2])); 883 try { 884 r.addReference(art, locations[1]); 885 fail("Should not be permitted to add ref to output2.jar yet"); 886 } catch (IllegalArgumentException e) { 887 } 889 assertTrue("output1.jar ref'd", r.isReferenced(art, locations[0])); 890 assertTrue("output3.jar ref'd", r.isReferenced(art, locations[2])); 891 try { 892 r.isReferenced(art, locations[1]); 893 fail("Should not be permitted to check ref to output2.jar yet"); 894 } catch (IllegalArgumentException e) { 895 } 897 Project sisterproj = ProjectManager.getDefault().findProject(sisterprojdir); 899 sisterproj.getLookup().lookup(RakeBasedTestUtil.RakeArtifactProviderMutable.class). 900 setBuildArtifacts(new RakeArtifact[] {art}); 901 assertEquals("output1.jar found", 903 Arrays.asList(new Object [] {art, locations[0]}), 904 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}"))); 905 assertEquals("output3.jar found", 906 Arrays.asList(new Object [] {art, locations[2]}), 907 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}"))); 908 includeLocations[1] = true; 910 assertEquals("output1.jar still there", 911 Arrays.asList(new Object [] {art, locations[0]}), 912 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}"))); 913 assertEquals("output2.jar now magically referenced instead of output3.jar (but oh well)", 914 Arrays.asList(new Object [] {art, locations[1]}), 915 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}"))); 916 assertEquals("output3.jar now magically referenced (even though we have no such property ourselves)", 917 Arrays.asList(new Object [] {art, locations[2]}), 918 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.2}"))); 919 includeLocations[0] = false; 921 includeLocations[1] = false; 922 assertEquals("output3.jar now only referent", 923 Arrays.asList(new Object [] {art, locations[2]}), 924 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}"))); 925 assertEquals("second item no longer exists", 926 Arrays.asList(new Object [] {null, null}), 927 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}"))); 928 assertEquals("third item no longer exists", 929 Arrays.asList(new Object [] {null, null}), 930 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.2}"))); 931 assertTrue("output3.jar ref'd", r.isReferenced(art, locations[2])); 932 try { 933 r.isReferenced(art, locations[0]); 934 fail("Should not be permitted to check ref to first item any more, oops"); 935 } catch (IllegalArgumentException e) { 936 } 938 try { 939 r.isReferenced(art, locations[1]); 940 fail("Should not be permitted to check ref to second item any more"); 941 } catch (IllegalArgumentException e) { 942 } 944 assertTrue("Can really get rid of output3.jar (even though it used to be output1.jar)", 946 r.destroyReference("${reference.proj2.build}")); 947 assertTrue("Can also get rid of what used to be output3.jar somehow - just the property at least", 948 r.destroyReference("${reference.proj2.build.1}")); 949 assertFalse("output3.jar no longer ref'd", r.isReferenced(art, locations[2])); 950 assertEquals("No raw references left", Collections.EMPTY_LIST, Arrays.asList(r.getRawReferences())); 951 assertEquals("No shared properties left", Collections.EMPTY_MAP, h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH)); 952 assertEquals("No private properties left", Collections.EMPTY_MAP, h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH)); 953 } 954 955 private void writeProperties(FileObject prop, String [] keys, String [] values) throws Exception { 956 EditableProperties p = new EditableProperties(); 957 958 for (int cntr = 0; cntr < keys.length; cntr++) { 959 p.setProperty(keys[cntr], values[cntr]); 960 } 961 962 FileLock lock = prop.lock(); 963 try { 964 OutputStream os = prop.getOutputStream(lock); 965 try { 966 p.store(os); 967 } finally { 968 os.close(); 969 } 970 } finally { 971 lock.releaseLock(); 972 } 973 } 974 975 public void testFixReferences() throws Exception { 976 FileObject originalProject = scratch.createFolder("orig-proj"); 977 FileObject originalSrcDir = originalProject.createFolder("src"); 978 979 FileObject testProject = scratch.createFolder("test-proj"); 980 FileObject nbprojectDir = testProject.createFolder("nbproject"); 981 982 TestUtil.createFileFromContent(ReferenceHelperTest.class.getResource("data/project.xml"), testProject, "nbproject/project.xml"); 983 984 FileObject publicProperties = nbprojectDir.createData("project.properties"); 985 FileObject privateDir = nbprojectDir.createFolder("private"); 986 FileObject privateProperties = privateDir.createData("private.properties"); 987 FileObject srcDir = testProject.createFolder("src"); 988 989 writeProperties(publicProperties, new String [] { 990 "file.reference.x", 991 }, new String [] { 992 "src", 993 }); 994 995 writeProperties(privateProperties, new String [] { 996 "file.reference.x", 997 }, new String [] { 998 FileUtil.toFile(originalSrcDir).getAbsolutePath(), 999 }); 1000 1001 Project nue = pm.findProject(testProject); 1002 assertNotNull("found project in " + testProject, nue); 1003 RakeProjectHelper h = nue.getLookup().lookup(RakeProjectHelper.class); 1004 assertNotNull("found helper for " + nue, h); 1005 ReferenceHelper r = nue.getLookup().lookup(ReferenceHelper.class); 1006 assertNotNull("found ref helper for " + p, r); 1007 1008 r.fixReferences(FileUtil.toFile(originalProject)); 1009 1010 pm.saveProject(nue); 1011 1012 String resolvedProperty = h.getStandardPropertyEvaluator().getProperty("file.reference.x"); 1013 1014 assertNotNull("the property can be resolved", resolvedProperty); 1015 1016 File resolvedFile = PropertyUtils.resolveFile(FileUtil.toFile(testProject), resolvedProperty); 1017 1018 assertNotNull("the file can be resolved", resolvedFile); 1019 1020 assertEquals("referencing correct file", FileUtil.toFile(srcDir).getAbsolutePath(), resolvedFile.getAbsolutePath()); 1021 } 1022 1023} 1024 | Popular Tags |