KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformProjectGeneratorTest


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.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.jar.JarEntry JavaDoc;
30 import java.util.jar.JarOutputStream JavaDoc;
31 import org.netbeans.api.project.Project;
32 import org.netbeans.api.project.ProjectInformation;
33 import org.netbeans.api.project.ProjectManager;
34 import org.netbeans.api.project.ProjectUtils;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.ant.freeform.spi.support.Util;
37 import org.netbeans.spi.project.ActionProvider;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.support.ant.EditableProperties;
40 import org.openide.filesystems.FileObject;
41 import org.openide.filesystems.FileUtil;
42 import org.openide.util.Lookup;
43 import org.w3c.dom.Element JavaDoc;
44 import javax.xml.parsers.SAXParser JavaDoc;
45 import javax.xml.parsers.SAXParserFactory JavaDoc;
46 import org.netbeans.modules.project.ant.AntBasedProjectFactorySingleton;
47 import org.xml.sax.SAXException JavaDoc;
48 import org.xml.sax.SAXParseException JavaDoc;
49 import org.xml.sax.helpers.DefaultHandler JavaDoc;
50
51 /**
52  * Tests for FreeformProjectGenerator.
53  *
54  * @author David Konecny
55  */

56 public class FreeformProjectGeneratorTest extends NbTestCase {
57
58     private File JavaDoc lib1;
59     private File JavaDoc lib2;
60     private File JavaDoc src;
61     private File JavaDoc test;
62     
63     public FreeformProjectGeneratorTest(java.lang.String JavaDoc testName) {
64         super(testName);
65     }
66     
67     protected void setUp() throws Exception JavaDoc {
68         clearWorkDir();
69     }
70     
71     public AntProjectHelper createEmptyProject(String JavaDoc projectFolder, String JavaDoc projectName) throws Exception JavaDoc {
72         File JavaDoc base = new File JavaDoc(getWorkDir(), projectFolder);
73         base.mkdir();
74         File JavaDoc antScript = new File JavaDoc(base, "build.xml");
75         antScript.createNewFile();
76         src = new File JavaDoc(base, "src");
77         src.mkdir();
78         test = new File JavaDoc(base, "test");
79         test.mkdir();
80         File JavaDoc libs = new File JavaDoc(base, "libs");
81         libs.mkdir();
82         lib1 = new File JavaDoc(libs, "some.jar");
83         createRealJarFile(lib1);
84         lib2 = new File JavaDoc(libs, "some2.jar");
85         createRealJarFile(lib2);
86         
87 // XXX: might need to call refresh here??
88
// FileObject fo = FileUtil.toFileObject(getWorkDir());
89
// fo.refresh();
90

91         AntProjectHelper helper = FreeformProjectGenerator.createProject(base, base, projectName, null);
92         return helper;
93     }
94     
95     public void testCreateProject() throws Exception JavaDoc {
96         AntProjectHelper helper = createEmptyProject("proj1", "proj-1");
97         FileObject base = helper.getProjectDirectory();
98         Project p = ProjectManager.getDefault().findProject(base);
99         assertNotNull("Project was not created", p);
100         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
101         
102         ProjectInformation pi = ProjectUtils.getInformation(p);
103         assertEquals("Project name was not set", "proj-1", pi.getName());
104     }
105     
106     public void testTargetMappings() throws Exception JavaDoc {
107         AntProjectHelper helper = createEmptyProject("proj2", "proj-2");
108         FileObject base = helper.getProjectDirectory();
109         Project p = ProjectManager.getDefault().findProject(base);
110         assertNotNull("Project was not created", p);
111         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
112         
113         ActionProvider ap = p.getLookup().lookup(ActionProvider.class);
114         assertNotNull("Project does not have ActionProvider", ap);
115         assertEquals("Project cannot have any action", 0, ap.getSupportedActions().length);
116         
117         List JavaDoc<FreeformProjectGenerator.TargetMapping> list = FreeformProjectGenerator.getTargetMappings(helper);
118         assertNotNull("getTargetMappings() cannot return null", list);
119         assertEquals("Project cannot have any action", 0, list.size());
120         
121         list = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
122         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
123         tm.name = "clean";
124         tm.targets = Collections.singletonList("clean-target");
125         list.add(tm);
126         tm = new FreeformProjectGenerator.TargetMapping();
127         tm.name = "build";
128         tm.targets = Collections.singletonList("build-target");
129         tm.script = "${ant.script.two}";
130         list.add(tm);
131         tm = new FreeformProjectGenerator.TargetMapping();
132         tm.name = "rebuild";
133         tm.targets = Arrays.asList("clean-target", "build-target");
134         tm.script = "${ant.script.three}";
135         list.add(tm);
136         tm = new FreeformProjectGenerator.TargetMapping();
137         FreeformProjectGenerator.putTargetMappings(helper, list);
138         List JavaDoc<FreeformProjectGenerator.TargetMapping> list2 = FreeformProjectGenerator.getTargetMappings(helper);
139         // once again: put and get
140
FreeformProjectGenerator.putTargetMappings(helper, list2);
141         list2 = FreeformProjectGenerator.getTargetMappings(helper);
142         assertNotNull("getTargetMappings() cannot return null", list2);
143         assertEquals("Project must have 3 actions", 3, list2.size());
144         assertEquals("Script was not correctly saved", null, list2.get(0).script);
145         assertEquals("Script was not correctly saved", "${ant.script.two}", list2.get(1).script);
146         assertEquals("Script was not correctly saved", "${ant.script.three}", list2.get(2).script);
147         assertEquals("Project must have 3 actions plus 4 extras (run, javadoc, test, redeploy) plus 4 project operations (copy, rename, move, delete): " +
148                 Arrays.asList(ap.getSupportedActions()), 11, ap.getSupportedActions().length);
149         assertTrue("Action clean must be enabled", ap.isActionEnabled("clean", Lookup.EMPTY));
150         assertTrue("Action build must be enabled", ap.isActionEnabled("build", Lookup.EMPTY));
151         assertTrue("Action rebuild must be enabled", ap.isActionEnabled("rebuild", Lookup.EMPTY));
152         boolean ok = false;
153         try {
154             assertFalse("Action twiddle must be disabled", ap.isActionEnabled("twiddle", Lookup.EMPTY));
155         } catch (IllegalArgumentException JavaDoc ex) {
156             ok = true;
157         }
158         assertTrue("Exception must be thrown for non-existing actions", ok);
159         ProjectManager.getDefault().saveAllProjects();
160     }
161
162     public void testRawTargetMappings() throws Exception JavaDoc {
163         AntProjectHelper helper = createEmptyProject("proj", "proj");
164         FileObject base = helper.getProjectDirectory();
165         Project p = ProjectManager.getDefault().findProject(base);
166         assertNotNull("Project was not created", p);
167         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
168         
169         // check that all data are correctly persisted
170

171         List JavaDoc<FreeformProjectGenerator.TargetMapping> mappings = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
172         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
173         tm.name = "first-targetName";
174         tm.script = "antScript";
175         tm.targets = new ArrayList JavaDoc<String JavaDoc>();
176         tm.targets.add("target-1");
177         tm.targets.add("target-2");
178         tm.targets.add("target-3");
179         tm.properties = new EditableProperties(false);
180         tm.properties.setProperty("k1", "v1");
181         tm.properties.setProperty("k2", "v2");
182         FreeformProjectGenerator.TargetMapping.Context ctx = new FreeformProjectGenerator.TargetMapping.Context();
183         ctx.folder = "someFolder1";
184         ctx.format = "relative-path";
185         ctx.property = "someProperty1";
186         ctx.pattern = "somePattern1";
187         tm.context = ctx;
188         mappings.add(tm);
189         tm = new FreeformProjectGenerator.TargetMapping();
190         tm.name = "second-targetName";
191         tm.script = "second-antScript";
192         tm.targets = new ArrayList JavaDoc<String JavaDoc>();
193         tm.targets.add("second-target-1");
194         tm.targets.add("second-target-2");
195         tm.targets.add("second-target-3");
196         tm.properties = new EditableProperties(false);
197         tm.properties.setProperty("second-k1", "second-v1");
198         tm.properties.setProperty("second-k2", "second-v2");
199         ctx = new FreeformProjectGenerator.TargetMapping.Context();
200         ctx.folder = "second-someFolder1";
201         ctx.format = "java-name";
202         ctx.property = "second-someProperty1";
203         ctx.separator = "someSeparator1";
204         tm.context = ctx;
205         mappings.add(tm);
206         FreeformProjectGenerator.putTargetMappings(helper, mappings);
207         // test getter and setter here:
208
mappings = FreeformProjectGenerator.getTargetMappings(helper);
209         FreeformProjectGenerator.putTargetMappings(helper, mappings);
210 // ProjectManager.getDefault().saveAllProjects();
211
Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
212         el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL);
213         assertNotNull("Target mapping were not saved correctly", el);
214         List JavaDoc<Element JavaDoc> subElements = Util.findSubElements(el);
215         assertEquals(2, subElements.size());
216         // compare first target mapping
217
Element JavaDoc el2 = subElements.get(0);
218         assertElement(el2, "action", null, "name", "first-targetName");
219         List JavaDoc<Element JavaDoc> l1 = Util.findSubElements(el2);
220         assertEquals(7, l1.size());
221         assertElementArray(l1,
222             new String JavaDoc[]{"script", "target", "target", "target", "property", "property", "context"},
223             new String JavaDoc[]{"antScript", "target-1", "target-2", "target-3", "v1", "v2", null});
224         el2 = l1.get(4);
225         assertElement(el2, "property", "v1", "name", "k1");
226         el2 = l1.get(5);
227         assertElement(el2, "property", "v2", "name", "k2");
228         el2 = l1.get(6);
229         List JavaDoc<Element JavaDoc> l2 = Util.findSubElements(el2);
230         assertEquals(5, l2.size());
231         assertElementArray(l2,
232             new String JavaDoc[]{"property", "folder", "pattern", "format", "arity"},
233             new String JavaDoc[]{"someProperty1", "someFolder1", "somePattern1", "relative-path", null});
234         assertNotNull("have <one-file-only>", Util.findElement(l2.get(4), "one-file-only", FreeformProjectType.NS_GENERAL));
235         // compare second target mapping
236
el2 = subElements.get(1);
237         assertElement(el2, "action", null, "name", "second-targetName");
238         l1 = Util.findSubElements(el2);
239         assertEquals(7, l1.size());
240         assertElementArray(l1,
241             new String JavaDoc[]{"script", "target", "target", "target", "property", "property", "context"},
242             new String JavaDoc[]{"second-antScript", "second-target-1", "second-target-2", "second-target-3", "second-v1", "second-v2", null});
243         el2 = l1.get(4);
244         assertElement(el2, "property", "second-v1", "name", "second-k1");
245         el2 = l1.get(5);
246         assertElement(el2, "property", "second-v2", "name", "second-k2");
247         el2 = l1.get(6);
248         l2 = Util.findSubElements(el2);
249         assertEquals(4, l2.size());
250         assertElementArray(l2,
251             new String JavaDoc[]{"property", "folder", "format", "arity"},
252             new String JavaDoc[]{"second-someProperty1", "second-someFolder1", "java-name", null});
253         Element JavaDoc sepFilesEl = Util.findElement(l2.get(3), "separated-files", FreeformProjectType.NS_GENERAL);
254         assertNotNull("have <separated-files>", sepFilesEl);
255         assertEquals("right separator", "someSeparator1", Util.findText(sepFilesEl));
256         // validate against schema:
257
ProjectManager.getDefault().saveAllProjects();
258         validate(p);
259             
260         // test updating
261

262         mappings = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
263         tm = new FreeformProjectGenerator.TargetMapping();
264         tm.name = "foo";
265         tm.script = "antScript";
266         tm.targets = new ArrayList JavaDoc<String JavaDoc>();
267         tm.targets.add("target-1");
268         tm.targets.add("target-2");
269         mappings.add(tm);
270         tm.properties = new EditableProperties(false);
271         tm.properties.setProperty("key1", "value1");
272         tm.properties.setProperty("key2", "value2");
273         FreeformProjectGenerator.putTargetMappings(helper, mappings);
274 // ProjectManager.getDefault().saveAllProjects();
275
el = Util.getPrimaryConfigurationData(helper);
276         el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL);
277         assertNotNull("Target mapping were not saved correctly", el);
278         subElements = Util.findSubElements(el);
279         assertEquals(1, subElements.size());
280         // compare first target mapping
281
el2 = subElements.get(0);
282         assertElement(el2, "action", null, "name", "foo");
283         l1 = Util.findSubElements(el2);
284         assertEquals(5, l1.size());
285         assertElementArray(l1,
286             new String JavaDoc[]{"script", "target", "target", "property", "property"},
287             new String JavaDoc[]{"antScript", "target-1", "target-2", "value1", "value2"});
288         el2 = l1.get(3);
289         assertElement(el2, "property", "value1", "name", "key1");
290         el2 = l1.get(4);
291         assertElement(el2, "property", "value2", "name", "key2");
292         mappings = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
293         tm = new FreeformProjectGenerator.TargetMapping();
294         tm.name = "foo";
295         tm.script = "diff-script";
296         tm.targets = new ArrayList JavaDoc<String JavaDoc>();
297         tm.targets.add("target-1");
298         tm.targets.add("target-B");
299         tm.properties = new EditableProperties(false);
300         tm.properties.setProperty("key-1", "value-1");
301         tm.properties.setProperty("key-2", "value-2");
302         mappings.add(tm);
303         FreeformProjectGenerator.putTargetMappings(helper, mappings);
304 // ProjectManager.getDefault().saveAllProjects();
305
el = Util.getPrimaryConfigurationData(helper);
306         el = Util.findElement(el, "ide-actions", FreeformProjectType.NS_GENERAL);
307         assertNotNull("Target mapping were not saved correctly", el);
308         subElements = Util.findSubElements(el);
309         assertEquals(1, subElements.size());
310         // compare first target mapping
311
el2 = subElements.get(0);
312         assertElement(el2, "action", null, "name", "foo");
313         l1 = Util.findSubElements(el2);
314         assertEquals(5, l1.size());
315         assertElementArray(l1,
316             new String JavaDoc[]{"script", "target", "target", "property", "property"},
317             new String JavaDoc[]{"diff-script", "target-1", "target-B", "value-1", "value-2"});
318         el2 = l1.get(3);
319         assertElement(el2, "property", "value-1", "name", "key-1");
320         el2 = l1.get(4);
321         assertElement(el2, "property", "value-2", "name", "key-2");
322         // validate against schema:
323
ProjectManager.getDefault().saveAllProjects();
324         validate(p);
325     }
326
327     public void testRawContextMenuActions() throws Exception JavaDoc {
328         AntProjectHelper helper = createEmptyProject("proj", "proj");
329         FileObject base = helper.getProjectDirectory();
330         Project p = ProjectManager.getDefault().findProject(base);
331         assertNotNull("Project was not created", p);
332         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
333         
334         // check that all data are correctly persisted
335

336         List JavaDoc<FreeformProjectGenerator.TargetMapping> mappings = new ArrayList JavaDoc<FreeformProjectGenerator.TargetMapping>();
337         FreeformProjectGenerator.TargetMapping tm = new FreeformProjectGenerator.TargetMapping();
338         tm.name = "first-targetName";
339         mappings.add(tm);
340         tm = new FreeformProjectGenerator.TargetMapping();
341         tm.name = "second-targetName";
342         mappings.add(tm);
343         tm = new FreeformProjectGenerator.TargetMapping();
344         tm.name = "context-sensitive";
345         tm.context = new FreeformProjectGenerator.TargetMapping.Context();
346         mappings.add(tm);
347         FreeformProjectGenerator.putContextMenuAction(helper, mappings);
348 // ProjectManager.getDefault().saveAllProjects();
349
Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
350         el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL);
351         assertNotNull("Target mapping were not saved correctly", el);
352         el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
353         assertNotNull("Target mapping were not saved correctly", el);
354         List JavaDoc<Element JavaDoc> subElements = Util.findSubElements(el);
355         assertEquals(2, subElements.size());
356         assertElementArray(subElements,
357             new String JavaDoc[]{"ide-action", "ide-action"},
358             new String JavaDoc[]{null, null},
359             new String JavaDoc[]{"name", "name"},
360             new String JavaDoc[]{"first-targetName", "second-targetName"}
361             );
362         // validate against schema:
363
ProjectManager.getDefault().saveAllProjects();
364         validate(p);
365             
366         // test updating
367

368         tm = new FreeformProjectGenerator.TargetMapping();
369         tm.name = "foo";
370         mappings.add(tm);
371         tm = new FreeformProjectGenerator.TargetMapping();
372         tm.name = "bar";
373         mappings.add(tm);
374         FreeformProjectGenerator.putContextMenuAction(helper, mappings);
375 // ProjectManager.getDefault().saveAllProjects();
376
el = Util.getPrimaryConfigurationData(helper);
377         el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL);
378         assertNotNull("Target mapping were not saved correctly", el);
379         el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
380         assertNotNull("Target mapping were not saved correctly", el);
381         subElements = Util.findSubElements(el);
382         assertEquals(4, subElements.size());
383         assertElementArray(subElements,
384             new String JavaDoc[]{"ide-action", "ide-action", "ide-action", "ide-action"},
385             new String JavaDoc[]{null, null, null, null},
386             new String JavaDoc[]{"name", "name", "name", "name"},
387             new String JavaDoc[]{"first-targetName", "second-targetName", "foo", "bar"}
388             );
389         // validate against schema:
390
ProjectManager.getDefault().saveAllProjects();
391         validate(p);
392     }
393
394     public void testRawCustomContextMenuActions() throws Exception JavaDoc {
395         AntProjectHelper helper = createEmptyProject("proj", "proj");
396         FileObject base = helper.getProjectDirectory();
397         Project p = ProjectManager.getDefault().findProject(base);
398         assertNotNull("Project was not created", p);
399         assertEquals("Project folder is incorrect", base, p.getProjectDirectory());
400         
401         // check that all data are correctly persisted
402

403         List JavaDoc<FreeformProjectGenerator.CustomTarget> customActions = new ArrayList JavaDoc<FreeformProjectGenerator.CustomTarget>();
404         FreeformProjectGenerator.CustomTarget ct = new FreeformProjectGenerator.CustomTarget();
405         ct.label = "customAction1";
406         ct.script = "customScript1";
407         ct.targets = new ArrayList JavaDoc<String JavaDoc>();
408         ct.targets.add("customTarget1");
409         ct.targets.add("customTarget2");
410         ct.properties = new EditableProperties(false);
411         ct.properties.setProperty("k1", "v1");
412         ct.properties.setProperty("k2", "v2");
413         customActions.add(ct);
414         ct = new FreeformProjectGenerator.CustomTarget();
415         ct.label = "customAction2";
416         ct.script = "customScript2";
417         ct.targets = new ArrayList JavaDoc<String JavaDoc>();
418         ct.targets.add("second-customTarget1");
419         ct.targets.add("second-customTarget2");
420         ct.properties = new EditableProperties(false);
421         ct.properties.setProperty("kk1", "vv1");
422         ct.properties.setProperty("kk2", "vv2");
423         customActions.add(ct);
424         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
425         // test getter and setter here:
426
customActions = FreeformProjectGenerator.getCustomContextMenuActions(helper);
427         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
428 // ProjectManager.getDefault().saveAllProjects();
429
Element JavaDoc el = Util.getPrimaryConfigurationData(helper);
430         el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL);
431         assertNotNull("Target mapping were not saved correctly", el);
432         el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
433         assertNotNull("Target mapping were not saved correctly", el);
434         List JavaDoc<Element JavaDoc> subElements = Util.findSubElements(el);
435         assertEquals(2, subElements.size());
436         assertElementArray(subElements,
437             new String JavaDoc[]{"action", "action"},
438             new String JavaDoc[]{null, null});
439         // compare first custom action
440
Element JavaDoc el2 = subElements.get(0);
441         List JavaDoc<Element JavaDoc> l1 = Util.findSubElements(el2);
442         assertEquals(6, l1.size());
443         assertElementArray(l1,
444             new String JavaDoc[]{"script", "label", "target", "target", "property", "property"},
445             new String JavaDoc[]{"customScript1", "customAction1", "customTarget1", "customTarget2", "v1", "v2"});
446         el2 = l1.get(4);
447         assertElement(el2, "property", "v1", "name", "k1");
448         el2 = l1.get(5);
449         assertElement(el2, "property", "v2", "name", "k2");
450         // compare second custom action
451
el2 = subElements.get(1);
452         l1 = Util.findSubElements(el2);
453         assertEquals(6, l1.size());
454         assertElementArray(l1,
455             new String JavaDoc[]{"script", "label", "target", "target", "property", "property"},
456             new String JavaDoc[]{"customScript2", "customAction2", "second-customTarget1", "second-customTarget2", "vv1", "vv2"});
457         el2 = l1.get(4);
458         assertElement(el2, "property", "vv1", "name", "kk1");
459         el2 = l1.get(5);
460         assertElement(el2, "property", "vv2", "name", "kk2");
461         // validate against schema:
462
ProjectManager.getDefault().saveAllProjects();
463         validate(p);
464             
465         // test updating
466

467         customActions = new ArrayList JavaDoc<FreeformProjectGenerator.CustomTarget>();
468         ct = new FreeformProjectGenerator.CustomTarget();
469         ct.label = "fooLabel";
470         customActions.add(ct);
471         ct = new FreeformProjectGenerator.CustomTarget();
472         ct.label = "barLabel";
473         customActions.add(ct);
474         FreeformProjectGenerator.putCustomContextMenuActions(helper, customActions);
475 // ProjectManager.getDefault().saveAllProjects();
476
el = Util.getPrimaryConfigurationData(helper);
477         el = Util.findElement(el, "view", FreeformProjectType.NS_GENERAL);
478         assertNotNull("Target mapping were not saved correctly", el);
479         el = Util.findElement(el, "context-menu", FreeformProjectType.NS_GENERAL);
480         assertNotNull("Target mapping were not saved correctly", el);
481         subElements = Util.findSubElements(el);
482         assertEquals(2, subElements.size());
483         assertElementArray(subElements,
484             new String JavaDoc[]{"action", "action"},
485             new String JavaDoc[]{null, null});
486         // compare first custom action
487
el2 = subElements.get(0);
488         l1 = Util.findSubElements(el2);
489         assertEquals(1, l1.size());
490         assertElementArray(l1,
491             new String JavaDoc[]{"label"},
492             new String JavaDoc[]{"fooLabel"});
493         // compare second custom action
494
el2 = subElements.get(1);
495         l1 = Util.findSubElements(el2);
496         assertEquals(1, l1.size());
497         assertElementArray(l1,
498             new String JavaDoc[]{"label"},
499             new String JavaDoc[]{"barLabel"});
500         // validate against schema:
501
ProjectManager.getDefault().saveAllProjects();
502         validate(p);
503     }
504
505     /**
506      * Asserts that given Element has expected name and its text match expected value.
507      * @param element element to test
508      * @param expectedName expected name of element; cannot be null
509      * @param expectedValue can be null in which case value is not tested
510      */

511     public static void assertElement(Element JavaDoc element, String JavaDoc expectedName, String JavaDoc expectedValue) {
512         String JavaDoc message = "Element "+element+" does not match [name="+expectedName+",value="+expectedValue+"]"; // NOI18N
513
assertEquals(message, expectedName, element.getLocalName());
514         if (expectedValue != null) {
515             assertEquals(message, expectedValue, Util.findText(element));
516         }
517     }
518
519     /**
520      * See {@link #assertElement(Element, String, String)} for more details. This
521      * method does exactly the same just on the list of elements and expected names.
522      */

523     public static void assertElementArray(List JavaDoc<Element JavaDoc> elements, String JavaDoc[] expectedNames, String JavaDoc[] expectedValues) {
524         for (int i=0; i<elements.size(); i++) {
525             assertElement(elements.get(i), expectedNames[i], expectedValues[i]);
526         }
527     }
528     
529     /**
530      * Asserts that given Element has expected name and its text match expected value and
531      * it also has expect attribute with expected value.
532      * @param element element to test
533      * @param expectedName expected name of element; cannot be null
534      * @param expectedValue can be null in which case value is not tested
535      * @param expectedAttrName expected name of attribute; cannot be null
536      * @param expectedAttrValue expected value of attribute; cannot be null
537      */

538     public static void assertElement(Element JavaDoc element, String JavaDoc expectedName, String JavaDoc expectedValue, String JavaDoc expectedAttrName, String JavaDoc expectedAttrValue) {
539         String JavaDoc message = "Element "+element+" does not match [name="+expectedName+",value="+
540             expectedValue+", attr="+expectedAttrName+", attrvalue="+expectedAttrValue+"]"; // NOI18N
541
assertEquals(message, expectedName, element.getLocalName());
542         if (expectedValue != null) {
543             assertEquals(message, expectedValue, Util.findText(element));
544         }
545         String JavaDoc val = element.getAttribute(expectedAttrName);
546         assertEquals(expectedAttrValue, val);
547     }
548     
549     /**
550      * See {@link #assertElement(Element, String, String)} for more details. This
551      * method does exactly the same just on the list of elements and expected names
552      * and expected attributes.
553      */

554     public static void assertElementArray(List JavaDoc<Element JavaDoc> elements, String JavaDoc[] expectedNames, String JavaDoc[] expectedValues, String JavaDoc[] expectedAttrName, String JavaDoc[] expectedAttrValue) {
555         assertEquals(expectedNames.length, elements.size());
556         for (int i=0; i<elements.size(); i++) {
557             assertElement(elements.get(i), expectedNames[i], expectedValues[i], expectedAttrName[i], expectedAttrValue[i]);
558         }
559     }
560
561
562     // create real Jar otherwise FileUtil.isArchiveFile returns false for it
563
public void createRealJarFile(File JavaDoc f) throws Exception JavaDoc {
564         OutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
565         try {
566             JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(os);
567 // jos.setMethod(ZipEntry.STORED);
568
JarEntry JavaDoc entry = new JarEntry JavaDoc("foo.txt");
569 // entry.setSize(0L);
570
// entry.setTime(System.currentTimeMillis());
571
// entry.setCrc(new CRC32().getValue());
572
jos.putNextEntry(entry);
573             jos.flush();
574             jos.close();
575         } finally {
576             os.close();
577         }
578     }
579
580     private static String JavaDoc[] getSchemas() throws Exception JavaDoc {
581         return new String JavaDoc[] {
582             FreeformProjectGenerator.class.getResource("resources/freeform-project-general.xsd").toExternalForm(),
583             FreeformProjectGenerator.class.getResource("resources/freeform-project-general-2.xsd").toExternalForm(),
584             AntBasedProjectFactorySingleton.class.getResource("project.xsd").toExternalForm(),
585         };
586     }
587     
588     public static void validate(Project proj) throws Exception JavaDoc {
589         File JavaDoc projF = FileUtil.toFile(proj.getProjectDirectory());
590         File JavaDoc xml = new File JavaDoc(new File JavaDoc(projF, "nbproject"), "project.xml");
591         SAXParserFactory JavaDoc f = (SAXParserFactory JavaDoc)Class.forName("org.apache.xerces.jaxp.SAXParserFactoryImpl").newInstance();
592         if (f == null) {
593             System.err.println("Validation skipped because org.apache.xerces.jaxp.SAXParserFactoryImpl was not found on classpath");
594             return;
595         }
596         f.setNamespaceAware(true);
597         f.setValidating(true);
598         SAXParser JavaDoc p = f.newSAXParser();
599         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
600             "http://www.w3.org/2001/XMLSchema");
601         p.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", getSchemas());
602         try {
603             p.parse(xml.toURI().toString(), new Handler JavaDoc());
604         } catch (SAXParseException JavaDoc e) {
605             assertTrue("Validation of XML document "+xml+" against schema failed. Details: "+
606             e.getSystemId() + ":" + e.getLineNumber() + ": " + e.getLocalizedMessage(), false);
607         }
608     }
609     
610     private static final class Handler extends DefaultHandler JavaDoc {
611         public void warning(SAXParseException JavaDoc e) throws SAXException JavaDoc {
612             throw e;
613         }
614         public void error(SAXParseException JavaDoc e) throws SAXException JavaDoc {
615             throw e;
616         }
617         public void fatalError(SAXParseException JavaDoc e) throws SAXException JavaDoc {
618             throw e;
619         }
620     }
621
622 }
623
Popular Tags