elena 2002/08/08 15:39:55 Modified: java/tests/dom/dom3 Test.java Log: Add testcases to verify namespace fixup algorithm. Revision Changes Path 1.2 +230 -13 xml-xerces/java/tests/dom/dom3/Test.java Index: Test.java =================================================================== RCS file: /home/cvs/xml-xerces/java/tests/dom/dom3/Test.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Test.java 10 Jul 2002 21:47:40 -0000 1.1 +++ Test.java 8 Aug 2002 22:39:55 -0000 1.2 @@ -60,6 +60,11 @@ import org.apache.xerces.dom.*; import org.w3c.dom.*; import org.w3c.dom.ls.*; + +import java.io.Reader; + +import java.io.StringReader; + import dom.util.Assertion; /** @@ -69,15 +74,18 @@ public static void main( String[] argv) { try { boolean namespaces = true; - System.out.println("Test DOM Level 3..."); + System.out.println("Running dom.dom3.Test..."); System.setProperty(DOMImplementationRegistry.PROPERTY,"org.apache.xerces.dom.DOMImplementationSourceImpl"); - DOMImplementationLS impl = (DOMImplementationLS)DOMImplementationRegistry.getDOMImplementation("LS-Load"); + + DOMImplementationLS impl = (DOMImplementationLS)DOMImplementationRegistry.newInstance().getDOMImplementation("LS-Load"); Assertion.assert(impl!=null, "domImplementation != null"); DOMBuilder builder = impl.createDOMBuilder(DOMImplementationLS.MODE_SYNCHRONOUS, null); + + DOMWriter writer = impl.createDOMWriter(); builder.setFeature("http://xml.org/sax/features/namespaces",namespaces); builder.setFeature("http://xml.org/sax/features/validation",false); @@ -150,7 +158,7 @@ //************************ //* Test normalizeDocument() //************************ - System.out.println("TEST #2: normalizeDocumention() (4 errors expected), input: tests/dom/dom3/schema.xml"); + System.out.println("TEST #2: normalizeDocumention() - 3 errors, input: tests/dom/dom3/schema.xml"); { builder.setFeature("validate", true); DocumentImpl core = (DocumentImpl)builder.parseURI("tests/dom/dom3/schema.xml"); @@ -177,31 +185,236 @@ newElem.appendChild(core.createTextNode("added new element")); root.insertBefore(newElem, testElem); + root.appendChild(core.createElementNS("UndefinedNamespace", "NS1:foo")); core.setErrorHandler(new Test()); core.setNormalizationFeature("validate", true); core.normalizeDocument(); core.setNormalizationFeature("validate", false); - root.appendChild(core.createElement("not:well:formed")); - core.normalizeDocument(); - core.setNormalizationFeature("comments", false); core.normalizeDocument(); + + + builder.setFeature("validate", false); + + + } + + + //************************ + //* Test normalizeDocument(): core tests + //************************ + System.out.println("TEST #3: normalizeDocument() core"); + { + + Document doc= new DocumentImpl(); + Element root = doc.createElementNS("http://www.w3.org/1999/XSL/Transform", "xsl:stylesheet"); + doc.appendChild(root); + root.setAttributeNS("http://attr1", "xsl:attr1",""); + + Element child1 = doc.createElementNS("http://child1", "NS2:child1"); + child1.setAttributeNS("http://attr2", "NS2:attr2",""); + root.appendChild(child1); + + Element child2 = doc.createElementNS("http://child2","NS4:child2"); + child2.setAttributeNS("http://attr3","attr3", ""); + root.appendChild(child2); + + Element child3 = doc.createElementNS("http://www.w3.org/1999/XSL/Transform","xsl:child3"); + child3.setAttributeNS("http://a1","attr1", ""); + child3.setAttributeNS("http://a2","xsl:attr2", ""); + child3.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:a1", "http://a1"); + child3.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsl", "http://a2"); + + Element child4 = doc.createElementNS(null, "child4"); + child4.setAttributeNS("http://a1", "xsl:attr1", ""); + child4.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "default"); + + child3.appendChild(child4); + root.appendChild(child3); + + doc.normalizeDocument(); + // + // make sure algorithm works correctly + // + + // xsl:stylesheet should include 2 namespace declarations + String name = root.getNodeName(); + Assertion.assert(name.equals("xsl:stylesheet"), "xsl:stylesheet"); + + String value = root.getAttributeNS("http://www.w3.org/2000/xmlns/", "xsl"); + Assertion.assert(value!=null, "xmlns:xsl != null"); + Assertion.assert(value.equals("http://www.w3.org/1999/XSL/Transform"), "xmlns:xsl="+value); + + value = root.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS1"); + + Assertion.assert(value!=null && + value.equals("http://attr1"), "xmlns:NS1="+value); + + // child includes 2 namespace decls + + Assertion.assert(child1.getNodeName().equals("NS2:child1"), "NS2:child1"); + value = child1.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS2"); + Assertion.assert(value!=null && + value.equals("http://child1"), "xmlns:NS2="+value); + + value = child1.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS3"); + Assertion.assert(value!=null && + value.equals("http://attr2"), "xmlns:NS3="+value); + + + // child3 + + + Assertion.assert(child3.getNodeName().equals("xsl:child3"), "xsl:child3"); + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS6"); + Assertion.assert(value!=null && + value.equals("http://a2"), "xmlns:NS6="+value); + + + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "a1"); + Assertion.assert(value!=null && + value.equals("http://a1"), "xmlns:a1="+value); + + + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "xsl"); + Assertion.assert(value!=null && + value.equals("http://www.w3.org/1999/XSL/Transform"), "xmlns:xsl="+value); + + + Attr attr = child3.getAttributeNodeNS("http://a2", "attr2"); + Assertion.assert(attr != null, "NS6:attr2 !=null"); + + Assertion.assert(child3.getAttributes().getLength() == 5, "xsl:child3 has 5 attrs"); } //************************ + //* Test normalizeDocument(): core tests + //************************ + System.out.println("TEST #4: namespace fixup during serialization"); + { + + Document doc= new DocumentImpl(); + Element root = doc.createElementNS("http://www.w3.org/1999/XSL/Transform", "xsl:stylesheet"); + doc.appendChild(root); + root.setAttributeNS("http://attr1", "xsl:attr1",""); + + Element child1 = doc.createElementNS("http://child1", "NS2:child1"); + child1.setAttributeNS("http://attr2", "NS2:attr2",""); + root.appendChild(child1); + + Element child2 = doc.createElementNS("http://child2","NS4:child2"); + child2.setAttributeNS("http://attr3","attr3", ""); + root.appendChild(child2); + + Element child3 = doc.createElementNS("http://www.w3.org/1999/XSL/Transform","xsl:child3"); + child3.setAttributeNS("http://a1","attr1", ""); + child3.setAttributeNS("http://a2","xsl:attr2", ""); + child3.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:a1", "http://a1"); + child3.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsl", "http://a2"); + + Element child4 = doc.createElementNS(null, "child4"); + child4.setAttributeNS("http://a1", "xsl:attr1", ""); + child4.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "default"); + + child3.appendChild(child4); + root.appendChild(child3); + + + // serialize data + String xmlData = writer.writeToString(doc); + Reader r = new StringReader(xmlData); + DOMInputSource in = impl.createDOMInputSource(); + in.setCharacterStream(r); + doc = builder.parse(in); + + // + // make sure algorithm works correctly + // + + root = doc.getDocumentElement(); + child1 = (Element)root.getFirstChild(); + child2 = (Element)child1.getNextSibling(); + child3 = (Element)child2.getNextSibling(); + + + // xsl:stylesheet should include 2 namespace declarations + String name = root.getNodeName(); + Assertion.assert(name.equals("xsl:stylesheet"), "xsl:stylesheet"); + + String value = root.getAttributeNS("http://www.w3.org/2000/xmlns/", "xsl"); + Assertion.assert(value!=null, "xmlns:xsl != null"); + Assertion.assert(value.equals("http://www.w3.org/1999/XSL/Transform"), "xmlns:xsl="+value); + + value = root.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS1"); + + Assertion.assert(value!=null && + value.equals("http://attr1"), "xmlns:NS1="+value); + + // child includes 2 namespace decls + + Assertion.assert(child1.getNodeName().equals("NS2:child1"), "NS2:child1"); + value = child1.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS2"); + Assertion.assert(value!=null && + value.equals("http://child1"), "xmlns:NS2="+value); + + value = child1.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS3"); + Assertion.assert(value!=null && + value.equals("http://attr2"), "xmlns:NS3="+value); + + + // child3 + + + Assertion.assert(child3.getNodeName().equals("xsl:child3"), "xsl:child3"); + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "NS6"); + Assertion.assert(value!=null && + value.equals("http://a2"), "xmlns:NS6="+value); + + + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "a1"); + Assertion.assert(value!=null && + value.equals("http://a1"), "xmlns:a1="+value); + + + value = child3.getAttributeNS("http://www.w3.org/2000/xmlns/", "xsl"); + Assertion.assert(value!=null && + value.equals("http://www.w3.org/1999/XSL/Transform"), "xmlns:xsl="+value); + + + Attr attr = child3.getAttributeNodeNS("http://a2", "attr2"); + Assertion.assert(attr != null, "NS6:attr2 !=null"); + + Assertion.assert(child3.getAttributes().getLength() == 5, "xsl:child3 has 5 attrs"); + + + + //OutputFormat format = new OutputFormat((Document)doc); + //format.setLineSeparator(LineSeparator.Windows); + //format.setIndenting(true); + //format.setLineWidth(0); + //format.setPreserveSpace(true); + + //XMLSerializer serializer = new XMLSerializer(System.out, format); + //serializer.serialize(doc); + + } + + + //************************ // TEST: replaceWholeText() // getWholeText() // //************************ - System.out.println("TEST #3: wholeText, input: tests/dom/dom3/wholeText.xml"); + System.out.println("TEST #4: wholeText, input: tests/dom/dom3/wholeText.xml"); { builder.setFeature("validate", false); + builder.setFeature("entities", true); DocumentImpl doc = (DocumentImpl)builder.parseURI("tests/dom/dom3/wholeText.xml"); Element root = doc.getDocumentElement(); @@ -210,19 +423,24 @@ test.appendChild(doc.createTextNode("Address: ")); test.appendChild(doc.createEntityReference("ent2")); test.appendChild(doc.createTextNode("City: ")); + test.appendChild(doc.createEntityReference("ent1")); + DocumentType doctype = doc.getDoctype(); + Node entity = doctype.getEntities().getNamedItem("ent3"); + NodeList ls = test.getChildNodes(); Assertion.assert(ls.getLength()==5, "List length"); String compare1 = "Home Address: 1900 Dallas Road (East) City: Dallas. California. USA PO #5668"; - Assertion.assert(((Text)ls.item(0)).getWholeText().equals(compare1), "Compare1"); + Assertion.assert(((TextImpl)ls.item(0)).getWholeText().equals(compare1), "Compare1"); String compare2 = "Address: 1900 Dallas Road (East) City: Dallas. California. USA PO #5668"; - Assertion.assert(((Text)ls.item(1)).getWholeText().equals(compare2), "Compare2"); + Assertion.assert(((TextImpl)ls.item(1)).getWholeText().equals(compare2), "Compare2"); + //TEST replaceWholeText() ((NodeImpl)ls.item(0)).setReadOnly(true, true); - Text original = (Text)ls.item(0); + TextImpl original = (TextImpl)ls.item(0); Node newNode = original.replaceWholeText("Replace with this text"); ls = test.getChildNodes(); Assertion.assert(ls.getLength() == 1, "Length == 1"); @@ -243,9 +461,8 @@ Assertion.assert(e !=null); } String compare3 = "Test: The Content ends here. "; - Assertion.assert(((Text)test.getFirstChild()).getWholeText().equals(compare3), "Compare3"); - - + //Assertion.assert(((Text)test.getFirstChild()).getWholeText().equals(compare3), "Compare3"); + } } catch ( Exception ex ) {
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]