KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > grammar > TestUtilTest


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.grammar;
21
22 import java.io.File JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import org.netbeans.junit.NbTestCase;
26 import org.netbeans.modules.xml.api.model.HintContext;
27 import org.openide.modules.InstalledFileLocator;
28 import org.w3c.dom.Attr JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31
32 /**
33  * Test functionality of TestUtil.
34  * @author Jesse Glick
35  */

36 public class TestUtilTest extends NbTestCase {
37
38     public TestUtilTest(String JavaDoc name) {
39         super(name);
40     }
41
42     public void testCreateCompletion() throws Exception JavaDoc {
43         HintContext c = TestUtil.createCompletion("<fooHERE/>");
44         assertHintContext(c, Node.ELEMENT_NODE, "foo", null, "foo");
45         assertTrue("right type acc. to instanceof", c instanceof Element JavaDoc);
46         c = TestUtil.createCompletion("<foo/>");
47         assertNull("no hint here", c);
48         c = TestUtil.createCompletion("<foo><barHERE/></foo>");
49         assertHintContext(c, Node.ELEMENT_NODE, "bar", null, "bar");
50         Node JavaDoc n = c.getParentNode();
51         assertEquals("parent is an element", Node.ELEMENT_NODE, n.getNodeType());
52         assertEquals("parent is <foo>", "foo", n.getNodeName());
53         c = TestUtil.createCompletion("<foo><bar attrHERE='whatever'/></foo>");
54         assertHintContext(c, Node.ATTRIBUTE_NODE, "attr", null, "attr");
55         Element JavaDoc owner = ((Attr JavaDoc)c).getOwnerElement();
56         assertEquals("parent is <bar>", "bar", owner.getNodeName());
57         c = TestUtil.createCompletion("<foo><bar attr='somethingHERE'/></foo>");
58         assertHintContext(c, Node.ATTRIBUTE_NODE, null, "something", "something");
59         owner = ((Attr JavaDoc)c).getOwnerElement();
60         assertEquals("parent is <bar>", "bar", owner.getNodeName());
61         c = TestUtil.createCompletion("<foo>somethingHERE</foo>");
62         assertHintContext(c, Node.TEXT_NODE, null, "something", "something");
63         n = c.getParentNode();
64         assertEquals("parent is an element", Node.ELEMENT_NODE, n.getNodeType());
65         assertEquals("parent is <foo>", "foo", n.getNodeName());
66     }
67     
68     private static void assertHintContext(HintContext c, int type, String JavaDoc name, String JavaDoc value, String JavaDoc prefix) {
69         assertNotNull("found it", c);
70         assertEquals("right type", type, c.getNodeType());
71         if (name != null) {
72             assertEquals("right node name", name, c.getNodeName());
73         }
74         if (value != null) {
75             assertEquals("right node value", value, c.getNodeValue());
76         }
77         assertEquals("right prefix", prefix, c.getCurrentPrefix());
78     }
79     
80     public void testCreateElementInDocument() throws Exception JavaDoc {
81         Element JavaDoc e = TestUtil.createElementInDocument("<foo><bar/></foo>", "bar", null);
82         assertNotNull("got it", e);
83         assertEquals("right one", "bar", e.getTagName());
84         Node JavaDoc p = e.getParentNode();
85         assertEquals("parent is an element too", Node.ELEMENT_NODE, p.getNodeType());
86         assertEquals("parent is right", "foo", p.getNodeName());
87     }
88     
89     private interface Foo {}
90     private interface Quux extends Foo {}
91     private static class Bar implements Quux {}
92     private static class Baz extends Bar {}
93     public void testFindAllInterfaces() throws Exception JavaDoc {
94         Set JavaDoc s = new HashSet JavaDoc();
95         TestUtil.findAllInterfaces(Baz.class, s);
96         assertEquals("two interfaces here", 2, s.size());
97         assertTrue("Foo included", s.contains(Foo.class));
98         assertTrue("Quux included", s.contains(Quux.class));
99     }
100     
101     public void testInstalledFileLocator() throws Exception JavaDoc {
102         File JavaDoc antHome = InstalledFileLocator.getDefault().locate("ant", null, false);
103         assertNotNull("found antHome", antHome);
104         assertTrue("is a directory", antHome.isDirectory());
105         assertTrue("contains ant.jar", new File JavaDoc(new File JavaDoc(antHome, "lib"), "ant.jar").isFile());
106         File JavaDoc antBridge = InstalledFileLocator.getDefault().locate("ant/nblib/bridge.jar", null, false);
107         assertNotNull("found antBridge", antBridge);
108         assertTrue("is a file", antBridge.isFile());
109     }
110     
111 }
112
Popular Tags