001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project;
043:
044: import java.io.BufferedReader;
045: import java.io.File;
046: import java.io.IOException;
047: import java.io.InputStreamReader;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.Collections;
051: import java.util.HashSet;
052: import java.util.Iterator;
053: import java.util.Map;
054: import java.util.Set;
055: import java.util.SortedSet;
056: import java.util.TreeSet;
057: import javax.xml.parsers.SAXParser;
058: import javax.xml.parsers.SAXParserFactory;
059: import org.netbeans.api.project.Project;
060: import org.netbeans.api.project.ProjectManager;
061: import org.netbeans.modules.apisupport.project.ui.customizer.ModuleDependency;
062: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
063: import org.netbeans.modules.apisupport.project.universe.ModuleList;
064: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
065: import org.netbeans.modules.apisupport.project.universe.TestModuleDependency;
066: import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
067: import org.openide.ErrorManager;
068: import org.openide.filesystems.FileObject;
069: import org.openide.filesystems.FileUtil;
070: import org.openide.util.Mutex;
071: import org.openide.util.MutexException;
072: import org.openide.xml.XMLUtil;
073: import org.w3c.dom.Document;
074: import org.w3c.dom.Element;
075: import org.xml.sax.InputSource;
076: import org.xml.sax.SAXException;
077: import org.xml.sax.SAXParseException;
078: import org.xml.sax.helpers.DefaultHandler;
079: import static org.netbeans.modules.apisupport.project.universe.TestModuleDependency.UNIT;
080: import static org.netbeans.modules.apisupport.project.universe.TestModuleDependency.QA_FUNCTIONAL;
081:
082: /**
083: * Tests ProjectXMLManager class.
084: *
085: * @author Martin Krauskopf
086: */
087: public class ProjectXMLManagerTest extends TestBase {
088:
089: private final static String ANT_PROJECT_SUPPORT = "org.netbeans.modules.project.ant";
090: private final static String DIALOGS = "org.openide.dialogs";
091: private final static Set<String> ASSUMED_CNBS;
092:
093: static {
094: Set<String> assumedCNBs = new HashSet<String>(2, 1.0f);
095: assumedCNBs.add(ANT_PROJECT_SUPPORT);
096: assumedCNBs.add(DIALOGS);
097: ASSUMED_CNBS = Collections.unmodifiableSet(assumedCNBs);
098: }
099:
100: public ProjectXMLManagerTest(String testName) {
101: super (testName);
102: }
103:
104: protected void setUp() throws Exception {
105: clearWorkDir();
106: super .setUp();
107: }
108:
109: private ProjectXMLManager createXercesPXM() throws IOException {
110: NbModuleProject xercesPrj = (NbModuleProject) ProjectManager
111: .getDefault().findProject(
112: nbRoot().getFileObject("libs.xerces"));
113: return new ProjectXMLManager(xercesPrj);
114: }
115:
116: // sanity check
117: public void testGeneratedProject() throws Exception {
118: validate(generateTestingProject(), false); // false - original project.xml is unordered
119: }
120:
121: public void testGetCodeNameBase() throws Exception {
122: NbModuleProject p = generateStandaloneModule("module1");
123: assertEquals("action-project cnb", "org.example.module1", p
124: .getCodeNameBase());
125: }
126:
127: public void testGetDirectDependencies() throws Exception {
128: final NbModuleProject testingProject = generateTestingProject();
129: ProjectXMLManager testingPXM = new ProjectXMLManager(
130: testingProject);
131: Set<ModuleDependency> deps = testingPXM.getDirectDependencies();
132: assertEquals("number of dependencies", 2, deps.size());
133:
134: Set<String> assumedCNBs = new HashSet<String>(ASSUMED_CNBS);
135: for (ModuleDependency md : deps) {
136: if (md.getModuleEntry().getCodeNameBase().equals(DIALOGS)) {
137: assertNotNull("module entry", md.getModuleEntry());
138: assertEquals("release version", null, md
139: .getReleaseVersion());
140: assertEquals("specification version", "6.2", md
141: .getSpecificationVersion());
142: }
143: if (md.getModuleEntry().getCodeNameBase().equals(
144: ANT_PROJECT_SUPPORT)) {
145: assertNotNull("module entry", md.getModuleEntry());
146: assertEquals("release version", "1", md
147: .getReleaseVersion());
148: assertEquals("specification version", "1.10", md
149: .getSpecificationVersion());
150: }
151: String cnbToRemove = md.getModuleEntry().getCodeNameBase();
152: assertTrue("unknown dependency: " + cnbToRemove,
153: assumedCNBs.remove(cnbToRemove));
154: }
155: assertTrue("following dependencies were found: " + assumedCNBs,
156: assumedCNBs.isEmpty());
157: }
158:
159: public void testGetDirectDependenciesForCustomPlatform()
160: throws Exception {
161: final NbModuleProject testingProject = generateTestingProject();
162: ProjectXMLManager testingPXM = new ProjectXMLManager(
163: testingProject);
164: Set deps = testingPXM.getDirectDependencies();
165: assertEquals("number of dependencies", 2, deps.size());
166: Set depsWithCustom = testingPXM
167: .getDirectDependencies(NbPlatform
168: .getPlatformByID("custom"));
169: assertEquals("number of dependencies", 0, depsWithCustom.size());
170: }
171:
172: public void testRemoveDependency() throws Exception {
173: final NbModuleProject testingProject = generateTestingProject();
174: final ProjectXMLManager testingPXM = new ProjectXMLManager(
175: testingProject);
176: // apply and save project
177: boolean result = ProjectManager.mutex().writeAccess(
178: new Mutex.ExceptionAction<Boolean>() {
179: public Boolean run() throws IOException {
180: testingPXM.removeDependency(DIALOGS);
181: return true;
182: }
183: });
184: assertTrue("removing dependency", result);
185: ProjectManager.getDefault().saveProject(testingProject);
186:
187: final Set<ModuleDependency> newDeps = testingPXM
188: .getDirectDependencies();
189: assertEquals("number of dependencies", 1, newDeps.size());
190: Set<String> newCNBs = new HashSet<String>();
191: newCNBs.add(ANT_PROJECT_SUPPORT);
192: for (ModuleDependency md : newDeps) {
193: String cnbToRemove = md.getModuleEntry().getCodeNameBase();
194: assertTrue("unknown dependency: " + cnbToRemove, newCNBs
195: .remove(cnbToRemove));
196: }
197: assertTrue("following dependencies were found: " + newCNBs,
198: newCNBs.isEmpty());
199: validate(testingProject, true);
200: }
201:
202: public void testEditDependency() throws Exception {
203: final NbModuleProject testingProject = generateTestingProject();
204: final ProjectXMLManager testingPXM = new ProjectXMLManager(
205: testingProject);
206: final Set<ModuleDependency> deps = testingPXM
207: .getDirectDependencies();
208:
209: // apply and save project
210: boolean result = ProjectManager.mutex().writeAccess(
211: new Mutex.ExceptionAction<Boolean>() {
212: public Boolean run() throws IOException {
213: boolean tested = false;
214: for (ModuleDependency origDep : deps) {
215: if (DIALOGS.equals(origDep.getModuleEntry()
216: .getCodeNameBase())) {
217: tested = true;
218: ModuleDependency newDep = new ModuleDependency(
219: origDep.getModuleEntry(),
220: "2",
221: origDep
222: .getSpecificationVersion(),
223: origDep.hasCompileDependency(),
224: origDep
225: .hasImplementationDepedendency());
226: testingPXM.editDependency(origDep,
227: newDep);
228: }
229: }
230: assertTrue(
231: "org.openide.dialogs dependency tested",
232: tested);
233: return true;
234: }
235: });
236: assertTrue("editing dependencies", result);
237: ProjectManager.getDefault().saveProject(testingProject);
238: // XXX this refresh shouldn't be needed (should listen on project.xml changes)
239: ProjectXMLManager freshTestingPXM = new ProjectXMLManager(
240: testingProject);
241:
242: final Set<ModuleDependency> newDeps = freshTestingPXM
243: .getDirectDependencies();
244: boolean tested = false;
245: for (ModuleDependency md : newDeps) {
246: if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
247: tested = true;
248: assertEquals("edited release version", "2", md
249: .getReleaseVersion());
250: assertEquals("unedited specification version", "6.2",
251: md.getSpecificationVersion());
252: break;
253: }
254: }
255: assertTrue("org.openide.dialogs dependency tested", tested);
256: validate(testingProject, false); // false - order is not touched after editing
257: }
258:
259: public void testAddDependencies() throws Exception {
260: final NbModuleProject testingProject = generateTestingProject();
261: final ProjectXMLManager testingPXM = new ProjectXMLManager(
262: testingProject);
263: final Set<ModuleDependency> newDeps = new HashSet<ModuleDependency>();
264: ModuleEntry me = testingProject.getModuleList().getEntry(
265: "org.netbeans.modules.java.project");
266: assertNotNull("java/project must be built", me);
267: String javaProjectRV = me.getReleaseVersion();
268: String javaProjectSV = me.getSpecificationVersion();
269: newDeps.add(new ModuleDependency(me));
270: me = testingProject.getModuleList().getEntry(
271: "org.netbeans.modules.java.j2seplatform");
272: assertNotNull("java/j2seplatform must be built", me);
273: newDeps.add(new ModuleDependency(me, "1", null, false, true));
274:
275: // apply and save project
276: boolean result = ProjectManager.mutex().writeAccess(
277: new Mutex.ExceptionAction<Boolean>() {
278: public Boolean run() throws IOException {
279: testingPXM.addDependencies(newDeps);
280: return true;
281: }
282: });
283: assertTrue("adding dependencies", result);
284: ProjectManager.getDefault().saveProject(testingProject);
285:
286: Set<ModuleDependency> deps = testingPXM.getDirectDependencies();
287:
288: Set<String> assumedCNBs = new HashSet<String>(ASSUMED_CNBS);
289: assumedCNBs.add("org.netbeans.modules.java.project");
290: assumedCNBs.add("org.netbeans.modules.java.j2seplatform");
291:
292: assertEquals("number of dependencies", deps.size(), assumedCNBs
293: .size());
294: for (ModuleDependency md : deps) {
295: assertTrue("unknown dependency", assumedCNBs.remove(md
296: .getModuleEntry().getCodeNameBase()));
297: if ("org.netbeans.modules.java.project".equals(md
298: .getModuleEntry().getCodeNameBase())) {
299: assertEquals("initial release version", javaProjectRV,
300: md.getReleaseVersion());
301: assertEquals("initial specification version",
302: javaProjectSV, md.getSpecificationVersion());
303: }
304: if ("org.netbeans.modules.java.j2seplatform".equals(md
305: .getModuleEntry().getCodeNameBase())) {
306: assertEquals("edited release version", "1", md
307: .getReleaseVersion());
308: assertFalse("has compile depedendency", md
309: .hasCompileDependency());
310: assertTrue("has implementation depedendency", md
311: .hasImplementationDepedendency());
312: }
313: }
314: assertTrue("following dependencies were found: " + assumedCNBs,
315: assumedCNBs.isEmpty());
316: validate(testingProject, true);
317: }
318:
319: public void testExceptionWhenAddingTheSameModuleDependencyTwice()
320: throws Exception {
321: final NbModuleProject testingProject = generateTestingProject();
322: final ProjectXMLManager testingPXM = new ProjectXMLManager(
323: testingProject);
324: ModuleEntry me = testingProject.getModuleList().getEntry(
325: "org.netbeans.modules.java.project");
326: final ModuleDependency md = new ModuleDependency(me, "1", null,
327: false, true);
328: boolean result = ProjectManager.mutex().writeAccess(
329: new Mutex.ExceptionAction<Boolean>() {
330: public Boolean run() throws IOException {
331: Element confData = testingProject
332: .getPrimaryConfigurationData();
333: Element moduleDependencies = ProjectXMLManager
334: .findModuleDependencies(confData);
335: ProjectXMLManager
336: .createModuleDependencyElement(
337: moduleDependencies, md, null);
338: ProjectXMLManager
339: .createModuleDependencyElement(
340: moduleDependencies, md, null);
341: testingProject
342: .putPrimaryConfigurationData(confData);
343: return true;
344: }
345: });
346: assertTrue("adding dependencies", result);
347: ProjectManager.getDefault().saveProject(testingProject);
348: try {
349: testingPXM.getDirectDependencies();
350: fail("IllegalStateException was expected");
351: } catch (IllegalStateException ise) {
352: // OK, expected exception was thrown
353: }
354: validate(testingProject, false); // false - we are not using regular way for adding
355: }
356:
357: public void testFindPublicPackages() throws Exception {
358: final NbModuleProject testingProject = generateTestingProject();
359: final File projectXML = FileUtil.toFile(testingProject
360: .getProjectDirectory().getFileObject(
361: "nbproject/project.xml"));
362: assert projectXML.exists();
363: Element confData = ProjectManager.mutex().readAccess(
364: new Mutex.Action<Element>() {
365: public Element run() {
366: Element data = null;
367: try {
368: Document doc = XMLUtil.parse(
369: new InputSource(projectXML.toURI()
370: .toString()), false, true,
371: null, null);
372: Element project = doc.getDocumentElement();
373: Element config = Util.findElement(project,
374: "configuration", null); // NOI18N
375: data = Util
376: .findElement(
377: config,
378: "data",
379: NbModuleProjectType.NAMESPACE_SHARED);
380: } catch (IOException e) {
381: ErrorManager.getDefault().notify(
382: ErrorManager.INFORMATIONAL, e);
383: } catch (SAXException e) {
384: ErrorManager.getDefault().notify(
385: ErrorManager.INFORMATIONAL, e);
386: }
387: return data;
388: }
389: });
390: assertNotNull("finding configuration data element", confData);
391: ManifestManager.PackageExport[] pp = ProjectXMLManager
392: .findPublicPackages(confData);
393: assertEquals("number of public packages", 1, pp.length);
394: assertEquals("public package",
395: "org.netbeans.examples.modules.misc", pp[0]
396: .getPackage());
397: validate(testingProject, false); // false - just looking around
398: }
399:
400: public void testReplaceDependencies() throws Exception {
401: final NbModuleProject testingProject = generateTestingProject();
402: final ProjectXMLManager testingPXM = new ProjectXMLManager(
403: testingProject);
404: final SortedSet<ModuleDependency> deps = new TreeSet<ModuleDependency>(
405: testingPXM.getDirectDependencies());
406: assertEquals("number of dependencies", 2, deps.size());
407: ModuleDependency newOO = null;
408: ModuleDependency oldOO = null;
409: for (Iterator<ModuleDependency> it = deps.iterator(); it
410: .hasNext();) {
411: ModuleDependency md = it.next();
412: if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
413: oldOO = md;
414: ModuleEntry me = md.getModuleEntry();
415: newOO = new ModuleDependency(me, "", // will be check if it is not written
416: oldOO.getSpecificationVersion(), md
417: .hasCompileDependency(), md
418: .hasImplementationDepedendency());
419: it.remove();
420: break;
421: }
422: }
423: deps.add(newOO);
424:
425: // apply and save project
426: boolean result = ProjectManager.mutex().writeAccess(
427: new Mutex.ExceptionAction<Boolean>() {
428: public Boolean run() throws IOException {
429: testingPXM.replaceDependencies(deps);
430: return true;
431: }
432: });
433: assertTrue("project successfully saved", result);
434: ProjectManager.getDefault().saveProject(testingProject);
435: validate(testingProject, true);
436:
437: final ProjectXMLManager newTestingPXM = new ProjectXMLManager(
438: testingProject);
439: final Set<ModuleDependency> newDeps = newTestingPXM
440: .getDirectDependencies();
441: for (ModuleDependency md : newDeps) {
442: if (DIALOGS.equals(md.getModuleEntry().getCodeNameBase())) {
443: assertNull("empty(null) release version", md
444: .getReleaseVersion());
445: assertEquals("unedited specification version", oldOO
446: .getSpecificationVersion(), md
447: .getSpecificationVersion());
448: break;
449: }
450: }
451: }
452:
453: public void testGetPublicPackages() throws Exception {
454: final NbModuleProject testingProject = generateTestingProject();
455: final ProjectXMLManager testingPXM = new ProjectXMLManager(
456: testingProject);
457: assertEquals("number of public packages", 1, testingPXM
458: .getPublicPackages().length);
459: assertEquals("package name",
460: "org.netbeans.examples.modules.misc", testingPXM
461: .getPublicPackages()[0].getPackage());
462: assertFalse("not recursive", testingPXM.getPublicPackages()[0]
463: .isRecursive());
464:
465: ProjectXMLManager xercesPXM = createXercesPXM();
466: assertEquals("number of binary origins", 1, xercesPXM
467: .getPublicPackages().length);
468: assertEquals("package name", "org", xercesPXM
469: .getPublicPackages()[0].getPackage());
470: assertTrue("recursive", xercesPXM.getPublicPackages()[0]
471: .isRecursive());
472: }
473:
474: public void testReplacePublicPackages() throws Exception {
475: final NbModuleProject testingProject = generateTestingProject();
476: final ProjectXMLManager testingPXM = new ProjectXMLManager(
477: testingProject);
478: ManifestManager.PackageExport[] publicPackages = testingPXM
479: .getPublicPackages();
480: assertEquals("number of public packages", 1,
481: publicPackages.length);
482: final String[] newPP = new String[] {
483: publicPackages[0].getPackage(),
484: "org.netbeans.examples.modules" };
485:
486: // apply and save project
487: boolean result = ProjectManager.mutex().writeAccess(
488: new Mutex.ExceptionAction<Boolean>() {
489: public Boolean run() throws IOException {
490: testingPXM.replacePublicPackages(newPP);
491: return true;
492: }
493: });
494: assertTrue("replace public packages", result);
495: ProjectManager.getDefault().saveProject(testingProject);
496: ManifestManager.PackageExport[] newPublicPackages = testingPXM
497: .getPublicPackages();
498: assertEquals("number of new public packages", 2,
499: newPublicPackages.length);
500: Collection newPPs = Arrays.asList(new String[] {
501: "org.netbeans.examples.modules",
502: "org.netbeans.examples.modules.misc" });
503: assertTrue(newPPs.contains(newPublicPackages[0].getPackage()));
504: assertTrue(newPPs.contains(newPublicPackages[1].getPackage()));
505: assertNull("there must not be friend", testingPXM.getFriends());
506: validate(testingProject, false); // false - just replacing public packages
507: }
508:
509: public void testReplaceFriends() throws Exception {
510: final NbModuleProject testingProject = generateTestingProject();
511: final ProjectXMLManager testingPXM = new ProjectXMLManager(
512: testingProject);
513: assertEquals("one friend", 1, testingPXM.getFriends().length);
514: assertEquals("friend org.module.examplemodule",
515: "org.module.examplemodule", testingPXM.getFriends()[0]);
516: final String[] newFriends = new String[] {
517: "org.exampleorg.somefriend",
518: "org.exampleorg.anotherfriend" };
519:
520: // apply and save project
521: boolean result = ProjectManager.mutex().writeAccess(
522: new Mutex.ExceptionAction<Boolean>() {
523: public Boolean run() throws IOException {
524: ManifestManager.PackageExport pkgs[] = testingPXM
525: .getPublicPackages();
526: String[] packagesToExpose = new String[pkgs.length];
527: for (int i = 0; i < pkgs.length; i++) {
528: packagesToExpose[i] = pkgs[i].getPackage();
529: }
530: testingPXM.replaceFriends(newFriends,
531: packagesToExpose);
532: return true;
533: }
534: });
535: assertTrue("replace friends", result);
536: ProjectManager.getDefault().saveProject(testingProject);
537: final ProjectXMLManager newTestingPXM = new ProjectXMLManager(
538: testingProject);
539: String[] actualFriends = newTestingPXM.getFriends();
540: assertEquals("number of new friend", 2, actualFriends.length);
541: Collection newFriendsCNBs = Arrays.asList(actualFriends);
542: assertTrue(newFriendsCNBs.contains(newFriends[0]));
543: assertTrue(newFriendsCNBs.contains(newFriends[1]));
544: assertEquals("public packages", 1, newTestingPXM
545: .getPublicPackages().length);
546: validate(testingProject, false); // false - just replacing friends
547: }
548:
549: public void testGetBinaryOrigins() throws Exception {
550: ProjectXMLManager xercesPXM = createXercesPXM();
551: assertEquals("number of binary origins", 1, xercesPXM
552: .getBinaryOrigins().length);
553: }
554:
555: public void testThatFriendPackagesAreGeneratedInTheRightOrder_61882()
556: throws Exception {
557: FileObject fo = TestBase.generateStandaloneModuleDirectory(
558: getWorkDir(), "testing");
559: FileObject projectXMLFO = fo
560: .getFileObject("nbproject/project.xml");
561: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
562: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n"
563: + "<type>org.netbeans.modules.apisupport.project</type>\n"
564: + "<configuration>\n"
565: + "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n"
566: + "<code-name-base>org.netbeans.modules.j2eeapis</code-name-base>\n"
567: + "<standalone/>\n"
568: + "<module-dependencies/>\n"
569: + "<public-packages>\n"
570: + "<subpackages>javax.enterprise.deploy</subpackages>\n"
571: + "</public-packages>\n"
572: + "<class-path-extension>\n"
573: + "<runtime-relative-path>ext/jsr88javax.jar</runtime-relative-path>\n"
574: + "<binary-origin>../external/jsr88javax.jar</binary-origin>\n"
575: + "</class-path-extension>\n" + "</data>\n"
576: + "</configuration>\n" + "</project>\n";
577: TestBase.dump(projectXMLFO, xml);
578: NbModuleProject project = (NbModuleProject) ProjectManager
579: .getDefault().findProject(fo);
580: validate(project, true);
581: }
582:
583: public void testDependenciesOrder() throws Exception { // #62003
584: final NbModuleProject testingProject = generateTestingProject();
585: final ProjectXMLManager testingPXM = new ProjectXMLManager(
586: testingProject);
587: ModuleEntry me = testingProject.getModuleList().getEntry(
588: "org.netbeans.modules.java.project");
589: final ModuleDependency md = new ModuleDependency(me, "1", null,
590: false, true);
591: boolean result = ProjectManager.mutex().writeAccess(
592: new Mutex.ExceptionAction<Boolean>() {
593: public Boolean run() throws IOException {
594: testingPXM.addDependency(md);
595: return true;
596: }
597: });
598: assertTrue("adding dependencies", result);
599: ProjectManager.getDefault().saveProject(testingProject);
600: validate(testingProject, true);
601: }
602:
603: public void testRemoveTestDependency() throws Exception {
604: final NbModuleProject testingProject = generateTestingProject();
605: final ProjectXMLManager pxm = new ProjectXMLManager(
606: testingProject);
607: final String cnb = "org.netbeans.modules.java.project";
608: final String cnb2 = "org.netbeans.modules.project.ant";
609: File projectDir = FileUtil.toFile(testingProject
610: .getProjectDirectory());
611: ModuleList ml = ModuleList.getModuleList(projectDir);
612: ModuleEntry meJP = testingProject.getModuleList().getEntry(cnb);
613: ModuleEntry meAnt = testingProject.getModuleList().getEntry(
614: cnb2);
615: TestModuleDependency tdJP_001 = new TestModuleDependency(meJP,
616: false, false, true);
617: TestModuleDependency tdAnt_111 = new TestModuleDependency(
618: meAnt, true, true, true);
619: //add two unit test dependencies
620: pxm.addTestDependency(UNIT, tdJP_001);
621: pxm.addTestDependency(UNIT, tdAnt_111);
622: ProjectManager.getDefault().saveProject(testingProject);
623: //try wrong usage of remove
624: assertFalse("no such cnb under QA func.", pxm
625: .removeTestDependency(QA_FUNCTIONAL, cnb));
626: assertFalse("no such cnb under UNIT func.", pxm
627: .removeTestDependency(UNIT, "someCNB"));
628: Set<TestModuleDependency> setBefore = pxm.getTestDependencies(
629: ml).get(UNIT);
630: assertEquals("unit test type contains two TD", 2, setBefore
631: .size());
632: //remove first one
633: assertTrue("one should be found && removed", pxm
634: .removeTestDependency(UNIT, cnb));
635: ProjectManager.getDefault().saveProject(testingProject);
636: //try to remove just removed
637: assertFalse("this was just removed", pxm.removeTestDependency(
638: UNIT, cnb));
639: Set<TestModuleDependency> setNow = pxm.getTestDependencies(ml)
640: .get(UNIT);
641: assertEquals("unit test type contains one TD", 1, setNow.size());
642: //remove last one
643: assertTrue("all unit test deps have been removed", pxm
644: .removeTestDependency(UNIT, cnb2));
645: ProjectManager.getDefault().saveProject(testingProject);
646: Set<TestModuleDependency> setAfter = pxm
647: .getTestDependencies(ml).get(UNIT);
648: assertTrue("unit test type is empty now", setAfter.isEmpty());
649: }
650:
651: public void testAddTestDependency() throws Exception {
652: final NbModuleProject testingProject = generateTestingProject();
653: final ProjectXMLManager pxm = new ProjectXMLManager(
654: testingProject);
655: File projectDir = FileUtil.toFile(testingProject
656: .getProjectDirectory());
657: ModuleList ml = ModuleList.getModuleList(projectDir);
658: ModuleEntry meJP = testingProject.getModuleList().getEntry(
659: "org.netbeans.modules.java.project");
660: ModuleEntry meAnt = testingProject.getModuleList().getEntry(
661: "org.netbeans.modules.project.ant");
662: ModuleEntry meDialogs = testingProject.getModuleList()
663: .getEntry("org.openide.dialogs");
664:
665: TestModuleDependency tdJP_001 = new TestModuleDependency(meJP,
666: false, false, true);
667: TestModuleDependency tdJP_010 = new TestModuleDependency(meJP,
668: false, true, false);
669: TestModuleDependency tdAnt_111 = new TestModuleDependency(
670: meAnt, true, true, true);
671: TestModuleDependency tdDialogs_000 = new TestModuleDependency(
672: meDialogs, false, false, false);
673:
674: Map<String, Set<TestModuleDependency>> mapOfTD = pxm
675: .getTestDependencies(ml);
676:
677: assertTrue("currently no TD", mapOfTD.isEmpty());
678: //first, add one unit test dep
679: pxm.addTestDependency(UNIT, tdJP_001);
680: ProjectManager.getDefault().saveProject(testingProject);
681: mapOfTD = pxm.getTestDependencies(ml);
682: assertEquals("map has already unit test type", 1, mapOfTD
683: .size());
684: Set<TestModuleDependency> unitTD = mapOfTD.get(UNIT);
685: Set<TestModuleDependency> qafuncTD = mapOfTD.get(QA_FUNCTIONAL);
686: assertEquals("set with unit TD has one TD", 1, unitTD.size());
687: assertNull("set with qafunc TD does not exist", qafuncTD);
688: //now add 2 other unit test dep;
689: pxm.addTestDependency(UNIT, tdDialogs_000);
690: ProjectManager.getDefault().saveProject(testingProject);
691: pxm.addTestDependency(UNIT, tdAnt_111);
692: ProjectManager.getDefault().saveProject(testingProject);
693: mapOfTD = pxm.getTestDependencies(ml);
694: assertEquals("map still has only unit test type", 1, mapOfTD
695: .size());
696: unitTD = mapOfTD.get(UNIT);
697: assertEquals("set with unit TD has now three TD", 3, unitTD
698: .size());
699: //now add qa-func test dependency
700: pxm.addTestDependency(QA_FUNCTIONAL, tdJP_010);
701: ProjectManager.getDefault().saveProject(testingProject);
702: mapOfTD = pxm.getTestDependencies(ml);
703: unitTD = mapOfTD.get(UNIT);
704: qafuncTD = mapOfTD.get(QA_FUNCTIONAL);
705: assertEquals("map has both test types", 2, mapOfTD.size());
706: assertEquals("set with unit TD has still three TD", 3, unitTD
707: .size());
708: assertEquals("set with qafunc TD has one TD", 1, qafuncTD
709: .size());
710: //TODO: rewrite order checking method to be able to check properly test dependencies order
711: validate(testingProject, false);
712: }
713:
714: public void testIssue92363FixAddDependencyWhereSomeIsAlreadyPresent()
715: throws Exception {
716: String testDependencies = "\n"
717: + "<test-type>\n"
718: + "<name>unit</name>\n"
719: + "<test-dependency>\n"
720: + "<code-name-base>org.netbeans.core</code-name-base>\n"
721: + "</test-dependency>\n" + "</test-type>\n";
722: //create a project that already contains testdependency
723: final NbModuleProject testingProject = generateTestingProject(testDependencies);
724: final ProjectXMLManager pxm = new ProjectXMLManager(
725: testingProject);
726: ModuleList ml = ModuleList.getModuleList(testingProject
727: .getProjectDirectoryFile());
728: Map<String, Set<TestModuleDependency>> testDeps = pxm
729: .getTestDependencies(ml);
730: assertEquals("map has already unit test type", 1, testDeps
731: .size());
732: Set<TestModuleDependency> setUnit = testDeps.get(UNIT);
733: assertEquals("contains one dependency", 1, setUnit.size());
734: //now add one more testdependency
735: ModuleEntry meJP = testingProject.getModuleList().getEntry(
736: "org.netbeans.modules.java.project");
737: TestModuleDependency tdJP = new TestModuleDependency(meJP,
738: false, false, true);
739: pxm.addTestDependency(UNIT, tdJP);
740: ProjectManager.getDefault().saveProject(testingProject);
741: testDeps = pxm.getTestDependencies(ml);
742: assertEquals("map has already unit test type", 1, testDeps
743: .size());
744: setUnit = testDeps.get(UNIT);
745: assertEquals("contains two dependencies now", 2, setUnit.size());
746: validate(testingProject, false);
747: }
748:
749: /** add dependency from newDepProj to testingProject
750: * Used in core/tasklist/.../TodoTest.java
751: */
752: public static void addDependecy(
753: final NbModuleProject testingProject,
754: NbModuleProject newDepPrj) throws IOException,
755: MutexException, Exception {
756: final ProjectXMLManager testingPXM = new ProjectXMLManager(
757: testingProject);
758: ModuleEntry me = testingProject.getModuleList().getEntry(
759: newDepPrj.getCodeNameBase());
760: final ModuleDependency md = new ModuleDependency(me, "1", null,
761: false, true);
762: boolean result = ProjectManager.mutex().writeAccess(
763: new Mutex.ExceptionAction<Boolean>() {
764: public Boolean run() throws IOException {
765: Element confData = testingProject
766: .getPrimaryConfigurationData();
767: Element moduleDependencies = ProjectXMLManager
768: .findModuleDependencies(confData);
769: ProjectXMLManager
770: .createModuleDependencyElement(
771: moduleDependencies, md, null);
772: ProjectXMLManager
773: .createModuleDependencyElement(
774: moduleDependencies, md, null);
775: testingProject
776: .putPrimaryConfigurationData(confData);
777: return true;
778: }
779: });
780: assertTrue("adding dependencies", result);
781: ProjectManager.getDefault().saveProject(testingProject);
782: validate(testingProject, false); // false - we are not using regular way for adding
783: }
784:
785: private NbModuleProject generateTestingProject() throws Exception {
786: return generateTestingProject("");
787: }
788:
789: private NbModuleProject generateTestingProject(
790: final String testDependencies) throws Exception {
791: FileObject fo = TestBase.generateStandaloneModuleDirectory(
792: getWorkDir(), "testing");
793: FileObject projectXMLFO = fo
794: .getFileObject("nbproject/project.xml");
795: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
796: + "<project xmlns=\"http://www.netbeans.org/ns/project/1\">\n"
797: + "<type>org.netbeans.modules.apisupport.project</type>\n"
798: + "<configuration>\n"
799: + "<data xmlns=\"http://www.netbeans.org/ns/nb-module-project/3\">\n"
800: + "<code-name-base>org.example.testing</code-name-base>\n"
801: + "<standalone/>\n" + "<module-dependencies>\n"
802: + "<dependency>\n" + "<code-name-base>"
803: + DIALOGS
804: + "</code-name-base>\n"
805: + "<build-prerequisite/>\n"
806: + "<compile-dependency/>\n"
807: + "<run-dependency>\n"
808: + "<specification-version>6.2</specification-version>\n"
809: + "</run-dependency>\n"
810: + "</dependency>\n"
811: + "<dependency>\n"
812: + "<code-name-base>"
813: + ANT_PROJECT_SUPPORT
814: + "</code-name-base>\n"
815: + "<build-prerequisite/>\n"
816: + "<compile-dependency/>\n"
817: + "<run-dependency>\n"
818: + "<release-version>1</release-version>\n"
819: + "<specification-version>1.10</specification-version>\n"
820: + "</run-dependency>\n"
821: + "</dependency>\n"
822: + "</module-dependencies>\n"
823: + "<test-dependencies>"
824: + testDependencies
825: + "</test-dependencies>\n"
826: + "<friend-packages>\n"
827: + "<friend>org.module.examplemodule</friend>\n"
828: + "<package>org.netbeans.examples.modules.misc</package>\n"
829: + "</friend-packages>\n"
830: + "<class-path-extension>\n"
831: + "<runtime-relative-path>ext/jsr88javax.jar</runtime-relative-path>\n"
832: + "<binary-origin>../external/jsr88javax.jar</binary-origin>\n"
833: + "</class-path-extension>\n"
834: + "</data>\n"
835: + "</configuration>\n" + "</project>\n";
836: TestBase.dump(projectXMLFO, xml);
837: return (NbModuleProject) ProjectManager.getDefault()
838: .findProject(fo);
839: }
840:
841: // below is stolen from ant/freeform
842: private static String[] getSchemas() throws Exception {
843: String[] URIs = new String[3];
844: URIs[0] = ProjectXMLManager.class.getResource(
845: "resources/nb-module-project2.xsd").toExternalForm();
846: URIs[1] = ProjectXMLManager.class.getResource(
847: "resources/nb-module-project3.xsd").toExternalForm();
848: URIs[2] = AntBasedProjectFactorySingleton.class.getResource(
849: "project.xsd").toExternalForm();
850: return URIs;
851: }
852:
853: public static void validate(final Project proj,
854: final boolean checkOrder) throws Exception {
855: File projF = FileUtil.toFile(proj.getProjectDirectory());
856: File xml = new File(new File(projF, "nbproject"), "project.xml");
857: SAXParserFactory f = (SAXParserFactory) Class.forName(
858: "org.apache.xerces.jaxp.SAXParserFactoryImpl")
859: .newInstance();
860: if (f == null) {
861: System.err
862: .println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
863: return;
864: }
865: f.setNamespaceAware(true);
866: f.setValidating(true);
867: SAXParser p = f.newSAXParser();
868: p
869: .setProperty(
870: "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
871: "http://www.w3.org/2001/XMLSchema");
872: p.setProperty(
873: "http://java.sun.com/xml/jaxp/properties/schemaSource",
874: getSchemas());
875: try {
876: p.parse(xml.toURI().toString(), new Handler());
877: } catch (SAXParseException e) {
878: fail("Validation of XML document " + xml
879: + " against schema failed. Details: "
880: + e.getSystemId() + ":" + e.getLineNumber() + ": "
881: + e.getLocalizedMessage());
882: }
883: if (checkOrder) {
884: checkDependenciesOrder(proj);
885: }
886: }
887:
888: private static void checkDependenciesOrder(final Project proj)
889: throws Exception {
890: FileObject projectXML = proj.getProjectDirectory()
891: .getFileObject("nbproject/project.xml");
892: BufferedReader r = new BufferedReader(new InputStreamReader(
893: projectXML.getInputStream()));
894: try {
895: String previousCNB = null;
896: String line;
897: while ((line = r.readLine()) != null) {
898: line = line.trim();
899: if (line.matches("<code-name-base>.+</code-name-base>")) {
900: String currentCNB = line.substring(16, line
901: .length() - 17);
902: assertTrue("dependencies order, previous = \""
903: + previousCNB + "\", current = \""
904: + currentCNB + "\"", previousCNB == null
905: || previousCNB.compareTo(currentCNB) < 0);
906: previousCNB = currentCNB;
907: }
908: }
909: } finally {
910: r.close();
911: }
912: }
913:
914: private static final class Handler extends DefaultHandler {
915: public void warning(SAXParseException e) throws SAXException {
916: throw e;
917: }
918:
919: public void error(SAXParseException e) throws SAXException {
920: throw e;
921: }
922:
923: public void fatalError(SAXParseException e) throws SAXException {
924: throw e;
925: }
926: }
927:
928: }
|