You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2007/12/06 17:45:42 UTC

svn commit: r601783 - in /geronimo/server/trunk: framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/ testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/ testsupport/testsupport-common/src/...

Author: gawor
Date: Thu Dec  6 08:45:41 2007
New Revision: 601783

URL: http://svn.apache.org/viewvc?rev=601783&view=rev
Log:
apache harmony fix: compare DOM tree instead of doing simple string comparision

Added:
    geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java   (with props)
    geronimo/server/trunk/testsupport/testsupport-common/src/test/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/
    geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java   (with props)
Modified:
    geronimo/server/trunk/framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/LocalAttributeManagerReadWriteTest.java

Modified: geronimo/server/trunk/framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/LocalAttributeManagerReadWriteTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/LocalAttributeManagerReadWriteTest.java?rev=601783&r1=601782&r2=601783&view=diff
==============================================================================
--- geronimo/server/trunk/framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/LocalAttributeManagerReadWriteTest.java (original)
+++ geronimo/server/trunk/framework/modules/geronimo-system/src/test/java/org/apache/geronimo/system/configuration/LocalAttributeManagerReadWriteTest.java Thu Dec  6 08:45:41 2007
@@ -26,6 +26,8 @@
 
 import junit.framework.TestCase;
 import org.apache.geronimo.system.configuration.condition.JexlExpressionParser;
+import org.apache.geronimo.testsupport.DOMUtils;
+import org.w3c.dom.Document;
 
 /**
  * @version $Rev$ $Date$
@@ -267,6 +269,10 @@
         StringWriter writer = new StringWriter();
         LocalAttributeManager.write(serverOverride, writer);
         String result = writer.toString();
-        assertEquals(CONFIG, result);
+        
+        Document expectedDoc = DOMUtils.load(CONFIG);
+        Document actualDoc = DOMUtils.load(result);
+        
+        DOMUtils.compareNodes(expectedDoc, actualDoc);
     }
 }

Added: geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java?rev=601783&view=auto
==============================================================================
--- geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java (added)
+++ geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java Thu Dec  6 08:45:41 2007
@@ -0,0 +1,123 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+
+package org.apache.geronimo.testsupport;
+
+import java.io.StringReader;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
+import org.xml.sax.InputSource;
+
+/**
+ * @version $Rev: 514087 $ $Date: 2007-03-03 01:13:40 -0500 (Sat, 03 Mar 2007) $
+ */
+public class DOMUtils {
+
+    public static Document load(String xml) throws Exception {
+        DocumentBuilder builder = getDocumentBuilder();         
+        Document document = builder.parse(new InputSource(new StringReader(xml)));
+        return document;
+    }
+
+    public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
+        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
+        builderFactory.setNamespaceAware(true);
+        DocumentBuilder builder = builderFactory.newDocumentBuilder();
+        return builder;
+    }
+    
+    public static void compareNodes(Node expected, Node actual) throws Exception {
+        if (expected.getNodeType() != actual.getNodeType()) {
+            throw new Exception("Different types of nodes: " + expected + " " + actual);
+        }
+        if (expected instanceof Document) {
+            Document expectedDoc = (Document)expected;
+            Document actualDoc = (Document)actual;
+            compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement());            
+        } else if (expected instanceof Element) {
+            Element expectedElement = (Element)expected;
+            Element actualElement = (Element)actual;
+            
+            // compare element names
+            if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) {
+                throw new Exception("Element names do match: " + expectedElement.getLocalName() + " " + actualElement.getLocalName());
+            }   
+            // compare element ns
+            String expectedNS = expectedElement.getNamespaceURI();
+            String actualNS = actualElement.getNamespaceURI();
+            if ((expectedNS != actualNS) || (expectedNS != null && !expectedNS.equals(actualNS))) {               
+                throw new Exception("Element namespaces names do match: " + expectedNS + " " + actualNS);
+            }
+            
+            String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName();
+            
+            // compare attributes
+            NamedNodeMap expectedAttrs = expectedElement.getAttributes();
+            NamedNodeMap actualAttrs = actualElement.getAttributes();
+            if (expectedAttrs.getLength() != actualAttrs.getLength()) {
+                throw new Exception(elementName + ": Number of attributes do not match up: " + expectedAttrs.getLength() + " " + actualAttrs.getLength());
+            }
+            for (int i = 0; i < expectedAttrs.getLength(); i++) {
+                Attr expectedAttr = (Attr)expectedAttrs.item(i);
+                Attr actualAttr = null;
+                if (expectedAttr.getNamespaceURI() == null) {
+                    actualAttr = (Attr)actualAttrs.getNamedItem(expectedAttr.getName());
+                } else {
+                    actualAttr = (Attr)actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(), expectedAttr.getLocalName());
+                }
+                if (actualAttr == null) {
+                    throw new Exception(elementName + ": No attribute found:" + expectedAttr);
+                }
+                if (!expectedAttr.getValue().equals(actualAttr.getValue())) {
+                    throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue() + " " + actualAttr.getValue());
+                }
+            }            
+            
+            // compare children
+            NodeList expectedChildren = expectedElement.getChildNodes();
+            NodeList actualChildren = actualElement.getChildNodes();
+            if (expectedChildren.getLength() != actualChildren.getLength()) {
+                throw new Exception(elementName + ": Number of children do not match up: " + expectedChildren.getLength() + " " + actualChildren.getLength());
+            }
+            for (int i = 0; i < expectedChildren.getLength(); i++) {
+                Node expectedChild = expectedChildren.item(i);
+                Node actualChild = actualChildren.item(i);
+                compareNodes(expectedChild, actualChild);
+            }
+        } else if (expected instanceof Text) {
+            String expectedData = ((Text)expected).getData();
+            String actualData = ((Text)actual).getData();
+            
+            if (!expectedData.equals(actualData)) {
+                throw new Exception("Text does not match: " + expectedData + " " + actualData);
+            }
+        }
+    }       
+
+}

Propchange: geronimo/server/trunk/testsupport/testsupport-common/src/main/java/org/apache/geronimo/testsupport/DOMUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java
URL: http://svn.apache.org/viewvc/geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java?rev=601783&view=auto
==============================================================================
--- geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java (added)
+++ geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java Thu Dec  6 08:45:41 2007
@@ -0,0 +1,85 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.geronimo.testsupport;
+
+import org.w3c.dom.Document;
+
+import junit.framework.TestCase;
+
+public class DOMUtilsTest extends TestCase {
+    
+    public void testSame() throws Exception {
+        String expected = "<foo x1=\"bar\">hello</foo>";
+        Document expectedDoc = DOMUtils.load(expected);        
+        DOMUtils.compareNodes(expectedDoc, expectedDoc);               
+    } 
+    
+    public void testElement() throws Exception {
+        String expected = "<foo>hello</foo>";
+        String actual = "<bar>hello</bar>";
+        compare(expected, actual);
+    }
+    
+    public void testElementNS() throws Exception {
+        String expected = "<foo>hello</foo>";
+        String actual = "<foo xmlns=\"foo\">hello</foo>";
+        compare(expected, actual);
+    }
+    
+    public void testElementChildren() throws Exception {
+        String expected = "<foo><x1/><x2/></foo>";
+        String actual = "<foo><x1/><x2/><x3/></foo>";
+        compare(expected, actual);
+    }
+    
+    public void testChildrenType() throws Exception {
+        String expected = "<foo><x1/></foo>";
+        String actual = "<foo>text</foo>";
+        compare(expected, actual);
+    }
+    
+    public void testText() throws Exception {
+        String expected = "<foo>hello</foo>";
+        String actual = "<bar>helloFoo</bar>";
+        compare(expected, actual);
+    }
+
+    public void testAttribute() throws Exception {
+        String expected = "<foo x1=\"bar\">hello</foo>";
+        String actual = "<foo x1=\"bar2\">hello</foo>";
+        compare(expected, actual);
+    }
+    
+    public void testAttributeNS() throws Exception {
+        String expected = "<foo xmlns:b=\"foo\" b:x1=\"bar\">hello</foo>";
+        String actual = "<foo xmlns:c=\"foo\" x1=\"bar2\">hello</foo>";
+        compare(expected, actual);
+    }
+    
+    private void compare(String x1, String x2) throws Exception {
+        Document d1 = DOMUtils.load(x1);
+        Document d2 = DOMUtils.load(x2);   
+        
+        try {
+            DOMUtils.compareNodes(d1, d2);
+            fail("Did not throw exception");
+        } catch (Exception e) {
+            e.printStackTrace(System.out);
+        }
+    }
+}

Propchange: geronimo/server/trunk/testsupport/testsupport-common/src/test/java/org/apache/geronimo/testsupport/DOMUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native