1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.File ; 23 import java.io.FileOutputStream ; 24 import java.io.OutputStream ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.jar.JarEntry ; 31 import java.util.jar.JarOutputStream ; 32 import javax.swing.event.ChangeEvent ; 33 import javax.swing.event.ChangeListener ; 34 import org.netbeans.api.java.classpath.ClassPath; 35 import org.netbeans.api.java.project.JavaProjectConstants; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectInformation; 38 import org.netbeans.api.project.ProjectManager; 39 import org.netbeans.api.project.ProjectUtils; 40 import org.netbeans.api.project.Sources; 41 import org.netbeans.junit.NbTestCase; 42 import org.netbeans.modules.ant.freeform.spi.support.Util; 43 import org.netbeans.spi.java.classpath.ClassPathProvider; 44 import org.netbeans.spi.project.AuxiliaryConfiguration; 45 import org.netbeans.spi.project.support.ant.AntProjectHelper; 46 import org.netbeans.spi.project.ui.LogicalViewProvider; 47 import org.openide.filesystems.FileObject; 48 import org.openide.filesystems.FileUtil; 49 import org.openide.nodes.Node; 50 import org.w3c.dom.Element ; 51 import javax.xml.parsers.SAXParser ; 52 import javax.xml.parsers.SAXParserFactory ; 53 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 54 import org.netbeans.modules.ant.freeform.FreeformProjectType; 55 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton; 56 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 57 import org.netbeans.spi.project.support.ant.PropertyProvider; 58 import org.netbeans.spi.project.support.ant.PropertyUtils; 59 import org.openide.modules.ModuleInfo; 60 import org.openide.util.Lookup; 61 import org.xml.sax.SAXException ; 62 import org.xml.sax.SAXParseException ; 63 import org.xml.sax.helpers.DefaultHandler ; 64 65 70 public class JavaProjectGeneratorTest extends NbTestCase { 71 72 private File lib1; 73 private File lib2; 74 private File src; 75 private File test; 76 77 public JavaProjectGeneratorTest(java.lang.String testName) { 78 super(testName); 79 } 80 81 protected void setUp() throws Exception { 82 Lookup.getDefault().lookup(ModuleInfo.class); 83 clearWorkDir(); 84 } 85 86 private AntProjectHelper createEmptyProject(String projectFolder, String projectName, boolean notSoEmpty) throws Exception { 87 File base = new File (getWorkDir(), projectFolder); 88 base.mkdir(); 89 File antScript = new File (base, "build.xml"); 90 antScript.createNewFile(); 91 src = new File (base, "src"); 92 src.mkdir(); 93 test = new File (base, "test"); 94 test.mkdir(); 95 File libs = new File (base, "libs"); 96 libs.mkdir(); 97 lib1 = new File (libs, "some.jar"); 98 createRealJarFile(lib1); 99 lib2 = new File (libs, "some2.jar"); 100 createRealJarFile(lib2); 101 102 106 ArrayList sources = new ArrayList (); 107 ArrayList compUnits = new ArrayList (); 108 AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null); 109 if (notSoEmpty) { 110 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 111 sf.label = "src"; 112 sf.type = "java"; 113 sf.style = "packages"; 114 sf.location = src.getAbsolutePath(); 115 sources.add(sf); 116 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 117 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 118 cp.classpath = lib1.getAbsolutePath(); 119 cp.mode = "compile"; 120 cu.classpath = Collections.singletonList(cp); 121 cu.sourceLevel = "1.4"; 122 cu.packageRoots = Collections.singletonList(src.getAbsolutePath()); 123 compUnits.add(cu); 124 JavaProjectGenerator.putSourceFolders(helper, sources, null); 125 JavaProjectGenerator.putSourceViews(helper, sources, null); 126 JavaProjectGenerator.putJavaCompilationUnits(helper, Util.getAuxiliaryConfiguration(helper), compUnits); 127 } 128 return helper; 129 } 130 131 public void testCreateProject() throws Exception { 132 AntProjectHelper helper = createEmptyProject("proj1", "proj-1", false); 133 FileObject base = helper.getProjectDirectory(); 134 Project p = ProjectManager.getDefault().findProject(base); 135 assertNotNull("Project was not created", p); 136 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 137 138 ProjectInformation pi = ProjectUtils.getInformation(p); 139 assertEquals("Project name was not set", "proj-1", pi.getName()); 140 } 141 142 public void testRawCreateProject() throws Exception { 143 File base = new File (getWorkDir(), "proj"); 144 base.mkdir(); 145 File diffFolder = new File (getWorkDir(), "separate"); 146 diffFolder.mkdir(); 147 File antScript = new File (diffFolder, "build.xml"); 148 antScript.createNewFile(); 149 150 154 AntProjectHelper helper = FreeformProjectGenerator.createProject(diffFolder, base, "p-r-o-j", antScript); 155 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 156 assertNotNull("Project was not created", p); 157 List mappings = new ArrayList (); 158 FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping(); 159 tm.name = "foo"; 160 tm.script = "antScript"; 161 mappings.add(tm); 162 List customActions = new ArrayList (); 163 FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget(); 164 ct.label = "customAction1"; 165 customActions.add(ct); 166 List folders = new ArrayList (); 167 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 168 sf.label = "folder3"; 169 sf.location = "location3"; 170 sf.style = "tree"; 171 folders.add(sf); 172 List exports = new ArrayList (); 173 JavaProjectGenerator.Export e = new JavaProjectGenerator.Export(); 174 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 175 e.location = "folder/output.jar"; 176 e.buildTarget = "target"; 177 exports.add(e); 178 List subprojects = new ArrayList (); 179 subprojects.add("/projA"); 180 181 FreeformProjectGenerator.putTargetMappings(helper, mappings); 182 FreeformProjectGenerator.putContextMenuAction(helper, mappings); 183 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 184 JavaProjectGenerator.putSourceFolders(helper, folders, null); 185 JavaProjectGenerator.putSourceViews(helper, folders, null); 186 JavaProjectGenerator.putExports(helper, exports); 187 JavaProjectGenerator.putSubprojects(helper, subprojects); 188 190 192 Element el = Util.getPrimaryConfigurationData(helper); 193 List subElements = Util.findSubElements(el); 194 assertEquals(7, subElements.size()); 195 assertElementArray(subElements, 196 new String []{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}, 197 new String []{null, null, null, null, null, null, null}); 198 Element el2 = (Element )subElements.get(5); 199 subElements = Util.findSubElements(el2); 200 assertEquals(2, subElements.size()); 201 assertElementArray(subElements, 202 new String []{"items", "context-menu"}, 203 new String []{null, null}); 204 Element el3 = (Element )subElements.get(0); 205 List subEls = Util.findSubElements(el3); 206 assertEquals(2, subEls.size()); 207 assertElementArray(subEls, 208 new String []{"source-folder", "source-file"}, 209 new String []{null, null}); 210 el3 = (Element )subElements.get(1); 211 subEls = Util.findSubElements(el3); 212 assertEquals(2, subEls.size()); 213 assertElementArray(subEls, 214 new String []{"ide-action", "action"}, 215 new String []{null, null}); 216 217 219 mappings = FreeformProjectGenerator.getTargetMappings(helper); 220 customActions = FreeformProjectGenerator.getCustomContextMenuActions(helper); 221 folders = JavaProjectGenerator.getSourceFolders(helper, null); 222 ((JavaProjectGenerator.SourceFolder)folders.get(0)).style = "tree"; 224 FreeformProjectGenerator.putTargetMappings(helper, mappings); 225 JavaProjectGenerator.putSubprojects(helper, subprojects); 226 FreeformProjectGenerator.putContextMenuAction(helper, mappings); 227 JavaProjectGenerator.putExports(helper, exports); 228 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 229 JavaProjectGenerator.putSourceFolders(helper, folders, null); 230 JavaProjectGenerator.putSourceViews(helper, folders, null); 231 JavaProjectGenerator.putSourceViews(helper, folders, null); 232 JavaProjectGenerator.putSourceFolders(helper, folders, null); 233 JavaProjectGenerator.putExports(helper, exports); 234 FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions); 235 FreeformProjectGenerator.putContextMenuAction(helper, mappings); 236 JavaProjectGenerator.putSubprojects(helper, subprojects); 237 FreeformProjectGenerator.putTargetMappings(helper, mappings); 238 el = Util.getPrimaryConfigurationData(helper); 240 subElements = Util.findSubElements(el); 241 assertEquals(7, subElements.size()); 242 assertElementArray(subElements, 243 new String []{"name", "properties", "folders", "ide-actions", "export", "view", "subprojects"}, 244 new String []{null, null, null, null, null, null, null}); 245 el2 = (Element )subElements.get(5); 246 subElements = Util.findSubElements(el2); 247 assertEquals(2, subElements.size()); 248 assertElementArray(subElements, 249 new String []{"items", "context-menu"}, 250 new String []{null, null}); 251 el3 = (Element )subElements.get(0); 252 subEls = Util.findSubElements(el3); 253 assertEquals(2, subEls.size()); 254 assertElementArray(subEls, 255 new String []{"source-folder", "source-file"}, 256 new String []{null, null}); 257 el3 = (Element )subElements.get(1); 258 subEls = Util.findSubElements(el3); 259 assertEquals(2, subEls.size()); 260 assertElementArray(subEls, 261 new String []{"ide-action", "action"}, 262 new String []{null, null}); 263 264 ProjectManager.getDefault().saveAllProjects(); 266 validate(p); 267 } 268 269 275 public static void assertElement(Element element, String expectedName, String expectedValue) { 276 String message = "Element "+element+" does not match [name="+expectedName+",value="+expectedValue+"]"; assertEquals(message, expectedName, element.getLocalName()); 278 if (expectedValue != null) { 279 assertEquals(message, expectedValue, Util.findText(element)); 280 } 281 } 282 283 287 public static void assertElementArray(List <Element > elements, String [] expectedNames, String [] expectedValues) { 288 for (int i=0; i<elements.size(); i++) { 289 assertElement((Element )elements.get(i), expectedNames[i], expectedValues[i]); 290 } 291 } 292 293 302 public static void assertElement(Element element, String expectedName, String expectedValue, String expectedAttrName, String expectedAttrValue) { 303 String message = "Element "+element+" does not match [name="+expectedName+",value="+ 304 expectedValue+", attr="+expectedAttrName+", attrvalue="+expectedAttrValue+"]"; assertEquals(message, expectedName, element.getLocalName()); 306 if (expectedValue != null) { 307 assertEquals(message, expectedValue, Util.findText(element)); 308 } 309 String val = element.getAttribute(expectedAttrName); 310 assertEquals(expectedAttrValue, val); 311 } 312 313 318 public static void assertElementArray(List <Element > elements, String [] expectedNames, String [] expectedValues, String [] expectedAttrName, String [] expectedAttrValue) { 319 assertEquals(expectedNames.length, elements.size()); 320 for (int i=0; i<elements.size(); i++) { 321 assertElement((Element )elements.get(i), expectedNames[i], expectedValues[i], expectedAttrName[i], expectedAttrValue[i]); 322 } 323 } 324 325 public void testSourceFolders() throws Exception { 326 AntProjectHelper helper = createEmptyProject("proj3", "proj-3", true); 327 FileObject base = helper.getProjectDirectory(); 328 Project p = ProjectManager.getDefault().findProject(base); 329 assertNotNull("Project was not created", p); 330 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 331 332 Sources ss = ProjectUtils.getSources(p); 333 assertEquals("Project must have one java source group", 1, ss.getSourceGroups("java").length); 334 assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length); 335 336 Listener l = new Listener (); 337 ss.addChangeListener(l); 338 339 List sfs = JavaProjectGenerator.getSourceFolders(helper, null); 340 assertEquals("There must be one source folder", 1, sfs.size()); 341 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 342 sf.label = "test"; 343 sf.type = "java"; 344 sf.location = test.getAbsolutePath(); 345 sfs.add(sf); 346 JavaProjectGenerator.putSourceFolders(helper, sfs, null); 347 assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length); 348 assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length); 349 assertEquals("Number of fired events does not match", 1, l.count); 350 l.reset(); 351 352 sfs = new ArrayList (); 353 sf = new JavaProjectGenerator.SourceFolder(); 354 sf.label = "xdoc"; 355 sf.type = "x-doc"; 356 sf.location = test.getAbsolutePath(); 358 sfs.add(sf); 359 JavaProjectGenerator.putSourceFolders(helper, sfs, "x-doc"); 360 assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length); 361 assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size()); 362 assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length); 363 assertEquals("Project must have one x-doc source group", 1, ss.getSourceGroups("x-doc").length); 364 sf = new JavaProjectGenerator.SourceFolder(); 365 sf.label = "xdoc2"; 366 sf.type = "x-doc"; 367 sf.location = src.getAbsolutePath(); 369 sfs.add(sf); 370 JavaProjectGenerator.putSourceFolders(helper, sfs, "x-doc"); 371 assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length); 372 assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size()); 373 assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length); 374 assertEquals("Project must have two x-doc source groups", 2, ss.getSourceGroups("x-doc").length); 375 assertEquals("Project must have two x-doc source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "x-doc").size()); 376 assertEquals("Project must have four source groups", 4, JavaProjectGenerator.getSourceFolders(helper, null).size()); 377 378 sfs = JavaProjectGenerator.getSourceFolders(helper, null); 379 JavaProjectGenerator.putSourceFolders(helper, sfs, null); 380 assertEquals("Project must have two java source groups", 2, ss.getSourceGroups("java").length); 381 assertEquals("Project must have two java source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "java").size()); 382 assertEquals("Project cannot have csharp source group", 0, ss.getSourceGroups("csharp").length); 383 assertEquals("Project must have two x-doc source groups", 2, ss.getSourceGroups("x-doc").length); 384 assertEquals("Project must have two x-doc source groups", 2, JavaProjectGenerator.getSourceFolders(helper, "x-doc").size()); 385 assertEquals("Project must have four source groups", 4, JavaProjectGenerator.getSourceFolders(helper, null).size()); 386 387 ProjectManager.getDefault().saveAllProjects(); 388 } 389 390 public void testRawSourceFolders() throws Exception { 391 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 392 FileObject base = helper.getProjectDirectory(); 393 Project p = ProjectManager.getDefault().findProject(base); 394 assertNotNull("Project was not created", p); 395 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 396 397 399 List folders = new ArrayList (); 400 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 401 sf.label = "folder1"; 402 sf.type = "type1"; 403 sf.location = "location1"; 404 folders.add(sf); 405 sf = new JavaProjectGenerator.SourceFolder(); 406 sf.label = "folder2"; 407 sf.type = "type2"; 408 sf.location = "location2"; 409 folders.add(sf); 410 JavaProjectGenerator.putSourceFolders(helper, folders, null); 411 folders = JavaProjectGenerator.getSourceFolders(helper, null); 413 JavaProjectGenerator.putSourceFolders(helper, folders, null); 414 Element el = Util.getPrimaryConfigurationData(helper); 416 el = Util.findElement(el, "folders", Util.NAMESPACE); 417 assertNotNull("Source folders were not saved correctly", el); 418 List subElements = Util.findSubElements(el); 419 assertEquals(2, subElements.size()); 420 Element el2 = (Element )subElements.get(0); 422 assertElement(el2, "source-folder", null); 423 List l1 = Util.findSubElements(el2); 424 assertEquals(3, l1.size()); 425 assertElementArray(l1, 426 new String []{"label", "type", "location"}, 427 new String []{"folder1", "type1", "location1"}); 428 el2 = (Element )subElements.get(1); 430 assertElement(el2, "source-folder", null); 431 l1 = Util.findSubElements(el2); 432 assertEquals(3, l1.size()); 433 assertElementArray(l1, 434 new String []{"label", "type", "location"}, 435 new String []{"folder2", "type2", "location2"}); 436 ProjectManager.getDefault().saveAllProjects(); 438 validate(p); 439 440 442 folders = new ArrayList (); 443 sf = new JavaProjectGenerator.SourceFolder(); 444 sf.label = "folder3"; 445 sf.type = "type2"; 446 sf.location = "location3"; 447 folders.add(sf); 448 JavaProjectGenerator.putSourceFolders(helper, folders, "type2"); 449 ProjectManager.getDefault().saveAllProjects(); 450 el = Util.getPrimaryConfigurationData(helper); 451 el = Util.findElement(el, "folders", Util.NAMESPACE); 452 assertNotNull("Source folders were not saved correctly", el); 453 subElements = Util.findSubElements(el); 454 assertEquals(2, subElements.size()); 455 el2 = (Element )subElements.get(0); 457 assertElement(el2, "source-folder", null); 458 l1 = Util.findSubElements(el2); 459 assertEquals(3, l1.size()); 460 assertElementArray(l1, 461 new String []{"label", "type", "location"}, 462 new String []{"folder1", "type1", "location1"}); 463 el2 = (Element )subElements.get(1); 465 assertElement(el2, "source-folder", null); 466 l1 = Util.findSubElements(el2); 467 assertEquals(3, l1.size()); 468 assertElementArray(l1, 469 new String []{"label", "type", "location"}, 470 new String []{"folder3", "type2", "location3"}); 471 ProjectManager.getDefault().saveAllProjects(); 473 validate(p); 474 } 475 476 public void testSourceViews() throws Exception { 477 AntProjectHelper helper = createEmptyProject("proj6", "proj-6", true); 478 FileObject base = helper.getProjectDirectory(); 479 Project p = ProjectManager.getDefault().findProject(base); 480 assertNotNull("Project was not created", p); 481 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 482 483 Sources ss = ProjectUtils.getSources(p); 484 assertEquals("Project must have one java source group", 1, ss.getSourceGroups("java").length); 485 486 LogicalViewProvider lvp = (LogicalViewProvider)p.getLookup().lookup(LogicalViewProvider.class); 487 assertNotNull("Project does not have LogicalViewProvider", lvp); 488 Node n = lvp.createLogicalView(); 489 assertEquals("There must be two subnodes in logical view", 2, n.getChildren().getNodesCount()); 491 492 List sfs = JavaProjectGenerator.getSourceViews(helper, null); 493 assertEquals("There must be one source view", 1, sfs.size()); 494 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 495 sf.label = "test"; 496 sf.style = "packages"; 497 sf.location = test.getAbsolutePath(); 498 sfs.add(sf); 499 JavaProjectGenerator.putSourceViews(helper, sfs, null); 500 assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size()); 501 assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size()); 502 503 n = lvp.createLogicalView(); 504 507 sfs = new ArrayList (); 508 sf = new JavaProjectGenerator.SourceFolder(); 509 sf.label = "xdoc"; 510 sf.style = "tree"; 511 sf.location = test.getAbsolutePath(); 513 sfs.add(sf); 514 JavaProjectGenerator.putSourceViews(helper, sfs, "tree"); 515 assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size()); 516 assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size()); 517 assertEquals("Project must have one tree source view", 1, JavaProjectGenerator.getSourceViews(helper, "tree").size()); 518 assertEquals("Project must have three source views", 3, JavaProjectGenerator.getSourceViews(helper, null).size()); 519 sf = new JavaProjectGenerator.SourceFolder(); 520 sf.label = "xdoc2"; 521 sf.style = "tree"; 522 sf.location = src.getAbsolutePath(); 524 sfs.add(sf); 525 JavaProjectGenerator.putSourceViews(helper, sfs, "tree"); 526 assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size()); 527 assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size()); 528 assertEquals("Project must have two tree source views", 2, JavaProjectGenerator.getSourceViews(helper, "tree").size()); 529 assertEquals("Project must have four source views", 4, JavaProjectGenerator.getSourceViews(helper, null).size()); 530 531 sfs = JavaProjectGenerator.getSourceViews(helper, null); 532 JavaProjectGenerator.putSourceViews(helper, sfs, null); 533 assertEquals("Project must have two packages source views", 2, JavaProjectGenerator.getSourceViews(helper, "packages").size()); 534 assertEquals("Project cannot have any flat source view", 0, JavaProjectGenerator.getSourceViews(helper, "flat").size()); 535 assertEquals("Project must have two tree source views", 2, JavaProjectGenerator.getSourceViews(helper, "tree").size()); 536 assertEquals("Project must have four source views", 4, JavaProjectGenerator.getSourceViews(helper, null).size()); 537 538 ProjectManager.getDefault().saveAllProjects(); 539 } 540 541 public void testRawSourceViews() throws Exception { 542 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 543 FileObject base = helper.getProjectDirectory(); 544 Project p = ProjectManager.getDefault().findProject(base); 545 assertNotNull("Project was not created", p); 546 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 547 548 550 List folders = new ArrayList (); 551 JavaProjectGenerator.SourceFolder sf = new JavaProjectGenerator.SourceFolder(); 552 sf.label = "folder1"; 553 sf.style = "tree"; 554 sf.location = "location1"; 555 folders.add(sf); 556 sf = new JavaProjectGenerator.SourceFolder(); 557 sf.label = "folder2"; 558 sf.style = "packages"; 559 sf.location = "location2"; 560 folders.add(sf); 561 JavaProjectGenerator.putSourceViews(helper, folders, null); 562 folders = JavaProjectGenerator.getSourceViews(helper, null); 564 JavaProjectGenerator.putSourceViews(helper, folders, null); 565 ProjectManager.getDefault().saveAllProjects(); 566 Element el = Util.getPrimaryConfigurationData(helper); 567 el = Util.findElement(el, "view", Util.NAMESPACE); 568 assertNotNull("View folders were not saved correctly", el); 569 el = Util.findElement(el, "items", Util.NAMESPACE); 570 assertNotNull("View folders were not saved correctly", el); 571 List subElements = Util.findSubElements(el); 572 assertEquals(3, subElements.size()); 574 Element el2 = (Element )subElements.get(0); 576 assertElement(el2, "source-folder", null, "style", "tree"); 577 List l1 = Util.findSubElements(el2); 578 assertEquals(2, l1.size()); 579 assertElementArray(l1, 580 new String []{"label", "location"}, 581 new String []{"folder1", "location1"}); 582 el2 = (Element )subElements.get(1); 584 assertElement(el2, "source-folder", null, "style", "packages"); 585 l1 = Util.findSubElements(el2); 586 assertEquals(2, l1.size()); 587 assertElementArray(l1, 588 new String []{"label", "location"}, 589 new String []{"folder2", "location2"}); 590 ProjectManager.getDefault().saveAllProjects(); 592 validate(p); 593 594 596 folders = new ArrayList (); 597 sf = new JavaProjectGenerator.SourceFolder(); 598 sf.label = "folder3"; 599 sf.style = "packages"; 600 sf.location = "location3"; 601 folders.add(sf); 602 JavaProjectGenerator.putSourceViews(helper, folders, "packages"); 603 ProjectManager.getDefault().saveAllProjects(); 604 el = Util.getPrimaryConfigurationData(helper); 605 el = Util.findElement(el, "view", Util.NAMESPACE); 606 assertNotNull("Source views were not saved correctly", el); 607 el = Util.findElement(el, "items", Util.NAMESPACE); 608 assertNotNull("View folders were not saved correctly", el); 609 subElements = Util.findSubElements(el); 610 assertEquals("3 elements in " + subElements, 3, subElements.size()); 612 el2 = (Element )subElements.get(0); 614 assertElement(el2, "source-folder", null, "style", "tree"); 615 l1 = Util.findSubElements(el2); 616 assertEquals(2, l1.size()); 617 assertElementArray(l1, 618 new String []{"label", "location"}, 619 new String []{"folder1", "location1"}); 620 el2 = (Element )subElements.get(1); 622 assertElement(el2, "source-folder", null, "style", "packages"); 623 l1 = Util.findSubElements(el2); 624 assertEquals(2, l1.size()); 625 assertElementArray(l1, 626 new String []{"label", "location"}, 627 new String []{"folder3", "location3"}); 628 ProjectManager.getDefault().saveAllProjects(); 630 validate(p); 631 } 632 633 public void testJavaCompilationUnits() throws Exception { 634 AntProjectHelper helper = createEmptyProject("proj5", "proj-5", true); 635 FileObject base = helper.getProjectDirectory(); 636 Project p = ProjectManager.getDefault().findProject(base); 637 assertNotNull("Project was not created", p); 638 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 639 640 ClassPathProvider cpp = (ClassPathProvider)p.getLookup().lookup(ClassPathProvider.class); 641 assertNotNull("Project does not have ClassPathProvider", cpp); 642 ClassPath cp = cpp.findClassPath(FileUtil.toFileObject(src), ClassPath.COMPILE); 643 assertEquals("Project must have one classpath root", 1, cp.getRoots().length); 644 assertEquals("Classpath root does not match", "jar:"+lib1.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm()); 645 cp = cpp.findClassPath(FileUtil.toFileObject(src).getParent(), ClassPath.COMPILE); 646 assertEquals("There is no classpath for this file", null, cp); 647 648 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper); 649 List cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux); 650 assertEquals("There must be one compilation unit", 1, cus.size()); 651 JavaProjectGenerator.JavaCompilationUnit cu = (JavaProjectGenerator.JavaCompilationUnit)cus.get(0); 652 assertEquals("The compilation unit must have one classpath", 1, cu.classpath.size()); 653 654 JavaProjectGenerator.JavaCompilationUnit.CP cucp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 655 cucp.classpath = lib2.getAbsolutePath(); 656 cucp.mode = "execute"; 657 cu.classpath.add(cucp); 658 ArrayList outputs = new ArrayList (); 659 outputs.add("output1.jar"); 660 outputs.add("output2.jar"); 661 outputs.add("output3.jar"); 662 cu.output = outputs; 663 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, cus); 664 cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux); 665 assertEquals("There must be one compilation unit", 1, cus.size()); 666 cu = (JavaProjectGenerator.JavaCompilationUnit)cus.get(0); 667 assertEquals("The compilation unit must have one classpath", 2, cu.classpath.size()); 668 assertEquals("The compilation unit must have one classpath", 3, cu.output.size()); 669 670 cu = new JavaProjectGenerator.JavaCompilationUnit(); 671 cu.sourceLevel = "1.4"; 672 cucp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 673 cucp.classpath = lib2.getAbsolutePath(); 674 cucp.mode = "compile"; 675 cu.classpath = Collections.singletonList(cucp); 676 cu.packageRoots = Collections.singletonList(test.getAbsolutePath()); 677 cus.add(cu); 678 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, cus); 679 cus = JavaProjectGenerator.getJavaCompilationUnits(helper, aux); 680 assertEquals("There must be two compilation units", 2, cus.size()); 681 cp = cpp.findClassPath(FileUtil.toFileObject(src), ClassPath.COMPILE); 682 assertEquals("Project must have one classpath root", 1, cp.getRoots().length); 683 assertEquals("Classpath root does not match", "jar:"+lib1.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm()); 684 cp = cpp.findClassPath(FileUtil.toFileObject(src).getParent(), ClassPath.COMPILE); 685 assertEquals("There is no classpath for this file", null, cp); 686 cp = cpp.findClassPath(FileUtil.toFileObject(test), ClassPath.COMPILE); 687 assertEquals("Project must have one classpath root", 1, cp.getRoots().length); 688 assertEquals("Classpath root does not match", "jar:"+lib2.toURI().toURL()+"!/", (cp.getRoots()[0]).getURL().toExternalForm()); 689 690 ProjectManager.getDefault().saveAllProjects(); 691 } 692 693 public void testRawJavaCompilationUnits() throws Exception { 694 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 695 FileObject base = helper.getProjectDirectory(); 696 Project p = ProjectManager.getDefault().findProject(base); 697 assertNotNull("Project was not created", p); 698 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 699 700 702 List units = new ArrayList (); 703 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 704 cu.packageRoots = new ArrayList (); 705 cu.packageRoots.add("pkgroot1"); 706 cu.packageRoots.add("pkgroot2"); 707 cu.output = new ArrayList (); 708 cu.output.add("output1"); 709 cu.output.add("output2"); 710 cu.classpath = new ArrayList (); 711 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 712 cp.mode = "compile"; 713 cp.classpath = "classpath1"; 714 cu.classpath.add(cp); 715 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 716 cp.mode = "boot"; 717 cp.classpath = "classpath2"; 718 cu.classpath.add(cp); 719 cu.sourceLevel = "1.3"; 720 units.add(cu); 721 cu = new JavaProjectGenerator.JavaCompilationUnit(); 722 cu.packageRoots = new ArrayList (); 723 cu.packageRoots.add("sec-pkgroot1"); 724 cu.packageRoots.add("sec-pkgroot2"); 725 cu.output = new ArrayList (); 726 cu.output.add("sec-output1"); 727 cu.output.add("sec-output2"); 728 cu.classpath = new ArrayList (); 729 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 730 cp.mode = "compile"; 731 cp.classpath = "sec-classpath1"; 732 cu.classpath.add(cp); 733 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 734 cp.mode = "boot"; 735 cp.classpath = "sec-classpath2"; 736 cu.classpath.add(cp); 737 cu.sourceLevel = "1.4"; 738 units.add(cu); 739 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper); 740 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 741 units = JavaProjectGenerator.getJavaCompilationUnits(helper, aux); 743 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 744 Element el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 746 assertNotNull("Java compilation units were not saved correctly", el); 747 List subElements = Util.findSubElements(el); 748 assertEquals(2, subElements.size()); 749 Element el2 = (Element )subElements.get(0); 751 assertElement(el2, "compilation-unit", null); 752 List l1 = Util.findSubElements(el2); 753 assertEquals(7, l1.size()); 754 assertElementArray(l1, 755 new String []{"package-root", "package-root", "classpath", "classpath", "built-to", "built-to", "source-level"}, 756 new String []{"pkgroot1", "pkgroot2", "classpath1", "classpath2", "output1", "output2", "1.3"}); 757 el2 = (Element )l1.get(2); 758 assertElement(el2, "classpath", "classpath1", "mode", "compile"); 759 el2 = (Element )l1.get(3); 760 assertElement(el2, "classpath", "classpath2", "mode", "boot"); 761 el2 = (Element )subElements.get(1); 763 assertElement(el2, "compilation-unit", null); 764 l1 = Util.findSubElements(el2); 765 assertEquals(7, l1.size()); 766 assertElementArray(l1, 767 new String []{"package-root", "package-root", "classpath", "classpath", "built-to", "built-to", "source-level"}, 768 new String []{"sec-pkgroot1", "sec-pkgroot2", "sec-classpath1", "sec-classpath2", "sec-output1", "sec-output2", "1.4"}); 769 el2 = (Element )l1.get(2); 770 assertElement(el2, "classpath", "sec-classpath1", "mode", "compile"); 771 el2 = (Element )l1.get(3); 772 assertElement(el2, "classpath", "sec-classpath2", "mode", "boot"); 773 ProjectManager.getDefault().saveAllProjects(); 775 validate(p); 776 777 779 units = new ArrayList (); 780 cu = new JavaProjectGenerator.JavaCompilationUnit(); 781 cu.packageRoots = new ArrayList (); 782 cu.packageRoots.add("foo-package-root"); 783 units.add(cu); 784 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 785 el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 787 assertNotNull("Java compilation units were not saved correctly", el); 788 subElements = Util.findSubElements(el); 789 assertEquals(1, subElements.size()); 790 el2 = (Element )subElements.get(0); 792 assertElement(el2, "compilation-unit", null); 793 l1 = Util.findSubElements(el2); 794 assertEquals(1, l1.size()); 795 assertElementArray(l1, 796 new String []{"package-root"}, 797 new String []{"foo-package-root"}); 798 ProjectManager.getDefault().saveAllProjects(); 800 validate(p); 801 } 802 803 public void testCompilationUnitUpgrades() throws Exception { 804 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 805 FileObject base = helper.getProjectDirectory(); 806 Project p = ProjectManager.getDefault().findProject(base); 807 assertNotNull("Project was not created", p); 808 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 809 List <JavaProjectGenerator.JavaCompilationUnit> units = new ArrayList (); 811 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 812 cu.packageRoots = new ArrayList (); 813 cu.packageRoots.add("pkgroot1"); 814 units.add(cu); 815 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper); 816 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 817 Element el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 819 assertNotNull("Java compilation units were saved in /1", el); 820 List <Element > subElements = Util.findSubElements(el); 821 assertEquals(1, subElements.size()); 822 Element el2 = (Element ) subElements.get(0); 824 assertElement(el2, "compilation-unit", null); 825 assertElementArray(Util.findSubElements(el2), 826 new String [] {"package-root"}, 827 new String [] {"pkgroot1"}); 828 ProjectManager.getDefault().saveAllProjects(); 830 validate(p); 831 units = new ArrayList (); 833 cu = new JavaProjectGenerator.JavaCompilationUnit(); 834 cu.packageRoots = new ArrayList (); 835 cu.packageRoots.add("pkgroot1"); 836 cu.isTests = true; 837 units.add(cu); 838 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 839 el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 841 assertNull("No /1 data", el); 842 el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 843 assertNotNull("Have /2 data", el); 844 subElements = Util.findSubElements(el); 845 assertEquals(1, subElements.size()); 846 el2 = (Element ) subElements.get(0); 848 assertElement(el2, "compilation-unit", null); 849 assertElementArray(Util.findSubElements(el2), 850 new String [] {"package-root", "unit-tests"}, 851 new String [] {"pkgroot1", null}); 852 ProjectManager.getDefault().saveAllProjects(); 854 validate(p); 855 assertTrue("removed /2 data", aux.removeConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true)); 857 units = new ArrayList (); 858 cu = new JavaProjectGenerator.JavaCompilationUnit(); 859 cu.packageRoots = new ArrayList (); 860 cu.packageRoots.add("pkgroot1"); 861 cu.javadoc = new ArrayList (); 862 cu.javadoc.add("javadoc1"); 863 cu.javadoc.add("javadoc2"); 864 units.add(cu); 865 JavaProjectGenerator.putJavaCompilationUnits(helper, aux, units); 866 el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_1, true); 868 assertNull("No /1 data", el); 869 el = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 870 assertNotNull("Have /2 data", el); 871 subElements = Util.findSubElements(el); 872 assertEquals(1, subElements.size()); 873 el2 = (Element ) subElements.get(0); 875 assertElement(el2, "compilation-unit", null); 876 assertElementArray(Util.findSubElements(el2), 877 new String [] {"package-root", "javadoc-built-to", "javadoc-built-to"}, 878 new String [] {"pkgroot1", "javadoc1", "javadoc2"}); 879 ProjectManager.getDefault().saveAllProjects(); 881 validate(p); 882 } 883 884 public void testGuessExports() throws Exception { 885 JavaProjectGenerator.TargetMapping tm = new JavaProjectGenerator.TargetMapping(); 886 tm.name = "build"; 887 tm.script = "${ant}"; 888 tm.targets = new ArrayList (); 889 tm.targets.add("target-1"); 890 ArrayList targets = new ArrayList (); 891 targets.add(tm); 892 893 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 894 cu.output = new ArrayList (); 895 cu.output.add("${outputfile}"); 896 ArrayList units = new ArrayList (); 897 units.add(cu); 898 899 Map m = new HashMap (); 900 m.put("outputfile", "out.jar"); 901 m.put("ant", getWorkDir().getAbsolutePath()+"/etc/antScript"); 902 PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 903 PropertyUtils.fixedPropertyProvider(m)}); 904 905 List exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 906 assertEquals("one export was created even though build script is not in project folder", 1, exports.size()); 907 909 m.put("ant", "etc/antScript"); 910 evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 911 PropertyUtils.fixedPropertyProvider(m)}); 912 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 913 assertEquals("one export was created", 1, exports.size()); 914 915 tm.script = null; 916 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 917 assertEquals("one export was created", 1, exports.size()); 918 JavaProjectGenerator.Export e = (JavaProjectGenerator.Export)exports.get(0); 919 assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type); 920 assertEquals("export is properly configured", "${outputfile}", e.location); 921 assertEquals("export is properly configured", null, e.script); 922 assertEquals("export is properly configured", "target-1", e.buildTarget); 923 924 tm.targets.add("target-2"); 925 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 926 assertEquals("no export was created when there are two targets", 0, exports.size()); 927 928 tm.targets.remove("target-2"); 929 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 930 assertEquals("one export was created", 1, exports.size()); 931 932 tm.name = "buildXX"; 933 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 934 assertEquals("no export was created when there is no action with build name", 0, exports.size()); 935 936 tm.name = "build"; 937 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 938 assertEquals("one export was created", 1, exports.size()); 939 940 JavaProjectGenerator.JavaCompilationUnit cu2 = new JavaProjectGenerator.JavaCompilationUnit(); 941 cu2.output = new ArrayList (); 942 cu2.output.add("build/classes"); 943 units.add(cu2); 944 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 945 assertEquals("two exports was created", 2, exports.size()); 946 947 cu2.output.add("dist/proj.jar"); 948 cu2.output.add("dist/proj2.jar"); 949 tm.script = "antScript"; 950 exports = JavaProjectGenerator.guessExports(evaluator, getWorkDir(), targets, units); 951 assertEquals("four exports were created", 4, exports.size()); 952 e = (JavaProjectGenerator.Export)exports.get(0); 953 assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type); 954 assertEquals("export is properly configured", "${outputfile}", e.location); 955 assertEquals("export is properly configured", "antScript", e.script); 956 assertEquals("export is properly configured", "target-1", e.buildTarget); 957 e = (JavaProjectGenerator.Export)exports.get(1); 958 assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_FOLDER, e.type); 959 assertEquals("export is properly configured", "build/classes", e.location); 960 assertEquals("export is properly configured", "antScript", e.script); 961 assertEquals("export is properly configured", "target-1", e.buildTarget); 962 e = (JavaProjectGenerator.Export)exports.get(2); 963 assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type); 964 assertEquals("export is properly configured", "dist/proj.jar", e.location); 965 assertEquals("export is properly configured", "antScript", e.script); 966 assertEquals("export is properly configured", "target-1", e.buildTarget); 967 e = (JavaProjectGenerator.Export)exports.get(3); 968 assertEquals("export is properly configured", JavaProjectConstants.ARTIFACT_TYPE_JAR, e.type); 969 assertEquals("export is properly configured", "dist/proj2.jar", e.location); 970 assertEquals("export is properly configured", "antScript", e.script); 971 assertEquals("export is properly configured", "target-1", e.buildTarget); 972 } 973 974 public void testPutExports() throws Exception { 975 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 976 FileObject base = helper.getProjectDirectory(); 977 Project p = ProjectManager.getDefault().findProject(base); 978 assertNotNull("Project was not created", p); 979 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 980 981 983 List exports = new ArrayList (); 984 JavaProjectGenerator.Export e = new JavaProjectGenerator.Export(); 985 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 986 e.location = "path/smth.jar"; 987 e.script = "someScript"; 988 e.buildTarget = "build_target"; 989 e.cleanTarget = "clean_target"; 990 exports.add(e); 991 e = new JavaProjectGenerator.Export(); 992 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 993 e.location = "something/else.jar"; 994 e.buildTarget = "bldtrg"; 995 exports.add(e); 996 997 JavaProjectGenerator.putExports(helper, exports); 998 Element el = Util.getPrimaryConfigurationData(helper); 999 List subElements = Util.findSubElements(el); 1000 assertEquals(5, subElements.size()); 1002 Element el2 = (Element )subElements.get(0); 1004 assertElement(el2, "name", null); 1005 el2 = (Element )subElements.get(1); 1006 assertElement(el2, "properties", null); 1007 el2 = (Element )subElements.get(2); 1008 assertElement(el2, "export", null); 1009 List l1 = Util.findSubElements(el2); 1010 assertEquals(5, l1.size()); 1011 assertElementArray(l1, 1012 new String []{"type", "location", "script", "build-target", "clean-target"}, 1013 new String []{JavaProjectConstants.ARTIFACT_TYPE_JAR, "path/smth.jar", "someScript", "build_target", "clean_target"}); 1014 el2 = (Element )subElements.get(3); 1016 assertElement(el2, "export", null); 1017 l1 = Util.findSubElements(el2); 1018 assertEquals(3, l1.size()); 1019 assertElementArray(l1, 1020 new String []{"type", "location", "build-target"}, 1021 new String []{JavaProjectConstants.ARTIFACT_TYPE_JAR, "something/else.jar", "bldtrg"}); 1022 el2 = (Element )subElements.get(4); 1023 assertElement(el2, "view", null); 1024 ProjectManager.getDefault().saveAllProjects(); 1026 validate(p); 1027 1028 1030 exports = new ArrayList (); 1031 e = new JavaProjectGenerator.Export(); 1032 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 1033 e.location = "aaa/bbb.jar"; 1034 e.buildTarget = "ccc"; 1035 exports.add(e); 1036 1037 JavaProjectGenerator.putExports(helper, exports); 1038 el = Util.getPrimaryConfigurationData(helper); 1039 subElements = Util.findSubElements(el); 1040 assertEquals(4, subElements.size()); 1042 el2 = (Element )subElements.get(0); 1044 assertElement(el2, "name", null); 1045 el2 = (Element )subElements.get(1); 1046 assertElement(el2, "properties", null); 1047 el2 = (Element )subElements.get(2); 1048 assertElement(el2, "export", null); 1049 l1 = Util.findSubElements(el2); 1050 assertEquals(3, l1.size()); 1051 assertElementArray(l1, 1052 new String []{"type", "location", "build-target"}, 1053 new String []{JavaProjectConstants.ARTIFACT_TYPE_JAR, "aaa/bbb.jar", "ccc"}); 1054 el2 = (Element )subElements.get(3); 1055 assertElement(el2, "view", null); 1056 ProjectManager.getDefault().saveAllProjects(); 1058 validate(p); 1059 1060 } 1061 1062 public void testGuessSubprojects() throws Exception { 1063 AntProjectHelper helper = createEmptyProject("proj1", "proj1", false); 1064 FileObject base = helper.getProjectDirectory(); 1065 Project p = ProjectManager.getDefault().findProject(base); 1066 assertNotNull("Project was not created", p); 1067 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 1068 ArrayList exports = new ArrayList (); 1069 JavaProjectGenerator.Export e = new JavaProjectGenerator.Export(); 1070 e.type = JavaProjectConstants.ARTIFACT_TYPE_JAR; 1071 e.location = "libs/some.jar"; e.buildTarget = "build_target"; 1073 exports.add(e); 1074 JavaProjectGenerator.putExports(helper, exports); 1075 ProjectManager.getDefault().saveAllProjects(); 1076 String lib1path = lib1.getAbsolutePath(); 1077 String proj1path = FileUtil.toFile(base).getAbsolutePath(); 1078 1079 AntProjectHelper helper2 = createEmptyProject("proj2", "proj2", false); 1080 FileObject base2 = helper.getProjectDirectory(); 1081 File projBase = FileUtil.toFile(base2); 1082 Project p2 = ProjectManager.getDefault().findProject(base2); 1083 assertNotNull("Project was not created", p2); 1084 assertEquals("Project folder is incorrect", base2, p.getProjectDirectory()); 1085 1086 PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 1087 PropertyUtils.fixedPropertyProvider( 1088 Collections.singletonMap("lib1", lib1path))}); 1089 1090 ArrayList units = new ArrayList (); 1091 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 1092 JavaProjectGenerator.JavaCompilationUnit.CP cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 1093 cp.mode = "compile"; 1094 cp.classpath = "../something.jar;${lib1};"; 1095 cu.classpath = new ArrayList (); 1096 cu.classpath.add(cp); 1097 units.add(cu); 1098 cu = new JavaProjectGenerator.JavaCompilationUnit(); 1099 cp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 1100 cp.mode = "compile"; 1101 cp.classpath = lib1path+";"; 1102 cu.classpath = new ArrayList (); 1103 cu.classpath.add(cp); 1104 units.add(cu); 1105 1106 List l = JavaProjectGenerator.guessSubprojects(evaluator, units, projBase, projBase); 1107 assertEquals("one subproject", 1, l.size()); 1108 assertEquals("project1 is subproject", ".", l.get(0)); 1109 } 1110 1111 public void testPutSubprojects() throws Exception { 1112 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 1113 FileObject base = helper.getProjectDirectory(); 1114 Project p = ProjectManager.getDefault().findProject(base); 1115 assertNotNull("Project was not created", p); 1116 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 1117 1118 1120 List subprojects = new ArrayList (); 1121 subprojects.add("/some/path/projA"); 1122 subprojects.add("C:\\dev\\projB"); 1123 1124 JavaProjectGenerator.putSubprojects(helper, subprojects); 1125 Element el = Util.getPrimaryConfigurationData(helper); 1126 Element subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE); 1127 assertNotNull("<subprojects> element exists", subprojectsEl); 1128 List subElements = Util.findSubElements(subprojectsEl); 1129 assertEquals("project depends on two subprojects", 2, subElements.size()); 1130 Element el2 = (Element )subElements.get(0); 1131 assertElement(el2, "project", "/some/path/projA"); 1132 el2 = (Element )subElements.get(1); 1133 assertElement(el2, "project", "C:\\dev\\projB"); 1134 1135 ProjectManager.getDefault().saveAllProjects(); 1137 validate(p); 1138 1139 1141 subprojects = new ArrayList (); 1142 subprojects.add("/projC"); 1143 JavaProjectGenerator.putSubprojects(helper, subprojects); 1144 el = Util.getPrimaryConfigurationData(helper); 1145 subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE); 1146 subElements = Util.findSubElements(subprojectsEl); 1147 assertEquals("project depends on one subproject", 1, subElements.size()); 1148 el2 = (Element )subElements.get(0); 1149 assertElement(el2, "project", "/projC"); 1150 subprojects = new ArrayList (); 1151 JavaProjectGenerator.putSubprojects(helper, subprojects); 1152 el = Util.getPrimaryConfigurationData(helper); 1153 subprojectsEl = Util.findElement(el, "subprojects", Util.NAMESPACE); 1154 subElements = Util.findSubElements(subprojectsEl); 1155 assertEquals("project depends on one subproject", 0, subElements.size()); 1156 1157 ProjectManager.getDefault().saveAllProjects(); 1159 validate(p); 1160 1161 } 1162 1163 public void testGuessBuildFolders() throws Exception { 1164 File base = new File (getWorkDir(), "folder"); 1165 File proj1 = new File (base, "proj1"); 1166 proj1.mkdir(); 1167 File base2 = new File (getWorkDir(), "folder2"); 1168 1169 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 1170 cu.output = new ArrayList (); 1171 cu.output.add("${outputfile}"); 1172 ArrayList units = new ArrayList (); 1173 units.add(cu); 1174 1175 Map m = new HashMap (); 1176 m.put("outputfile", "out.jar"); 1177 PropertyEvaluator evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 1178 PropertyUtils.fixedPropertyProvider(m)}); 1179 List buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1180 assertEquals("no build folder", 0, buildFolders.size()); 1181 1182 m.put("outputfile", "../proj1_diff/out.jar"); 1183 evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 1184 PropertyUtils.fixedPropertyProvider(m)}); 1185 buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1186 assertEquals("one build-folder created", 1, buildFolders.size()); 1187 assertEquals("export is properly configured", base.getAbsolutePath()+File.separator+"proj1_diff", buildFolders.get(0)); 1188 1189 m.put("outputfile", "../out.jar"); 1190 evaluator = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[]{ 1191 PropertyUtils.fixedPropertyProvider(m)}); 1192 buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1193 assertEquals("one build-folder created", 1, buildFolders.size()); 1194 assertEquals("export is properly configured", base.getAbsolutePath(), buildFolders.get(0)); 1195 1196 cu.output.add(base2.getAbsolutePath()); 1197 cu.output.add("other.jar"); 1198 buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1199 assertEquals("two build-folder created", 2, buildFolders.size()); 1200 assertEquals("export is properly configured", base.getAbsolutePath(), buildFolders.get(0)); 1201 assertEquals("export is properly configured", base2.getAbsolutePath(), buildFolders.get(1)); 1202 1203 cu.output.add(getWorkDir().getAbsolutePath()); 1204 buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1205 assertEquals("one build-folder created", 1, buildFolders.size()); 1206 assertEquals("export is properly configured", getWorkDir().getAbsolutePath(), buildFolders.get(0)); 1207 1208 File diskRoot = getWorkDir(); 1210 while (diskRoot.getParentFile() != null) { 1211 diskRoot = diskRoot.getParentFile(); 1212 } 1213 cu.output.add(diskRoot.getAbsolutePath()); 1214 buildFolders = JavaProjectGenerator.guessBuildFolders(evaluator, units, proj1, proj1); 1215 assertEquals("one build-folder created", 1, buildFolders.size()); 1216 assertEquals("export is properly configured", diskRoot.getAbsolutePath(), buildFolders.get(0)); 1217 } 1218 1219 public void testPutBuildFolders() throws Exception { 1220 AntProjectHelper helper = createEmptyProject("proj", "proj", false); 1221 FileObject base = helper.getProjectDirectory(); 1222 Project p = ProjectManager.getDefault().findProject(base); 1223 assertNotNull("Project was not created", p); 1224 assertEquals("Project folder is incorrect", base, p.getProjectDirectory()); 1225 1226 1228 List buildFolders = new ArrayList (); 1229 buildFolders.add("/some/path/projA"); 1230 buildFolders.add("C:\\dev\\projB"); 1231 1232 JavaProjectGenerator.putBuildFolders(helper, buildFolders); 1233 Element el = Util.getPrimaryConfigurationData(helper); 1234 Element foldersEl = Util.findElement(el, "folders", Util.NAMESPACE); 1235 assertNotNull("<folders> element exists", foldersEl); 1236 List subElements = Util.findSubElements(foldersEl); 1237 assertEquals("project has two build-folders", 2, subElements.size()); 1238 Element el2 = (Element )subElements.get(0); 1239 assertElement(el2, "build-folder", null); 1240 assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size()); 1241 assertElement((Element )Util.findSubElements(el2).get(0), "location", "/some/path/projA"); 1242 el2 = (Element )subElements.get(1); 1243 assertElement(el2, "build-folder", null); 1244 assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size()); 1245 assertElement((Element )Util.findSubElements(el2).get(0), "location", "C:\\dev\\projB"); 1246 1247 ProjectManager.getDefault().saveAllProjects(); 1249 validate(p); 1250 1251 1253 buildFolders = new ArrayList (); 1254 buildFolders.add("/projC"); 1255 JavaProjectGenerator.putBuildFolders(helper, buildFolders); 1256 el = Util.getPrimaryConfigurationData(helper); 1257 foldersEl = Util.findElement(el, "folders", Util.NAMESPACE); 1258 subElements = Util.findSubElements(foldersEl); 1259 assertEquals("project has one build-folder", 1, subElements.size()); 1260 el2 = (Element )subElements.get(0); 1261 assertElement(el2, "build-folder", null); 1262 assertEquals("build-folder has one subelement", 1, Util.findSubElements(el2).size()); 1263 assertElement((Element )Util.findSubElements(el2).get(0), "location", "/projC"); 1264 buildFolders = new ArrayList (); 1265 JavaProjectGenerator.putBuildFolders(helper, buildFolders); 1266 el = Util.getPrimaryConfigurationData(helper); 1267 foldersEl = Util.findElement(el, "folders", Util.NAMESPACE); 1268 subElements = Util.findSubElements(foldersEl); 1269 assertEquals("project has no build-folder", 0, subElements.size()); 1270 1271 ProjectManager.getDefault().saveAllProjects(); 1273 validate(p); 1274 1275 } 1276 1277 private static class Listener implements ChangeListener { 1278 int count = 0; 1279 public void stateChanged(ChangeEvent ev) { 1280 count++; 1281 } 1282 public void reset() { 1283 count = 0; 1284 } 1285 } 1286 1287 public void createRealJarFile(File f) throws Exception { 1289 OutputStream os = new FileOutputStream (f); 1290 try { 1291 JarOutputStream jos = new JarOutputStream (os); 1292 JarEntry entry = new JarEntry ("foo.txt"); 1294 jos.putNextEntry(entry); 1298 jos.flush(); 1299 jos.close(); 1300 } finally { 1301 os.close(); 1302 } 1303 } 1304 1305 private static String [] getSchemas() throws Exception { 1306 return new String [] { 1307 FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(), 1308 JavaProjectGenerator.class.getResource("resources/freeform-project-java.xsd").toExternalForm(), 1309 JavaProjectGenerator.class.getResource("resources/freeform-project-java-2.xsd").toExternalForm(), 1310 AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(), 1311 }; 1312 } 1313 1314 public static void validate(Project proj) throws Exception { 1315 File projF = FileUtil.toFile(proj.getProjectDirectory()); 1316 File xml = new File (new File (projF, "nbproject"), "project.xml"); 1317 SAXParserFactory f = (SAXParserFactory )Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance(); 1318 if (f == null) { 1319 System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath"); 1320 return; 1321 } 1322 f.setNamespaceAware(true); 1323 f.setValidating(true); 1324 SAXParser p = f.newSAXParser(); 1325 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 1326 "http://www.w3.org/2001/XMLSchema"); 1327 p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas()); 1328 try { 1329 p.parse(xml.toURI().toString(), new Handler ()); 1330 } catch (SAXParseException e) { 1331 assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+ 1332 e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false); 1333 } 1334 } 1335 1336 private static final class Handler extends DefaultHandler { 1337 public void warning(SAXParseException e) throws SAXException { 1338 throw e; 1339 } 1340 public void error(SAXParseException e) throws SAXException { 1341 throw e; 1342 } 1343 public void fatalError(SAXParseException e) throws SAXException { 1344 throw e; 1345 } 1346 } 1347 1348} 1349 | Popular Tags |