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