KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ruby > spi > project > support > rake > 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.modules.ruby.spi.project.support.rake;
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.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectManager;
33 import org.netbeans.api.project.ProjectUtils;
34 import org.netbeans.api.project.TestUtil;
35 import org.netbeans.modules.ruby.api.project.rake.RakeArtifact;
36 import org.netbeans.modules.ruby.api.project.rake.RakeArtifactQuery;
37 import org.netbeans.api.queries.CollocationQuery;
38 import org.netbeans.junit.NbTestCase;
39 import org.netbeans.modules.ruby.modules.project.rake.Util;
40 import org.netbeans.modules.queries.AlwaysRelativeCollocationQuery;
41 import org.netbeans.modules.ruby.spi.project.SubprojectProvider;
42 import org.openide.filesystems.FileLock;
43 import org.openide.filesystems.FileObject;
44 import org.openide.filesystems.FileUtil;
45 import org.openide.util.Lookup;
46 import org.openide.util.lookup.Lookups;
47 import org.openide.util.lookup.ProxyLookup;
48 import org.w3c.dom.Document JavaDoc;
49 import org.w3c.dom.Element JavaDoc;
50 import org.w3c.dom.NodeList JavaDoc;
51
52 /* XXX tests needed
53  * - testAddArtifactCollocation
54  * check that collocated foreign projects update project.properties
55  * while noncollocated foreign projects update private.properties
56  * and that absolute artifact locns -> private.properties
57  * also addForeignFileReference(File,String) on plain files needs to
58  * check collocation
59  * - testUniquifyProjectNames
60  * check that foreign project names are uniquified correctly
61  * both in addReference and addForeignFileReference etc.
62  * - testVcsFriendliness
63  * check that refs are added in a predictable order
64  */

65
66 /**
67  * Test functionality of ReferenceHelper.
68  * @author Jesse Glick
69  */

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

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

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

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

100     private FileObject sepprojdir;
101     /** The project manager singleton. */
102     private ProjectManager pm;
103     /** The master project. */
104     private Project p;
105     /** The master project's helper. */
106     private RakeProjectHelper h;
107     /** The collocated sister project's helper. */
108     private RakeProjectHelper sisterh;
109     /** The collocated sister2 project's helper. */
110     private RakeProjectHelper sisterh2;
111     /** The noncollocated project's helper. */
112     private RakeProjectHelper seph;
113     /** The master project's reference helper. */
114     private ReferenceHelper r;
115     //private RakeBasedTestUtil.TestListener l;
116
private PropertyEvaluator pev;
117     
118     private static void setCodeNameOfTestProject(RakeProjectHelper helper, String JavaDoc name) {
119         Element JavaDoc data = helper.getPrimaryConfigurationData(true);
120         Element JavaDoc nameEl = data.getOwnerDocument().createElementNS("urn:test:shared", "name");
121         nameEl.appendChild(data.getOwnerDocument().createTextNode(name));
122         data.appendChild(nameEl);
123         helper.putPrimaryConfigurationData(data, true);
124     }
125     
126     protected void setUp() throws Exception JavaDoc {
127         super.setUp();
128         Object JavaDoc[] instances = new Object JavaDoc[] {
129             RakeBasedTestUtil.testRakeBasedProjectType(),
130             RakeBasedTestUtil.testCollocationQueryImplementation(getWorkDir()),
131         };
132         ClassLoader JavaDoc l = ReferenceHelper.class.getClassLoader();
133         TestUtil.setLookup (new ProxyLookup (new Lookup[] {
134             Lookups.fixed(instances),
135             Lookups.singleton(l),
136             Lookups.exclude(Lookups.metaInfServices(l), new Class JavaDoc[] {AlwaysRelativeCollocationQuery.class})
137         }));
138         scratch = TestUtil.makeScratchDir(this);
139         projdir = scratch.createFolder("proj");
140         TestUtil.createFileFromContent(ReferenceHelperTest.class.getResource("data/project.xml"), projdir, "nbproject/project.xml");
141         pm = ProjectManager.getDefault();
142         p = pm.findProject(projdir);
143         assertNotNull("found project in " + projdir, p);
144         h = p.getLookup().lookup(RakeProjectHelper.class);
145         assertNotNull("found helper for " + p, h);
146         r = p.getLookup().lookup(ReferenceHelper.class);
147         assertNotNull("found ref helper for " + p, r);
148         sisterprojdir = FileUtil.createFolder(scratch, "proj2");
149         assertTrue("projdir and sisterprojdir collocated",
150             CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sisterprojdir)));
151         sisterh = ProjectGenerator.createProject(sisterprojdir, "test");
152         setCodeNameOfTestProject(sisterh, "proj2");
153         EditableProperties props = sisterh.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
154         props.setProperty("build.jar", "dist/proj2.jar");
155         props.setProperty("build.javadoc", "build/javadoc");
156         props.setProperty("build.javadoc.complete", "build/complete-javadoc");
157         sisterh.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
158         // Probably unnecessary: pm.saveProject(pm.findProject(sisterprojdir));
159

160         sisterprojdir2 = FileUtil.createFolder(scratch, "proj2-copy");
161         assertTrue("projdir and sisterprojdir2 collocated",
162             CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sisterprojdir2)));
163         sisterh2 = ProjectGenerator.createProject(sisterprojdir2, "test");
164         setCodeNameOfTestProject(sisterh2, "proj2");
165         props = sisterh2.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
166         props.setProperty("build.jar", "dist/proj2.jar");
167         props.setProperty("build.javadoc", "build/javadoc");
168         props.setProperty("build.javadoc.complete", "build/complete-javadoc");
169         sisterh2.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
170         
171         sepprojdir = FileUtil.createFolder(scratch, "separate/proj3");
172         // If following assert fails then there is a global impl of collocation
173
// query which says that these two files are collocated. Hidden it
174
// similarly as it is done for SampleCVSCollocationQueryImpl
175
// in META-INF/services.
176
assertFalse("" + projdir + " and " + sepprojdir + " cannot be collocated",
177             CollocationQuery.areCollocated(FileUtil.toFile(projdir), FileUtil.toFile(sepprojdir)));
178         seph = ProjectGenerator.createProject(sepprojdir, "test");
179         setCodeNameOfTestProject(seph, "proj3");
180         props = seph.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
181         props.setProperty("build.jar", "d i s t/p r o j 3.jar");
182         seph.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
183         //l = new RakeBasedTestUtil.TestListener();
184

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

206     @SuppressWarnings JavaDoc("deprecation")
207     public void testRawReferenceManipulation() throws Exception JavaDoc {
208         assertEquals("starting with no raw references", Collections.EMPTY_LIST, Arrays.asList(r.getRawReferences()));
209         // Test simple adding of a reference.
210
ReferenceHelper.RawReference ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID");
211         assertTrue("successfully added a raw ref to otherproj.dojar", r.addRawReference(ref));
212         assertNull("project.properties not changed", pev.getProperty("project.otherproj"));
213         assertTrue("project is modified", pm.isModified(p));
214         ref = r.getRawReference("otherproj", "dojarID");
215         assertNotNull("found otherproj.dojar", ref);
216         assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName());
217         assertEquals("correct artifact type", "jar", ref.getArtifactType());
218         assertEquals("correct script location", URI.create("build.xml"), ref.getScriptLocation());
219         assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue());
220         assertEquals("correct target name", "dojar", ref.getTargetName());
221         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
222         assertEquals("correct ID name", "dojarID", ref.getID());
223         // Nonexistent references are not returned.
224
ref = r.getRawReference("otherproj2", "dojarID");
225         assertNull("no such ref otherproj2.dojar", ref);
226         ref = r.getRawReference("otherproj", "dojar2");
227         assertNull("no such ref otherproj.dojar2", ref);
228         ref = r.getRawReference("otherproj2", "dojar2");
229         assertNull("no such ref otherproj2.dojar2", ref);
230         // The reference is found now.
231
ReferenceHelper.RawReference[] refs = r.getRawReferences();
232         assertEquals("one reference here", 1, refs.length);
233         ref = refs[0];
234         assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName());
235         assertEquals("correct artifact type", "jar", ref.getArtifactType());
236         assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue());
237         assertEquals("correct target name", "dojar", ref.getTargetName());
238         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
239         assertEquals("correct ID name", "dojarID", ref.getID());
240         // Test removing it.
241
assertTrue("successfully removed otherproj.dojar", r.removeRawReference("otherproj", "dojarID"));
242         refs = r.getRawReferences();
243         assertEquals("no references here", 0, refs.length);
244         ref = r.getRawReference("otherproj", "dojar");
245         assertNull("otherproj.dojar is gone", ref);
246         // Test adding several references.
247
ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID");
248         assertTrue("added ref to otherproj.dojar", r.addRawReference(ref));
249         ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar2", "clean", "dojar2ID");
250         assertTrue("added ref to otherproj.dojar2", r.addRawReference(ref));
251         ref = new ReferenceHelper.RawReference("otherproj2", "ear", URI.create("build.xml"), "dojar", "clean", "dojarID");
252         assertTrue("added ref to otherproj2.dojar", r.addRawReference(ref));
253         assertEquals("have three refs", 3, r.getRawReferences().length);
254         // Test no-op adds and removes.
255
pm.saveProject(p);
256         assertFalse("project is saved", pm.isModified(p));
257         ref = new ReferenceHelper.RawReference("otherproj", "jar", URI.create("build.xml"), "dojar", "clean", "dojarID");
258         assertFalse("already had ref to otherproj.dojar", r.addRawReference(ref));
259         assertFalse("project is not modified by no-op add", pm.isModified(p));
260         assertEquals("still have three refs", 3, r.getRawReferences().length);
261         assertFalse("did not have ref to foo.bar", r.removeRawReference("foo", "bar"));
262         assertFalse("project is not modified by no-op remove", pm.isModified(p));
263         assertEquals("still have three refs", 3, r.getRawReferences().length);
264         // Test modifications.
265
ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build.xml"), "dojar", "clean", "dojarID");
266         assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref));
267         assertTrue("project is modified by changed ref", pm.isModified(p));
268         assertEquals("still have three refs", 3, r.getRawReferences().length);
269         ref = r.getRawReference("otherproj", "dojarID");
270         assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName());
271         assertEquals("correct modified artifact type", "war", ref.getArtifactType());
272         assertEquals("correct script location", "${project.otherproj}/build.xml", ref.getScriptLocationValue());
273         assertEquals("correct target name", "dojar", ref.getTargetName());
274         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
275         assertEquals("correct ID name", "dojarID", ref.getID());
276         ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build2.xml"), "dojar", "clean", "dojarID");
277         assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref));
278         ref = new ReferenceHelper.RawReference("otherproj", "war", URI.create("build2.xml"), "dojar", "clean2", "dojarID");
279         assertTrue("modified ref to otherproj.dojar", r.addRawReference(ref));
280         ref = r.getRawReference("otherproj", "dojarID");
281         assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName());
282         assertEquals("correct modified artifact type", "war", ref.getArtifactType());
283         assertEquals("correct script location", "${project.otherproj}/build2.xml", ref.getScriptLocationValue());
284         assertEquals("correct target name", "dojar", ref.getTargetName());
285         assertEquals("correct clean target name", "clean2", ref.getCleanTargetName());
286         assertEquals("correct ID name", "dojarID", ref.getID());
287         assertEquals("still have three refs", 3, r.getRawReferences().length);
288         // More removals and adds.
289
assertTrue("now removing otherproj.dojar2", r.removeRawReference("otherproj", "dojar2ID"));
290         assertNull("otherproj.dojar2 is gone", r.getRawReference("otherproj", "dojar2ID"));
291         assertNotNull("otherproj.jar is still there", r.getRawReference("otherproj", "dojarID"));
292         assertNotNull("otherproj2.dojar is still there", r.getRawReference("otherproj2", "dojarID"));
293         assertEquals("down to two refs", 2, r.getRawReferences().length);
294         ref = new ReferenceHelper.RawReference("aardvark", "jar", URI.create("build.xml"), "jar", "clean", "jarID");
295         assertTrue("added ref to aardvark.jar", r.addRawReference(ref));
296         // Check list of refs.
297
refs = r.getRawReferences();
298         assertEquals("back to three refs", 3, refs.length);
299         // NOTE on undocumented constraint: getRawReferences should sort results by proj then target
300
ref = refs[0];
301         assertEquals("correct foreign project name", "aardvark", ref.getForeignProjectName());
302         assertEquals("correct modified artifact type", "jar", ref.getArtifactType());
303         assertEquals("correct script location", "${project.aardvark}/build.xml", ref.getScriptLocationValue());
304         assertEquals("correct target name", "jar", ref.getTargetName());
305         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
306         assertEquals("correct ID name", "jarID", ref.getID());
307         ref = refs[1];
308         assertEquals("correct foreign project name", "otherproj", ref.getForeignProjectName());
309         assertEquals("correct modified artifact type", "war", ref.getArtifactType());
310         assertEquals("correct script location", "${project.otherproj}/build2.xml", ref.getScriptLocationValue());
311         assertEquals("correct target name", "dojar", ref.getTargetName());
312         assertEquals("correct clean target name", "clean2", ref.getCleanTargetName());
313         assertEquals("correct ID name", "dojarID", ref.getID());
314         ref = refs[2];
315         assertEquals("correct foreign project name", "otherproj2", ref.getForeignProjectName());
316         assertEquals("correct modified artifact type", "ear", ref.getArtifactType());
317         assertEquals("correct script location", "${project.otherproj2}/build.xml", ref.getScriptLocationValue());
318         assertEquals("correct target name", "dojar", ref.getTargetName());
319         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
320         assertEquals("correct ID name", "dojarID", ref.getID());
321         // Try saving and checking that project.xml is correct.
322
assertTrue("Project is still modified", pm.isModified(p));
323         pm.saveProject(p);
324         Document JavaDoc doc = RakeBasedTestUtil.slurpXml(h, RakeProjectHelper.PROJECT_XML_PATH);
325         Element JavaDoc config = Util.findElement(doc.getDocumentElement(), "configuration", RakeProjectHelper.PROJECT_NS);
326         assertNotNull("have <configuration>", config);
327         Element JavaDoc references = Util.findElement(config, ReferenceHelper.REFS_NAME, ReferenceHelper.REFS_NS);
328         assertNotNull("have <references>", references);
329         NodeList JavaDoc nl = references.getElementsByTagNameNS(ReferenceHelper.REFS_NS, "reference");
330         assertEquals("have three <reference>s", 3, nl.getLength());
331         String JavaDoc[] elementNames = {
332             "foreign-project",
333             "artifact-type",
334             "script",
335             "target",
336             "clean-target",
337             "id",
338         };
339         String JavaDoc[][] values = {
340             {
341                 "aardvark",
342                 "jar",
343                 "build.xml",
344                 "jar",
345                 "clean",
346                 "jarID",
347             },
348             {
349                 "otherproj",
350                 "war",
351                 "build2.xml",
352                 "dojar",
353                 "clean2",
354                 "dojarID",
355             },
356             {
357                 "otherproj2",
358                 "ear",
359                 "build.xml",
360                 "dojar",
361                 "clean",
362                 "dojarID",
363             },
364         };
365         for (int i = 0; i < 3; i++) {
366             Element JavaDoc reference = (Element JavaDoc)nl.item(i);
367             for (int j = 0; j < 6; j++) {
368                 String JavaDoc elementName = elementNames[j];
369                 Element JavaDoc element = Util.findElement(reference, elementName, ReferenceHelper.REFS_NS);
370                 assertNotNull("had element " + elementName + " in ref #" + i, element);
371                 assertEquals("correct text in " + elementName + " in ref #" + i, values[i][j], Util.findText(element));
372             }
373         }
374     }
375     
376     /**
377      * Check that the adding and removing artifact objects updates everything it should.
378      * @throws Exception in case of unexpected failures
379      */

380     @SuppressWarnings JavaDoc("deprecation")
381     public void testAddRemoveArtifact() throws Exception JavaDoc {
382         // Add one artifact. Check that the raw reference is there.
383
assertFalse("project not initially modified", pm.isModified(p));
384         RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean");
385         assertFalse("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
386         assertTrue("added a ref to proj2.dojar", r.addReference(art));
387         assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
388         assertTrue("project now modified", pm.isModified(p));
389         ReferenceHelper.RawReference[] refs = r.getRawReferences();
390         assertEquals("one ref now", 1, refs.length);
391         ReferenceHelper.RawReference ref = refs[0];
392         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
393         assertEquals("correct artifact type", "jar", ref.getArtifactType());
394         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
395         assertEquals("correct target name", "dojar", ref.getTargetName());
396         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
397         // Check that the project properties are correct.
398
EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
399         assertEquals("correct ${project.proj2}", "../proj2",
400             props.getProperty("project.proj2"));
401         assertEquals("correct evaluated ${project.proj2}",
402             FileUtil.toFile(sisterprojdir),
403             h.resolveFile(pev.getProperty("project.proj2")));
404         assertEquals("correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar",
405             props.getProperty("reference.proj2.dojar"));
406         assertEquals("correct evaluated ${reference.proj2.dojar}",
407             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "proj2.jar"),
408             h.resolveFile(pev.getProperty("reference.proj2.dojar")));
409         // Check no-op adds.
410
pm.saveProject(p);
411         assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
412         assertFalse("no-op add", r.addReference(art));
413         assertFalse("project not modified by no-op add", pm.isModified(p));
414         // Try another artifact from the same project.
415
art = sisterh.createSimpleRakeArtifact("javadoc", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean");
416         assertFalse("reference does not exist", r.isReferenced(art, art.getArtifactLocations()[0]));
417         assertNotNull("added a ref to proj2.dojavadoc", r.addReference(art, art.getArtifactLocations()[0]));
418         assertTrue("project now modified", pm.isModified(p));
419         refs = r.getRawReferences();
420         assertEquals("two refs now", 2, refs.length);
421         ref = refs[0];
422         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
423         assertEquals("correct target name", "dojar", ref.getTargetName());
424         ref = refs[1];
425         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
426         assertEquals("correct artifact type", "javadoc", ref.getArtifactType());
427         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
428         assertEquals("correct target name", "dojavadoc", ref.getTargetName());
429         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
430         props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
431         assertEquals("correct ${project.proj2}", "../proj2",
432             props.getProperty("project.proj2"));
433         assertEquals("still correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar",
434             props.getProperty("reference.proj2.dojar"));
435         assertEquals("correct ${reference.proj2.dojavadoc}",
436             "${project.proj2}/build/javadoc",
437             props.getProperty("reference.proj2.dojavadoc"));
438         assertEquals("correct evaluated ${reference.proj2.dojavadoc}",
439             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "build"), "javadoc"),
440             h.resolveFile(pev.getProperty("reference.proj2.dojavadoc")));
441         pm.saveProject(p);
442         assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
443         r.addReference(art, art.getArtifactLocations()[0]);
444         assertFalse("project not modified by no-op add", pm.isModified(p));
445         // Try modifying the second artifact in some way.
446
// Note that only changes in the type, clean target, and artifact path count as modifications.
447
art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean");
448         assertFalse("reference exist but needs to be updated", r.isReferenced(art, art.getArtifactLocations()[0]));
449         r.addReference(art, art.getArtifactLocations()[0]);
450         assertTrue("project modified by ref mod", pm.isModified(p));
451         refs = r.getRawReferences();
452         assertEquals("still two refs", 2, refs.length);
453         ref = refs[1];
454         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
455         assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType());
456         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
457         assertEquals("correct target name", "dojavadoc", ref.getTargetName());
458         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
459         art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "realclean");
460         r.addReference(art, art.getArtifactLocations()[0]);
461         pm.saveProject(p);
462         art = sisterh.createSimpleRakeArtifact("javadoc.html", "build.javadoc.complete", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "realclean");
463         r.addReference(art, art.getArtifactLocations()[0]);
464         assertTrue("project modified by ref mod", pm.isModified(p));
465         refs = r.getRawReferences();
466         assertEquals("still two refs", 2, refs.length);
467         ref = refs[1];
468         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
469         assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType());
470         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
471         assertEquals("correct target name", "dojavadoc", ref.getTargetName());
472         assertEquals("correct modified clean target name", "realclean", ref.getCleanTargetName());
473         // Check that changing the artifact location property changed the reference property too.
474
props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
475         assertEquals("correct ${project.proj2}", "../proj2",
476             props.getProperty("project.proj2"));
477         assertEquals("still correct ${reference.proj2.dojar}", "${project.proj2}/dist/proj2.jar",
478             props.getProperty("reference.proj2.dojar"));
479         assertEquals("correct ${reference.proj2.dojavadoc}",
480             "${project.proj2}/build/complete-javadoc",
481             props.getProperty("reference.proj2.dojavadoc"));
482         assertEquals("correct evaluated ${reference.proj2.dojavadoc}",
483             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "build"), "complete-javadoc"),
484             h.resolveFile(pev.getProperty("reference.proj2.dojavadoc")));
485         // Check that changing the value of the artifact location property
486
// in the subproject modifies this project.
487
pm.saveProject(p);
488         assertTrue("reference exist but needs to be updated", r.isReferenced(art, art.getArtifactLocations()[0]));
489         r.addReference(art, art.getArtifactLocations()[0]);
490         assertFalse("project not modified by no-op add", pm.isModified(p));
491         props = sisterh.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
492         props.setProperty("build.javadoc.complete", "build/total-javadoc");
493         sisterh.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
494         r.addReference(art, art.getArtifactLocations()[0]);
495         assertTrue("project modified by new ${reference.proj2.dojavadoc}", pm.isModified(p));
496         props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
497         assertEquals("correct ${reference.proj2.dojavadoc}",
498             "${project.proj2}/build/total-javadoc",
499             props.getProperty("reference.proj2.dojavadoc"));
500         assertEquals("correct evaluated ${reference.proj2.dojavadoc}",
501             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "build"), "total-javadoc"),
502             h.resolveFile(pev.getProperty("reference.proj2.dojavadoc")));
503         // Now try removing first ref. Should remove raw ref, ref property, but not project property.
504
pm.saveProject(p);
505         assertTrue("remove proj2.dojar succeeded", r.destroyReference("${reference.proj2.dojar}"));
506         assertTrue("remove ref modified project", pm.isModified(p));
507         refs = r.getRawReferences();
508         assertEquals("now have just one ref", 1, refs.length);
509         ref = refs[0];
510         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
511         assertEquals("correct modified artifact type", "javadoc.html", ref.getArtifactType());
512         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
513         assertEquals("correct target name", "dojavadoc", ref.getTargetName());
514         assertEquals("correct modified clean target name", "realclean", ref.getCleanTargetName());
515         props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
516         assertEquals("correct ${project.proj2}", "../proj2",
517             props.getProperty("project.proj2"));
518         assertEquals("no more ${reference.proj2.dojar}", null,
519             props.getProperty("reference.proj2.dojar"));
520         assertEquals("still correct ${reference.proj2.dojavadoc}",
521             "${project.proj2}/build/total-javadoc",
522             props.getProperty("reference.proj2.dojavadoc"));
523         pm.saveProject(p);
524         assertFalse("no-op remove proj2.dojar failed", r.destroyReference("${reference.proj2.dojar}"));
525         assertFalse("no-op remove did not modify project", pm.isModified(p));
526         // Try removing second ref. Should now remove project property.
527
assertTrue("remove proj2.dojavadoc succeeded", r.destroyReference("${reference.proj2.dojavadoc}"));
528         assertTrue("remove ref modified project", pm.isModified(p));
529         refs = r.getRawReferences();
530         assertEquals("now have no refs", 0, refs.length);
531         props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
532         assertEquals("no more ${project.proj2}", null,
533             props.getProperty("project.proj2"));
534         assertEquals("no more ${reference.proj2.dojar}", null,
535             props.getProperty("reference.proj2.dojar"));
536         assertEquals("no more ${reference.proj2.dojavadoc}", null,
537             props.getProperty("reference.proj2.dojavadoc"));
538         // XXX check add ref not coming from project gives IAE
539

540         // test AA with multiple outputs:
541
RakeArtifact aa = new RakeBasedTestUtil.TestRakeArtifact(new URI JavaDoc[]{new URI JavaDoc("dist/foo.jar"), new URI JavaDoc("dist/bar.jar")}, sisterh);
542         String JavaDoc ref1 = r.addReference(aa, aa.getArtifactLocations()[0]);
543         String JavaDoc ref2 = r.addReference(aa, aa.getArtifactLocations()[1]);
544         assertEquals("correct evaluated ref1",
545             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "foo.jar"),
546             h.resolveFile(pev.evaluate(ref1)));
547         assertEquals("correct evaluated ref2",
548             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "bar.jar"),
549             h.resolveFile(pev.evaluate(ref2)));
550         r.destroyReference(ref1);
551         assertEquals("correct evaluated ref2",
552             new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "bar.jar"),
553             h.resolveFile(pev.evaluate(ref2)));
554         assertEquals("ref1 does not exist", ref1, pev.evaluate(ref1));
555         r.destroyReference(ref2);
556         assertEquals("ref1 does not exist", ref1, pev.evaluate(ref1));
557         assertEquals("ref2 does not exist", ref2, pev.evaluate(ref2));
558         pm.saveProject(p);
559     }
560     
561     @SuppressWarnings JavaDoc("deprecation")
562     public void testReferenceEscaping() throws Exception JavaDoc {
563         // check that artifact reference is correctly escaped. All dot characters
564
// in project name or artifact ID must be escaped, etc.
565
FileObject proj4Folder = FileUtil.createFolder(scratch, "proj4");
566         RakeProjectHelper proj4Helper = ProjectGenerator.createProject(proj4Folder, "test");
567         setCodeNameOfTestProject(proj4Helper, "pro-ject.4");
568         Project p = pm.findProject(projdir);
569         ReferenceHelper referenceHelperProj4 = p.getLookup().lookup(ReferenceHelper.class);
570         RakeArtifact art = proj4Helper.createSimpleRakeArtifact("jar", "build.jar", proj4Helper.getStandardPropertyEvaluator(), "do.jar", "clean");
571         String JavaDoc ref = referenceHelperProj4.addReference(art, art.getArtifactLocations()[0]);
572         assertEquals("Project reference was not correctly escaped", "${reference.pro-ject_4.do_jar}", ref);
573         
574         // test that it can be found
575
ReferenceHelper.RawReference rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", false);
576         assertNull("Cannot be found because it was escaped", rr);
577         rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", true);
578         assertNotNull("Created reference was not created", rr);
579         assertEquals("do.jar", rr.getID());
580         assertEquals("pro-ject_4", rr.getForeignProjectName());
581         
582         // test deletion
583
referenceHelperProj4.destroyForeignFileReference(ref);
584         rr = referenceHelperProj4.getRawReference("pro-ject_4", "do_jar", true);
585         assertNull("Reference was not deleted", rr);
586     }
587         
588     public void testArtifactProperties() throws Exception JavaDoc {
589         assertFalse("project not initially modified", pm.isModified(p));
590         RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean");
591         art.getProperties().setProperty("configuration", "debug");
592         art.getProperties().setProperty("empty", "");
593         assertFalse("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
594         assertEquals("added a ref to proj2.dojar", "${reference.proj2.dojar}", r.addReference(art, art.getArtifactLocations()[0]));
595         assertTrue("reference exist", r.isReferenced(art, art.getArtifactLocations()[0]));
596         assertTrue("project now modified", pm.isModified(p));
597         ProjectManager.getDefault().saveAllProjects();
598         ReferenceHelper.RawReference[] refs = r.getRawReferences();
599         assertEquals("one ref now", 1, refs.length);
600         ReferenceHelper.RawReference ref = refs[0];
601         assertEquals("correct foreign project name", "proj2", ref.getForeignProjectName());
602         assertEquals("correct artifact type", "jar", ref.getArtifactType());
603         assertEquals("correct script location", "${project.proj2}/build.xml", ref.getScriptLocationValue());
604         assertEquals("correct target name", "dojar", ref.getTargetName());
605         assertEquals("correct clean target name", "clean", ref.getCleanTargetName());
606         assertEquals("correct property keys", new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{"configuration", "empty"})), ref.getProperties().keySet());
607         assertEquals("correct property values", new TreeSet JavaDoc<String JavaDoc>(Arrays.asList(new String JavaDoc[]{"debug", ""})),
608                 new TreeSet JavaDoc<Object JavaDoc>(ref.getProperties().values()));
609     }
610
611     /**
612      * Check that the {@link SubprojectProvider} implementation behaves correctly.
613      * @throws Exception in case of unexpected failures
614      */

615     public void testSubprojectProviderImpl() throws Exception JavaDoc {
616         RakeArtifact art = sisterh.createSimpleRakeArtifact("jar", "build.jar", sisterh.getStandardPropertyEvaluator(), "dojar", "clean");
617         assertNotNull("added a ref to proj2.dojar", r.addReference(art, art.getArtifactLocations()[0]));
618         art = sisterh.createSimpleRakeArtifact("javadoc", "build.javadoc", sisterh.getStandardPropertyEvaluator(), "dojavadoc", "clean");
619         assertNotNull("added a ref to proj2.dojavadoc", r.addReference(art, art.getArtifactLocations()[0]));
620         art = seph.createSimpleRakeArtifact("jar", "build.jar", seph.getStandardPropertyEvaluator(), "dojar", "clean");
621         assertNotNull("added a ref to proj3.dojar", r.addReference(art, art.getArtifactLocations()[0]));
622         SubprojectProvider sp = r.createSubprojectProvider();
623         Set JavaDoc<? extends Project> subprojs = sp.getSubprojects();
624         assertEquals("two subprojects", 2, subprojs.size());
625         Project[] subprojsA = subprojs.toArray(new Project[2]);
626         Project proj2, proj3;
627         if (ProjectUtils.getInformation(subprojsA[0]).getName().equals("proj2")) {
628             proj2 = subprojsA[0];
629             proj3 = subprojsA[1];
630         } else {
631             proj2 = subprojsA[1];
632             proj3 = subprojsA[0];
633         }
634         assertEquals("proj2 was found correctly", pm.findProject(sisterprojdir), proj2);
635         assertEquals("proj3 was found correctly", pm.findProject(sepprojdir), proj3);
636     }
637
638     /**
639      * Check that methods to add foreign file references really work.
640      * @throws Exception in case of unexpected failure
641      */

642     @SuppressWarnings JavaDoc("deprecation")
643     public void testForeignFileReferences() throws Exception JavaDoc {
644         // test collocated foreign project reference
645
File JavaDoc f = new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir), "dist"), "proj2.jar");
646         assertEquals("can add a ref to an artifact", "${reference.proj2.dojar}", r.createForeignFileReference(f, "jar"));
647         assertEquals("creating reference second time must return already existing ID", "${reference.proj2.dojar}", r.createForeignFileReference(f, "jar"));
648         assertNotNull("ref added", r.getRawReference("proj2", "dojar"));
649         EditableProperties privateProps = h.getProperties (RakeProjectHelper.PRIVATE_PROPERTIES_PATH);
650         String JavaDoc refval = privateProps.getProperty("project.proj2");
651         assertEquals("reference correctly stored into private.properties", FileUtil.toFile(sisterprojdir).getAbsolutePath(), refval);
652         EditableProperties projectProps = h.getProperties (RakeProjectHelper.PROJECT_PROPERTIES_PATH);
653         refval = projectProps.getProperty("project.proj2");
654         assertEquals("reference correctly stored into project.properties", "../proj2", refval);
655         refval = pev.getProperty("reference.proj2.dojar");
656         assertNotNull("reference correctly evaluated", refval);
657         assertEquals("reference correctly evaluated", f, h.resolveFile(refval));
658         RakeArtifact art = r.getForeignFileReferenceAsArtifact("${reference.proj2.dojar}");
659         assertNotNull("got the reference back", art);
660         assertEquals("correct project", sisterprojdir, art.getProject().getProjectDirectory());
661         assertEquals("correct target name", "dojar", art.getTargetName());
662         assertEquals("correct type", "jar", art.getType());
663         assertEquals("correct artifact location", URI.create("dist/proj2.jar"), art.getArtifactLocations()[0]);
664         art = (RakeArtifact)r.findArtifactAndLocation("reference.proj2.dojar")[0];
665         assertNull("bad format", art);
666         art = (RakeArtifact)r.findArtifactAndLocation("${reference.proj2.doojar}")[0];
667         assertNull("wrong target name", art);
668         File JavaDoc f2 = new File JavaDoc(new File JavaDoc(FileUtil.toFile(sisterprojdir2), "dist"), "proj2.jar");
669         assertEquals("reference ID must be unique", "${reference.proj2-1.dojar}", r.createForeignFileReference(f2, "jar"));
670         assertEquals("creating reference second time must return already existing ID", "${reference.proj2-1.dojar}", r.createForeignFileReference(f2, "jar"));
671         r.destroyForeignFileReference("${reference.proj2-1.dojar}");
672         assertNull("ref removed", r.getRawReference("proj2-1", "dojar"));
673         r.destroyForeignFileReference("${reference.proj2.dojar}");
674         assertNull("ref removed", r.getRawReference("proj2", "dojar"));
675         assertNull("project ref property removed", pev.getProperty("reference.proj2.dojar"));
676         assertEquals("no refs remaining", 0, r.getRawReferences().length);
677         
678         // test non-collocated foreign project reference
679
FileObject nonCollocatedProjectLib = scratch.getFileObject("separate/proj3").createFolder("d i s t").createData("p r o j 3.jar");
680         f = FileUtil.toFile(nonCollocatedProjectLib);
681         art = RakeArtifactQuery.findArtifactByID(pm.findProject(sepprojdir), "dojar");
682         assertNotNull("have an artifact proj3.dojar", art);
683         assertEquals("can add a reference to a direct artifact", "${reference.proj3.dojar}", r.createForeignFileReference(art));
684         assertEquals("creating reference second time must return already existing ID", "${reference.proj3.dojar}", r.createForeignFileReference(art));
685         assertNotNull("ref added", r.getRawReference("proj3", "dojar"));
686         refval = pev.getProperty("reference.proj3.dojar");
687         String JavaDoc val = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty("reference.proj3.dojar");
688         assertEquals("reference was not correctly set", "${project.proj3}/d i s t/p r o j 3.jar", val);
689         assertEquals("reference correctly evaluated", f, h.resolveFile(refval));
690         val = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty("project.proj3");
691         assertEquals("reference correctly evaluated", FileUtil.toFile(sepprojdir).getAbsolutePath(), val);
692         art = (RakeArtifact)r.findArtifactAndLocation("${reference.proj3.dojar}")[0];
693         assertNotNull("got the reference back", art);
694         assertEquals("correct project", sepprojdir, art.getProject().getProjectDirectory());
695         assertEquals("correct target name", "dojar", art.getTargetName());
696         r.destroyForeignFileReference("${reference.proj3.dojar}");
697         assertNull("ref removed", r.getRawReference("proj3", "dojar"));
698         assertNull("project ref property removed", pev.getProperty("reference.proj3.dojar"));
699         assertEquals("no refs remaining", 0, r.getRawReferences().length);
700
701         // test foreign file reference for collocated jar
702
FileObject collocatedLib = scratch.createFolder("j a r s").createData("m y l i b.jar");
703         f = FileUtil.toFile(collocatedLib);
704         String JavaDoc ref = r.createForeignFileReference(f, "jar");
705         String JavaDoc ref2 = r.createForeignFileReference(f, "jar");
706         assertEquals("Duplicate reference created", ref, ref2);
707         assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar}", ref);
708         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
709         assertEquals("Reference was not correctly evaluated from project.properties", "../j a r s/m y l i b.jar", refval);
710         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
711         assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval);
712         
713         assertEquals("Reference was not correctly evaluated", f, h.resolveFile(refval));
714         collocatedLib = scratch.createFolder("jars2").createData("m y l i b.jar");
715         f = FileUtil.toFile(collocatedLib);
716         ref = r.createForeignFileReference(f, "jar");
717         ref2 = r.createForeignFileReference(f, "jar");
718         assertEquals("Duplicate reference created", ref, ref2);
719         assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar-1}", ref);
720         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
721         assertEquals("Reference was not correctly evaluated form project.properties", "../jars2/m y l i b.jar", refval);
722         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
723         assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval);
724         collocatedLib = scratch.createFolder("jars3").createData("m y l i b.jar");
725         f = FileUtil.toFile(collocatedLib);
726         ref = r.createForeignFileReference(f, "jar");
727         ref2 = r.createForeignFileReference(f, "jar");
728         assertEquals("Duplicate reference created", ref, ref2);
729         assertEquals("Foreign file reference was not correctly created", "${file.reference.m_y_l_i_b.jar-2}", ref);
730         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
731         assertEquals("Reference was not correctly evaluated from project.properties", "../jars3/m y l i b.jar", refval);
732         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
733         assertEquals("Reference was not correctly evaluated from private.properties", f.getAbsolutePath(), refval);
734         
735         assertTrue("Reference was not removed", r.destroyReference(ref));
736         assertFalse("There should not be any reference", r.destroyReference(ref));
737         refval = pev.evaluate(ref);
738         assertEquals("Reference was not removed", ref, refval);
739         
740         // test foreign file reference for non-collocated jar
741
FileObject nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars").createData("mylib2.jar");
742         f = FileUtil.toFile(nonCollocatedLib);
743         ref = r.createForeignFileReference(f, "jar");
744         ref2 = r.createForeignFileReference(f, "jar");
745         assertEquals("Duplicate reference created", ref, ref2);
746         assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar}", ref);
747         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
748         assertNull ("Foreign file reference is stored into project.properties",refval);
749         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
750         assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval);
751         assertEquals("Reference was not correctly evaluated", f, h.resolveFile(refval));
752         nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars2").createData("mylib2.jar");
753         f = FileUtil.toFile(nonCollocatedLib);
754         ref = r.createForeignFileReference(f, "jar");
755         ref2 = r.createForeignFileReference(f, "jar");
756         assertEquals("Duplicate reference created", ref, ref2);
757         assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar-1}", ref);
758         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
759         assertNull ("Foreign file reference is stored into project.properties",refval);
760         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
761         assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval);
762         nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars3").createData("mylib2.jar");
763         f = FileUtil.toFile(nonCollocatedLib);
764         ref = r.createForeignFileReference(f, "jar");
765         ref2 = r.createForeignFileReference(f, "jar");
766         assertEquals("Duplicate reference created", ref, ref2);
767         assertEquals("Foreign file reference was not correctly created", "${file.reference.mylib2.jar-2}", ref);
768         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
769         assertNull ("Foreign file reference is stored into project.properties",refval);
770         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
771         assertEquals("Reference was not correctly evaluated", f.getAbsolutePath(), refval);
772         r.destroyForeignFileReference(ref);
773         refval = pev.evaluate(ref);
774         assertEquals("Reference was not removed", ref, refval);
775         
776     }
777     
778     public void testToRakeArtifact() throws Exception JavaDoc {
779         ReferenceHelper.RawReference ref = new ReferenceHelper.RawReference(
780             "proj2", "irrelevant", new URI JavaDoc("also-irrelevant"), "dojar", "totally-irrelevant", "dojar");
781         RakeArtifact art = ref.toRakeArtifact(r);
782         assertNull("${project.proj2} not set, will not be found", art);
783         EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
784         props.setProperty("project.proj2", "../proj2");
785         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
786         art = ref.toRakeArtifact(r);
787         assertNotNull("now artifact will be found", art);
788         assertEquals("correct directory", sisterprojdir, art.getProject().getProjectDirectory());
789         assertEquals("correct artifact location", URI.create("dist/proj2.jar"), art.getArtifactLocations()[0]);
790         assertEquals("correct script location", new File JavaDoc(FileUtil.toFile(sisterprojdir), "build.xml"), art.getScriptLocation());
791         assertEquals("correct target name", "dojar", art.getTargetName());
792         assertEquals("correct clean target name", "clean", art.getCleanTargetName());
793         ref = new ReferenceHelper.RawReference(
794             "proj2", "irrelevant", new URI JavaDoc("also-irrelevant"), "doojar", "totally-irrelevant", "doojar");
795         art = ref.toRakeArtifact(r);
796         assertNull("wrong target name, will not be found", art);
797     }
798
799     public void testAddRemoveExtraBaseDirectory() throws Exception JavaDoc {
800         // test foreign file reference for non-collocated jar under extra base folder
801
FileObject nonCollocatedLib = scratch.getFileObject("separate").createFolder("jars").createData("mylib.jar");
802         File JavaDoc f = FileUtil.toFile(nonCollocatedLib);
803         EditableProperties props = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
804         props.setProperty("externalSourceRoot", "../separate");
805         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, props);
806         r.addExtraBaseDirectory("externalSourceRoot");
807         String JavaDoc ref = r.createForeignFileReference(f, "jar");
808         assertEquals("foreign file reference created", "${file.reference.mylib.jar}", ref);
809         String JavaDoc refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
810         assertEquals("reference is using extra base folder", "${externalSourceRoot}/jars/mylib.jar", refval);
811         assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval)));
812         // test removal of extra base folder
813
r.removeExtraBaseDirectory("externalSourceRoot");
814         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
815         assertEquals("reference does not contain extra base folder", "../separate/jars/mylib.jar", refval);
816         assertEquals("reference is correctly evaluated", f, h.resolveFile(refval));
817
818         // the same test as above but with extra base folder defined in PRIVATE props
819
nonCollocatedLib = scratch.createFolder("separate2").createFolder("jars").createData("mylib2.jar");
820         f = FileUtil.toFile(nonCollocatedLib);
821         props = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH);
822         String JavaDoc absolutePath = FileUtil.toFile(scratch.getFileObject("separate2")).getAbsolutePath();
823         props.setProperty("externalSourceRootAbsolute", absolutePath);
824         h.putProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH, props);
825         r.addExtraBaseDirectory("externalSourceRootAbsolute");
826         ref = r.createForeignFileReference(f, "jar");
827         assertEquals("foreign file reference created", "${file.reference.mylib2.jar}", ref);
828         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
829         assertEquals("reference is using extra base folder", "${externalSourceRootAbsolute}/jars/mylib2.jar", refval);
830         assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval)));
831         r.removeExtraBaseDirectory("externalSourceRootAbsolute");
832         refval = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
833         assertNull("reference was removed from PROJECT_PROPERTIES_PATH", refval);
834         refval = h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH).getProperty(ref.substring(2, ref.length()-1));
835         assertNotNull("reference was moved to PRIVATE_PROPERTIES_PATH", refval);
836         assertEquals("reference does not contain extra base folder", absolutePath+"/jars/mylib2.jar", refval);
837         assertEquals("reference is correctly evaluated", f, h.resolveFile(h.getStandardPropertyEvaluator().evaluate(refval)));
838     }
839
840     /**
841      * Check that adding and removing artifact locations dynamically doesn't break anything.
842      * Cf. #55413 and #55423.
843      */

844     public void testDeletionOfArtifactLocations() throws Exception JavaDoc {
845         final URI JavaDoc[] locations = {
846             URI.create("dist/output1.jar"),
847             URI.create("dist/output2.jar"),
848             URI.create("dist/output3.jar"),
849         };
850         final boolean[] includeLocations = {
851             true,
852             false,
853             true,
854         };
855         class MultiRakeArtifact extends RakeArtifact {
856             public String JavaDoc getType() {
857                 return "jar";
858             }
859             public String JavaDoc getTargetName() {
860                 return "build";
861             }
862             public String JavaDoc getCleanTargetName() {
863                 return "clean";
864             }
865             public File JavaDoc getScriptLocation() {
866                 return sisterh.resolveFile("build.xml");
867             }
868             public URI JavaDoc[] getArtifactLocations() {
869                 List JavaDoc<URI JavaDoc> locs = new ArrayList JavaDoc<URI JavaDoc>();
870                 for (int i = 0; i < locations.length; i++) {
871                     if (includeLocations[i]) {
872                         locs.add(locations[i]);
873                     }
874                 }
875                 return locs.toArray(new URI JavaDoc[locs.size()]);
876             }
877         }
878         RakeArtifact art = new MultiRakeArtifact();
879         assertFalse("project not initially modified", pm.isModified(p));
880         assertFalse("no refs yet", r.isReferenced(art, locations[0]));
881         assertEquals("added a ref to output1.jar", "${reference.proj2.build}", r.addReference(art, locations[0]));
882         assertEquals("added a ref to output3.jar", "${reference.proj2.build.1}", r.addReference(art, locations[2]));
883         try {
884             r.addReference(art, locations[1]);
885             fail("Should not be permitted to add ref to output2.jar yet");
886         } catch (IllegalArgumentException JavaDoc e) {
887             // Expected.
888
}
889         assertTrue("output1.jar ref'd", r.isReferenced(art, locations[0]));
890         assertTrue("output3.jar ref'd", r.isReferenced(art, locations[2]));
891         try {
892             r.isReferenced(art, locations[1]);
893             fail("Should not be permitted to check ref to output2.jar yet");
894         } catch (IllegalArgumentException JavaDoc e) {
895             // Expected.
896
}
897         // Make sure proj2 actually reports our special provider:
898
Project sisterproj = ProjectManager.getDefault().findProject(sisterprojdir);
899         sisterproj.getLookup().lookup(RakeBasedTestUtil.RakeArtifactProviderMutable.class).
900             setBuildArtifacts(new RakeArtifact[] {art});
901         // Now check findArtifactAndLocation usage.
902
assertEquals("output1.jar found",
903                 Arrays.asList(new Object JavaDoc[] {art, locations[0]}),
904                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}")));
905         assertEquals("output3.jar found",
906                 Arrays.asList(new Object JavaDoc[] {art, locations[2]}),
907                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}")));
908         // Now add output2.jar to list and see that lookups work somehow.
909
includeLocations[1] = true;
910         assertEquals("output1.jar still there",
911                 Arrays.asList(new Object JavaDoc[] {art, locations[0]}),
912                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}")));
913         assertEquals("output2.jar now magically referenced instead of output3.jar (but oh well)",
914                 Arrays.asList(new Object JavaDoc[] {art, locations[1]}),
915                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}")));
916         assertEquals("output3.jar now magically referenced (even though we have no such property ourselves)",
917                 Arrays.asList(new Object JavaDoc[] {art, locations[2]}),
918                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.2}")));
919         // Now *remove* some items and see what happens!
920
includeLocations[0] = false;
921         includeLocations[1] = false;
922         assertEquals("output3.jar now only referent",
923                 Arrays.asList(new Object JavaDoc[] {art, locations[2]}),
924                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build}")));
925         assertEquals("second item no longer exists",
926                 Arrays.asList(new Object JavaDoc[] {null, null}),
927                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.1}")));
928         assertEquals("third item no longer exists",
929                 Arrays.asList(new Object JavaDoc[] {null, null}),
930                 Arrays.asList(r.findArtifactAndLocation("${reference.proj2.build.2}")));
931         assertTrue("output3.jar ref'd", r.isReferenced(art, locations[2]));
932         try {
933             r.isReferenced(art, locations[0]);
934             fail("Should not be permitted to check ref to first item any more, oops");
935         } catch (IllegalArgumentException JavaDoc e) {
936             // Expected.
937
}
938         try {
939             r.isReferenced(art, locations[1]);
940             fail("Should not be permitted to check ref to second item any more");
941         } catch (IllegalArgumentException JavaDoc e) {
942             // Expected.
943
}
944         // Now destroy the references and make sure there are no issues.
945
assertTrue("Can really get rid of output3.jar (even though it used to be output1.jar)",
946             r.destroyReference("${reference.proj2.build}"));
947         assertTrue("Can also get rid of what used to be output3.jar somehow - just the property at least",
948             r.destroyReference("${reference.proj2.build.1}"));
949         assertFalse("output3.jar no longer ref'd", r.isReferenced(art, locations[2]));
950         assertEquals("No raw references left", Collections.EMPTY_LIST, Arrays.asList(r.getRawReferences()));
951         assertEquals("No shared properties left", Collections.EMPTY_MAP, h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH));
952         assertEquals("No private properties left", Collections.EMPTY_MAP, h.getProperties(RakeProjectHelper.PRIVATE_PROPERTIES_PATH));
953     }
954     
955     private void writeProperties(FileObject prop, String JavaDoc[] keys, String JavaDoc[] values) throws Exception JavaDoc {
956     EditableProperties p = new EditableProperties();
957     
958     for (int cntr = 0; cntr < keys.length; cntr++) {
959         p.setProperty(keys[cntr], values[cntr]);
960     }
961     
962     FileLock lock = prop.lock();
963     try {
964         OutputStream JavaDoc os = prop.getOutputStream(lock);
965         try {
966         p.store(os);
967         } finally {
968         os.close();
969         }
970     } finally {
971         lock.releaseLock();
972     }
973     }
974     
975     public void testFixReferences() throws Exception JavaDoc {
976     FileObject originalProject = scratch.createFolder("orig-proj");
977     FileObject originalSrcDir = originalProject.createFolder("src");
978     
979     FileObject testProject = scratch.createFolder("test-proj");
980     FileObject nbprojectDir = testProject.createFolder("nbproject");
981     
982     TestUtil.createFileFromContent(ReferenceHelperTest.class.getResource("data/project.xml"), testProject, "nbproject/project.xml");
983     
984     FileObject publicProperties = nbprojectDir.createData("project.properties");
985     FileObject privateDir = nbprojectDir.createFolder("private");
986     FileObject privateProperties = privateDir.createData("private.properties");
987     FileObject srcDir = testProject.createFolder("src");
988     
989     writeProperties(publicProperties, new String JavaDoc[] {
990         "file.reference.x",
991     }, new String JavaDoc[] {
992         "src",
993     });
994     
995     writeProperties(privateProperties, new String JavaDoc[] {
996         "file.reference.x",
997     }, new String JavaDoc[] {
998         FileUtil.toFile(originalSrcDir).getAbsolutePath(),
999     });
1000    
1001    Project nue = pm.findProject(testProject);
1002    assertNotNull("found project in " + testProject, nue);
1003        RakeProjectHelper h = nue.getLookup().lookup(RakeProjectHelper.class);
1004        assertNotNull("found helper for " + nue, h);
1005        ReferenceHelper r = nue.getLookup().lookup(ReferenceHelper.class);
1006        assertNotNull("found ref helper for " + p, r);
1007    
1008    r.fixReferences(FileUtil.toFile(originalProject));
1009    
1010    pm.saveProject(nue);
1011    
1012    String JavaDoc resolvedProperty = h.getStandardPropertyEvaluator().getProperty("file.reference.x");
1013    
1014    assertNotNull("the property can be resolved", resolvedProperty);
1015    
1016    File JavaDoc resolvedFile = PropertyUtils.resolveFile(FileUtil.toFile(testProject), resolvedProperty);
1017    
1018    assertNotNull("the file can be resolved", resolvedFile);
1019    
1020    assertEquals("referencing correct file", FileUtil.toFile(srcDir).getAbsolutePath(), resolvedFile.getAbsolutePath());
1021    }
1022    
1023}
1024
Popular Tags