KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ProjectXMLManagerTest


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.apisupport.project;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.SortedSet JavaDoc;
34 import java.util.TreeSet JavaDoc;
35 import javax.xml.parsers.SAXParser JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37 import org.netbeans.api.project.Project;
38 import org.netbeans.api.project.ProjectManager;
39 import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
40 import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
41 import org.netbeans.modules.apisupport.project.universe.ModuleList;
42 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
43 import org.netbeans.modules.apisupport.project.universe.TestModuleDependency;
44 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
45 import org.openide.ErrorManager;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.util.Mutex;
49 import org.openide.xml.XMLUtil;
50 import org.w3c.dom.Document JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54 import org.xml.sax.SAXParseException JavaDoc;
55 import org.xml.sax.helpers.DefaultHandler JavaDoc;
56 import static org.netbeans.modules.apisupport.project.universe.TestModuleDependency.UNIT;
57 import static org.netbeans.modules.apisupport.project.universe.TestModuleDependency.QA_FUNCTIONAL;
58
59 /**
60  * Tests ProjectXMLManager class.
61  *
62  * @author Martin Krauskopf
63  */

64 public class ProjectXMLManagerTest extends TestBase {
65     
66     private final static String JavaDoc ANT_PROJECT_SUPPORT = "org.netbeans.modules.project.ant";
67     private final static String JavaDoc DIALOGS = "org.openide.dialogs";
68     private final static Set JavaDoc<String JavaDoc> ASSUMED_CNBS;
69     
70     static {
71         Set JavaDoc<String JavaDoc> assumedCNBs = new HashSet JavaDoc<String JavaDoc>(2, 1.0f);
72         assumedCNBs.add(ANT_PROJECT_SUPPORT);
73         assumedCNBs.add(DIALOGS);
74         ASSUMED_CNBS = Collections.unmodifiableSet(assumedCNBs);
75     }
76     
77     public ProjectXMLManagerTest(String JavaDoc testName) {
78         super(testName);
79     }
80     
81     protected void setUp() throws Exception JavaDoc {
82         clearWorkDir();
83         super.setUp();
84     }
85     
86     private ProjectXMLManager createXercesPXM() throws IOException JavaDoc {
87         NbModuleProject xercesPrj = (NbModuleProject) ProjectManager.getDefault().
88                 findProject(nbCVSRoot().getFileObject("libs/xerces"));
89         return new ProjectXMLManager(xercesPrj);
90     }
91     
92     // sanity check
93
public void testGeneratedProject() throws Exception JavaDoc {
94         validate(generateTestingProject(), false); // false - original project.xml is unordered
95
}
96     
97     public void testGetCodeNameBase() throws Exception JavaDoc {
98         NbModuleProject p = generateStandaloneModule("module1");
99         assertEquals("action-project cnb", "org.example.module1", p.getCodeNameBase());
100     }
101     
102     public void testGetDirectDependencies() throws Exception JavaDoc {
103         final NbModuleProject testingProject = generateTestingProject();
104         ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
105         Set JavaDoc<ModuleDependency> deps = testingPXM.getDirectDependencies();
106         assertEquals("number of dependencies", 2, deps.size());
107         
108         Set JavaDoc<String JavaDoc> assumedCNBs = new HashSet JavaDoc<String JavaDoc>(ASSUMED_CNBS);
109         for (ModuleDependency md : deps) {
110             if (md.getModuleEntry().getCodeNameBase().equals(DIALOGS)) {
111                 assertNotNull("module entry", md.getModuleEntry());
112                 assertEquals("release version", null, md.getReleaseVersion());
113                 assertEquals("specification version", "6.2", md.getSpecificationVersion());
114             }
115             if (md.getModuleEntry().getCodeNameBase().equals(ANT_PROJECT_SUPPORT)) {
116                 assertNotNull("module entry", md.getModuleEntry());
117                 assertEquals("release version", "1", md.getReleaseVersion());
118                 assertEquals("specification version", "1.10", md.getSpecificationVersion());
119             }
120             String JavaDoc cnbToRemove = md.getModuleEntry().getCodeNameBase();
121             assertTrue("unknown dependency: " + cnbToRemove, assumedCNBs.remove(cnbToRemove));
122         }
123         assertTrue("following dependencies were found: " + assumedCNBs, assumedCNBs.isEmpty());
124     }
125     
126     public void testGetDirectDependenciesForCustomPlatform() throws Exception JavaDoc {
127         final NbModuleProject testingProject = generateTestingProject();
128         ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
129         Set JavaDoc deps = testingPXM.getDirectDependencies();
130         assertEquals("number of dependencies", 2, deps.size());
131         Set JavaDoc depsWithCustom = testingPXM.getDirectDependencies(
132                 NbPlatform.getPlatformByID("custom"));
133         assertEquals("number of dependencies", 0, depsWithCustom.size());
134     }
135     
136     public void testRemoveDependency() throws Exception JavaDoc {
137         final NbModuleProject testingProject = generateTestingProject();
138         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
139         // apply and save project
140
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
141             public Boolean JavaDoc run() throws IOException JavaDoc {
142                 testingPXM.removeDependency(DIALOGS);
143                 return true;
144             }
145         });
146         assertTrue("removing dependency", result);
147         ProjectManager.getDefault().saveProject(testingProject);
148         
149         final Set JavaDoc<ModuleDependency> newDeps = testingPXM.getDirectDependencies();
150         assertEquals("number of dependencies", 1, newDeps.size());
151         Set JavaDoc<String JavaDoc> newCNBs = new HashSet JavaDoc<String JavaDoc>();
152         newCNBs.add(ANT_PROJECT_SUPPORT);
153         for (ModuleDependency md : newDeps) {
154             String JavaDoc cnbToRemove = md.getModuleEntry().getCodeNameBase();
155             assertTrue("unknown dependency: " + cnbToRemove, newCNBs.remove(cnbToRemove));
156         }
157         assertTrue("following dependencies were found: " + newCNBs, newCNBs.isEmpty());
158         validate(testingProject, true);
159     }
160     
161     public void testEditDependency() throws Exception JavaDoc {
162         final NbModuleProject testingProject = generateTestingProject();
163         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
164         final Set JavaDoc<ModuleDependency> deps = testingPXM.getDirectDependencies();
165         
166         // apply and save project
167
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
168             public Boolean JavaDoc run() throws IOException JavaDoc {
169                 boolean tested = false;
170                 for (ModuleDependency origDep : deps) {
171                     if (DIALOGS.equals(origDep.getModuleEntry().getCodeNameBase())) {
172                         tested = true;
173                         ModuleDependency newDep = new ModuleDependency(
174                                 origDep.getModuleEntry(),
175                                 "2",
176                                 origDep.getSpecificationVersion(),
177                                 origDep.hasCompileDependency(),
178                                 origDep.hasImplementationDepedendency());
179                         testingPXM.editDependency(origDep, newDep);
180                     }
181                 }
182                 assertTrue("org.openide.dialogs dependency tested", tested);
183                 return true;
184             }
185         });
186         assertTrue("editing dependencies", result);
187         ProjectManager.getDefault().saveProject(testingProject);
188         // XXX this refresh shouldn't be needed (should listen on project.xml changes)
189
ProjectXMLManager freshTestingPXM = new ProjectXMLManager(testingProject);
190         
191         final Set JavaDoc<ModuleDependency> newDeps = freshTestingPXM.getDirectDependencies();
192         boolean tested = false;
193         for (ModuleDependency md : newDeps) {
194             if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
195                 tested = true;
196                 assertEquals("edited release version", "2", md.getReleaseVersion());
197                 assertEquals("unedited specification version", "6.2", md.getSpecificationVersion());
198                 break;
199             }
200         }
201         assertTrue("org.openide.dialogs dependency tested", tested);
202         validate(testingProject, false); // false - order is not touched after editing
203
}
204     
205     public void testAddDependencies() throws Exception JavaDoc {
206         final NbModuleProject testingProject = generateTestingProject();
207         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
208         final Set JavaDoc<ModuleDependency> newDeps = new HashSet JavaDoc<ModuleDependency>();
209         ModuleEntry me = testingProject.getModuleList().getEntry(
210                 "org.netbeans.modules.java.project");
211         assertNotNull("java/project must be built", me);
212         String JavaDoc javaProjectRV = me.getReleaseVersion();
213         String JavaDoc javaProjectSV = me.getSpecificationVersion();
214         newDeps.add(new ModuleDependency(me));
215         me = testingProject.getModuleList().getEntry("org.netbeans.modules.java.j2seplatform");
216         assertNotNull("java/j2seplatform must be built", me);
217         newDeps.add(new ModuleDependency(me, "1", null, false, true));
218         
219         // apply and save project
220
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
221             public Boolean JavaDoc run() throws IOException JavaDoc {
222                 testingPXM.addDependencies(newDeps);
223                 return true;
224             }
225         });
226         assertTrue("adding dependencies", result);
227         ProjectManager.getDefault().saveProject(testingProject);
228         
229         Set JavaDoc<ModuleDependency> deps = testingPXM.getDirectDependencies();
230         
231         Set JavaDoc<String JavaDoc> assumedCNBs = new HashSet JavaDoc<String JavaDoc>(ASSUMED_CNBS);
232         assumedCNBs.add("org.netbeans.modules.java.project");
233         assumedCNBs.add("org.netbeans.modules.java.j2seplatform");
234         
235         assertEquals("number of dependencies", deps.size(), assumedCNBs.size());
236         for (ModuleDependency md : deps) {
237             assertTrue("unknown dependency",
238                     assumedCNBs.remove(md.getModuleEntry().getCodeNameBase()));
239             if ("org.netbeans.modules.java.project".equals(md.getModuleEntry().getCodeNameBase())) {
240                 assertEquals("initial release version", javaProjectRV, md.getReleaseVersion());
241                 assertEquals("initial specification version", javaProjectSV, md.getSpecificationVersion());
242             }
243             if ("org.netbeans.modules.java.j2seplatform".equals(md.getModuleEntry().getCodeNameBase())) {
244                 assertEquals("edited release version", "1", md.getReleaseVersion());
245                 assertFalse("has compile depedendency", md.hasCompileDependency());
246                 assertTrue("has implementation depedendency", md.hasImplementationDepedendency());
247             }
248         }
249         assertTrue("following dependencies were found: " + assumedCNBs, assumedCNBs.isEmpty());
250         validate(testingProject, true);
251     }
252     
253     public void testExceptionWhenAddingTheSameModuleDependencyTwice() throws Exception JavaDoc {
254         final NbModuleProject testingProject = generateTestingProject();
255         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
256         ModuleEntry me = testingProject.getModuleList().getEntry(
257                 "org.netbeans.modules.java.project");
258         final ModuleDependency md = new ModuleDependency(me, "1", null, false, true);
259         boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
260             public Boolean JavaDoc run() throws IOException JavaDoc {
261                 Element JavaDoc confData = testingProject.getPrimaryConfigurationData();
262                 Element JavaDoc moduleDependencies = ProjectXMLManager.findModuleDependencies(confData);
263                 ProjectXMLManager.createModuleDependencyElement(moduleDependencies, md, null);
264                 ProjectXMLManager.createModuleDependencyElement(moduleDependencies, md, null);
265                 testingProject.putPrimaryConfigurationData(confData);
266                 return true;
267             }
268         });
269         assertTrue("adding dependencies", result);
270         ProjectManager.getDefault().saveProject(testingProject);
271         try {
272             testingPXM.getDirectDependencies();
273             fail("IllegalStateException was expected");
274         } catch (IllegalStateException JavaDoc ise) {
275             // OK, expected exception was thrown
276
}
277         validate(testingProject, false); // false - we are not using regular way for adding
278
}
279     
280     public void testFindPublicPackages() throws Exception JavaDoc {
281         final NbModuleProject testingProject = generateTestingProject();
282         final File JavaDoc projectXML = FileUtil.toFile(
283                 testingProject.getProjectDirectory().getFileObject("nbproject/project.xml"));
284         assert projectXML.exists();
285         Element JavaDoc confData = ProjectManager.mutex().readAccess(new Mutex.Action<Element JavaDoc>() {
286             public Element JavaDoc run() {
287                 Element JavaDoc data = null;
288                 try {
289                     Document JavaDoc doc = XMLUtil.parse(new InputSource JavaDoc(projectXML.toURI().toString()),
290                             false, true, null, null);
291                     Element JavaDoc project = doc.getDocumentElement();
292                     Element JavaDoc config = Util.findElement(project, "configuration", null); // NOI18N
293
data = Util.findElement(config, "data", NbModuleProjectType.NAMESPACE_SHARED);
294                 } catch (IOException JavaDoc e) {
295                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
296                 } catch (SAXException JavaDoc e) {
297                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
298                 }
299                 return data;
300             }
301         });
302         assertNotNull("finding configuration data element", confData);
303         ManifestManager.PackageExport[] pp = ProjectXMLManager.findPublicPackages(confData);
304         assertEquals("number of public packages", 1, pp.length);
305         assertEquals("public package", "org.netbeans.examples.modules.misc", pp[0].getPackage());
306         validate(testingProject, false); // false - just looking around
307
}
308     
309     public void testReplaceDependencies() throws Exception JavaDoc {
310         final NbModuleProject testingProject = generateTestingProject();
311         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
312         final SortedSet JavaDoc<ModuleDependency> deps = new TreeSet JavaDoc<ModuleDependency>(testingPXM.getDirectDependencies());
313         assertEquals("number of dependencies", 2, deps.size());
314         ModuleDependency newOO = null;
315         ModuleDependency oldOO = null;
316         for (Iterator JavaDoc<ModuleDependency> it = deps.iterator(); it.hasNext(); ) {
317             ModuleDependency md = it.next();
318             if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
319                 oldOO = md;
320                 ModuleEntry me = md.getModuleEntry();
321                 newOO = new ModuleDependency(me,
322                         "", // will be check if it is not written
323
oldOO.getSpecificationVersion(),
324                         md.hasCompileDependency(),
325                         md.hasImplementationDepedendency());
326                 it.remove();
327                 break;
328             }
329         }
330         deps.add(newOO);
331         
332         // apply and save project
333
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
334             public Boolean JavaDoc run() throws IOException JavaDoc {
335                 testingPXM.replaceDependencies(deps);
336                 return true;
337             }
338         });
339         assertTrue("project successfully saved", result);
340         ProjectManager.getDefault().saveProject(testingProject);
341         validate(testingProject, true);
342         
343         final ProjectXMLManager newTestingPXM = new ProjectXMLManager(testingProject);
344         final Set JavaDoc<ModuleDependency> newDeps = newTestingPXM.getDirectDependencies();
345         for (ModuleDependency md : newDeps) {
346             if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
347                 assertNull("empty(null) release version", md.getReleaseVersion());
348                 assertEquals("unedited specification version",
349                         oldOO.getSpecificationVersion(),
350                         md.getSpecificationVersion());
351                 break;
352             }
353         }
354     }
355     
356     public void testGetPublicPackages() throws Exception JavaDoc {
357         final NbModuleProject testingProject = generateTestingProject();
358         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
359         assertEquals("number of public packages", 1, testingPXM.getPublicPackages().length);
360         assertEquals("package name", "org.netbeans.examples.modules.misc", testingPXM.getPublicPackages()[0].getPackage());
361         assertFalse("not recursive", testingPXM.getPublicPackages()[0].isRecursive());
362         
363         ProjectXMLManager xercesPXM = createXercesPXM();
364         assertEquals("number of binary origins", 1, xercesPXM.getPublicPackages().length);
365         assertEquals("package name", "org", xercesPXM.getPublicPackages()[0].getPackage());
366         assertTrue("recursive", xercesPXM.getPublicPackages()[0].isRecursive());
367     }
368     
369     public void testReplacePublicPackages() throws Exception JavaDoc {
370         final NbModuleProject testingProject = generateTestingProject();
371         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
372         ManifestManager.PackageExport[] publicPackages = testingPXM.getPublicPackages();
373         assertEquals("number of public packages", 1, publicPackages.length);
374         final String JavaDoc[] newPP = new String JavaDoc[] { publicPackages[0].getPackage(), "org.netbeans.examples.modules" };
375         
376         // apply and save project
377
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
378             public Boolean JavaDoc run() throws IOException JavaDoc {
379                 testingPXM.replacePublicPackages(newPP);
380                 return true;
381             }
382         });
383         assertTrue("replace public packages", result);
384         ProjectManager.getDefault().saveProject(testingProject);
385         ManifestManager.PackageExport[] newPublicPackages = testingPXM.getPublicPackages();
386         assertEquals("number of new public packages", 2, newPublicPackages.length);
387         Collection JavaDoc newPPs = Arrays.asList(new String JavaDoc[] {"org.netbeans.examples.modules", "org.netbeans.examples.modules.misc"});
388         assertTrue(newPPs.contains(newPublicPackages[0].getPackage()));
389         assertTrue(newPPs.contains(newPublicPackages[1].getPackage()));
390         assertNull("there must not be friend", testingPXM.getFriends());
391         validate(testingProject, false); // false - just replacing public packages
392
}
393     
394     public void testReplaceFriends() throws Exception JavaDoc {
395         final NbModuleProject testingProject = generateTestingProject();
396         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
397         assertEquals("one friend", 1, testingPXM.getFriends().length);
398         assertEquals("friend org.module.examplemodule", "org.module.examplemodule", testingPXM.getFriends()[0]);
399         final String JavaDoc[] newFriends = new String JavaDoc[] { "org.exampleorg.somefriend", "org.exampleorg.anotherfriend" };
400         
401         // apply and save project
402
boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
403             public Boolean JavaDoc run() throws IOException JavaDoc {
404                 ManifestManager.PackageExport pkgs[] = testingPXM.getPublicPackages();
405                 String JavaDoc[] packagesToExpose = new String JavaDoc[pkgs.length];
406                 for (int i = 0; i < pkgs.length; i++) {
407                     packagesToExpose[i] = pkgs[i].getPackage();
408                 }
409                 testingPXM.replaceFriends(newFriends, packagesToExpose);
410                 return true;
411             }
412         });
413         assertTrue("replace friends", result);
414         ProjectManager.getDefault().saveProject(testingProject);
415         final ProjectXMLManager newTestingPXM = new ProjectXMLManager(testingProject);
416         String JavaDoc[] actualFriends = newTestingPXM.getFriends();
417         assertEquals("number of new friend", 2, actualFriends.length);
418         Collection JavaDoc newFriendsCNBs = Arrays.asList(actualFriends);
419         assertTrue(newFriendsCNBs.contains(newFriends[0]));
420         assertTrue(newFriendsCNBs.contains(newFriends[1]));
421         assertEquals("public packages", 1, newTestingPXM.getPublicPackages().length);
422         validate(testingProject, false); // false - just replacing friends
423
}
424     
425     public void testGetBinaryOrigins() throws Exception JavaDoc {
426         ProjectXMLManager xercesPXM = createXercesPXM();
427         assertEquals("number of binary origins", 2, xercesPXM.getBinaryOrigins().length);
428     }
429     
430     public void testThatFriendPackagesAreGeneratedInTheRightOrder_61882() throws Exception JavaDoc {
431         FileObject fo = TestBase.generateStandaloneModuleDirectory(getWorkDir(), "testing");
432         FileObject projectXMLFO = fo.getFileObject("nbproject/project.xml");
433         String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
434                 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n" +
435                 "<type>org.netbeans.modules.apisupport.project</type>\n" +
436                 "<configuration>\n" +
437                 "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n" +
438                 "<code-name-base>org.netbeans.modules.j2eeapis</code-name-base>\n" +
439                 "<standalone/>\n" +
440                 "<module-dependencies/>\n" +
441                 "<public-packages>\n" +
442                 "<subpackages>javax.enterprise.deploy</subpackages>\n" +
443                 "</public-packages>\n" +
444                 "<class-path-extension>\n" +
445                 "<runtime-relative-path>ext/jsr88javax.jar</runtime-relative-path>\n" +
446                 "<binary-origin>../external/jsr88javax.jar</binary-origin>\n" +
447                 "</class-path-extension>\n" +
448                 "</data>\n" +
449                 "</configuration>\n" +
450                 "</project>\n";
451         TestBase.dump(projectXMLFO, xml);
452         NbModuleProject project = (NbModuleProject) ProjectManager.getDefault().findProject(fo);
453         validate(project, true);
454     }
455     
456     public void testDependenciesOrder() throws Exception JavaDoc { // #62003
457
final NbModuleProject testingProject = generateTestingProject();
458         final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject);
459         ModuleEntry me = testingProject.getModuleList().getEntry(
460                 "org.netbeans.modules.java.project");
461         final ModuleDependency md = new ModuleDependency(me, "1", null, false, true);
462         boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean JavaDoc>() {
463             public Boolean JavaDoc run() throws IOException JavaDoc {
464                 testingPXM.addDependency(md);
465                 return true;
466             }
467         });
468         assertTrue("adding dependencies", result);
469         ProjectManager.getDefault().saveProject(testingProject);
470         validate(testingProject, true);
471     }
472     
473   
474     public void testRemoveTestDependency() throws Exception JavaDoc {
475         final NbModuleProject testingProject = generateTestingProject();
476         final ProjectXMLManager pxm = new ProjectXMLManager(testingProject);
477         final String JavaDoc cnb = "org.netbeans.modules.java.project";
478         final String JavaDoc cnb2 = "org.netbeans.modules.project.ant";
479         File JavaDoc projectDir = FileUtil.toFile(testingProject.getProjectDirectory());
480         ModuleList ml = ModuleList.getModuleList(projectDir);
481         ModuleEntry meJP = testingProject.getModuleList().getEntry(cnb);
482         ModuleEntry meAnt = testingProject.getModuleList().getEntry(cnb2);
483         TestModuleDependency tdJP_001 = new TestModuleDependency(meJP, false, false, true);
484         TestModuleDependency tdAnt_111 = new TestModuleDependency(meAnt, true, true, true);
485         //add two unit test dependencies
486
pxm.addTestDependency(UNIT, tdJP_001);
487         pxm.addTestDependency(UNIT, tdAnt_111);
488         ProjectManager.getDefault().saveProject(testingProject);
489         //try wrong usage of remove
490
assertFalse("no such cnb under QA func.", pxm.removeTestDependency(QA_FUNCTIONAL, cnb));
491         assertFalse("no such cnb under UNIT func.", pxm.removeTestDependency(UNIT, "someCNB"));
492         Set JavaDoc<TestModuleDependency> setBefore = pxm.getTestDependencies(ml).get(UNIT);
493         assertEquals("unit test type contains two TD", 2 , setBefore.size());
494         //remove first one
495
assertTrue("one should be found && removed", pxm.removeTestDependency(UNIT, cnb));
496         ProjectManager.getDefault().saveProject(testingProject);
497         //try to remove just removed
498
assertFalse("this was just removed", pxm.removeTestDependency(UNIT, cnb));
499         Set JavaDoc<TestModuleDependency> setNow = pxm.getTestDependencies(ml).get(UNIT);
500         assertEquals("unit test type contains one TD", 1 , setNow.size());
501         //remove last one
502
assertTrue("all unit test deps have been removed", pxm.removeTestDependency(UNIT, cnb2));
503         ProjectManager.getDefault().saveProject(testingProject);
504         Set JavaDoc<TestModuleDependency> setAfter = pxm.getTestDependencies(ml).get(UNIT);
505         assertTrue("unit test type is empty now", setAfter.isEmpty());
506     }
507     
508     public void testAddTestDependency () throws Exception JavaDoc{
509         final NbModuleProject testingProject = generateTestingProject();
510         final ProjectXMLManager pxm = new ProjectXMLManager(testingProject);
511         File JavaDoc projectDir = FileUtil.toFile(testingProject.getProjectDirectory());
512         ModuleList ml = ModuleList.getModuleList(projectDir);
513         ModuleEntry meJP = testingProject.getModuleList().getEntry(
514                 "org.netbeans.modules.java.project");
515         ModuleEntry meAnt = testingProject.getModuleList().getEntry(
516                 "org.netbeans.modules.project.ant");
517         ModuleEntry meDialogs = testingProject.getModuleList().getEntry(
518                 "org.openide.dialogs");
519         
520         TestModuleDependency tdJP_001 = new TestModuleDependency(meJP, false, false, true);
521         TestModuleDependency tdJP_010 = new TestModuleDependency(meJP, false, true, false);
522         TestModuleDependency tdAnt_111 = new TestModuleDependency(meAnt, true, true, true);
523         TestModuleDependency tdDialogs_000 = new TestModuleDependency(meDialogs, false, false, false);
524         
525         Map JavaDoc<String JavaDoc,Set JavaDoc<TestModuleDependency>> mapOfTD = pxm.getTestDependencies(ml);
526         
527         assertTrue("currently no TD", mapOfTD.isEmpty());
528         //first, add one unit test dep
529
pxm.addTestDependency(UNIT, tdJP_001);
530         ProjectManager.getDefault().saveProject(testingProject);
531         mapOfTD = pxm.getTestDependencies(ml);
532         assertEquals("map has already unit test type", 1, mapOfTD.size());
533         Set JavaDoc<TestModuleDependency> unitTD = mapOfTD.get(UNIT);
534         Set JavaDoc<TestModuleDependency> qafuncTD = mapOfTD.get(QA_FUNCTIONAL);
535         assertEquals("set with unit TD has one TD", 1, unitTD.size());
536         assertNull("set with qafunc TD does not exist", qafuncTD);
537         //now add 2 other unit test dep;
538
pxm.addTestDependency(UNIT, tdDialogs_000);
539         ProjectManager.getDefault().saveProject(testingProject);
540         pxm.addTestDependency(UNIT, tdAnt_111);
541         ProjectManager.getDefault().saveProject(testingProject);
542         mapOfTD = pxm.getTestDependencies(ml);
543         assertEquals("map still has only unit test type", 1, mapOfTD.size());
544         unitTD = mapOfTD.get(UNIT);
545         assertEquals("set with unit TD has now three TD", 3, unitTD.size());
546         //now add qa-func test dependency
547
pxm.addTestDependency(QA_FUNCTIONAL, tdJP_010);
548         ProjectManager.getDefault().saveProject(testingProject);
549         mapOfTD = pxm.getTestDependencies(ml);
550         unitTD = mapOfTD.get(UNIT);
551         qafuncTD = mapOfTD.get(QA_FUNCTIONAL);
552         assertEquals("map has both test types", 2, mapOfTD.size());
553         assertEquals("set with unit TD has still three TD", 3, unitTD.size());
554         assertEquals("set with qafunc TD has one TD", 1, qafuncTD.size());
555         //TODO: rewrite order checking method to be able to check properly test dependencies order
556
validate(testingProject, false);
557     }
558     
559     public void testIssue92363FixAddDependencyWhereSomeIsAlreadyPresent() throws Exception JavaDoc{
560         String JavaDoc testDependencies = "\n" +
561                 "<test-type>\n" +
562                 "<name>unit</name>\n" +
563                 "<test-dependency>\n" +
564                 "<code-name-base>org.netbeans.core</code-name-base>\n" +
565                 "</test-dependency>\n" +
566                 "</test-type>\n";
567         //create a project that already contains testdependency
568
final NbModuleProject testingProject = generateTestingProject(testDependencies);
569         final ProjectXMLManager pxm = new ProjectXMLManager(testingProject);
570         ModuleList ml = ModuleList.getModuleList(testingProject.getProjectDirectoryFile());
571         Map JavaDoc<String JavaDoc,Set JavaDoc<TestModuleDependency>> testDeps = pxm.getTestDependencies(ml);
572         assertEquals("map has already unit test type", 1, testDeps.size());
573         Set JavaDoc<TestModuleDependency> setUnit = testDeps.get(UNIT);
574         assertEquals("contains one dependency", 1, setUnit.size());
575         //now add one more testdependency
576
ModuleEntry meJP = testingProject.getModuleList().getEntry(
577                 "org.netbeans.modules.java.project");
578         TestModuleDependency tdJP = new TestModuleDependency(meJP, false, false, true);
579         pxm.addTestDependency(UNIT, tdJP);
580         ProjectManager.getDefault().saveProject(testingProject);
581         testDeps = pxm.getTestDependencies(ml);
582         assertEquals("map has already unit test type", 1, testDeps.size());
583         setUnit = testDeps.get(UNIT);
584         assertEquals("contains two dependencies now", 2, setUnit.size());
585         validate(testingProject, false);
586     }
587     
588     private NbModuleProject generateTestingProject() throws Exception JavaDoc {
589         return generateTestingProject("");
590     }
591     
592     private NbModuleProject generateTestingProject(final String JavaDoc testDependencies) throws Exception JavaDoc {
593         FileObject fo = TestBase.generateStandaloneModuleDirectory(getWorkDir(), "testing");
594         FileObject projectXMLFO = fo.getFileObject("nbproject/project.xml");
595         String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
596                 "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n" +
597                 "<type>org.netbeans.modules.apisupport.project</type>\n" +
598                 "<configuration>\n" +
599                 "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n" +
600                 "<code-name-base>org.example.testing</code-name-base>\n" +
601                 "<standalone/>\n" +
602                 "<module-dependencies>\n" +
603                 "<dependency>\n" +
604                 "<code-name-base>" + DIALOGS + "</code-name-base>\n" +
605                 "<build-prerequisite/>\n" +
606                 "<compile-dependency/>\n" +
607                 "<run-dependency>\n" +
608                 "<specification-version>6.2</specification-version>\n" +
609                 "</run-dependency>\n" +
610                 "</dependency>\n" +
611                 "<dependency>\n" +
612                 "<code-name-base>" + ANT_PROJECT_SUPPORT + "</code-name-base>\n" +
613                 "<build-prerequisite/>\n" +
614                 "<compile-dependency/>\n" +
615                 "<run-dependency>\n" +
616                 "<release-version>1</release-version>\n" +
617                 "<specification-version>1.10</specification-version>\n" +
618                 "</run-dependency>\n" +
619                 "</dependency>\n" +
620                 "</module-dependencies>\n" +
621                 "<test-dependencies>"+ testDependencies + "</test-dependencies>\n" +
622                 "<friend-packages>\n" +
623                 "<friend>org.module.examplemodule</friend>\n" +
624                 "<package>org.netbeans.examples.modules.misc</package>\n" +
625                 "</friend-packages>\n" +
626                 "<class-path-extension>\n" +
627                 "<runtime-relative-path>ext/jsr88javax.jar</runtime-relative-path>\n" +
628                 "<binary-origin>../external/jsr88javax.jar</binary-origin>\n" +
629                 "</class-path-extension>\n" +
630                 "</data>\n" +
631                 "</configuration>\n" +
632                 "</project>\n";
633         TestBase.dump(projectXMLFO, xml);
634         return (NbModuleProject) ProjectManager.getDefault().findProject(fo);
635     }
636     
637     // below is stolen from ant/freeform
638
private static String JavaDoc[] getSchemas() throws Exception JavaDoc {
639         String JavaDoc[] URIs = new String JavaDoc[3];
640         URIs[0] = ProjectXMLManager.class.getResource("resources/nb-module-project2.xsd").toExternalForm();
641         URIs[1] = ProjectXMLManager.class.getResource("resources/nb-module-project3.xsd").toExternalForm();
642         URIs[2] = AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm();
643         return URIs;
644     }
645     
646     public static void validate(final Project proj, final boolean checkOrder) throws Exception JavaDoc {
647         File JavaDoc projF = FileUtil.toFile(proj.getProjectDirectory());
648         File JavaDoc xml = new File JavaDoc(new File JavaDoc(projF, "nbproject"), "project.xml");
649         SAXParserFactory JavaDoc f = (SAXParserFactory JavaDoc)Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
650         if (f == null) {
651             System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
652             return;
653         }
654         f.setNamespaceAware(true);
655         f.setValidating(true);
656         SAXParser JavaDoc p = f.newSAXParser();
657         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
658                 "http://www.w3.org/2001/XMLSchema");
659         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas());
660         try {
661             p.parse(xml.toURI().toString(), new Handler JavaDoc());
662         } catch (SAXParseException JavaDoc e) {
663             fail("Validation of XML document " + xml + " against schema failed. Details: " +
664                     e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage());
665         }
666         if (checkOrder) {
667             checkDependenciesOrder(proj);
668         }
669     }
670     
671     private static void checkDependenciesOrder(final Project proj) throws Exception JavaDoc {
672         FileObject projectXML = proj.getProjectDirectory().getFileObject("nbproject/project.xml");
673         BufferedReader JavaDoc r = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(projectXML.getInputStream()));
674         try {
675             String JavaDoc previousCNB = null;
676             String JavaDoc line;
677             while ((line = r.readLine()) != null) {
678                 line = line.trim();
679                 if (line.matches("<code-name-base>.+</code-name-base>")) {
680                     String JavaDoc currentCNB = line.substring(16, line.length() - 17);
681                     assertTrue("dependencies order, previous = \"" + previousCNB + "\", current = \"" + currentCNB + "\"",
682                             previousCNB == null || previousCNB.compareTo(currentCNB) < 0);
683                     previousCNB = currentCNB;
684                 }
685             }
686         } finally {
687             r.close();
688         }
689     }
690     
691     private static final class Handler extends DefaultHandler JavaDoc {
692         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
693             throw e;
694         }
695         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
696             throw e;
697         }
698         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
699             throw e;
700         }
701     }
702         
703 }
704
705
Popular Tags