1 19 20 package org.netbeans.modules.apisupport.project; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStreamReader ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.SortedSet ; 34 import java.util.TreeSet ; 35 import javax.xml.parsers.SAXParser ; 36 import javax.xml.parsers.SAXParserFactory ; 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 ; 51 import org.w3c.dom.Element ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.SAXException ; 54 import org.xml.sax.SAXParseException ; 55 import org.xml.sax.helpers.DefaultHandler ; 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 64 public class ProjectXMLManagerTest extends TestBase { 65 66 private final static String ANT_PROJECT_SUPPORT = "org.netbeans.modules.project.ant"; 67 private final static String DIALOGS = "org.openide.dialogs"; 68 private final static Set <String > ASSUMED_CNBS; 69 70 static { 71 Set <String > assumedCNBs = new HashSet <String >(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 testName) { 78 super(testName); 79 } 80 81 protected void setUp() throws Exception { 82 clearWorkDir(); 83 super.setUp(); 84 } 85 86 private ProjectXMLManager createXercesPXM() throws IOException { 87 NbModuleProject xercesPrj = (NbModuleProject) ProjectManager.getDefault(). 88 findProject(nbCVSRoot().getFileObject("libs/xerces")); 89 return new ProjectXMLManager(xercesPrj); 90 } 91 92 public void testGeneratedProject() throws Exception { 94 validate(generateTestingProject(), false); } 96 97 public void testGetCodeNameBase() throws Exception { 98 NbModuleProject p = generateStandaloneModule("module1"); 99 assertEquals("action-project cnb", "org.example.module1", p.getCodeNameBase()); 100 } 101 102 public void testGetDirectDependencies() throws Exception { 103 final NbModuleProject testingProject = generateTestingProject(); 104 ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 105 Set <ModuleDependency> deps = testingPXM.getDirectDependencies(); 106 assertEquals("number of dependencies", 2, deps.size()); 107 108 Set <String > assumedCNBs = new HashSet <String >(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 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 { 127 final NbModuleProject testingProject = generateTestingProject(); 128 ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 129 Set deps = testingPXM.getDirectDependencies(); 130 assertEquals("number of dependencies", 2, deps.size()); 131 Set depsWithCustom = testingPXM.getDirectDependencies( 132 NbPlatform.getPlatformByID("custom")); 133 assertEquals("number of dependencies", 0, depsWithCustom.size()); 134 } 135 136 public void testRemoveDependency() throws Exception { 137 final NbModuleProject testingProject = generateTestingProject(); 138 final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 139 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 141 public Boolean run() throws IOException { 142 testingPXM.removeDependency(DIALOGS); 143 return true; 144 } 145 }); 146 assertTrue("removing dependency", result); 147 ProjectManager.getDefault().saveProject(testingProject); 148 149 final Set <ModuleDependency> newDeps = testingPXM.getDirectDependencies(); 150 assertEquals("number of dependencies", 1, newDeps.size()); 151 Set <String > newCNBs = new HashSet <String >(); 152 newCNBs.add(ANT_PROJECT_SUPPORT); 153 for (ModuleDependency md : newDeps) { 154 String 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 { 162 final NbModuleProject testingProject = generateTestingProject(); 163 final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 164 final Set <ModuleDependency> deps = testingPXM.getDirectDependencies(); 165 166 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 168 public Boolean run() throws IOException { 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 ProjectXMLManager freshTestingPXM = new ProjectXMLManager(testingProject); 190 191 final Set <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); } 204 205 public void testAddDependencies() throws Exception { 206 final NbModuleProject testingProject = generateTestingProject(); 207 final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 208 final Set <ModuleDependency> newDeps = new HashSet <ModuleDependency>(); 209 ModuleEntry me = testingProject.getModuleList().getEntry( 210 "org.netbeans.modules.java.project"); 211 assertNotNull("java/project must be built", me); 212 String javaProjectRV = me.getReleaseVersion(); 213 String 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 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 221 public Boolean run() throws IOException { 222 testingPXM.addDependencies(newDeps); 223 return true; 224 } 225 }); 226 assertTrue("adding dependencies", result); 227 ProjectManager.getDefault().saveProject(testingProject); 228 229 Set <ModuleDependency> deps = testingPXM.getDirectDependencies(); 230 231 Set <String > assumedCNBs = new HashSet <String >(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 { 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 >() { 260 public Boolean run() throws IOException { 261 Element confData = testingProject.getPrimaryConfigurationData(); 262 Element 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 ise) { 275 } 277 validate(testingProject, false); } 279 280 public void testFindPublicPackages() throws Exception { 281 final NbModuleProject testingProject = generateTestingProject(); 282 final File projectXML = FileUtil.toFile( 283 testingProject.getProjectDirectory().getFileObject("nbproject/project.xml")); 284 assert projectXML.exists(); 285 Element confData = ProjectManager.mutex().readAccess(new Mutex.Action<Element >() { 286 public Element run() { 287 Element data = null; 288 try { 289 Document doc = XMLUtil.parse(new InputSource (projectXML.toURI().toString()), 290 false, true, null, null); 291 Element project = doc.getDocumentElement(); 292 Element config = Util.findElement(project, "configuration", null); data = Util.findElement(config, "data", NbModuleProjectType.NAMESPACE_SHARED); 294 } catch (IOException e) { 295 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 296 } catch (SAXException 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); } 308 309 public void testReplaceDependencies() throws Exception { 310 final NbModuleProject testingProject = generateTestingProject(); 311 final ProjectXMLManager testingPXM = new ProjectXMLManager(testingProject); 312 final SortedSet <ModuleDependency> deps = new TreeSet <ModuleDependency>(testingPXM.getDirectDependencies()); 313 assertEquals("number of dependencies", 2, deps.size()); 314 ModuleDependency newOO = null; 315 ModuleDependency oldOO = null; 316 for (Iterator <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 "", oldOO.getSpecificationVersion(), 324 md.hasCompileDependency(), 325 md.hasImplementationDepedendency()); 326 it.remove(); 327 break; 328 } 329 } 330 deps.add(newOO); 331 332 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 334 public Boolean run() throws IOException { 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 <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 { 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 { 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 [] newPP = new String [] { publicPackages[0].getPackage(), "org.netbeans.examples.modules" }; 375 376 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 378 public Boolean run() throws IOException { 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 newPPs = Arrays.asList(new String [] {"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); } 393 394 public void testReplaceFriends() throws Exception { 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 [] newFriends = new String [] { "org.exampleorg.somefriend", "org.exampleorg.anotherfriend" }; 400 401 boolean result = ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Boolean >() { 403 public Boolean run() throws IOException { 404 ManifestManager.PackageExport pkgs[] = testingPXM.getPublicPackages(); 405 String [] packagesToExpose = new String [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 [] actualFriends = newTestingPXM.getFriends(); 417 assertEquals("number of new friend", 2, actualFriends.length); 418 Collection 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); } 424 425 public void testGetBinaryOrigins() throws Exception { 426 ProjectXMLManager xercesPXM = createXercesPXM(); 427 assertEquals("number of binary origins", 2, xercesPXM.getBinaryOrigins().length); 428 } 429 430 public void testThatFriendPackagesAreGeneratedInTheRightOrder_61882() throws Exception { 431 FileObject fo = TestBase.generateStandaloneModuleDirectory(getWorkDir(), "testing"); 432 FileObject projectXMLFO = fo.getFileObject("nbproject/project.xml"); 433 String 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 { 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 >() { 463 public Boolean run() throws IOException { 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 { 475 final NbModuleProject testingProject = generateTestingProject(); 476 final ProjectXMLManager pxm = new ProjectXMLManager(testingProject); 477 final String cnb = "org.netbeans.modules.java.project"; 478 final String cnb2 = "org.netbeans.modules.project.ant"; 479 File 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 pxm.addTestDependency(UNIT, tdJP_001); 487 pxm.addTestDependency(UNIT, tdAnt_111); 488 ProjectManager.getDefault().saveProject(testingProject); 489 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 <TestModuleDependency> setBefore = pxm.getTestDependencies(ml).get(UNIT); 493 assertEquals("unit test type contains two TD", 2 , setBefore.size()); 494 assertTrue("one should be found && removed", pxm.removeTestDependency(UNIT, cnb)); 496 ProjectManager.getDefault().saveProject(testingProject); 497 assertFalse("this was just removed", pxm.removeTestDependency(UNIT, cnb)); 499 Set <TestModuleDependency> setNow = pxm.getTestDependencies(ml).get(UNIT); 500 assertEquals("unit test type contains one TD", 1 , setNow.size()); 501 assertTrue("all unit test deps have been removed", pxm.removeTestDependency(UNIT, cnb2)); 503 ProjectManager.getDefault().saveProject(testingProject); 504 Set <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 { 509 final NbModuleProject testingProject = generateTestingProject(); 510 final ProjectXMLManager pxm = new ProjectXMLManager(testingProject); 511 File 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 <String ,Set <TestModuleDependency>> mapOfTD = pxm.getTestDependencies(ml); 526 527 assertTrue("currently no TD", mapOfTD.isEmpty()); 528 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 <TestModuleDependency> unitTD = mapOfTD.get(UNIT); 534 Set <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 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 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 validate(testingProject, false); 557 } 558 559 public void testIssue92363FixAddDependencyWhereSomeIsAlreadyPresent() throws Exception { 560 String 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 final NbModuleProject testingProject = generateTestingProject(testDependencies); 569 final ProjectXMLManager pxm = new ProjectXMLManager(testingProject); 570 ModuleList ml = ModuleList.getModuleList(testingProject.getProjectDirectoryFile()); 571 Map <String ,Set <TestModuleDependency>> testDeps = pxm.getTestDependencies(ml); 572 assertEquals("map has already unit test type", 1, testDeps.size()); 573 Set <TestModuleDependency> setUnit = testDeps.get(UNIT); 574 assertEquals("contains one dependency", 1, setUnit.size()); 575 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 { 589 return generateTestingProject(""); 590 } 591 592 private NbModuleProject generateTestingProject(final String testDependencies) throws Exception { 593 FileObject fo = TestBase.generateStandaloneModuleDirectory(getWorkDir(), "testing"); 594 FileObject projectXMLFO = fo.getFileObject("nbproject/project.xml"); 595 String 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 private static String [] getSchemas() throws Exception { 639 String [] URIs = new String [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 { 647 File projF = FileUtil.toFile(proj.getProjectDirectory()); 648 File xml = new File (new File (projF, "nbproject"), "project.xml"); 649 SAXParserFactory f = (SAXParserFactory )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 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 ()); 662 } catch (SAXParseException 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 { 672 FileObject projectXML = proj.getProjectDirectory().getFileObject("nbproject/project.xml"); 673 BufferedReader r = new BufferedReader (new InputStreamReader (projectXML.getInputStream())); 674 try { 675 String previousCNB = null; 676 String line; 677 while ((line = r.readLine()) != null) { 678 line = line.trim(); 679 if (line.matches("<code-name-base>.+</code-name-base>")) { 680 String 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 { 692 public void warning(SAXParseException e) throws SAXException { 693 throw e; 694 } 695 public void error(SAXParseException e) throws SAXException { 696 throw e; 697 } 698 public void fatalError(SAXParseException e) throws SAXException { 699 throw e; 700 } 701 } 702 703 } 704 705 | Popular Tags |