1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.ByteArrayOutputStream ; 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.OutputStream ; 26 import java.util.Arrays ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import org.netbeans.api.project.ProjectManager; 31 import org.netbeans.modules.ant.freeform.FreeformProject; 32 import org.netbeans.modules.ant.freeform.FreeformProjectType; 33 import org.netbeans.modules.ant.freeform.TestBase; 34 import org.netbeans.modules.ant.freeform.spi.support.Util; 35 import org.netbeans.spi.project.ActionProvider; 36 import org.netbeans.spi.project.AuxiliaryConfiguration; 37 import org.openide.filesystems.FileObject; 38 import org.openide.loaders.DataObject; 39 import org.openide.util.Lookup; 40 import org.openide.util.lookup.Lookups; 41 import org.openide.xml.XMLUtil; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Element ; 44 45 49 public class JavaActionsTest extends TestBase { 50 51 static { 52 Locale.setDefault(Locale.US); 54 } 55 56 public JavaActionsTest(String name) { 57 super(name); 58 } 59 60 private FreeformProject prj; 61 private JavaActions ja; 62 private FileObject src, myAppJava, someFileJava, someResourceTxt, antsrc, specialTaskJava, buildProperties; 63 64 protected void setUp() throws Exception { 65 super.setUp(); 66 prj = copyProject(simple); 67 Element data = prj.getPrimaryConfigurationData(); 69 Element ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 70 assertNotNull(ideActions); 71 Iterator <Element > actionsIt = Util.findSubElements(ideActions).iterator(); 72 while (actionsIt.hasNext()) { 73 Element action = actionsIt.next(); 74 assertEquals("action", action.getLocalName()); 75 if (Util.findElement(action, "context", Util.NAMESPACE) != null) { 76 ideActions.removeChild(action); 77 } 78 } 79 prj.putPrimaryConfigurationData(data); 80 ProjectManager.getDefault().saveProject(prj); 81 AuxiliaryConfiguration origAux = prj.getLookup().lookup(AuxiliaryConfiguration.class); 82 AuxiliaryConfiguration aux = new LookupProviderImpl.UpgradingAuxiliaryConfiguration(origAux); 83 ja = new JavaActions(prj, prj.helper(), prj.evaluator(), aux); 84 src = prj.getProjectDirectory().getFileObject("src"); 85 assertNotNull(src); 86 myAppJava = src.getFileObject("org/foo/myapp/MyApp.java"); 87 assertNotNull(myAppJava); 88 someFileJava = src.getFileObject("org/foo/myapp/SomeFile.java"); 89 assertNotNull(someFileJava); 90 someResourceTxt = src.getFileObject("org/foo/myapp/some-resource.txt"); 91 assertNotNull(someResourceTxt); 92 antsrc = prj.getProjectDirectory().getFileObject("antsrc"); 93 assertNotNull(antsrc); 94 specialTaskJava = antsrc.getFileObject("org/foo/ant/SpecialTask.java"); 95 assertNotNull(specialTaskJava); 96 buildProperties = prj.getProjectDirectory().getFileObject("build.properties"); 97 assertNotNull(buildProperties); 98 } 99 100 public void testContainsSelectedJavaSources() throws Exception { 101 assertTrue(ja.containsSelectedJavaSources(src, context(new FileObject[] {myAppJava}))); 102 assertFalse(ja.containsSelectedJavaSources(src, context(new FileObject[] {myAppJava, someResourceTxt}))); 103 } 104 105 public void testFindPackageRoot() throws Exception { 106 Lookup context = context(new FileObject[] {myAppJava}); 107 JavaActions.AntLocation loc = ja.findPackageRoot(context); 108 assertNotNull("found a package root for " + context, loc); 109 assertEquals("right name", "${src.dir}", loc.virtual); 110 assertEquals("right physical", src, loc.physical); 111 context = context(new FileObject[] {myAppJava, someFileJava}); 112 loc = ja.findPackageRoot(context); 113 assertNotNull("found a package root for " + context, loc); 114 assertEquals("right name", "${src.dir}", loc.virtual); 115 assertEquals("right physical", src, loc.physical); 116 context = context(new FileObject[] {src}); 117 loc = ja.findPackageRoot(context); 118 assertNotNull("found a package root for " + context, loc); 119 assertEquals("right name", "${src.dir}", loc.virtual); 120 assertEquals("right physical", src, loc.physical); 121 context = context(new FileObject[] {myAppJava, someResourceTxt}); 122 loc = ja.findPackageRoot(context); 123 assertNull("found no package root for " + context + ": " + loc, loc); 124 context = context(new FileObject[] {myAppJava, specialTaskJava}); 125 loc = ja.findPackageRoot(context); 126 assertNull("found no package root for " + context, loc); 127 context = context(new FileObject[] {}); 128 loc = ja.findPackageRoot(context); 129 assertNull("found no package root for " + context, loc); 130 context = context(new FileObject[] {specialTaskJava}); 131 loc = ja.findPackageRoot(context); 132 assertNotNull("found a package root for " + context, loc); 133 assertEquals("right name", "${ant.src.dir}", loc.virtual); 134 assertEquals("right physical", antsrc, loc.physical); 135 context = context(new FileObject[] {buildProperties}); 136 loc = ja.findPackageRoot(context); 137 assertNull("found no package root for " + context, loc); 138 } 139 140 public void testGetSupportedActions() throws Exception { 141 assertEquals("initially all context-sensitive actions supported", 142 Arrays.asList(new String [] { 143 ActionProvider.COMMAND_COMPILE_SINGLE, 144 ActionProvider.COMMAND_DEBUG, 145 ActionProvider.COMMAND_RUN_SINGLE, 146 ActionProvider.COMMAND_DEBUG_SINGLE 147 }), 148 Arrays.asList(ja.getSupportedActions())); 149 155 } 156 157 public void testIsActionEnabled() throws Exception { 158 assertTrue("enabled on some source files", ja.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context(new FileObject[] {myAppJava, someFileJava}))); 159 assertFalse("disabled on other stuff", ja.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context(new FileObject[] {buildProperties}))); 160 } 161 162 private static boolean useDO = false; private Lookup context(FileObject[] files) throws Exception { 164 Object [] objs = new Object [files.length]; 165 for (int i = 0; i < files.length; i++) { 166 objs[i] = useDO ? (Object ) DataObject.find(files[i]) : files[i]; 167 useDO = !useDO; 168 } 169 return Lookups.fixed(objs); 170 } 171 172 public void testFindClassesOutputDir() throws Exception { 173 assertEquals("Output for src", "${classes.dir}", ja.findClassesOutputDir("${src.dir}")); 174 assertEquals("Output for antsrc", "${ant.classes.dir}", ja.findClassesOutputDir("${ant.src.dir}")); 175 assertEquals("No output for bogussrc", null, ja.findClassesOutputDir("${bogus.src.dir}")); 176 } 177 178 public void testAddBinding() throws Exception { 179 ja.addBinding("some.action", "special.xml", "special-target", "selection", "${some.src.dir}", "\\.java$", "relative-path", ","); 180 Element data = prj.getPrimaryConfigurationData(); 181 assertNotNull(data); 182 Element ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 183 assertNotNull(ideActions); 184 List <Element > actions = Util.findSubElements(ideActions); 185 Element lastAction = actions.get(actions.size() - 1); 186 String expectedXml = 187 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 188 "<action xmlns=\"" + Util.NAMESPACE + "\" name=\"some.action\">\n" + 189 " <script>special.xml</script>\n" + 190 " <target>special-target</target>\n" + 191 " <context>\n" + 192 " <property>selection</property>\n" + 193 " <folder>${some.src.dir}</folder>\n" + 194 " <pattern>\\.java$</pattern>\n" + 195 " <format>relative-path</format>\n" + 196 " <arity>\n" + 197 " <separated-files>,</separated-files>\n" + 198 " </arity>\n" + 199 " </context>\n" + 200 "</action>\n"; 201 assertEquals(expectedXml, xmlToString(lastAction)); 202 ja.addBinding("some.other.action", "special.xml", "special-target", "selection", "${some.src.dir}", null, "relative-path", null); 203 data = prj.getPrimaryConfigurationData(); 204 ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 205 actions = Util.findSubElements(ideActions); 206 lastAction = actions.get(actions.size() - 1); 207 expectedXml = 208 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 209 "<action xmlns=\"" + Util.NAMESPACE + "\" name=\"some.other.action\">\n" + 210 " <script>special.xml</script>\n" + 211 " <target>special-target</target>\n" + 212 " <context>\n" + 213 " <property>selection</property>\n" + 214 " <folder>${some.src.dir}</folder>\n" + 215 " <format>relative-path</format>\n" + 216 " <arity>\n" + 217 " <one-file-only/>\n" + 218 " </arity>\n" + 219 " </context>\n" + 220 "</action>\n"; 221 assertEquals(expectedXml, xmlToString(lastAction)); 222 ja.addBinding("general.action", "special.xml", "special-target", null, null, null, null, null); 224 data = prj.getPrimaryConfigurationData(); 225 ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 226 actions = Util.findSubElements(ideActions); 227 lastAction = actions.get(actions.size() - 1); 228 expectedXml = 229 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 230 "<action xmlns=\"" + Util.NAMESPACE + "\" name=\"general.action\">\n" + 231 " <script>special.xml</script>\n" + 232 " <target>special-target</target>\n" + 233 "</action>\n"; 234 assertEquals(expectedXml, xmlToString(lastAction)); 235 Element view = Util.findElement(data, "view", Util.NAMESPACE); 236 assertNotNull(view); 237 Element contextMenu = Util.findElement(view, "context-menu", Util.NAMESPACE); 238 assertNotNull(contextMenu); 239 List <Element > contextMenuActions = Util.findSubElements(contextMenu); 241 Element lastContextMenuAction = contextMenuActions.get(contextMenuActions.size() - 1); 242 expectedXml = 243 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 244 "<ide-action xmlns=\"" + Util.NAMESPACE + "\" name=\"general.action\"/>\n"; 245 assertEquals(expectedXml, xmlToString(lastContextMenuAction)); 246 247 data = prj.getPrimaryConfigurationData(); 249 ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 250 data.removeChild(ideActions); 251 252 ja.addBinding("some.other.action", "special.xml", "special-target", "selection", "${some.src.dir}", null, "relative-path", null); 253 data = prj.getPrimaryConfigurationData(); 254 ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE); 255 actions = Util.findSubElements(ideActions); 256 lastAction = actions.get(actions.size() - 1); 257 expectedXml = 258 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 259 "<action xmlns=\"" + Util.NAMESPACE + "\" name=\"some.other.action\">\n" + 260 " <script>special.xml</script>\n" + 261 " <target>special-target</target>\n" + 262 " <context>\n" + 263 " <property>selection</property>\n" + 264 " <folder>${some.src.dir}</folder>\n" + 265 " <format>relative-path</format>\n" + 266 " <arity>\n" + 267 " <one-file-only/>\n" + 268 " </arity>\n" + 269 " </context>\n" + 270 "</action>\n"; 271 assertEquals(expectedXml, xmlToString(lastAction)); 272 } 273 274 public void testCreateCompileSingleTarget() throws Exception { 275 Document doc = XMLUtil.createDocument("fake", null, null, null); 276 Lookup context = context(new FileObject[] {someFileJava}); 277 Element target = ja.createCompileSingleTarget(doc, context, "files", new JavaActions.AntLocation("${src.dir}", src)); 278 String expectedXml = 279 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 280 "<target name=\"compile-selected-files-in-src\">\n" + 281 " <fail unless=\"files\">Must set property 'files'</fail>\n" + 282 " <mkdir dir=\"${classes.dir}\"/>\n" + 283 " <javac destdir=\"${classes.dir}\" includes=\"${files}\" source=\"1.4\" srcdir=\"${src.dir}\">\n" + 284 " <classpath path=\"${src.cp}\"/>\n" + 285 " </javac>\n" + 286 "</target>\n"; 287 assertEquals(expectedXml, xmlToString(target)); 288 } 289 290 public void testReadWriteCustomScript() throws Exception { 291 Document script = ja.readCustomScript(JavaActions.FILE_SCRIPT_PATH); 292 String expectedXml = 293 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 294 "<project name=\"Simple Freeform Project-IDE\"/>\n"; 295 assertEquals(expectedXml, xmlToString(script.getDocumentElement())); 296 script.getDocumentElement().appendChild(script.createElement("foo")); 297 ja.writeCustomScript(script, JavaActions.FILE_SCRIPT_PATH); 298 script = ja.readCustomScript(JavaActions.FILE_SCRIPT_PATH); 299 expectedXml = 300 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 301 "<project name=\"Simple Freeform Project-IDE\">\n" + 302 " <foo/>\n" + 303 "</project>\n"; 304 assertEquals(expectedXml, xmlToString(script.getDocumentElement())); 305 } 306 307 public void testFindSourceLevel() throws Exception { 308 assertEquals("1.4", ja.findSourceLevel("${src.dir}")); 309 assertEquals("1.4", ja.findSourceLevel("${ant.src.dir}")); 310 assertEquals(null, ja.findSourceLevel("${bogus.src.dir}")); 311 } 312 313 public void testFindCompileClasspath() throws Exception { 314 assertEquals("${src.cp}", ja.findCUClasspath("${src.dir}", "compile")); 315 assertEquals("${ant.src.cp}", ja.findCUClasspath("${ant.src.dir}", "compile")); 316 assertEquals(null, ja.findCUClasspath("${bogus.src.dir}", "compile")); 317 } 318 319 public void testFindLine() throws Exception { 320 Document script = ja.readCustomScript("special.xml"); 321 Element target = script.createElement("target"); 322 target.setAttribute("name", "targ1"); 323 target.appendChild(script.createElement("task1")); 324 target.appendChild(script.createElement("task2")); 325 script.getDocumentElement().appendChild(target); 326 target = script.createElement("target"); 327 target.setAttribute("name", "targ2"); 328 target.appendChild(script.createElement("task3")); 329 script.getDocumentElement().appendChild(target); 330 ja.writeCustomScript(script, "special.xml"); 331 FileObject scriptFile = prj.getProjectDirectory().getFileObject("special.xml"); 332 assertNotNull(scriptFile); 333 assertEquals(2, JavaActions.findLine(scriptFile, "targ1", "target", "name")); 344 assertEquals(6, JavaActions.findLine(scriptFile, "targ2", "target", "name")); 345 assertEquals(-1, JavaActions.findLine(scriptFile, "no-such-targ", "target", "name")); 346 FileObject pxml = prj.getProjectDirectory().getFileObject("nbproject/project.xml"); 348 assertNotNull(pxml); 349 assertTrue(JavaActions.findLine(pxml, "build", "action", "name") != -1); 350 assertEquals(-1, JavaActions.findLine(pxml, "nonexistent", "action", "name")); 351 } 352 353 public void testFindCommandBinding() throws Exception { 354 String [] binding = ja.findCommandBinding(ActionProvider.COMMAND_RUN); 355 assertNotNull(binding); 356 assertEquals(Arrays.asList(new String [] {"build.xml", "start"}), Arrays.asList(binding)); 357 binding = ja.findCommandBinding(ActionProvider.COMMAND_REBUILD); 358 assertNotNull(binding); 359 assertEquals(Arrays.asList(new String [] {"build.xml", "clean", "jar"}), Arrays.asList(binding)); 360 binding = ja.findCommandBinding("bogus"); 361 assertNull(binding); 362 } 363 364 public void testFindExistingBuildTarget() throws Exception { 365 Element target = ja.findExistingBuildTarget(ActionProvider.COMMAND_RUN); 366 assertNotNull("found a target for 'run'", target); 367 assertEquals("found correct target", "start", target.getAttribute("name")); 368 } 369 370 public void testTargetUsesTaskExactlyOnce() throws Exception { 371 Element runTarget = ja.findExistingBuildTarget(ActionProvider.COMMAND_RUN); 372 Element javaTask = ja.targetUsesTaskExactlyOnce(runTarget, "java"); 373 assertNotNull("found <java>", javaTask); 374 assertEquals("java", javaTask.getLocalName()); 375 assertEquals("org.foo.myapp.MyApp", javaTask.getAttribute("classname")); 376 assertNull("no <javac> here", ja.targetUsesTaskExactlyOnce(runTarget, "javac")); 377 Element cleanTarget = ja.findExistingBuildTarget(ActionProvider.COMMAND_CLEAN); 378 assertNotNull(cleanTarget); 379 assertNull(">1 <delete> found so skipping", ja.targetUsesTaskExactlyOnce(cleanTarget, "delete")); 380 } 381 382 public void testEnsurePropertiesCopied() throws Exception { 383 Document doc = XMLUtil.createDocument("project", null, null, null); 384 Element root = doc.getDocumentElement(); 385 ja.ensurePropertiesCopied(root); 386 String expectedXml = 387 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 388 "<project basedir=\"..\">\n" + 389 " <property name=\"build.properties\" value=\"build.properties\"/>\n" + 390 " <property file=\"${build.properties}\"/>\n" + 391 "</project>\n"; 392 assertEquals("Correct code generated", expectedXml, xmlToString(root)); 393 ja.ensurePropertiesCopied(root); 394 assertEquals("Idempotent", expectedXml, xmlToString(root)); 395 } 396 397 public void testEnsureImports() throws Exception { 398 Element root = XMLUtil.createDocument("project", null, null, null).getDocumentElement(); 400 ja.ensureImports(root, "build.xml"); 401 String expectedXml = 402 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 403 "<project basedir=\"..\">\n" + 404 " <import file=\"../build.xml\"/>\n" + 405 "</project>\n"; 406 assertEquals("Correct code generated", expectedXml, xmlToString(root)); 407 ja.ensureImports(root, "build.xml"); 408 assertEquals("Idempotent", expectedXml, xmlToString(root)); 409 File testdir = getWorkDir(); 411 File subtestdir = new File (testdir, "sub"); 412 subtestdir.mkdir(); 413 File script = new File (subtestdir, "external.xml"); 414 Document doc = XMLUtil.createDocument("project", null, null, null); 415 doc.getDocumentElement().setAttribute("basedir", ".."); 416 OutputStream os = new FileOutputStream (script); 417 try { 418 XMLUtil.write(doc, os, "UTF-8"); 419 } finally { 420 os.close(); 421 } 422 root = XMLUtil.createDocument("project", null, null, null).getDocumentElement(); 423 String scriptPath = script.getAbsolutePath(); 424 ja.ensureImports(root, scriptPath); 425 expectedXml = 426 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 427 "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" + 428 " <import file=\"" + scriptPath + "\"/>\n" + 429 "</project>\n"; 430 assertEquals("Correct code generated for external script", expectedXml, xmlToString(root)); 431 Element data = prj.getPrimaryConfigurationData(); 433 Element properties = Util.findElement(data, "properties", Util.NAMESPACE); 434 assertNotNull(properties); 435 Element property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property"); 436 property.setAttribute("name", "external.xml"); 437 property.appendChild(data.getOwnerDocument().createTextNode(scriptPath)); 438 properties.appendChild(property); 439 property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property"); 440 property.setAttribute("name", "subtestdir"); 441 property.appendChild(data.getOwnerDocument().createTextNode(subtestdir.getAbsolutePath())); 442 properties.appendChild(property); 443 property = data.getOwnerDocument().createElementNS(Util.NAMESPACE, "property"); 444 property.setAttribute("name", "testdir"); 445 property.appendChild(data.getOwnerDocument().createTextNode(testdir.getAbsolutePath())); 446 properties.appendChild(property); 447 prj.putPrimaryConfigurationData(data); 448 ProjectManager.getDefault().saveProject(prj); root = XMLUtil.createDocument("project", null, null, null).getDocumentElement(); 450 ja.ensureImports(root, "${external.xml}"); 451 expectedXml = 452 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 453 "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" + 454 " <import file=\"" + scriptPath + "\"/>\n" + 455 "</project>\n"; 456 assertEquals("Correct code generated for ${external.xml}", expectedXml, xmlToString(root)); 457 root = XMLUtil.createDocument("project", null, null, null).getDocumentElement(); 458 ja.ensureImports(root, "${subtestdir}" + File.separator + "external.xml"); 459 expectedXml = 460 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 461 "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" + 462 " <import file=\"" + scriptPath + "\"/>\n" + 463 "</project>\n"; 464 assertEquals("Correct code generated for ${subtestdir}/external.xml", expectedXml, xmlToString(root)); 465 root = XMLUtil.createDocument("project", null, null, null).getDocumentElement(); 466 ja.ensureImports(root, "${testdir}" + File.separator + "sub" +File.separator + "external.xml"); 467 expectedXml = 468 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 469 "<project basedir=\"" + testdir.getAbsolutePath() + "\">\n" + 470 " <import file=\"" + scriptPath + "\"/>\n" + 471 "</project>\n"; 472 assertEquals("Correct code generated for ${testdir}/sub/external.xml", expectedXml, xmlToString(root)); 473 } 475 476 public void testCreateDebugTargetFromTemplate() throws Exception { 477 Document doc = XMLUtil.createDocument("project", null, null, null); 478 Document origDoc = XMLUtil.createDocument("target", null, null, null); 479 Element origTarget = origDoc.getDocumentElement(); 480 origTarget.setAttribute("name", "ignored"); 481 origTarget.setAttribute("depends", "compile"); 482 origTarget.appendChild(origDoc.createElement("task1")); 483 Element task = origDoc.createElement("java"); 484 task.setAttribute("classpath", "${cp}"); 486 task.appendChild(origDoc.createElement("stuff")); 487 origTarget.appendChild(task); 488 origTarget.appendChild(origDoc.createElement("task2")); 489 Element genTarget = ja.createDebugTargetFromTemplate("debug", origTarget, task, doc); 490 doc.getDocumentElement().appendChild(genTarget); 491 String expectedXml = 492 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 493 "<project>\n" + 494 " <target depends=\"compile\" name=\"debug\">\n" + 495 " <task1/>\n" + 496 " <nbjpdastart addressproperty=\"jpda.address\" name=\"Simple Freeform Project\" transport=\"dt_socket\">\n" + 497 " <classpath path=\"${cp}\"/>\n" + 498 " </nbjpdastart>\n" + 499 " <java classpath=\"${cp}\" fork=\"true\">\n" + 500 " <stuff/>\n" + 501 " <jvmarg value=\"-Xdebug\"/>\n" + 502 " <jvmarg value=\"-Xrunjdwp:transport=dt_socket,address=${jpda.address}\"/>\n" + 503 " </java>\n" + 504 " <task2/>\n" + 505 " </target>\n" + 506 "</project>\n"; 507 assertEquals(expectedXml, xmlToString(doc.getDocumentElement())); 508 } 509 510 public void testCreateDebugTargetFromScratch() throws Exception { 511 Document doc = XMLUtil.createDocument("project", null, null, null); 512 Element genTarget = ja.createDebugTargetFromScratch("debug", doc); 513 doc.getDocumentElement().appendChild(genTarget); 514 String expectedXml = 515 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 516 "<project>\n" + 517 " <target name=\"debug\">\n" + 518 " <path id=\"cp\">\n" + 519 " <!---->\n" + 520 " </path>\n" + 521 " <nbjpdastart addressproperty=\"jpda.address\" name=\"Simple Freeform Project\" transport=\"dt_socket\">\n" + 522 " <classpath refid=\"cp\"/>\n" + 523 " </nbjpdastart>\n" + 524 " <!---->\n" + 525 " <java classname=\"some.main.Class\" fork=\"true\">\n" + 526 " <classpath refid=\"cp\"/>\n" + 527 " <jvmarg value=\"-Xdebug\"/>\n" + 528 " <jvmarg value=\"-Xrunjdwp:transport=dt_socket,address=${jpda.address}\"/>\n" + 529 " </java>\n" + 530 " </target>\n" + 531 "</project>\n"; 532 assertEquals(expectedXml, xmlToString(doc.getDocumentElement())); 533 } 534 535 public void testCreateRunSingleTargetElem() throws Exception { 536 Document doc = XMLUtil.createDocument("project", null, null, null); 537 Lookup context = context(new FileObject[] {myAppJava}); 538 JavaActions.AntLocation root = ja.findPackageRoot(context); 539 Element targetElem = ja.createRunSingleTargetElem(doc, "run-single-test-target", "test.class", root); 540 doc.getDocumentElement().appendChild(targetElem); 541 String expectedXml = 542 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 543 "<project>\n" + 544 " <target name=\"run-single-test-target\">\n" + 545 " <fail unless=\"test.class\">Must set property 'test.class'</fail>\n" + 546 " <ant antfile=\"build.xml\" inheritall=\"false\" target=\"jar\"/>\n" + 547 " <java classname=\"${test.class}\" failonerror=\"true\" fork=\"true\">\n" + 548 " <classpath>\n" + 549 " <pathelement path=\"${src.cp}\"/>\n" + 550 " <pathelement location=\"${classes.dir}\"/>\n" + 551 " <pathelement location=\"${main.jar}\"/>\n" + 552 " </classpath>\n" + 553 " </java>\n" + 554 " </target>\n" + 555 "</project>\n"; 556 assertEquals(expectedXml, xmlToString(doc.getDocumentElement())); 557 } 558 559 public void testCreateDebugSingleTargetElem() throws Exception { 560 Document doc = XMLUtil.createDocument("project", null, null, null); 561 Lookup context = context(new FileObject[] {myAppJava}); 562 JavaActions.AntLocation root = ja.findPackageRoot(context); 563 Element targetElem = ja.createDebugSingleTargetElem(doc, "debug-single-test-target", "test.class", root); 564 doc.getDocumentElement().appendChild(targetElem); 565 String expectedXml = 566 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 567 "<project>\n" + 568 " <target name=\"debug-single-test-target\">\n" + 569 " <fail unless=\"test.class\">Must set property 'test.class'</fail>\n" + 570 " <ant antfile=\"build.xml\" inheritall=\"false\" target=\"jar\"/>\n" + 571 " <path id=\"cp\">\n" + 572 " <pathelement path=\"${src.cp}\"/>\n" + 573 " <pathelement location=\"${classes.dir}\"/>\n" + 574 " <pathelement location=\"${main.jar}\"/>\n" + 575 " </path>\n" + 576 " <nbjpdastart addressproperty=\"jpda.address\" name=\"Simple Freeform Project\" transport=\"dt_socket\">\n" + 577 " <classpath refid=\"cp\"/>\n" + 578 " </nbjpdastart>\n" + 579 " <java classname=\"${test.class}\" fork=\"true\">\n" + 580 " <classpath refid=\"cp\"/>\n" + 581 " <jvmarg value=\"-Xdebug\"/>\n" + 582 " <jvmarg value=\"-Xrunjdwp:transport=dt_socket,address=${jpda.address}\"/>\n" + 583 " </java>\n" + 584 " </target>\n" + 585 "</project>\n"; 586 assertEquals(expectedXml, xmlToString(doc.getDocumentElement())); 587 } 588 589 public void testCreatePathLikeElem() throws Exception { 590 Document doc = XMLUtil.createDocument("testdoc", null, null, null); 591 Element pathElem = ja.createPathLikeElem(doc, "path", "id", 592 new String [] {"lib/File.jar;lib/File2.jar", "testlib/Lib1.jar;testlib/Lib2"}, 593 new String [] {"c:\\workfiles\\library.jar", "/workfiles/library2.jar"}, 594 "refid", "comment"); 595 String expectedXml = 596 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 597 "<path id=\"id\" refid=\"refid\">\n" + 598 " <!---->\n" + 599 " <pathelement path=\"lib/File.jar;lib/File2.jar\"/>\n" + 600 " <pathelement path=\"testlib/Lib1.jar;testlib/Lib2\"/>\n" + 601 " <pathelement location=\"c:\\workfiles\\library.jar\"/>\n" + 602 " <pathelement location=\"/workfiles/library2.jar\"/>\n" + 603 "</path>\n"; 604 assertEquals(expectedXml, xmlToString(pathElem)); 605 pathElem = ja.createPathLikeElem(doc, "classpath", null, null, null, null, "comment"); 606 expectedXml = 607 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 608 "<classpath>\n" + 609 " <!---->\n" + 610 "</classpath>\n"; 611 assertEquals(expectedXml, xmlToString(pathElem)); 612 } 613 614 public void testCreateAntElem() throws Exception { 615 Document doc = XMLUtil.createDocument("testdoc", null, null, null); 616 Element antElem = ja.createAntElem(doc, "antscript.xml", "test.target"); 617 String expectedXml = 618 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 619 "<ant antfile=\"antscript.xml\" inheritall=\"false\" target=\"test.target\"/>\n"; 620 assertEquals(expectedXml, xmlToString(antElem)); 621 } 622 623 public void testGetPathFromCU() throws Exception { 624 Document doc = XMLUtil.createDocument("testdoc", null, null, null); 625 Lookup context = context(new FileObject[] {myAppJava}); 626 JavaActions.AntLocation root = ja.findPackageRoot(context); 627 Element cpElem = ja.getPathFromCU(doc, root.virtual, "classpath"); 628 String expectedXml = 629 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 630 "<classpath>\n" + 631 " <pathelement path=\"${src.cp}\"/>\n" + 632 " <pathelement location=\"${classes.dir}\"/>\n" + 633 " <pathelement location=\"${main.jar}\"/>\n" + 634 "</classpath>\n"; 635 assertEquals(expectedXml, xmlToString(cpElem)); 636 } 637 638 public void testGetRunDepends() throws Exception { 639 String s[] = ja.getRunDepends(); 640 assertEquals("build.xml", s[0]); 641 assertEquals("jar", s[1]); 642 } 643 644 648 private static String xmlToString(Element el) throws Exception { 649 Document doc = XMLUtil.createDocument("fake", null, null, null); 650 doc.removeChild(doc.getDocumentElement()); 651 doc.appendChild(doc.importNode(el, true)); 652 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 653 XMLUtil.write(doc, baos, "UTF-8"); 654 return baos.toString("UTF-8").replaceAll("<!--([^-]|-[^-])*-->", "<!---->").replaceAll(System.getProperty("line.separator"), "\n"); 655 } 656 657 } 658 | Popular Tags |