001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.ant.grammar;
043:
044: import java.io.File;
045: import java.util.HashSet;
046: import java.util.Set;
047: import org.netbeans.junit.NbTestCase;
048: import org.netbeans.modules.xml.api.model.HintContext;
049: import org.openide.modules.InstalledFileLocator;
050: import org.w3c.dom.Attr;
051: import org.w3c.dom.Element;
052: import org.w3c.dom.Node;
053:
054: /**
055: * Test functionality of TestUtil.
056: * @author Jesse Glick
057: */
058: public class TestUtilTest extends NbTestCase {
059:
060: public TestUtilTest(String name) {
061: super (name);
062: }
063:
064: public void testCreateCompletion() throws Exception {
065: HintContext c = TestUtil.createCompletion("<fooHERE/>");
066: assertHintContext(c, Node.ELEMENT_NODE, "foo", null, "foo");
067: assertTrue("right type acc. to instanceof",
068: c instanceof Element);
069: c = TestUtil.createCompletion("<foo/>");
070: assertNull("no hint here", c);
071: c = TestUtil.createCompletion("<foo><barHERE/></foo>");
072: assertHintContext(c, Node.ELEMENT_NODE, "bar", null, "bar");
073: Node n = c.getParentNode();
074: assertEquals("parent is an element", Node.ELEMENT_NODE, n
075: .getNodeType());
076: assertEquals("parent is <foo>", "foo", n.getNodeName());
077: c = TestUtil
078: .createCompletion("<foo><bar attrHERE='whatever'/></foo>");
079: assertHintContext(c, Node.ATTRIBUTE_NODE, "attr", null, "attr");
080: Element owner = ((Attr) c).getOwnerElement();
081: assertEquals("parent is <bar>", "bar", owner.getNodeName());
082: c = TestUtil
083: .createCompletion("<foo><bar attr='somethingHERE'/></foo>");
084: assertHintContext(c, Node.ATTRIBUTE_NODE, null, "something",
085: "something");
086: owner = ((Attr) c).getOwnerElement();
087: assertEquals("parent is <bar>", "bar", owner.getNodeName());
088: c = TestUtil.createCompletion("<foo>somethingHERE</foo>");
089: assertHintContext(c, Node.TEXT_NODE, null, "something",
090: "something");
091: n = c.getParentNode();
092: assertEquals("parent is an element", Node.ELEMENT_NODE, n
093: .getNodeType());
094: assertEquals("parent is <foo>", "foo", n.getNodeName());
095: }
096:
097: private static void assertHintContext(HintContext c, int type,
098: String name, String value, String prefix) {
099: assertNotNull("found it", c);
100: assertEquals("right type", type, c.getNodeType());
101: if (name != null) {
102: assertEquals("right node name", name, c.getNodeName());
103: }
104: if (value != null) {
105: assertEquals("right node value", value, c.getNodeValue());
106: }
107: assertEquals("right prefix", prefix, c.getCurrentPrefix());
108: }
109:
110: public void testCreateElementInDocument() throws Exception {
111: Element e = TestUtil.createElementInDocument(
112: "<foo><bar/></foo>", "bar", null);
113: assertNotNull("got it", e);
114: assertEquals("right one", "bar", e.getTagName());
115: Node p = e.getParentNode();
116: assertEquals("parent is an element too", Node.ELEMENT_NODE, p
117: .getNodeType());
118: assertEquals("parent is right", "foo", p.getNodeName());
119: }
120:
121: private interface Foo {
122: }
123:
124: private interface Quux extends Foo {
125: }
126:
127: private static class Bar implements Quux {
128: }
129:
130: private static class Baz extends Bar {
131: }
132:
133: public void testFindAllInterfaces() throws Exception {
134: Set<Class> s = new HashSet<Class>();
135: TestUtil.findAllInterfaces(Baz.class, s);
136: assertEquals("two interfaces here", 2, s.size());
137: assertTrue("Foo included", s.contains(Foo.class));
138: assertTrue("Quux included", s.contains(Quux.class));
139: }
140:
141: public void testInstalledFileLocator() throws Exception {
142: File antHome = InstalledFileLocator.getDefault().locate("ant",
143: null, false);
144: assertNotNull("found antHome", antHome);
145: assertTrue("is a directory", antHome.isDirectory());
146: assertTrue("contains ant.jar", new File(
147: new File(antHome, "lib"), "ant.jar").isFile());
148: File antBridge = InstalledFileLocator.getDefault().locate(
149: "ant/nblib/bridge.jar", null, false);
150: assertNotNull("found antBridge", antBridge);
151: assertTrue("is a file", antBridge.isFile());
152: }
153:
154: }
|