KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > JavaActionsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.freeform;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
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 JavaDoc;
43 import org.w3c.dom.Element JavaDoc;
44
45 /**
46  * Test how well JavaActions is working to generate targets.
47  * @author Jesse Glick
48  */

49 public class JavaActionsTest extends TestBase {
50     
51     static {
52         // Simplify testing of stuff that can include localized messages.
53
Locale.setDefault(Locale.US);
54     }
55     
56     public JavaActionsTest(String JavaDoc 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 JavaDoc {
65         super.setUp();
66         prj = copyProject(simple);
67         // Remove existing context-sensitive bindings to make a clean slate.
68
Element JavaDoc data = prj.getPrimaryConfigurationData();
69         Element JavaDoc ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE);
70         assertNotNull(ideActions);
71         Iterator JavaDoc<Element JavaDoc> actionsIt = Util.findSubElements(ideActions).iterator();
72         while (actionsIt.hasNext()) {
73             Element JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
141         assertEquals("initially all context-sensitive actions supported",
142             Arrays.asList(new String JavaDoc[] {
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         /* Not really necessary; once there is a binding, the main ant/freeform Actions will mask this anyway:
150         ja.addBinding(ActionProvider.COMMAND_COMPILE_SINGLE, "target", "prop", "${dir}", null, "relative-path", null);
151         assertEquals("binding a context-sensitive action makes it not be supported any longer",
152             Collections.EMPTY_LIST,
153             Arrays.asList(ja.getSupportedActions()));
154          */

155     }
156     
157     public void testIsActionEnabled() throws Exception JavaDoc {
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; // exercise lookup of both FO and DO
163
private Lookup context(FileObject[] files) throws Exception JavaDoc {
164         Object JavaDoc[] objs = new Object JavaDoc[files.length];
165         for (int i = 0; i < files.length; i++) {
166             objs[i] = useDO ? (Object JavaDoc) DataObject.find(files[i]) : files[i];
167             useDO = !useDO;
168         }
169         return Lookups.fixed(objs);
170     }
171     
172     public void testFindClassesOutputDir() throws Exception JavaDoc {
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 JavaDoc {
179         ja.addBinding("some.action", "special.xml", "special-target", "selection", "${some.src.dir}", "\\.java$", "relative-path", ",");
180         Element JavaDoc data = prj.getPrimaryConfigurationData();
181         assertNotNull(data);
182         Element JavaDoc ideActions = Util.findElement(data, "ide-actions", Util.NAMESPACE);
183         assertNotNull(ideActions);
184         List JavaDoc<Element JavaDoc> actions = Util.findSubElements(ideActions);
185         Element JavaDoc lastAction = actions.get(actions.size() - 1);
186         String JavaDoc 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         // Non-context-sensitive bindings have no <context> but need to add a view item.
223
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 JavaDoc view = Util.findElement(data, "view", Util.NAMESPACE);
236         assertNotNull(view);
237         Element JavaDoc contextMenu = Util.findElement(view, "context-menu", Util.NAMESPACE);
238         assertNotNull(contextMenu);
239         // Currently (no FPG to help) it is always added as the last item.
240
List JavaDoc<Element JavaDoc> contextMenuActions = Util.findSubElements(contextMenu);
241         Element JavaDoc 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         //test #58442:
248
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 JavaDoc {
275         Document JavaDoc doc = XMLUtil.createDocument("fake", null, null, null);
276         Lookup context = context(new FileObject[] {someFileJava});
277         Element JavaDoc target = ja.createCompileSingleTarget(doc, context, "files", new JavaActions.AntLocation("${src.dir}", src));
278         String JavaDoc 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 JavaDoc {
291         Document JavaDoc script = ja.readCustomScript(JavaActions.FILE_SCRIPT_PATH);
292         String JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
320         Document JavaDoc script = ja.readCustomScript("special.xml");
321         Element JavaDoc 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         //0 <?xml?>
334
//1 <project>
335
//2 <target name="targ1">
336
//3 <task1/>
337
//4 <task2/>
338
//5 </>
339
//6 <target name="targ2">
340
//7 <task3/>
341
//8 </>
342
//9 </>
343
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         // Try w/ project.xml which uses namespaces, too.
347
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 JavaDoc {
354         String JavaDoc[] binding = ja.findCommandBinding(ActionProvider.COMMAND_RUN);
355         assertNotNull(binding);
356         assertEquals(Arrays.asList(new String JavaDoc[] {"build.xml", "start"}), Arrays.asList(binding));
357         binding = ja.findCommandBinding(ActionProvider.COMMAND_REBUILD);
358         assertNotNull(binding);
359         assertEquals(Arrays.asList(new String JavaDoc[] {"build.xml", "clean", "jar"}), Arrays.asList(binding));
360         binding = ja.findCommandBinding("bogus");
361         assertNull(binding);
362     }
363     
364     public void testFindExistingBuildTarget() throws Exception JavaDoc {
365         Element JavaDoc 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 JavaDoc {
371         Element JavaDoc runTarget = ja.findExistingBuildTarget(ActionProvider.COMMAND_RUN);
372         Element JavaDoc 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 JavaDoc 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 JavaDoc {
383         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
384         Element JavaDoc root = doc.getDocumentElement();
385         ja.ensurePropertiesCopied(root);
386         String JavaDoc 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 JavaDoc {
398         // Start with the simple case:
399
Element JavaDoc root = XMLUtil.createDocument("project", null, null, null).getDocumentElement();
400         ja.ensureImports(root, "build.xml");
401         String JavaDoc 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         // Test strange locations too. Make a script somewhere different.
410
File JavaDoc testdir = getWorkDir();
411         File JavaDoc subtestdir = new File JavaDoc(testdir, "sub");
412         subtestdir.mkdir();
413         File JavaDoc script = new File JavaDoc(subtestdir, "external.xml");
414         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
415         doc.getDocumentElement().setAttribute("basedir", "..");
416         OutputStream JavaDoc os = new FileOutputStream JavaDoc(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 JavaDoc 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         // And also with locations defined as special properties in various ways...
432
Element JavaDoc data = prj.getPrimaryConfigurationData();
433         Element JavaDoc properties = Util.findElement(data, "properties", Util.NAMESPACE);
434         assertNotNull(properties);
435         Element JavaDoc 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); // ease of debugging
449
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         // XXX try also <import file="somewhere-relative/build.xml"/>
474
}
475     
476     public void testCreateDebugTargetFromTemplate() throws Exception JavaDoc {
477         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
478         Document JavaDoc origDoc = XMLUtil.createDocument("target", null, null, null);
479         Element JavaDoc origTarget = origDoc.getDocumentElement();
480         origTarget.setAttribute("name", "ignored");
481         origTarget.setAttribute("depends", "compile");
482         origTarget.appendChild(origDoc.createElement("task1"));
483         Element JavaDoc task = origDoc.createElement("java");
484         // XXX also test nested <classpath>:
485
task.setAttribute("classpath", "${cp}");
486         task.appendChild(origDoc.createElement("stuff"));
487         origTarget.appendChild(task);
488         origTarget.appendChild(origDoc.createElement("task2"));
489         Element JavaDoc genTarget = ja.createDebugTargetFromTemplate("debug", origTarget, task, doc);
490         doc.getDocumentElement().appendChild(genTarget);
491         String JavaDoc 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 JavaDoc {
511         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
512         Element JavaDoc genTarget = ja.createDebugTargetFromScratch("debug", doc);
513         doc.getDocumentElement().appendChild(genTarget);
514         String JavaDoc 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 JavaDoc {
536         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
537         Lookup context = context(new FileObject[] {myAppJava});
538         JavaActions.AntLocation root = ja.findPackageRoot(context);
539         Element JavaDoc targetElem = ja.createRunSingleTargetElem(doc, "run-single-test-target", "test.class", root);
540         doc.getDocumentElement().appendChild(targetElem);
541         String JavaDoc 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 JavaDoc {
560         Document JavaDoc doc = XMLUtil.createDocument("project", null, null, null);
561         Lookup context = context(new FileObject[] {myAppJava});
562         JavaActions.AntLocation root = ja.findPackageRoot(context);
563         Element JavaDoc targetElem = ja.createDebugSingleTargetElem(doc, "debug-single-test-target", "test.class", root);
564         doc.getDocumentElement().appendChild(targetElem);
565         String JavaDoc 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 JavaDoc {
590         Document JavaDoc doc = XMLUtil.createDocument("testdoc", null, null, null);
591         Element JavaDoc pathElem = ja.createPathLikeElem(doc, "path", "id",
592                 new String JavaDoc[] {"lib/File.jar;lib/File2.jar", "testlib/Lib1.jar;testlib/Lib2"},
593                 new String JavaDoc[] {"c:\\workfiles\\library.jar", "/workfiles/library2.jar"},
594                 "refid", "comment");
595         String JavaDoc 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 JavaDoc {
615         Document JavaDoc doc = XMLUtil.createDocument("testdoc", null, null, null);
616         Element JavaDoc antElem = ja.createAntElem(doc, "antscript.xml", "test.target");
617         String JavaDoc 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 JavaDoc {
624         Document JavaDoc doc = XMLUtil.createDocument("testdoc", null, null, null);
625         Lookup context = context(new FileObject[] {myAppJava});
626         JavaActions.AntLocation root = ja.findPackageRoot(context);
627         Element JavaDoc cpElem = ja.getPathFromCU(doc, root.virtual, "classpath");
628         String JavaDoc 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 JavaDoc {
639         String JavaDoc s[] = ja.getRunDepends();
640         assertEquals("build.xml", s[0]);
641         assertEquals("jar", s[1]);
642     }
643
644     /**
645      * Format XML as a string. Assumes Xerces serializer in current impl.
646      * Collapse all comments to no body.
647      */

648     private static String JavaDoc xmlToString(Element JavaDoc el) throws Exception JavaDoc {
649         Document JavaDoc doc = XMLUtil.createDocument("fake", null, null, null);
650         doc.removeChild(doc.getDocumentElement());
651         doc.appendChild(doc.importNode(el, true));
652         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
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