summaryrefslogtreecommitdiffstats
path: root/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany
diff options
context:
space:
mode:
Diffstat (limited to 'sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany')
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/AllTests.java49
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryGenTestCase.java71
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/CreateTestClasses.java46
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/GenPatternsTestCase.java115
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/InheritanceTestCase.java102
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTestCase.java86
-rw-r--r--sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java288
7 files changed, 757 insertions, 0 deletions
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/AllTests.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/AllTests.java
new file mode 100644
index 0000000000..3e41d1cde6
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/AllTests.java
@@ -0,0 +1,49 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class AllTests extends TestCase {
+
+
+ public static TestSuite suite() {
+
+ TestSuite suite = new TestSuite();
+
+ suite.addTestSuite(ChangeSummaryGenTestCase.class);
+ suite.addTestSuite(GenPatternsTestCase.class);
+ suite.addTestSuite(InheritanceTestCase.class);
+ suite.addTestSuite(SimpleStaticTestCase.class);
+
+ return suite;
+ }
+
+
+
+ /**
+ * Runs the test suite using the textual runner.
+ */
+ public static void main(String[] args) {
+ junit.textui.TestRunner.run(suite());
+ }
+}
+
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryGenTestCase.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryGenTestCase.java
new file mode 100644
index 0000000000..a2bce3c0a7
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryGenTestCase.java
@@ -0,0 +1,71 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed 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.tuscany.sdo.test;
+
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import com.example.simple.cs.CSFactory;
+import com.example.simple.cs.Quote;
+import com.example.simple.cs.QuoteBase;
+import commonj.sdo.ChangeSummary;
+import commonj.sdo.DataObject;
+
+
+public class ChangeSummaryGenTestCase extends TestCase {
+
+ public void testMixedQuoteType() throws IOException {
+ QuoteBase quote = CSFactory.INSTANCE.createQuoteBase();
+ DataObject dQuote = (DataObject)quote;
+
+ // ChangeSummary csp = quote.getChanges();
+ ChangeSummary cs = dQuote.getChangeSummary();
+ ChangeSummary csp = quote.getChanges();
+ cs.beginLogging();
+
+ assertSame(cs, csp);
+
+ quote.setSymbol("fbnt");
+ quote.setCompanyName("FlyByNightTechnology");
+ quote.setPrice(new BigDecimal("1000.0"));
+ quote.setOpen1(new BigDecimal("1000.0"));
+ quote.setHigh(new BigDecimal("1000.0"));
+ quote.setLow(new BigDecimal("1000.0"));
+ quote.setVolume(1000);
+ quote.setChange1(1000);
+
+ List quotes = quote.getQuotes();
+ Quote innerQuote = CSFactory.INSTANCE.createQuote();
+
+ quotes.add(innerQuote);
+ innerQuote.setPrice(new BigDecimal("2000.0"));
+
+ cs.endLogging();
+
+ //XMLHelper.INSTANCE.save(dQuote, "http://www.example.com/simpleCS", "quoteBase", System.out);
+
+ cs.undoChanges();
+
+ //System.out.println("\nAfter Undo Changes:");
+ //XMLHelper.INSTANCE.save(dQuote, "http://www.example.com/simpleCS", "quoteBase", System.out);
+ }
+
+}
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/CreateTestClasses.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/CreateTestClasses.java
new file mode 100644
index 0000000000..92fef99006
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/CreateTestClasses.java
@@ -0,0 +1,46 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+
+import org.apache.tuscany.sdo.generate.XSD2JavaGenerator;
+
+/**
+ * A utility class to regenerate the classes for the test cases
+ */
+public class CreateTestClasses {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+
+ CreateTestClasses ctc = new CreateTestClasses();
+ ctc.run();
+
+
+ }
+
+ private void run() {
+
+ XSD2JavaGenerator.generateFromXMLSchema(getClass().getResource("/simpleWithChangeSummary.xsd").getFile(), "http://www.example.com/simpleCS", "src/test/java/", "org.example.simple.cs", "CS", 0);
+
+ }
+
+}
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/GenPatternsTestCase.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/GenPatternsTestCase.java
new file mode 100644
index 0000000000..19fbc7b10e
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/GenPatternsTestCase.java
@@ -0,0 +1,115 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+
+import java.math.BigDecimal;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import com.example.simple.SimpleFactory;
+import commonj.sdo.helper.HelperContext;
+
+
+public class GenPatternsTestCase extends TestCase
+{
+ /**
+ * Simple Default Generation Pattern Static SDO 2.1 test.
+ */
+ public void testSimpleDefaultGeneratedClasses()
+ {
+ try
+ {
+ HelperContext scope = SDOUtil.createHelperContext();
+ com.example.simple.SimpleFactory.INSTANCE.register(scope);
+
+ com.example.simple.Quote quote =
+ (com.example.simple.Quote)scope.getDataFactory().create(com.example.simple.Quote.class);
+
+ quote.setSymbol("fbnt");
+ quote.setCompanyName("FlyByNightTechnology");
+ quote.setPrice(new BigDecimal("1000.0"));
+ quote.setOpen1(new BigDecimal("1000.0"));
+ quote.setHigh(new BigDecimal("1000.0"));
+ quote.setLow(new BigDecimal("1000.0"));
+ quote.setVolume(1000);
+ quote.setChange1(1000);
+
+ com.example.simple.Quote child =
+ (com.example.simple.Quote)scope.getDataFactory().create(com.example.simple.Quote.class);
+ quote.getQuotes().add(child);
+ child.setPrice(new BigDecimal("2000.0"));
+
+ //XMLHelper.INSTANCE.save((DataObject)quote, "http://www.example.com/simple", "stockQuote", System.out);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Simple Default Generation Pattern Static SDO 2.1 test.
+ */
+ public void testSimpleNoInterfaceGeneratedClasses()
+ {
+ try
+ {
+ HelperContext scope = SDOUtil.createHelperContext();
+ com.example.noInterfaces.simple.SimpleFactory.INSTANCE.register(scope);
+
+ com.example.noInterfaces.simple.Quote quote =
+ (com.example.noInterfaces.simple.Quote)scope.getDataFactory().create(com.example.noInterfaces.simple.Quote.class);
+
+ quote.setSymbol("fbnt");
+ quote.setCompanyName("FlyByNightTechnology");
+ quote.setPrice(new BigDecimal("1000.0"));
+ quote.setOpen1(new BigDecimal("1000.0"));
+ quote.setHigh(new BigDecimal("1000.0"));
+ quote.setLow(new BigDecimal("1000.0"));
+ quote.setVolume(1000);
+ quote.setChange1(1000);
+
+ com.example.noInterfaces.simple.Quote child =
+ (com.example.noInterfaces.simple.Quote)scope.getDataFactory().create(com.example.noInterfaces.simple.Quote.class);
+ quote.getQuotes().add(child);
+ child.setPrice(new BigDecimal("2000.0"));
+
+ //XMLHelper.INSTANCE.save((DataObject)quote, "http://www.example.com/simple", "stockQuote", System.out);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ public void testFailureOnNullScope() {
+ try {
+ SimpleFactory.INSTANCE.register(null);
+ assertTrue("Should not be able to register in null scope", false);
+ }
+ catch (Exception e) {
+ // caught expected exception
+ }
+ }
+
+
+}
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/InheritanceTestCase.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/InheritanceTestCase.java
new file mode 100644
index 0000000000..a971f79f0f
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/InheritanceTestCase.java
@@ -0,0 +1,102 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import com.example.subgroup.A;
+import com.example.subgroup.B;
+import com.example.subgroup.Bprime;
+import com.example.subgroup.SubgroupFactory;
+import com.example.subgroup.impl.AImpl;
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.HelperContext;
+import commonj.sdo.helper.XMLDocument;
+
+public class InheritanceTestCase extends TestCase {
+
+ private HelperContext scope;
+
+ public void testSubGroupLoad() throws IOException {
+ XMLDocument doc = scope.getXMLHelper().load(
+ getClass().getResourceAsStream("/subgroup1.xml"));
+ assertEquals("a", doc.getRootElementName());
+ String strdoc = scope.getXMLHelper().save(
+ doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName());
+ assertTrue(strdoc.indexOf("<sg:imInTypeB>thisIsElB</sg:imInTypeB>") != -1);
+ }
+
+ public void testSubGroup_AcontainsB() {
+ A a = (A) scope.getDataFactory().create(A.class);
+ B b = (B) scope.getDataFactory().create(B.class);
+ b.setImInTypeB("thisIsElB");
+ a.setGe1(b);
+
+ assertSame(b, a.getGe1());
+ assertSame(b, ((AImpl) a).get(AImpl.GE1));
+
+ String doc = scope.getXMLHelper().save((DataObject) a,
+ "http://example.com/subgroup", "a");
+ assertTrue(doc.indexOf("<sg:imInTypeB>thisIsElB</sg:imInTypeB>") != -1);
+
+ }
+
+ public void testSubGroup_AcontainsBprime() {
+ A a = (A) scope.getDataFactory().create(A.class);
+ Bprime bp = (Bprime) scope.getDataFactory().create(Bprime.class);
+ bp.setImInTypeBprime("thisIsElBprime");
+ a.setGe1(bp);
+
+ assertSame(bp, a.getGe1());
+ assertSame(bp, ((AImpl) a).get(AImpl.GE1));
+
+ String doc = scope.getXMLHelper().save((DataObject) a,
+ "http://example.com/subgroup", "a");
+
+ assertTrue(doc.indexOf("<sg:imInTypeBprime>thisIsElBprime</sg:imInTypeBprime>") != -1);
+
+ }
+
+ public void testSubGroup_Bprime() {
+ Bprime bp = (Bprime) scope.getDataFactory().create(Bprime.class);
+ bp.setImInTypeB("bValue");
+ bp.setImInTypeBprime("bpvalue");
+ String doc = scope.getXMLHelper().save((DataObject) bp,
+ "http://example.com/subgroup", "bp");
+ assertTrue(doc.indexOf("<sg:imInTypeB>bValue</sg:imInTypeB>") != -1);
+ assertTrue(doc.indexOf("<sg:imInTypeBprime>bpvalue</sg:imInTypeBprime>") != -1);
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ scope = SDOUtil.createHelperContext();
+
+ SubgroupFactory.INSTANCE.register(scope);
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+}
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTestCase.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTestCase.java
new file mode 100644
index 0000000000..207ccd4484
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTestCase.java
@@ -0,0 +1,86 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+
+import java.math.BigDecimal;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.util.SDOUtil;
+
+import com.example.simple.Quote;
+import com.example.simple.SimpleFactory;
+import com.example.simple.impl.SimpleFactoryImpl;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.helper.HelperContext;
+import commonj.sdo.helper.XMLHelper;
+
+
+public class SimpleStaticTestCase extends TestCase
+{
+
+ HelperContext scope;
+ /**
+ * Simple Static SDO 2 test.
+ */
+ public void testSimpleStatic()
+ {
+ try
+ {
+ SimpleFactory.INSTANCE.register(scope);
+ // System.out.println(SimpleFactoryImpl.PATTERN_VERSION);
+
+ //Quote quote = (Quote)DataFactory.INSTANCE.create(Quote.class);
+ Quote quote = SimpleFactory.INSTANCE.createQuote();
+
+ quote.setSymbol("fbnt");
+ quote.setCompanyName("FlyByNightTechnology");
+ quote.setPrice(new BigDecimal("1000.0"));
+ quote.setOpen1(new BigDecimal("1000.0"));
+ quote.setHigh(new BigDecimal("1000.0"));
+ quote.setLow(new BigDecimal("1000.0"));
+ quote.setVolume(1000);
+ quote.setChange1(1000);
+
+ //Quote child = (Quote)((DataObject)quote).createDataObject(8);
+ Quote child = SimpleFactory.INSTANCE.createQuote();
+ quote.getQuotes().add(child);
+ child.setPrice(new BigDecimal("2000.0"));
+
+ // scope.getXMLHelper().save((DataObject)quote, "http://www.example.com/simple", "stockQuote", System.out);
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+
+ protected void setUp() throws Exception {
+ scope = SDOUtil.createHelperContext();
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+
+}
diff --git a/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java
new file mode 100644
index 0000000000..0ca49b6122
--- /dev/null
+++ b/sdo-java/tags/1.0-incubating-beta1/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java
@@ -0,0 +1,288 @@
+/**
+ *
+ * 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.tuscany.sdo.test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.DocumentType;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
+public class TestUtil
+{
+ private static void getAllNodes(NodeList nodeList, List nodes)
+ {
+ int length = nodeList.getLength();
+ if (length == 0)
+ {
+ return;
+ }
+
+ for (int i=0; i<length; i++)
+ {
+ Node node = nodeList.item(i);
+ nodes.add(node);
+ getAllNodes(node.getChildNodes(), nodes);
+ } // for
+ }
+
+ private static boolean equalNamedNodeMap(NamedNodeMap mapA, NamedNodeMap mapB) {
+ if (mapA == null) {
+ if (mapB == null) {
+ return true;
+ }
+ return false;
+ }
+ if (mapA.getLength() != mapB.getLength()) {
+ return false;
+ }
+ for (int i = 0; i < mapA.getLength(); i++) {
+ Node trialNode = mapA.item(i);
+ if (trialNode == null) {
+ return false;
+ }
+ Node checkNode = mapB.getNamedItem(trialNode.getNodeName());
+ if (checkNode == null) {
+ return false;
+ }
+ if (!equalNode(trialNode, checkNode)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static boolean equalNode(Node nodeA, Node nodeB) {
+ if (nodeA == null) {
+ if (nodeB == null) {
+ return true;
+ }
+ return false;
+ }
+ // following is intended to provide same function as 1.5 isEqualNode()
+ if (nodeA.getNodeType() != nodeB.getNodeType()) {
+ return false;
+ }
+ if (!equalString(nodeA.getNodeName(), nodeB.getNodeName())) {
+ return false;
+ }
+ if (!equalString(nodeA.getLocalName(), nodeB.getLocalName())) {
+ return false;
+ }
+ if (!equalString(nodeA.getNamespaceURI(), nodeB.getNamespaceURI())) {
+ return false;
+ }
+ if (!equalString(nodeA.getNamespaceURI(), nodeB.getNamespaceURI())) {
+ return false;
+ }
+ if (!equalString(nodeA.getPrefix(), nodeB.getPrefix())) {
+ return false;
+ }
+ if (!equalString(nodeA.getNodeValue(), nodeB.getNodeValue())) {
+ return false;
+ }
+ if (!equalNamedNodeMap(nodeA.getAttributes(), nodeB.getAttributes())) {
+ return false;
+ }
+ if (!equalNodeList(nodeA.getChildNodes(), nodeB.getChildNodes())) {
+ return false;
+ }
+ if (nodeA.getNodeType() == Node.DOCUMENT_TYPE_NODE) {
+ DocumentType documentTypeA = (DocumentType) nodeA;
+ DocumentType documentTypeB = (DocumentType) nodeB;
+ if (!equalString(documentTypeA.getPublicId(), documentTypeB.getPublicId())) {
+ return false;
+ }
+ if (!equalString(documentTypeA.getSystemId(), documentTypeB.getSystemId())) {
+ return false;
+ }
+ if (!equalString(documentTypeA.getInternalSubset(), documentTypeB.getInternalSubset())) {
+ return false;
+ }
+ if (!equalNamedNodeMap(documentTypeA.getEntities(), documentTypeB.getEntities())) {
+ return false;
+ }
+ if (!equalNamedNodeMap(documentTypeA.getNotations(), documentTypeB.getNotations())) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private static boolean equalNodeList(NodeList nodeListA, NodeList nodeListB) {
+ if (nodeListA == null) {
+ if (nodeListB == null) {
+ return true;
+ }
+ return false;
+ }
+ return equalNodes(nodeListA, nodeListB);
+ }
+
+ private static boolean equalString(String stringA, String stringB) {
+ if (stringA == null) {
+ if (stringB == null) {
+ return true;
+ }
+ return false;
+ }
+ return stringA.equals(stringB);
+ }
+
+ private static boolean equalNodes(NodeList sourceNodeList, NodeList targetNodeList)
+ {
+ ArrayList sourceNodes = new ArrayList();
+ ArrayList targetNodes = new ArrayList();
+
+ getAllNodes(sourceNodeList, sourceNodes);
+ getAllNodes(targetNodeList, targetNodes);
+
+ int sourceLength = sourceNodes.size();
+ int targetLength = targetNodes.size();
+
+ if (sourceLength != targetLength)
+ {
+ return false;
+ }
+
+ for (int i=0; i<sourceLength; i++)
+ {
+ Node sourceNode = (Node)sourceNodes.get(i);
+ Node targetNode = (Node)targetNodes.get(i);
+
+ /* remove comment when migrated to Java 1.5
+ if (!sourceNode.isEqualNode(targetNode))
+ {
+ return false;
+ }
+ */
+ // following is intended as 1.4 equivalent of isEqualNode()
+ if (!equalNode(sourceNode, targetNode))
+ {
+ return false;
+ }
+ } // for
+
+ return true;
+ }
+
+ public static boolean equalXmlFiles(URL source, URL target)
+ {
+ try {
+ return equalXmlFiles(source.openStream(), target.openStream());
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ }
+
+ public static boolean equalXmlFiles(InputStream sourceStream, URL target)
+ {
+ try {
+ return equalXmlFiles(sourceStream, target.openStream());
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ }
+
+ public static boolean equalXmlFiles(URL source, InputStream targetStream)
+ {
+ try {
+ return equalXmlFiles(source.openStream(), targetStream);
+ }
+ catch (IOException e)
+ {
+ return false;
+ }
+ }
+
+ public static boolean equalXmlFiles(InputStream sourceStream, InputStream targetStream)
+ {
+ DocumentBuilder builder;
+ Document sourceDocument;
+ Document targetDocument;
+
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setIgnoringComments(true);
+ builder = factory.newDocumentBuilder();
+ //builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ sourceDocument = builder.parse(sourceStream);
+ targetDocument = builder.parse(targetStream);
+ }
+ catch (FactoryConfigurationError fce) {
+ return false;
+ }
+ catch (ParserConfigurationException ce) {
+ return false;
+ }
+ catch (SAXException se)
+ {
+ return false;
+ }
+ catch (IOException ie)
+ {
+ return false;
+ }
+
+ sourceDocument.normalize();
+ targetDocument.normalize();
+
+ /* remove comment when migrated to Java 1.5
+ if (!sourceDocument.getXmlVersion().equals(targetDocument.getXmlVersion()))
+ {
+ return false;
+ }
+
+ String sourceXmlEncoding = sourceDocument.getXmlEncoding();
+ String targetXmlEncoding = targetDocument.getXmlEncoding();
+
+ if (sourceXmlEncoding != null && targetXmlEncoding != null &&
+ sourceXmlEncoding.equalsIgnoreCase(targetXmlEncoding))
+ {
+ // continue
+ }
+ else
+ {
+ return false;
+ }
+ */
+
+ NodeList sourceNodes = sourceDocument.getChildNodes();
+ NodeList targetNodes = targetDocument.getChildNodes();
+
+ return equalNodes(sourceNodes, targetNodes);
+ }
+}