KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > JavaProjectGeneratorTest


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.java.freeform;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarOutputStream JavaDoc;
32 import javax.swing.event.ChangeEvent JavaDoc;
33 import javax.swing.event.ChangeListener JavaDoc;
34 import org.netbeans.api.java.classpath.ClassPath;
35 import org.netbeans.api.java.project.JavaProjectConstants;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectInformation;
38 import org.netbeans.api.project.ProjectManager;
39 import org.netbeans.api.project.ProjectUtils;
40 import org.netbeans.api.project.Sources;
41 import org.netbeans.junit.NbTestCase;
42 import org.netbeans.modules.ant.freeform.spi.support.Util;
43 import org.netbeans.spi.java.classpath.ClassPathProvider;
44 import org.netbeans.spi.project.AuxiliaryConfiguration;
45 import org.netbeans.spi.project.support.ant.AntProjectHelper;
46 import org.netbeans.spi.project.ui.LogicalViewProvider;
47 import org.openide.filesystems.FileObject;
48 import org.openide.filesystems.FileUtil;
49 import org.openide.nodes.Node;
50 import org.w3c.dom.Element JavaDoc;
51 import javax.xml.parsers.SAXParser JavaDoc;
52 import javax.xml.parsers.SAXParserFactory JavaDoc;
53 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
54 import org.netbeans.modules.ant.freeform.FreeformProjectType;
55 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
56 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
57 import org.netbeans.spi.project.support.ant.PropertyProvider;
58 import org.netbeans.spi.project.support.ant.PropertyUtils;
59 import org.openide.modules.ModuleInfo;
60 import org.openide.util.Lookup;
61 import org.xml.sax.SAXException JavaDoc;
62 import org.xml.sax.SAXParseException JavaDoc;
63 import org.xml.sax.helpers.DefaultHandler JavaDoc;
64
65 /**
66  * Tests for JavaProjectGenerator.
67  *
68  * @author David Konecny
69  */

70 public class JavaProjectGeneratorTest extends NbTestCase {
71
72     private File JavaDoc lib1;
73     private File JavaDoc lib2;
74     private File JavaDoc src;
75     private File JavaDoc test;
76     
77     public JavaProjectGeneratorTest(java.lang.String JavaDoc testName) {
78         super(testName);
79     }
80     
81     protected void setUp() throws Exception JavaDoc {
82         Lookup.getDefault().lookup(ModuleInfo.class);
83         clearWorkDir();
84     }
85     
86     private AntProjectHelper createEmptyProject(String JavaDoc projectFolder, String JavaDoc projectName, boolean notSoEmpty) throws Exception JavaDoc {
87         File JavaDoc base = new File JavaDoc(getWorkDir(), projectFolder);
88         base.mkdir();
89         File JavaDoc antScript = new File JavaDoc(base, "build.xml");
90         antScript.createNewFile();
91         src = new File JavaDoc(base, "src");
92         src.mkdir();
93         test = new File JavaDoc(base, "test");
94         test.mkdir();
95         File JavaDoc libs = new File JavaDoc(base, "libs");
96         libs.mkdir();
97         lib1 = new File JavaDoc(libs, "some.jar");
98         createRealJarFile(lib1);
99         lib2 = new File JavaDoc(libs, "some2.jar");
100         createRealJarFile(lib2);
101         
102 // XXX: might need to call refresh here??
103
// FileObject fo = FileUtil.toFileObject(getWorkDir());
104
// fo.refresh();
105

106         ArrayList JavaDoc sources = new ArrayList JavaDoc();
107         ArrayList JavaDoc compUnits = new ArrayList JavaDoc();
108         AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null);
109         if (notSoEmpty) {
110             JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
111             sf.label = "src";
112             sf.type = "java";
113             sf.style = "packages";
114             sf.location = src.getAbsolutePath();
115             sources.add(sf);
116             JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
117             JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
118             cp.classpath = lib1.getAbsolutePath();
119             cp.mode = "compile";
120             cu.classpath = Collections.singletonList(cp);
121             cu.sourceLevel = "1.4";
122             cu.packageRoots = Collections.singletonList(src.getAbsolutePath());
123             compUnits.add(cu);
124             JavaProjectGenerator.putSourceFolders(helper, sources, null);
125             JavaProjectGenerator.putSourceViews(helper, sources, null);
126             JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits);
127         }
128         return helper;
129     }
130     
131     public void testCreateProject() throws Exception JavaDoc {
132         AntProjectHelper helper = createEmptyProject("proj1", "proj-1", false);
133         FileObject base = helper.getProjectDirectory();
134         Project p = ProjectManager.getDefault().findProject(base);
135         assertNotNull("Project was not created", p);
136         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
137         
138         ProjectInformation pi = ProjectUtils.getInformation(p);
139         assertEquals("Project name was not set", "proj-1", pi.getName());
140     }
141     
142     public void testRawCreateProject() throws Exception JavaDoc {
143         File JavaDoc base = new File JavaDoc(getWorkDir(), "proj");
144         base.mkdir();
145         File JavaDoc diffFolder = new File JavaDoc(getWorkDir(), "separate");
146         diffFolder.mkdir();
147         File JavaDoc antScript = new File JavaDoc(diffFolder, "build.xml");
148         antScript.createNewFile();
149         
150 // XXX: might need to call refresh here??
151
// FileObject fo = FileUtil.toFileObject(getWorkDir());
152
// fo.refresh();
153

154         AntProjectHelper helper = FreeformProjectGenerator.createProject(diffFolder, base, "p-r-o-j", antScript);
155         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
156         assertNotNull("Project was not created", p);
157         List JavaDoc mappings = new ArrayList JavaDoc();
158         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
159         tm.name = "foo";
160         tm.script = "antScript";
161         mappings.add(tm);
162         List JavaDoc customActions = new ArrayList JavaDoc();
163         FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget();
164         ct.label = "customAction1";
165         customActions.add(ct);
166         List JavaDoc folders = new ArrayList JavaDoc();
167         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
168         sf.label = "folder3";
169         sf.location = "location3";
170         sf.style = "tree";
171         folders.add(sf);
172         List JavaDoc exports = new ArrayList JavaDoc();
173         JavaProjectGenerator.Export e = new JavaProjectGenerator.Export();
174         e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR;
175         e.location = "folder/output.jar";
176         e.buildTarget = "target";
177         exports.add(e);
178         List JavaDoc subprojects = new ArrayList JavaDoc();
179         subprojects.add("/projA");
180         
181         FreeformProjectGenerator.putTargetMappings(helper, mappings);
182         FreeformProjectGenerator.putContextMenuAction(helper, mappings);
183         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
184         JavaProjectGenerator.putSourceFolders(helper, folders, null);
185         JavaProjectGenerator.putSourceViews(helper, folders, null);
186         JavaProjectGenerator.putExports(helper, exports);
187         JavaProjectGenerator.putSubprojects(helper, subprojects);
188 // ProjectManager.getDefault().saveAllProjects();
189

190         // check that all elements are written in expected order
191

192         Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
193         List JavaDoc subElements = Util.findSubElements(el);
194         assertEquals(7, subElements.size());
195         assertElementArray(subElements,
196             new String JavaDoc[]{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"},
197             new String JavaDoc[]{null, null, null, null, null, null, null});
198         Element JavaDoc el2 = (Element JavaDoc)subElements.get(5);
199         subElements = Util.findSubElements(el2);
200         assertEquals(2, subElements.size());
201         assertElementArray(subElements,
202             new String JavaDoc[]{"items", "context-menu"},
203             new String JavaDoc[]{null, null});
204         Element JavaDoc el3 = (Element JavaDoc)subElements.get(0);
205         List JavaDoc subEls = Util.findSubElements(el3);
206         assertEquals(2, subEls.size());
207         assertElementArray(subEls,
208             new String JavaDoc[]{"source-folder", "source-file"},
209             new String JavaDoc[]{null, null});
210         el3 = (Element JavaDoc)subElements.get(1);
211         subEls = Util.findSubElements(el3);
212         assertEquals(2, subEls.size());
213         assertElementArray(subEls,
214             new String JavaDoc[]{"ide-action", "action"},
215             new String JavaDoc[]{null, null});
216             
217         // calling getters and setters in random order cannot change order of elements
218

219         mappings = FreeformProjectGenerator.getTargetMappings(helper);
220         customActions = FreeformProjectGenerator.getCustomContextMenuActions(helper);
221         folders = JavaProjectGenerator.getSourceFolders(helper, null);
222         // style is not read by getSourceFolders and needs to be fixed here:
223
((JavaProjectGenerator.SourceFolder)folders.get(0)).style = "tree";
224         FreeformProjectGenerator.putTargetMappings(helper, mappings);
225         JavaProjectGenerator.putSubprojects(helper, subprojects);
226         FreeformProjectGenerator.putContextMenuAction(helper, mappings);
227         JavaProjectGenerator.putExports(helper, exports);
228         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
229         JavaProjectGenerator.putSourceFolders(helper, folders, null);
230         JavaProjectGenerator.putSourceViews(helper, folders, null);
231         JavaProjectGenerator.putSourceViews(helper, folders, null);
232         JavaProjectGenerator.putSourceFolders(helper, folders, null);
233         JavaProjectGenerator.putExports(helper, exports);
234         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
235         FreeformProjectGenerator.putContextMenuAction(helper, mappings);
236         JavaProjectGenerator.putSubprojects(helper, subprojects);
237         FreeformProjectGenerator.putTargetMappings(helper, mappings);
238 // ProjectManager.getDefault().saveAllProjects();
239
el = Util.getPrimaryConfigurationData(helper);
240         subElements = Util.findSubElements(el);
241         assertEquals(7, subElements.size());
242         assertElementArray(subElements,
243             new String JavaDoc[]{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"},
244             new String JavaDoc[]{null, null, null, null, null, null, null});
245         el2 = (Element JavaDoc)subElements.get(5);
246         subElements = Util.findSubElements(el2);
247         assertEquals(2, subElements.size());
248         assertElementArray(subElements,
249             new String JavaDoc[]{"items", "context-menu"},
250             new String JavaDoc[]{null, null});
251         el3 = (Element JavaDoc)subElements.get(0);
252         subEls = Util.findSubElements(el3);
253         assertEquals(2, subEls.size());
254         assertElementArray(subEls,
255             new String JavaDoc[]{"source-folder", "source-file"},
256             new String JavaDoc[]{null, null});
257         el3 = (Element JavaDoc)subElements.get(1);
258         subEls = Util.findSubElements(el3);
259         assertEquals(2, subEls.size());
260         assertElementArray(subEls,
261             new String JavaDoc[]{"ide-action", "action"},
262             new String JavaDoc[]{null, null});
263
264         // validate against schema:
265
ProjectManager.getDefault().saveAllProjects();
266         validate(p);
267     }
268     
269     /**
270      * Asserts that given Element has expected name and its text match expected value.
271      * @param element element to test
272      * @param expectedName expected name of element; cannot be null
273      * @param expectedValue can be null in which case value is not tested
274      */

275     public static void assertElement(Element JavaDoc element, String JavaDoc expectedName, String JavaDoc expectedValue) {
276         String JavaDoc message = "Element "+element+" does not match [name="+expectedName+",value="+expectedValue+"]"; // NOI18N
277
assertEquals(message, expectedName, element.getLocalName());
278         if (expectedValue != null) {
279             assertEquals(message, expectedValue, Util.findText(element));
280         }
281     }
282
283     /**
284      * See {@link #assertElement(Element, String, String)} for more details. This
285      * method does exactly the same just on the list of elements and expected names.
286      */

287     public static void assertElementArray(List JavaDoc<Element JavaDoc> elements, String JavaDoc[] expectedNames, String JavaDoc[] expectedValues) {
288         for (int i=0; i<elements.size(); i++) {
289             assertElement((Element JavaDoc)elements.get(i), expectedNames[i], expectedValues[i]);
290         }
291     }
292     
293     /**
294      * Asserts that given Element has expected name and its text match expected value and
295      * it also has expect attribute with expected value.
296      * @param element element to test
297      * @param expectedName expected name of element; cannot be null
298      * @param expectedValue can be null in which case value is not tested
299      * @param expectedAttrName expected name of attribute; cannot be null
300      * @param expectedAttrValue expected value of attribute; cannot be null
301      */

302     public static void assertElement(Element JavaDoc element, String JavaDoc expectedName, String JavaDoc expectedValue, String JavaDoc expectedAttrName, String JavaDoc expectedAttrValue) {
303         String JavaDoc message = "Element "+element+" does not match [name="+expectedName+",value="+
304             expectedValue+", attr="+expectedAttrName+", attrvalue="+expectedAttrValue+"]"; // NOI18N
305
assertEquals(message, expectedName, element.getLocalName());
306         if (expectedValue != null) {
307             assertEquals(message, expectedValue, Util.findText(element));
308         }
309         String JavaDoc val = element.getAttribute(expectedAttrName);
310         assertEquals(expectedAttrValue, val);
311     }
312     
313     /**
314      * See {@link #assertElement(Element, String, String)} for more details. This
315      * method does exactly the same just on the list of elements and expected names
316      * and expected attributes.
317      */

318     public static void assertElementArray(List JavaDoc<Element JavaDoc> elements, String JavaDoc[] expectedNames, String JavaDoc[] expectedValues, String JavaDoc[] expectedAttrName, String JavaDoc[] expectedAttrValue) {
319         assertEquals(expectedNames.length, elements.size());
320         for (int i=0; i<elements.size(); i++) {
321             assertElement((Element JavaDoc)elements.get(i), expectedNames[i], expectedValues[i], expectedAttrName[i], expectedAttrValue[i]);
322         }
323     }
324     
325     public void testSourceFolders() throws Exception JavaDoc {
326         AntProjectHelper helper = createEmptyProject("proj3", "proj-3", true);
327         FileObject base = helper.getProjectDirectory();
328         Project p = ProjectManager.getDefault().findProject(base);
329         assertNotNull("Project was not created", p);
330         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
331         
332         Sources ss = ProjectUtils.getSources(p);
333         assertEquals("Project must have one java source group", 1, ss.getSourceGroups("java").length);
334         assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length);
335
336         Listener JavaDoc l = new Listener JavaDoc();
337         ss.addChangeListener(l);
338         
339         List JavaDoc sfs = JavaProjectGenerator.getSourceFolders(helper, null);
340         assertEquals("There must be one source folder", 1, sfs.size());
341         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
342         sf.label = "test";
343         sf.type = "java";
344         sf.location = test.getAbsolutePath();
345         sfs.add(sf);
346         JavaProjectGenerator.putSourceFolders(helper, sfs, null);
347         assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length);
348         assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length);
349         assertEquals("Number of fired events does not match", 1, l.count);
350         l.reset();
351         
352         sfs = new ArrayList JavaDoc();
353         sf = new JavaProjectGenerator.SourceFolder();
354         sf.label = "xdoc";
355         sf.type = "x-doc";
356         // just some path
357
sf.location = test.getAbsolutePath();
358         sfs.add(sf);
359         JavaProjectGenerator.putSourceFolders(helper, sfs, "x-doc");
360         assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length);
361         assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size());
362         assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length);
363         assertEquals("Project must have one x-doc source group", 1, ss.getSourceGroups("x-doc").length);
364         sf = new JavaProjectGenerator.SourceFolder();
365         sf.label = "xdoc2";
366         sf.type = "x-doc";
367         // just some path
368
sf.location = src.getAbsolutePath();
369         sfs.add(sf);
370         JavaProjectGenerator.putSourceFolders(helper, sfs, "x-doc");
371         assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length);
372         assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size());
373         assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length);
374         assertEquals("Project must have two x-doc source groups", 2, ss.getSourceGroups("x-doc").length);
375         assertEquals("Project must have two x-doc source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "x-doc").size());
376         assertEquals("Project must have four source groups", 4, JavaProjectGenerator.getSourceFolders(helper, null).size());
377
378         sfs = JavaProjectGenerator.getSourceFolders(helper, null);
379         JavaProjectGenerator.putSourceFolders(helper, sfs, null);
380         assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length);
381         assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size());
382         assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length);
383         assertEquals("Project must have two x-doc source groups", 2, ss.getSourceGroups("x-doc").length);
384         assertEquals("Project must have two x-doc source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "x-doc").size());
385         assertEquals("Project must have four source groups", 4, JavaProjectGenerator.getSourceFolders(helper, null).size());
386
387         ProjectManager.getDefault().saveAllProjects();
388     }
389     
390     public void testRawSourceFolders() throws Exception JavaDoc {
391         AntProjectHelper helper = createEmptyProject("proj", "proj", false);
392         FileObject base = helper.getProjectDirectory();
393         Project p = ProjectManager.getDefault().findProject(base);
394         assertNotNull("Project was not created", p);
395         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
396         
397         // check that all data are correctly persisted
398

399         List JavaDoc folders = new ArrayList JavaDoc();
400         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
401         sf.label = "folder1";
402         sf.type = "type1";
403         sf.location = "location1";
404         folders.add(sf);
405         sf = new JavaProjectGenerator.SourceFolder();
406         sf.label = "folder2";
407         sf.type = "type2";
408         sf.location = "location2";
409         folders.add(sf);
410         JavaProjectGenerator.putSourceFolders(helper, folders, null);
411         // test getter and setter here:
412
folders = JavaProjectGenerator.getSourceFolders(helper, null);
413         JavaProjectGenerator.putSourceFolders(helper, folders, null);
414 // ProjectManager.getDefault().saveAllProjects();
415
Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
416         el = Util.findElement(el, "folders", Util.NAMESPACE);
417         assertNotNull("Source folders were not saved correctly", el);
418         List JavaDoc subElements = Util.findSubElements(el);
419         assertEquals(2, subElements.size());
420         // compare first source folder
421
Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
422         assertElement(el2, "source-folder", null);
423         List JavaDoc l1 = Util.findSubElements(el2);
424         assertEquals(3, l1.size());
425         assertElementArray(l1,
426             new String JavaDoc[]{"label", "type", "location"},
427             new String JavaDoc[]{"folder1", "type1", "location1"});
428         // compare second source folder
429
el2 = (Element JavaDoc)subElements.get(1);
430         assertElement(el2, "source-folder", null);
431         l1 = Util.findSubElements(el2);
432         assertEquals(3, l1.size());
433         assertElementArray(l1,
434             new String JavaDoc[]{"label", "type", "location"},
435             new String JavaDoc[]{"folder2", "type2", "location2"});
436         // validate against schema:
437
ProjectManager.getDefault().saveAllProjects();
438         validate(p);
439             
440         // test rewriting of source folder of some type
441

442         folders = new ArrayList JavaDoc();
443         sf = new JavaProjectGenerator.SourceFolder();
444         sf.label = "folder3";
445         sf.type = "type2";
446         sf.location = "location3";
447         folders.add(sf);
448         JavaProjectGenerator.putSourceFolders(helper, folders, "type2");
449         ProjectManager.getDefault().saveAllProjects();
450         el = Util.getPrimaryConfigurationData(helper);
451         el = Util.findElement(el, "folders", Util.NAMESPACE);
452         assertNotNull("Source folders were not saved correctly", el);
453         subElements = Util.findSubElements(el);
454         assertEquals(2, subElements.size());
455         // compare first source folder
456
el2 = (Element JavaDoc)subElements.get(0);
457         assertElement(el2, "source-folder", null);
458         l1 = Util.findSubElements(el2);
459         assertEquals(3, l1.size());
460         assertElementArray(l1,
461             new String JavaDoc[]{"label", "type", "location"},
462             new String JavaDoc[]{"folder1", "type1", "location1"});
463         // compare second source folder
464
el2 = (Element JavaDoc)subElements.get(1);
465         assertElement(el2, "source-folder", null);
466         l1 = Util.findSubElements(el2);
467         assertEquals(3, l1.size());
468         assertElementArray(l1,
469             new String JavaDoc[]{"label", "type", "location"},
470             new String JavaDoc[]{"folder3", "type2", "location3"});
471         // validate against schema:
472
ProjectManager.getDefault().saveAllProjects();
473         validate(p);
474     }
475
476     public void testSourceViews() throws Exception JavaDoc {
477         AntProjectHelper helper = createEmptyProject("proj6", "proj-6", true);
478         FileObject base = helper.getProjectDirectory();
479         Project p = ProjectManager.getDefault().findProject(base);
480         assertNotNull("Project was not created", p);
481         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
482         
483         Sources ss = ProjectUtils.getSources(p);
484         assertEquals("Project must have one java source group", 1, ss.getSourceGroups("java").length);
485
486         LogicalViewProvider lvp = (LogicalViewProvider)p.getLookup().lookup(LogicalViewProvider.class);
487         assertNotNull("Project does not have LogicalViewProvider", lvp);
488         Node n = lvp.createLogicalView();
489         // expected subnodes: #1) src folder and #2) build.xml
490
assertEquals("There must be two subnodes in logical view", 2, n.getChildren().getNodesCount());
491         
492         List JavaDoc sfs = JavaProjectGenerator.getSourceViews(helper, null);
493         assertEquals("There must be one source view", 1, sfs.size());
494         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
495         sf.label = "test";
496         sf.style = "packages";
497         sf.location = test.getAbsolutePath();
498         sfs.add(sf);
499         JavaProjectGenerator.putSourceViews(helper, sfs, null);
500         assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size());
501         assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size());
502         
503         n = lvp.createLogicalView();
504         // expected subnodes: #1) src folder and #2) build.xml and #3) tests
505
// assertEquals("There must be three subnodes in logical view", 3, n.getChildren().getNodesCount());
506

507         sfs = new ArrayList JavaDoc();
508         sf = new JavaProjectGenerator.SourceFolder();
509         sf.label = "xdoc";
510         sf.style = "tree";
511         // just some path
512
sf.location = test.getAbsolutePath();
513         sfs.add(sf);
514         JavaProjectGenerator.putSourceViews(helper, sfs, "tree");
515         assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size());
516         assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size());
517         assertEquals("Project must have one tree source view", 1, JavaProjectGenerator.getSourceViews(helper, "tree").size());
518         assertEquals("Project must have three source views", 3, JavaProjectGenerator.getSourceViews(helper, null).size());
519         sf = new JavaProjectGenerator.SourceFolder();
520         sf.label = "xdoc2";
521         sf.style = "tree";
522         // just some path
523
sf.location = src.getAbsolutePath();
524         sfs.add(sf);
525         JavaProjectGenerator.putSourceViews(helper, sfs, "tree");
526         assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size());
527         assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size());
528         assertEquals("Project must have two tree source views", 2, JavaProjectGenerator.getSourceViews(helper, "tree").size());
529         assertEquals("Project must have four source views", 4, JavaProjectGenerator.getSourceViews(helper, null).size());
530
531         sfs = JavaProjectGenerator.getSourceViews(helper, null);
532         JavaProjectGenerator.putSourceViews(helper, sfs, null);
533         assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size());
534         assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size());
535         assertEquals("Project must have two tree source views", 2, JavaProjectGenerator.getSourceViews(helper, "tree").size());
536         assertEquals("Project must have four source views", 4, JavaProjectGenerator.getSourceViews(helper, null).size());
537
538         ProjectManager.getDefault().saveAllProjects();
539     }
540     
541     public void testRawSourceViews() throws Exception JavaDoc {
542         AntProjectHelper helper = createEmptyProject("proj", "proj", false);
543         FileObject base = helper.getProjectDirectory();
544         Project p = ProjectManager.getDefault().findProject(base);
545         assertNotNull("Project was not created", p);
546         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
547         
548         // check that all data are correctly persisted
549

550         List JavaDoc folders = new ArrayList JavaDoc();
551         JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder();
552         sf.label = "folder1";
553         sf.style = "tree";
554         sf.location = "location1";
555         folders.add(sf);
556         sf = new JavaProjectGenerator.SourceFolder();
557         sf.label = "folder2";
558         sf.style = "packages";
559         sf.location = "location2";
560         folders.add(sf);
561         JavaProjectGenerator.putSourceViews(helper, folders, null);
562         // test getter and setter here:
563
folders = JavaProjectGenerator.getSourceViews(helper, null);
564         JavaProjectGenerator.putSourceViews(helper, folders, null);
565         ProjectManager.getDefault().saveAllProjects();
566         Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
567         el = Util.findElement(el, "view", Util.NAMESPACE);
568         assertNotNull("View folders were not saved correctly", el);
569         el = Util.findElement(el, "items", Util.NAMESPACE);
570         assertNotNull("View folders were not saved correctly", el);
571         List JavaDoc subElements = Util.findSubElements(el);
572         // there will be three sublements: <source-file> is added for build.xml during project.creation
573
assertEquals(3, subElements.size());
574         // compare first source view
575
Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
576         assertElement(el2, "source-folder", null, "style", "tree");
577         List JavaDoc l1 = Util.findSubElements(el2);
578         assertEquals(2, l1.size());
579         assertElementArray(l1,
580             new String JavaDoc[]{"label", "location"},
581             new String JavaDoc[]{"folder1", "location1"});
582         // compare second source view
583
el2 = (Element JavaDoc)subElements.get(1);
584         assertElement(el2, "source-folder", null, "style", "packages");
585         l1 = Util.findSubElements(el2);
586         assertEquals(2, l1.size());
587         assertElementArray(l1,
588             new String JavaDoc[]{"label", "location"},
589             new String JavaDoc[]{"folder2", "location2"});
590         // validate against schema:
591
ProjectManager.getDefault().saveAllProjects();
592         validate(p);
593             
594         // test rewriting of source view of some style
595

596         folders = new ArrayList JavaDoc();
597         sf = new JavaProjectGenerator.SourceFolder();
598         sf.label = "folder3";
599         sf.style = "packages";
600         sf.location = "location3";
601         folders.add(sf);
602         JavaProjectGenerator.putSourceViews(helper, folders, "packages");
603         ProjectManager.getDefault().saveAllProjects();
604         el = Util.getPrimaryConfigurationData(helper);
605         el = Util.findElement(el, "view", Util.NAMESPACE);
606         assertNotNull("Source views were not saved correctly", el);
607         el = Util.findElement(el, "items", Util.NAMESPACE);
608         assertNotNull("View folders were not saved correctly", el);
609         subElements = Util.findSubElements(el);
610         // there will be three sublements: <source-file> is added for build.xml during project.creation
611
assertEquals("3 elements in " + subElements, 3, subElements.size());
612         // compare first source view
613
el2 = (Element JavaDoc)subElements.get(0);
614         assertElement(el2, "source-folder", null, "style", "tree");
615         l1 = Util.findSubElements(el2);
616         assertEquals(2, l1.size());
617         assertElementArray(l1,
618             new String JavaDoc[]{"label", "location"},
619             new String JavaDoc[]{"folder1", "location1"});
620         // compare second source view
621
el2 = (Element JavaDoc)subElements.get(1);
622         assertElement(el2, "source-folder", null, "style", "packages");
623         l1 = Util.findSubElements(el2);
624         assertEquals(2, l1.size());
625         assertElementArray(l1,
626             new String JavaDoc[]{"label", "location"},
627             new String JavaDoc[]{"folder3", "location3"});
628         // validate against schema:
629
ProjectManager.getDefault().saveAllProjects();
630         validate(p);
631     }
632
633     public void testJavaCompilationUnits() throws Exception JavaDoc {
634         AntProjectHelper helper = createEmptyProject("proj5", "proj-5", true);
635         FileObject base = helper.getProjectDirectory();
636         Project p = ProjectManager.getDefault().findProject(base);
637         assertNotNull("Project was not created", p);
638         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
639         
640         ClassPathProvider cpp = (ClassPathProvider)p.getLookup().lookup(ClassPathProvider.class);
641         assertNotNull("Project does not have ClassPathProvider", cpp);
642         ClassPath cp = cpp.findClassPath(FileUtil.toFileObject(src), ClassPath.COMPILE);
643         assertEquals("Project must have one classpath root", 1, cp.getRoots().length);
644         assertEquals("Classpath root does not match", "jar:"+lib1.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm());
645         cp = cpp.findClassPath(FileUtil.toFileObject(src).getParent(), ClassPath.COMPILE);
646         assertEquals("There is no classpath for this file", null, cp);
647         
648         AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
649         List JavaDoc cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux);
650         assertEquals("There must be one compilation unit", 1, cus.size());
651         JavaProjectGenerator.JavaCompilationUnit cu = (JavaProjectGenerator.JavaCompilationUnit)cus.get(0);
652         assertEquals("The compilation unit must have one classpath", 1, cu.classpath.size());
653         
654         JavaProjectGenerator.JavaCompilationUnit.CP cucp = new JavaProjectGenerator.JavaCompilationUnit.CP();
655         cucp.classpath = lib2.getAbsolutePath();
656         cucp.mode = "execute";
657         cu.classpath.add(cucp);
658         ArrayList JavaDoc outputs = new ArrayList JavaDoc();
659         outputs.add("output1.jar");
660         outputs.add("output2.jar");
661         outputs.add("output3.jar");
662         cu.output = outputs;
663         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, cus);
664         cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux);
665         assertEquals("There must be one compilation unit", 1, cus.size());
666         cu = (JavaProjectGenerator.JavaCompilationUnit)cus.get(0);
667         assertEquals("The compilation unit must have one classpath", 2, cu.classpath.size());
668         assertEquals("The compilation unit must have one classpath", 3, cu.output.size());
669         
670         cu = new JavaProjectGenerator.JavaCompilationUnit();
671         cu.sourceLevel = "1.4";
672         cucp = new JavaProjectGenerator.JavaCompilationUnit.CP();
673         cucp.classpath = lib2.getAbsolutePath();
674         cucp.mode = "compile";
675         cu.classpath = Collections.singletonList(cucp);
676         cu.packageRoots = Collections.singletonList(test.getAbsolutePath());
677         cus.add(cu);
678         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, cus);
679         cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux);
680         assertEquals("There must be two compilation units", 2, cus.size());
681         cp = cpp.findClassPath(FileUtil.toFileObject(src), ClassPath.COMPILE);
682         assertEquals("Project must have one classpath root", 1, cp.getRoots().length);
683         assertEquals("Classpath root does not match", "jar:"+lib1.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm());
684         cp = cpp.findClassPath(FileUtil.toFileObject(src).getParent(), ClassPath.COMPILE);
685         assertEquals("There is no classpath for this file", null, cp);
686         cp = cpp.findClassPath(FileUtil.toFileObject(test), ClassPath.COMPILE);
687         assertEquals("Project must have one classpath root", 1, cp.getRoots().length);
688         assertEquals("Classpath root does not match", "jar:"+lib2.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm());
689         
690         ProjectManager.getDefault().saveAllProjects();
691     }
692     
693     public void testRawJavaCompilationUnits() throws Exception JavaDoc {
694         AntProjectHelper helper = createEmptyProject("proj", "proj", false);
695         FileObject base = helper.getProjectDirectory();
696         Project p = ProjectManager.getDefault().findProject(base);
697         assertNotNull("Project was not created", p);
698         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
699         
700         // check that all data are correctly persisted
701

702         List JavaDoc units = new ArrayList JavaDoc();
703         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
704         cu.packageRoots = new ArrayList JavaDoc();
705         cu.packageRoots.add("pkgroot1");
706         cu.packageRoots.add("pkgroot2");
707         cu.output = new ArrayList JavaDoc();
708         cu.output.add("output1");
709         cu.output.add("output2");
710         cu.classpath = new ArrayList JavaDoc();
711         JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
712         cp.mode = "compile";
713         cp.classpath = "classpath1";
714         cu.classpath.add(cp);
715         cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
716         cp.mode = "boot";
717         cp.classpath = "classpath2";
718         cu.classpath.add(cp);
719         cu.sourceLevel = "1.3";
720         units.add(cu);
721         cu = new JavaProjectGenerator.JavaCompilationUnit();
722         cu.packageRoots = new ArrayList JavaDoc();
723         cu.packageRoots.add("sec-pkgroot1");
724         cu.packageRoots.add("sec-pkgroot2");
725         cu.output = new ArrayList JavaDoc();
726         cu.output.add("sec-output1");
727         cu.output.add("sec-output2");
728         cu.classpath = new ArrayList JavaDoc();
729         cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
730         cp.mode = "compile";
731         cp.classpath = "sec-classpath1";
732         cu.classpath.add(cp);
733         cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
734         cp.mode = "boot";
735         cp.classpath = "sec-classpath2";
736         cu.classpath.add(cp);
737         cu.sourceLevel = "1.4";
738         units.add(cu);
739         AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
740         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
741         // test getter and setter here:
742
units = JavaProjectGenerator.getJavaCompilationUnits(helper, aux);
743         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
744 // ProjectManager.getDefault().saveAllProjects();
745
Element JavaDoc el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
746         assertNotNull("Java compilation units were not saved correctly", el);
747         List JavaDoc subElements = Util.findSubElements(el);
748         assertEquals(2, subElements.size());
749         // compare first compilation unit
750
Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
751         assertElement(el2, "compilation-unit", null);
752         List JavaDoc l1 = Util.findSubElements(el2);
753         assertEquals(7, l1.size());
754         assertElementArray(l1,
755             new String JavaDoc[]{"package-root", "package-root", "classpath", "classpath", "built-to", "built-to", "source-level"},
756             new String JavaDoc[]{"pkgroot1", "pkgroot2", "classpath1", "classpath2", "output1", "output2", "1.3"});
757         el2 = (Element JavaDoc)l1.get(2);
758         assertElement(el2, "classpath", "classpath1", "mode", "compile");
759         el2 = (Element JavaDoc)l1.get(3);
760         assertElement(el2, "classpath", "classpath2", "mode", "boot");
761         // compare second compilation unit
762
el2 = (Element JavaDoc)subElements.get(1);
763         assertElement(el2, "compilation-unit", null);
764         l1 = Util.findSubElements(el2);
765         assertEquals(7, l1.size());
766         assertElementArray(l1,
767             new String JavaDoc[]{"package-root", "package-root", "classpath", "classpath", "built-to", "built-to", "source-level"},
768             new String JavaDoc[]{"sec-pkgroot1", "sec-pkgroot2", "sec-classpath1", "sec-classpath2", "sec-output1", "sec-output2", "1.4"});
769         el2 = (Element JavaDoc)l1.get(2);
770         assertElement(el2, "classpath", "sec-classpath1", "mode", "compile");
771         el2 = (Element JavaDoc)l1.get(3);
772         assertElement(el2, "classpath", "sec-classpath2", "mode", "boot");
773         // validate against schema:
774
ProjectManager.getDefault().saveAllProjects();
775         validate(p);
776             
777         // test updating
778

779         units = new ArrayList JavaDoc();
780         cu = new JavaProjectGenerator.JavaCompilationUnit();
781         cu.packageRoots = new ArrayList JavaDoc();
782         cu.packageRoots.add("foo-package-root");
783         units.add(cu);
784         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
785 // ProjectManager.getDefault().saveAllProjects();
786
el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
787         assertNotNull("Java compilation units were not saved correctly", el);
788         subElements = Util.findSubElements(el);
789         assertEquals(1, subElements.size());
790         // compare first compilation unit
791
el2 = (Element JavaDoc)subElements.get(0);
792         assertElement(el2, "compilation-unit", null);
793         l1 = Util.findSubElements(el2);
794         assertEquals(1, l1.size());
795         assertElementArray(l1,
796             new String JavaDoc[]{"package-root"},
797             new String JavaDoc[]{"foo-package-root"});
798         // validate against schema:
799
ProjectManager.getDefault().saveAllProjects();
800         validate(p);
801     }
802
803     public void testCompilationUnitUpgrades() throws Exception JavaDoc {
804         AntProjectHelper helper = createEmptyProject("proj", "proj", false);
805         FileObject base = helper.getProjectDirectory();
806         Project p = ProjectManager.getDefault().findProject(base);
807         assertNotNull("Project was not created", p);
808         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
809         // Start with a /1-friendly data set.
810
List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> units = new ArrayList JavaDoc();
811         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
812         cu.packageRoots = new ArrayList JavaDoc();
813         cu.packageRoots.add("pkgroot1");
814         units.add(cu);
815         AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper);
816         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
817         // Check that the correct /1 data was saved.
818
Element JavaDoc el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
819         assertNotNull("Java compilation units were saved in /1", el);
820         List JavaDoc<Element JavaDoc> subElements = Util.findSubElements(el);
821         assertEquals(1, subElements.size());
822         // compare the compilation unit
823
Element JavaDoc el2 = (Element JavaDoc) subElements.get(0);
824         assertElement(el2, "compilation-unit", null);
825         assertElementArray(Util.findSubElements(el2),
826             new String JavaDoc[] {"package-root"},
827             new String JavaDoc[] {"pkgroot1"});
828         // validate against schema:
829
ProjectManager.getDefault().saveAllProjects();
830         validate(p);
831         // Now check that setting isTests = true on that element forces a /2 save.
832
units = new ArrayList JavaDoc();
833         cu = new JavaProjectGenerator.JavaCompilationUnit();
834         cu.packageRoots = new ArrayList JavaDoc();
835         cu.packageRoots.add("pkgroot1");
836         cu.isTests = true;
837         units.add(cu);
838         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
839         // Check that we now have it in /2.
840
el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
841         assertNull("No /1 data", el);
842         el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
843         assertNotNull("Have /2 data", el);
844         subElements = Util.findSubElements(el);
845         assertEquals(1, subElements.size());
846         // compare the compilation unit
847
el2 = (Element JavaDoc) subElements.get(0);
848         assertElement(el2, "compilation-unit", null);
849         assertElementArray(Util.findSubElements(el2),
850             new String JavaDoc[] {"package-root", "unit-tests"},
851             new String JavaDoc[] {"pkgroot1", null});
852         // validate against schema:
853
ProjectManager.getDefault().saveAllProjects();
854         validate(p);
855         // Now try fresh save of /2-requiring data (using javadoc).
856
assertTrue("removed /2 data", aux.removeConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true));
857         units = new ArrayList JavaDoc();
858         cu = new JavaProjectGenerator.JavaCompilationUnit();
859         cu.packageRoots = new ArrayList JavaDoc();
860         cu.packageRoots.add("pkgroot1");
861         cu.javadoc = new ArrayList JavaDoc();
862         cu.javadoc.add("javadoc1");
863         cu.javadoc.add("javadoc2");
864         units.add(cu);
865         JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units);
866         // Check that we have it in /2.
867
el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true);
868         assertNull("No /1 data", el);
869         el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
870         assertNotNull("Have /2 data", el);
871         subElements = Util.findSubElements(el);
872         assertEquals(1, subElements.size());
873         // compare the compilation unit
874
el2 = (Element JavaDoc) subElements.get(0);
875         assertElement(el2, "compilation-unit", null);
876         assertElementArray(Util.findSubElements(el2),
877             new String JavaDoc[] {"package-root", "javadoc-built-to", "javadoc-built-to"},
878             new String JavaDoc[] {"pkgroot1", "javadoc1", "javadoc2"});
879         // validate against schema:
880
ProjectManager.getDefault().saveAllProjects();
881         validate(p);
882     }
883
884     public void testGuessExports() throws Exception JavaDoc {
885         JavaProjectGenerator.TargetMapping tm = new JavaProjectGenerator.TargetMapping();
886         tm.name = "build";
887         tm.script = "${ant}";
888         tm.targets = new ArrayList JavaDoc();
889         tm.targets.add("target-1");
890         ArrayList JavaDoc targets = new ArrayList JavaDoc();
891         targets.add(tm);
892         
893         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
894         cu.output = new ArrayList JavaDoc();
895         cu.output.add("${outputfile}");
896         ArrayList JavaDoc units = new ArrayList JavaDoc();
897         units.add(cu);
898         
899         Map JavaDoc m = new HashMap JavaDoc();
900         m.put("outputfile", "out.jar");
901         m.put("ant", getWorkDir().getAbsolutePath()+"/etc/antScript");
902         PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
903             PropertyUtils.fixedPropertyProvider(m)});
904         
905         List JavaDoc exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
906         assertEquals("one export was created even though build script is not in project folder", 1, exports.size());
907         // XXX test stuff about that export
908

909         m.put("ant", "etc/antScript");
910         evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
911             PropertyUtils.fixedPropertyProvider(m)});
912         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
913         assertEquals("one export was created", 1, exports.size());
914         
915         tm.script = null;
916         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
917         assertEquals("one export was created", 1, exports.size());
918         JavaProjectGenerator.Export e = (JavaProjectGenerator.Export)exports.get(0);
919         assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type);
920         assertEquals("export is properly configured", "${outputfile}", e.location);
921         assertEquals("export is properly configured", null, e.script);
922         assertEquals("export is properly configured", "target-1", e.buildTarget);
923         
924         tm.targets.add("target-2");
925         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
926         assertEquals("no export was created when there are two targets", 0, exports.size());
927         
928         tm.targets.remove("target-2");
929         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
930         assertEquals("one export was created", 1, exports.size());
931         
932         tm.name = "buildXX";
933         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
934         assertEquals("no export was created when there is no action with build name", 0, exports.size());
935         
936         tm.name = "build";
937         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
938         assertEquals("one export was created", 1, exports.size());
939
940         JavaProjectGenerator.JavaCompilationUnit cu2 = new JavaProjectGenerator.JavaCompilationUnit();
941         cu2.output = new ArrayList JavaDoc();
942         cu2.output.add("build/classes");
943         units.add(cu2);
944         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
945         assertEquals("two exports was created", 2, exports.size());
946         
947         cu2.output.add("dist/proj.jar");
948         cu2.output.add("dist/proj2.jar");
949         tm.script = "antScript";
950         exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units);
951         assertEquals("four exports were created", 4, exports.size());
952         e = (JavaProjectGenerator.Export)exports.get(0);
953         assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type);
954         assertEquals("export is properly configured", "${outputfile}", e.location);
955         assertEquals("export is properly configured", "antScript", e.script);
956         assertEquals("export is properly configured", "target-1", e.buildTarget);
957         e = (JavaProjectGenerator.Export)exports.get(1);
958         assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_FOLDER, e.type);
959         assertEquals("export is properly configured", "build/classes", e.location);
960         assertEquals("export is properly configured", "antScript", e.script);
961         assertEquals("export is properly configured", "target-1", e.buildTarget);
962         e = (JavaProjectGenerator.Export)exports.get(2);
963         assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type);
964         assertEquals("export is properly configured", "dist/proj.jar", e.location);
965         assertEquals("export is properly configured", "antScript", e.script);
966         assertEquals("export is properly configured", "target-1", e.buildTarget);
967         e = (JavaProjectGenerator.Export)exports.get(3);
968         assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type);
969         assertEquals("export is properly configured", "dist/proj2.jar", e.location);
970         assertEquals("export is properly configured", "antScript", e.script);
971         assertEquals("export is properly configured", "target-1", e.buildTarget);
972     }
973     
974     public void testPutExports() throws Exception JavaDoc {
975         AntProjectHelper helper = createEmptyProject("proj", "proj", false);
976         FileObject base = helper.getProjectDirectory();
977         Project p = ProjectManager.getDefault().findProject(base);
978         assertNotNull("Project was not created", p);
979         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
980         
981         // check that all data are correctly persisted
982

983         List JavaDoc exports = new ArrayList JavaDoc();
984         JavaProjectGenerator.Export e = new JavaProjectGenerator.Export();
985         e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR;
986         e.location = "path/smth.jar";
987         e.script = "someScript";
988         e.buildTarget = "build_target";
989         e.cleanTarget = "clean_target";
990         exports.add(e);
991         e = new JavaProjectGenerator.Export();
992         e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR;
993         e.location = "something/else.jar";
994         e.buildTarget = "bldtrg";
995         exports.add(e);
996         
997         JavaProjectGenerator.putExports(helper, exports);
998         Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
999         List JavaDoc subElements = Util.findSubElements(el);
1000        // 4, i.e. name, two exports and one view of build.xml file
1001
assertEquals(5, subElements.size());
1002        // compare first compilation unit
1003
Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
1004        assertElement(el2, "name", null);
1005        el2 = (Element JavaDoc)subElements.get(1);
1006        assertElement(el2, "properties", null);
1007        el2 = (Element JavaDoc)subElements.get(2);
1008        assertElement(el2, "export", null);
1009        List JavaDoc l1 = Util.findSubElements(el2);
1010        assertEquals(5, l1.size());
1011        assertElementArray(l1,
1012            new String JavaDoc[]{"type", "location", "script", "build-target", "clean-target"},
1013            new String JavaDoc[]{JavaProjectConstants.ARTIFACT_TYPE_JAR, "path/smth.jar", "someScript", "build_target", "clean_target"});
1014        // compare second compilation unit
1015
el2 = (Element JavaDoc)subElements.get(3);
1016        assertElement(el2, "export", null);
1017        l1 = Util.findSubElements(el2);
1018        assertEquals(3, l1.size());
1019        assertElementArray(l1,
1020            new String JavaDoc[]{"type", "location", "build-target"},
1021            new String JavaDoc[]{JavaProjectConstants.ARTIFACT_TYPE_JAR, "something/else.jar", "bldtrg"});
1022        el2 = (Element JavaDoc)subElements.get(4);
1023        assertElement(el2, "view", null);
1024        // validate against schema:
1025
ProjectManager.getDefault().saveAllProjects();
1026        validate(p);
1027            
1028        // now test updating
1029

1030        exports = new ArrayList JavaDoc();
1031        e = new JavaProjectGenerator.Export();
1032        e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR;
1033        e.location = "aaa/bbb.jar";
1034        e.buildTarget = "ccc";
1035        exports.add(e);
1036        
1037        JavaProjectGenerator.putExports(helper, exports);
1038        el = Util.getPrimaryConfigurationData(helper);
1039        subElements = Util.findSubElements(el);
1040        // 3, i.e. name, export and one view of build.xml file
1041
assertEquals(4, subElements.size());
1042        // compare first compilation unit
1043
el2 = (Element JavaDoc)subElements.get(0);
1044        assertElement(el2, "name", null);
1045        el2 = (Element JavaDoc)subElements.get(1);
1046        assertElement(el2, "properties", null);
1047        el2 = (Element JavaDoc)subElements.get(2);
1048        assertElement(el2, "export", null);
1049        l1 = Util.findSubElements(el2);
1050        assertEquals(3, l1.size());
1051        assertElementArray(l1,
1052            new String JavaDoc[]{"type", "location", "build-target"},
1053            new String JavaDoc[]{JavaProjectConstants.ARTIFACT_TYPE_JAR, "aaa/bbb.jar", "ccc"});
1054        el2 = (Element JavaDoc)subElements.get(3);
1055        assertElement(el2, "view", null);
1056        // validate against schema:
1057
ProjectManager.getDefault().saveAllProjects();
1058        validate(p);
1059            
1060    }
1061    
1062    public void testGuessSubprojects() throws Exception JavaDoc {
1063        AntProjectHelper helper = createEmptyProject("proj1", "proj1", false);
1064        FileObject base = helper.getProjectDirectory();
1065        Project p = ProjectManager.getDefault().findProject(base);
1066        assertNotNull("Project was not created", p);
1067        assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
1068        ArrayList JavaDoc exports = new ArrayList JavaDoc();
1069        JavaProjectGenerator.Export e = new JavaProjectGenerator.Export();
1070        e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR;
1071        e.location = "libs/some.jar"; // this jar is created in createEmptyProject() so let's use it as export
1072
e.buildTarget = "build_target";
1073        exports.add(e);
1074        JavaProjectGenerator.putExports(helper, exports);
1075        ProjectManager.getDefault().saveAllProjects();
1076        String JavaDoc lib1path = lib1.getAbsolutePath();
1077        String JavaDoc proj1path = FileUtil.toFile(base).getAbsolutePath();
1078        
1079        AntProjectHelper helper2 = createEmptyProject("proj2", "proj2", false);
1080        FileObject base2 = helper.getProjectDirectory();
1081        File JavaDoc projBase = FileUtil.toFile(base2);
1082        Project p2 = ProjectManager.getDefault().findProject(base2);
1083        assertNotNull("Project was not created", p2);
1084        assertEquals("Project folder is incorrect", base2, p.getProjectDirectory());
1085        
1086        PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
1087            PropertyUtils.fixedPropertyProvider(
1088            Collections.singletonMap("lib1", lib1path))});
1089            
1090        ArrayList JavaDoc units = new ArrayList JavaDoc();
1091        JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
1092        JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
1093        cp.mode = "compile";
1094        cp.classpath = "../something.jar;${lib1};";
1095        cu.classpath = new ArrayList JavaDoc();
1096        cu.classpath.add(cp);
1097        units.add(cu);
1098        cu = new JavaProjectGenerator.JavaCompilationUnit();
1099        cp = new JavaProjectGenerator.JavaCompilationUnit.CP();
1100        cp.mode = "compile";
1101        cp.classpath = lib1path+";";
1102        cu.classpath = new ArrayList JavaDoc();
1103        cu.classpath.add(cp);
1104        units.add(cu);
1105        
1106        List JavaDoc l = JavaProjectGenerator.guessSubprojects(evaluator, units, projBase, projBase);
1107        assertEquals("one subproject", 1, l.size());
1108        assertEquals("project1 is subproject", /*proj1path*/ ".", l.get(0));
1109    }
1110    
1111    public void testPutSubprojects() throws Exception JavaDoc {
1112        AntProjectHelper helper = createEmptyProject("proj", "proj", false);
1113        FileObject base = helper.getProjectDirectory();
1114        Project p = ProjectManager.getDefault().findProject(base);
1115        assertNotNull("Project was not created", p);
1116        assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
1117        
1118        // check that all data are correctly persisted
1119

1120        List JavaDoc subprojects = new ArrayList JavaDoc();
1121        subprojects.add("/some/path/projA");
1122        subprojects.add("C:\\dev\\projB");
1123        
1124        JavaProjectGenerator.putSubprojects(helper, subprojects);
1125        Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
1126        Element JavaDoc subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE);
1127        assertNotNull("<subprojects> element exists", subprojectsEl);
1128        List JavaDoc subElements = Util.findSubElements(subprojectsEl);
1129        assertEquals("project depends on two subprojects", 2, subElements.size());
1130        Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
1131        assertElement(el2, "project", "/some/path/projA");
1132        el2 = (Element JavaDoc)subElements.get(1);
1133        assertElement(el2, "project", "C:\\dev\\projB");
1134        
1135        // validate against schema:
1136
ProjectManager.getDefault().saveAllProjects();
1137        validate(p);
1138        
1139        // now test updating
1140

1141        subprojects = new ArrayList JavaDoc();
1142        subprojects.add("/projC");
1143        JavaProjectGenerator.putSubprojects(helper, subprojects);
1144        el = Util.getPrimaryConfigurationData(helper);
1145        subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE);
1146        subElements = Util.findSubElements(subprojectsEl);
1147        assertEquals("project depends on one subproject", 1, subElements.size());
1148        el2 = (Element JavaDoc)subElements.get(0);
1149        assertElement(el2, "project", "/projC");
1150        subprojects = new ArrayList JavaDoc();
1151        JavaProjectGenerator.putSubprojects(helper, subprojects);
1152        el = Util.getPrimaryConfigurationData(helper);
1153        subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE);
1154        subElements = Util.findSubElements(subprojectsEl);
1155        assertEquals("project depends on one subproject", 0, subElements.size());
1156        
1157        // validate against schema:
1158
ProjectManager.getDefault().saveAllProjects();
1159        validate(p);
1160        
1161    }
1162
1163    public void testGuessBuildFolders() throws Exception JavaDoc {
1164        File JavaDoc base = new File JavaDoc(getWorkDir(), "folder");
1165        File JavaDoc proj1 = new File JavaDoc(base, "proj1");
1166        proj1.mkdir();
1167        File JavaDoc base2 = new File JavaDoc(getWorkDir(), "folder2");
1168        
1169        JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
1170        cu.output = new ArrayList JavaDoc();
1171        cu.output.add("${outputfile}");
1172        ArrayList JavaDoc units = new ArrayList JavaDoc();
1173        units.add(cu);
1174        
1175        Map JavaDoc m = new HashMap JavaDoc();
1176        m.put("outputfile", "out.jar");
1177        PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
1178            PropertyUtils.fixedPropertyProvider(m)});
1179        List JavaDoc buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1180        assertEquals("no build folder", 0, buildFolders.size());
1181        
1182        m.put("outputfile", "../proj1_diff/out.jar");
1183        evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
1184            PropertyUtils.fixedPropertyProvider(m)});
1185        buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1186        assertEquals("one build-folder created", 1, buildFolders.size());
1187        assertEquals("export is properly configured", base.getAbsolutePath()+File.separator+"proj1_diff", buildFolders.get(0));
1188        
1189        m.put("outputfile", "../out.jar");
1190        evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{
1191            PropertyUtils.fixedPropertyProvider(m)});
1192        buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1193        assertEquals("one build-folder created", 1, buildFolders.size());
1194        assertEquals("export is properly configured", base.getAbsolutePath(), buildFolders.get(0));
1195        
1196        cu.output.add(base2.getAbsolutePath());
1197        cu.output.add("other.jar");
1198        buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1199        assertEquals("two build-folder created", 2, buildFolders.size());
1200        assertEquals("export is properly configured", base.getAbsolutePath(), buildFolders.get(0));
1201        assertEquals("export is properly configured", base2.getAbsolutePath(), buildFolders.get(1));
1202        
1203        cu.output.add(getWorkDir().getAbsolutePath());
1204        buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1205        assertEquals("one build-folder created", 1, buildFolders.size());
1206        assertEquals("export is properly configured", getWorkDir().getAbsolutePath(), buildFolders.get(0));
1207        
1208        // check that root of this is handled correctly
1209
File JavaDoc diskRoot = getWorkDir();
1210        while (diskRoot.getParentFile() != null) {
1211            diskRoot = diskRoot.getParentFile();
1212        }
1213        cu.output.add(diskRoot.getAbsolutePath());
1214        buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1);
1215        assertEquals("one build-folder created", 1, buildFolders.size());
1216        assertEquals("export is properly configured", diskRoot.getAbsolutePath(), buildFolders.get(0));
1217    }
1218    
1219    public void testPutBuildFolders() throws Exception JavaDoc {
1220        AntProjectHelper helper = createEmptyProject("proj", "proj", false);
1221        FileObject base = helper.getProjectDirectory();
1222        Project p = ProjectManager.getDefault().findProject(base);
1223        assertNotNull("Project was not created", p);
1224        assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
1225        
1226        // check that all data are correctly persisted
1227

1228        List JavaDoc buildFolders = new ArrayList JavaDoc();
1229        buildFolders.add("/some/path/projA");
1230        buildFolders.add("C:\\dev\\projB");
1231        
1232        JavaProjectGenerator.putBuildFolders(helper, buildFolders);
1233        Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
1234        Element JavaDoc foldersEl = Util.findElement(el, "folders", Util.NAMESPACE);
1235        assertNotNull("<folders> element exists", foldersEl);
1236        List JavaDoc subElements = Util.findSubElements(foldersEl);
1237        assertEquals("project has two build-folders", 2, subElements.size());
1238        Element JavaDoc el2 = (Element JavaDoc)subElements.get(0);
1239        assertElement(el2, "build-folder", null);
1240        assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size());
1241        assertElement((Element JavaDoc)Util.findSubElements(el2).get(0), "location", "/some/path/projA");
1242        el2 = (Element JavaDoc)subElements.get(1);
1243        assertElement(el2, "build-folder", null);
1244        assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size());
1245        assertElement((Element JavaDoc)Util.findSubElements(el2).get(0), "location", "C:\\dev\\projB");
1246        
1247        // validate against schema:
1248
ProjectManager.getDefault().saveAllProjects();
1249        validate(p);
1250        
1251        // now test updating
1252

1253        buildFolders = new ArrayList JavaDoc();
1254        buildFolders.add("/projC");
1255        JavaProjectGenerator.putBuildFolders(helper, buildFolders);
1256        el = Util.getPrimaryConfigurationData(helper);
1257        foldersEl = Util.findElement(el, "folders", Util.NAMESPACE);
1258        subElements = Util.findSubElements(foldersEl);
1259        assertEquals("project has one build-folder", 1, subElements.size());
1260        el2 = (Element JavaDoc)subElements.get(0);
1261        assertElement(el2, "build-folder", null);
1262        assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size());
1263        assertElement((Element JavaDoc)Util.findSubElements(el2).get(0), "location", "/projC");
1264        buildFolders = new ArrayList JavaDoc();
1265        JavaProjectGenerator.putBuildFolders(helper, buildFolders);
1266        el = Util.getPrimaryConfigurationData(helper);
1267        foldersEl = Util.findElement(el, "folders", Util.NAMESPACE);
1268        subElements = Util.findSubElements(foldersEl);
1269        assertEquals("project has no build-folder", 0, subElements.size());
1270        
1271        // validate against schema:
1272
ProjectManager.getDefault().saveAllProjects();
1273        validate(p);
1274        
1275    }
1276
1277    private static class Listener implements ChangeListener JavaDoc {
1278        int count = 0;
1279        public void stateChanged(ChangeEvent JavaDoc ev) {
1280            count++;
1281        }
1282        public void reset() {
1283            count = 0;
1284        }
1285    }
1286
1287    // create real Jar otherwise FileUtil.isArchiveFile returns false for it
1288
public void createRealJarFile(File JavaDoc f) throws Exception JavaDoc {
1289        OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
1290        try {
1291            JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
1292// jos.setMethod(ZipEntry.STORED);
1293
JarEntry JavaDoc entry = new JarEntry JavaDoc("foo.txt");
1294// entry.setSize(0L);
1295
// entry.setTime(System.currentTimeMillis());
1296
// entry.setCrc(new CRC32().getValue());
1297
jos.putNextEntry(entry);
1298            jos.flush();
1299            jos.close();
1300        } finally {
1301            os.close();
1302        }
1303    }
1304
1305    private static String JavaDoc[] getSchemas() throws Exception JavaDoc {
1306        return new String JavaDoc[] {
1307            FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(),
1308            JavaProjectGenerator.class.getResource("resources/freeform-project-java.xsd").toExternalForm(),
1309            JavaProjectGenerator.class.getResource("resources/freeform-project-java-2.xsd").toExternalForm(),
1310            AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(),
1311        };
1312    }
1313    
1314    public static void validate(Project proj) throws Exception JavaDoc {
1315        File JavaDoc projF = FileUtil.toFile(proj.getProjectDirectory());
1316        File JavaDoc xml = new File JavaDoc(new File JavaDoc(projF, "nbproject"), "project.xml");
1317        SAXParserFactory JavaDoc f = (SAXParserFactory JavaDoc)Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
1318        if (f == null) {
1319            System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
1320            return;
1321        }
1322        f.setNamespaceAware(true);
1323        f.setValidating(true);
1324        SAXParser JavaDoc p = f.newSAXParser();
1325        p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
1326            "http://www.w3.org/2001/XMLSchema");
1327        p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas());
1328        try {
1329            p.parse(xml.toURI().toString(), new Handler JavaDoc());
1330        } catch (SAXParseException JavaDoc e) {
1331            assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+
1332            e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false);
1333        }
1334    }
1335    
1336    private static final class Handler extends DefaultHandler JavaDoc {
1337        public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
1338            throw e;
1339        }
1340        public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
1341            throw e;
1342        }
1343        public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
1344            throw e;
1345        }
1346    }
1347
1348}
1349
Popular Tags