1 9 10 package test.dom4j; 11 12 import junit.framework.Test; 13 import junit.framework.TestSuite; 14 import junit.textui.TestRunner; 15 import org.dom4j.Document; 16 import org.dom4j.Element; 17 import org.dom4j.Node; 18 import org.ozoneDB.xml.dom4j.O3DocumentHelper; 19 20 25 public class TestMakeElement extends AbstractTestCase { 26 27 public static void main( String [] args ) { 28 TestRunner.run( suite() ); 29 } 30 31 public static Test suite() { 32 return new TestSuite( TestMakeElement.class ); 33 } 34 35 public TestMakeElement(String name) { 36 super(name); 37 } 38 39 public void testMakeElement() throws Exception { 42 Document doc = O3DocumentHelper.createDocument(); 43 44 Element c = O3DocumentHelper.makeElement( doc, "a/b/c" ); 45 assertTrue( "Should return a valid element", c != null ); 46 47 Element c2 = O3DocumentHelper.makeElement( doc, "a/b/c" ); 48 49 assertTrue( "Found same element again", c == c2 ); 50 51 c.addAttribute( "x", "123" ); 52 53 Node found = doc.selectSingleNode( "/a/b/c[@x='123']" ); 54 55 assertEquals( "Found same node via XPath", c, found ); 56 57 Element b = c.getParent(); 58 59 Element e = O3DocumentHelper.makeElement( b, "c/d/e" ); 60 61 assertTrue( "Should return a valid element", e != null ); 62 63 Element e2 = O3DocumentHelper.makeElement( b, "c/d/e" ); 64 65 assertTrue( "Found same element again", e == e2 ); 66 67 e.addAttribute( "y", "456" ); 68 69 found = b.selectSingleNode( "c/d/e[@y='456']" ); 70 71 assertEquals( "Found same node via XPath", e, found ); 72 } 73 74 public void testMakeQualifiedElement() throws Exception { 75 Document doc = O3DocumentHelper.createDocument(); 76 Element root = doc.addElement( "root" ); 77 root.addNamespace( "", "defaultURI" ); 78 root.addNamespace( "foo", "fooURI" ); 79 root.addNamespace( "bar", "barURI" ); 80 81 Element c = O3DocumentHelper.makeElement( doc, "root/foo:b/bar:c" ); 82 assertTrue( "Should return a valid element", c != null ); 83 84 assertEquals( "c has a valid namespace", "barURI", c.getNamespaceURI() ); 85 86 Element b = c.getParent(); 87 88 assertEquals( "b has a valid namespace", "fooURI", b.getNamespaceURI() ); 89 90 log( "Created: " + c ); 91 92 Element c2 = O3DocumentHelper.makeElement( doc, "root/foo:b/bar:c" ); 93 assertTrue( "Found same element again", c == c2 ); 94 } 95 96 } 97 98 99 100 101 145 | Popular Tags |