1 19 20 package org.netbeans.modules.ant.grammar; 21 22 import java.io.File ; 23 import java.util.HashSet ; 24 import java.util.Set ; 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 ; 29 import org.w3c.dom.Element ; 30 import org.w3c.dom.Node ; 31 32 36 public class TestUtilTest extends NbTestCase { 37 38 public TestUtilTest(String name) { 39 super(name); 40 } 41 42 public void testCreateCompletion() throws Exception { 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 ); 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 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 owner = ((Attr )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 )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 name, String value, String 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 { 81 Element e = TestUtil.createElementInDocument("<foo><bar/></foo>", "bar", null); 82 assertNotNull("got it", e); 83 assertEquals("right one", "bar", e.getTagName()); 84 Node 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 { 94 Set s = new HashSet (); 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 { 102 File 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 (new File (antHome, "lib"), "ant.jar").isFile()); 106 File 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 |