KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > ReferenceHelperTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.spi.project.support.ant;
21
22 import java.io.File JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.Set JavaDoc;
31 import java.util.TreeSet JavaDoc;
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 JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52 import org.w3c.dom.NodeList JavaDoc;
53
54 /* XXX tests needed
55  * - testAddArtifactCollocation
56  * check that collocated foreign projects update project.properties
57  * while noncollocated foreign projects update private.properties
58  * and that absolute artifact locns -> private.properties
59  * also addForeignFileReference(File,String) on plain files needs to
60  * check collocation
61  * - testUniquifyProjectNames
62  * check that foreign project names are uniquified correctly
63  * both in addReference and addForeignFileReference etc.
64  * - testVcsFriendliness
65  * check that refs are added in a predictable order
66  */

67
68 /**
69  * Test functionality of ReferenceHelper.
70  * @author Jesse Glick
71  */

72 public class ReferenceHelperTest extends NbTestCase {
73
74     /**
75      * Create the test suite.
76      * @param name suite name
77      */

78     public ReferenceHelperTest(String JavaDoc name) {
79         super(name);
80     }
81     
82     /** Scratch directory. */
83     private FileObject scratch;
84     /** Directory of master project (proj). */
85     private FileObject projdir;
86     /**
87      * Directory of a collocated sister project (proj2).
88      * Has artifacts build.jar=dist/proj2.jar and
89      * build.javadoc=build/javadoc as well as
90      * build.javadoc.complete=build/complete-javadoc.
91      */

92     private FileObject sisterprojdir;
93     /**
94      * The same structure as sisterprojdir but in different folder.
95      * Useful for testing that referenceIDs are uniquified.
96      */

97     private FileObject sisterprojdir2;
98     /**
99      * Directory of a noncollocated project (proj3).
100      * Has artifact build.jar=d i s t/p r o j 3.jar.
101      */

102     private FileObject sepprojdir;
103     /** The project manager singleton. */
104     private ProjectManager pm;
105     /** The master project. */
106     private Project p;
107     /** The master project's helper. */
108     private AntProjectHelper h;
109     /** The collocated sister project's helper. */
110     private AntProjectHelper sisterh;
111     /** The collocated sister2 project's helper. */
112     private AntProjectHelper sisterh2;
113     /** The noncollocated project's helper. */
114     private AntProjectHelper seph;
115     /** The master project's reference helper. */
116     private ReferenceHelper r;
117     //private AntBasedTestUtil.TestListener l;
118
private PropertyEvaluator pev;
119     
120     private static void setCodeNameOfTestProject(AntProjectHelper helper, String JavaDoc name) {
121         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
122         Element JavaDoc 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 JavaDoc {
129         super.setUp();
130         Object JavaDoc[] instances = new Object JavaDoc[] {
131             AntBasedTestUtil.testAntBasedProjectType(),
132             AntBasedTestUtil.testCollocationQueryImplementation(getWorkDir()),
133         };
134         ClassLoader JavaDoc 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 JavaDoc[] {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         // Probably unnecessary: pm.saveProject(pm.findProject(sisterprojdir));
161

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         // If following assert fails then there is a global impl of collocation
175
// query which says that these two files are collocated. Hidden it
176
// similarly as it is done for SampleCVSCollocationQueryImpl
177
// in META-INF/services.
178
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         //l = new AntBasedTestUtil.TestListener();
186

187         pev = h.getStandardPropertyEvaluator();
188     }
189     
190     protected void tearDown() throws Exception JavaDoc {
191         scratch = null;
192         projdir = null;
193         sisterprojdir = null;
194         sisterh = null;
195         //sepprojdir = null;
196
pm = null;
197         p = null;
198         h = null;
199         //l = null;
200
TestUtil.setLookup(Lookup.EMPTY);
201         super.tearDown();
202     }
203
204     /**
205      * Check that the raw add, get, and remove calls work.
206      * @throws Exception in case of unexpected failures
207      */

208     @SuppressWarnings JavaDoc("deprecation")
209     public void testRawReferenceManipulation() throws Exception JavaDoc {
210         assertEquals("starting with no raw references", Collections.EMPTY_LIST, Arrays.asList(r.getRawReferences()));
211         // Test simple adding of a reference.
212
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         // Nonexistent references are not returned.
226
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         // The reference is found now.
233
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         // Test removing it.
243
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         // Test adding several references.
249
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         // Test no-op adds and removes.
257
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         // Test modifications.
267
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         // More removals and adds.
291
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         // Check list of refs.
299
refs = r.getRawReferences();
300         assertEquals("back to three refs", 3, refs.length);
301         // NOTE on undocumented constraint: getRawReferences should sort results by proj then target
302
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         // Try saving and checking that project.xml is correct.
324
assertTrue("Project is still modified", pm.isModified(p));
325         pm.saveProject(p);
326         Document JavaDoc doc = AntBasedTestUtil.slurpXml(h, AntProjectHelper.PROJECT_XML_PATH);
327         Element JavaDoc config = Util.findElement(doc.getDocumentElement(), "configuration", AntProjectHelper.PROJECT_NS);
328         assertNotNull("have <configuration>", config);
329         Element JavaDoc references = Util.findElement(config, ReferenceHelper.REFS_NAME, ReferenceHelper.REFS_NS);
330         assertNotNull("have <references>", references);
331         NodeList JavaDoc nl = references.getElementsByTagNameNS(ReferenceHelper.REFS_NS, "reference");
332         assertEquals("have three <reference>s", 3, nl.getLength());
333         String JavaDoc[] elementNames = {
334             "foreign-project",
335             "artifact-type",
336             "script",
337             "target",
338             "clean-target",
339             "id",
340         };
341         String JavaDoc[][] 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 JavaDoc reference = (Element JavaDoc)nl.item(i);
369             for (int j = 0; j < 6; j++) {
370                 String JavaDoc elementName = elementNames[j];
371                 Element JavaDoc 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     /**
379      * Check that the adding and removing artifact objects updates everything it should.
380      * @throws Exception in case of unexpected failures
381      */

382     @SuppressWarnings JavaDoc("deprecation")
383     public void testAddRemoveArtifact() throws Exception JavaDoc {
384         // Add one artifact. Check that the raw reference is there.
385
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         // Check that the project properties are correct.
400
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 JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "proj2.jar"),
410             h.resolveFile(pev.getProperty("reference.proj2.dojar")));
411         // Check no-op adds.
412
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         // Try another artifact from the same project.
417
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 JavaDoc(new File JavaDoc(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         // Try modifying the second artifact in some way.
448
// Note that only changes in the type, clean target, and artifact path count as modifications.
449
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         // Check that changing the artifact location property changed the reference property too.
476
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 JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "build"), "complete-javadoc"),
486             h.resolveFile(pev.getProperty("reference.proj2.dojavadoc")));
487         // Check that changing the value of the artifact location property
488
// in the subproject modifies this project.
489
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 JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "build"), "total-javadoc"),
504             h.resolveFile(pev.getProperty("reference.proj2.dojavadoc")));
505         // Now try removing first ref. Should remove raw ref, ref property, but not project property.
506
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         // Try removing second ref. Should now remove project property.
529
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         // XXX check add ref not coming from project gives IAE
541

542         // test AA with multiple outputs:
543
AntArtifact aa = new AntBasedTestUtil.TestAntArtifact(new URI JavaDoc[]{new URI JavaDoc("dist/foo.jar"), new URI JavaDoc("dist/bar.jar")}, sisterh);
544         String JavaDoc ref1 = r.addReference(aa, aa.getArtifactLocations()[0]);
545         String JavaDoc ref2 = r.addReference(aa, aa.getArtifactLocations()[1]);
546         assertEquals("correct evaluated ref1",
547             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "foo.jar"),
548             h.resolveFile(pev.evaluate(ref1)));
549         assertEquals("correct evaluated ref2",
550             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "bar.jar"),
551             h.resolveFile(pev.evaluate(ref2)));
552         r.destroyReference(ref1);
553         assertEquals("correct evaluated ref2",
554             new File JavaDoc(new File JavaDoc(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 JavaDoc("deprecation")
564     public void testReferenceEscaping() throws Exception JavaDoc {
565         // check that artifact reference is correctly escaped. All dot characters
566
// in project name or artifact ID must be escaped, etc.
567
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 JavaDoc ref = referenceHelperProj4.addReference(art, art.getArtifactLocations()[0]);
574         assertEquals("Project reference was not correctly escaped", "${reference.pro-ject_4.do_jar}", ref);
575         
576         // test that it can be found
577
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         // test deletion
585
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 JavaDoc {
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 JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{"configuration", "empty"})), ref.getProperties().keySet());
609         assertEquals("correct property values", new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{"debug", ""})),
610                 new TreeSet JavaDoc<Object JavaDoc>(ref.getProperties().values()));
611     }
612
613     /**
614      * Check that the {@link SubprojectProvider} implementation behaves correctly.
615      * @throws Exception in case of unexpected failures
616      */

617     public void testSubprojectProviderImpl() throws Exception JavaDoc {
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 JavaDoc<? 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     /**
641      * Check that methods to add foreign file references really work.
642      * @throws Exception in case of unexpected failure
643      */

644     @SuppressWarnings JavaDoc("deprecation")
645     public void testForeignFileReferences() throws Exception JavaDoc {
646         // test collocated foreign project reference
647
File JavaDoc f = new File JavaDoc(new File JavaDoc(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 JavaDoc 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 JavaDoc f2 = new File JavaDoc(new File JavaDoc(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         // test non-collocated foreign project reference
681
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 JavaDoc 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         // test foreign file reference for collocated jar
704
FileObject collocatedLib = scratch.createFolder("j a r s").createData("m y l i b.jar");
705         f = FileUtil.toFile(collocatedLib);
706         String JavaDoc ref = r.createForeignFileReference(f, "jar");
707         String JavaDoc 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         // test foreign file reference for non-collocated jar
743
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 JavaDoc {
781         ReferenceHelper.RawReference ref = new ReferenceHelper.RawReference(
782             "proj2", "irrelevant", new URI JavaDoc("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 JavaDoc(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 JavaDoc("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 JavaDoc {
802         // test foreign file reference for non-collocated jar under extra base folder
803
FileObject nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars").createData("mylib.jar");
804         File JavaDoc 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 JavaDoc ref = r.createForeignFileReference(f, "jar");
810         assertEquals("foreign file reference created", "${file.reference.mylib.jar}", ref);
811         String JavaDoc 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         // test removal of extra base folder
815
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         // the same test as above but with extra base folder defined in PRIVATE props
821
nonCollocatedLib = scratch.createFolder("separate2").createFolder("jars").createData("mylib2.jar");
822         f = FileUtil.toFile(nonCollocatedLib);
823         props = h.getProperties(AntProjectHelper.PRIVATE_PROPERTIES_PATH);
824         String JavaDoc 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     /**
843      * Check that adding and removing artifact locations dynamically doesn't break anything.
844      * Cf. #55413 and #55423.
845      */

846     public void testDeletionOfArtifactLocations() throws Exception JavaDoc {
847         final URI JavaDoc[] 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 JavaDoc getType() {
859                 return "jar";
860             }
861             public String JavaDoc getTargetName() {
862                 return "build";
863             }
864             public String JavaDoc getCleanTargetName() {
865                 return "clean";
866             }
867             public File JavaDoc getScriptLocation() {
868                 return sisterh.resolveFile("build.xml");
869             }
870             public URI JavaDoc[] getArtifactLocations() {
871                 List JavaDoc<URI JavaDoc> locs = new ArrayList JavaDoc<URI JavaDoc>();
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 JavaDoc[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 JavaDoc e) {
889             // Expected.
890
}
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 JavaDoc e) {
897             // Expected.
898
}
899         // Make sure proj2 actually reports our special provider:
900
Project sisterproj = ProjectManager.getDefault().findProject(sisterprojdir);
901         sisterproj.getLookup().lookup(AntBasedTestUtil.AntArtifactProviderMutable.class).
902             setBuildArtifacts(new AntArtifact[] {art});
903         // Now check findArtifactAndLocation usage.
904
assertEquals("output1.jar found",
905                 Arrays.asList(new Object JavaDoc[] {art, locations[0]}),
906                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}")));
907         assertEquals("output3.jar found",
908                 Arrays.asList(new Object JavaDoc[] {art, locations[2]}),
909                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}")));
910         // Now add output2.jar to list and see that lookups work somehow.
911
includeLocations[1] = true;
912         assertEquals("output1.jar still there",
913                 Arrays.asList(new Object JavaDoc[] {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 JavaDoc[] {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 JavaDoc[] {art, locations[2]}),
920                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.2}")));
921         // Now *remove* some items and see what happens!
922
includeLocations[0] = false;
923         includeLocations[1] = false;
924         assertEquals("output3.jar now only referent",
925                 Arrays.asList(new Object JavaDoc[] {art, locations[2]}),
926                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}")));
927         assertEquals("second item no longer exists",
928                 Arrays.asList(new Object JavaDoc[] {null, null}),
929                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}")));
930         assertEquals("third item no longer exists",
931                 Arrays.asList(new Object JavaDoc[] {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 JavaDoc e) {
938             // Expected.
939
}
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 JavaDoc e) {
944             // Expected.
945
}
946         // Now destroy the references and make sure there are no issues.
947
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 JavaDoc[] keys, String JavaDoc[] values) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc {
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 JavaDoc[] {
992         "file.reference.x",
993     }, new String JavaDoc[] {
994         "src",
995     });
996     
997     writeProperties(privateProperties, new String JavaDoc[] {
998         "file.reference.x",
999     }, new String JavaDoc[] {
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 JavaDoc resolvedProperty = h.getStandardPropertyEvaluator().getProperty("file.reference.x");
1015    
1016    assertNotNull("the property can be resolved", resolvedProperty);
1017    
1018    File JavaDoc 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 JavaDoc { // #91760
1026
class AnArtifact extends AntArtifact {
1027            URI JavaDoc u;
1028            Properties JavaDoc p;
1029            public AnArtifact(URI JavaDoc u, Properties JavaDoc p) {
1030                this.u = u;
1031                this.p = p;
1032            }
1033            public String JavaDoc getType() {
1034                return "jar";
1035            }
1036            public File JavaDoc getScriptLocation() {
1037                return sisterh.resolveFile(getTargetName() + ".xml");
1038            }
1039            public String JavaDoc getTargetName() {
1040                return u.toString().replaceAll("\\.jar$", "");
1041            }
1042            public String JavaDoc getCleanTargetName() {
1043                return "clean";
1044            }
1045            @Override JavaDoc
1046            public URI JavaDoc[] getArtifactLocations() {
1047                return new URI JavaDoc[] {u};
1048            }
1049            @Override JavaDoc
1050            public Properties JavaDoc getProperties() {
1051                return p;
1052            }
1053
1054        }
1055        URI JavaDoc xJar = new URI JavaDoc(null, null, "x.jar", null);
1056        r.addReference(new AnArtifact(xJar, new Properties JavaDoc()), xJar);
1057        assertReferenceXmlFragment(ReferenceHelper.REFS_NS, "x.xml");
1058        Properties JavaDoc p = new Properties JavaDoc();
1059        p.setProperty("k", "v");
1060        URI JavaDoc yJar = new URI JavaDoc(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 JavaDoc zJar = new URI JavaDoc(null, null, "z.jar", null);
1064        r.addReference(new AnArtifact(zJar, new Properties JavaDoc()), zJar);
1065        assertReferenceXmlFragment(ReferenceHelper.REFS_NS2, "${project.proj2}/x.xml", "${project.proj2}/y.xml", "${project.proj2}/z.xml");
1066    }
1067    private void assertReferenceXmlFragment(String JavaDoc namespace, String JavaDoc... scriptLocations) {
1068        Element JavaDoc refs = p.getLookup().lookup(AuxiliaryConfiguration.class).getConfigurationFragment(ReferenceHelper.REFS_NAME, namespace, true);
1069        assertNotNull(refs);
1070        List JavaDoc<String JavaDoc> actualScriptLocations = new ArrayList JavaDoc<String JavaDoc>();
1071        for (Element JavaDoc ref : Util.findSubElements(refs)) {
1072            Element JavaDoc 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