diff options
Diffstat (limited to 'sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo')
3 files changed, 607 insertions, 0 deletions
diff --git a/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTest.java b/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTest.java new file mode 100644 index 0000000000..b177373b5e --- /dev/null +++ b/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/SimpleStaticTest.java @@ -0,0 +1,67 @@ +/** + * + * 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 org.apache.tuscany.sdo.util.SDOUtil; + +import com.example.simple.Quote; +import com.example.simple.SimpleFactory; +import commonj.sdo.DataObject; +import commonj.sdo.helper.XMLHelper; + + +public class SimpleStaticTest +{ + /** + * Simple Static SDO 2 test. + */ + public static void main(String[] args) + { + try + { + SDOUtil.registerStaticTypes(SimpleFactory.class); + + //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")); + + XMLHelper.INSTANCE.save((DataObject)quote, "http://www.example.com/simple", "stockQuote", System.out); + } + catch (Exception e) + { + e.printStackTrace(); + } + } +} diff --git a/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/StaticSequenceNoEmfTestCase.java b/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/StaticSequenceNoEmfTestCase.java new file mode 100644 index 0000000000..966e52cdba --- /dev/null +++ b/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/StaticSequenceNoEmfTestCase.java @@ -0,0 +1,252 @@ +/** + * + * 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import com.example.sequences.MixedQuote; +import com.example.sequences.MixedRepeatingChoice; +import com.example.sequences.RepeatingChoice; +import com.example.sequences.SequencesFactory; +import com.example.sequences.TwoRCs; +import com.example.sequences.TwoRCsMixed; +import commonj.sdo.DataObject; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.XMLHelper; + + +public class StaticSequenceNoEmfTestCase extends TestCase { + private final String TEST_NAMESPACE = "http://www.example.com/sequences"; + private final String MQ_TEST_DATA = "/mixedStaticTestResult.xml"; + private final String RC_TEST_DATA = "/repeatingChoiceTestResult.xml"; + private final String MRC_TEST_DATA = "/mixedRepeatingChoiceTestResult.xml"; + private final String RC2_TEST_DATA = "/twoRepeatingChoicesTestResult.xml"; + private final String RC2M_TEST_DATA = "/twoRepeatingChoicesMixedTestResult.xml"; + + /** + * Sequenced type SDO 2 test. + */ + public void testMixedQuoteType() throws IOException { + MixedQuote quote = SequencesFactory.INSTANCE.createMixedQuote(); + + Type t = ((DataObject)quote).getType(); + List ps = t.getProperties(); + + + Sequence sequence = quote.getMixed(); + + sequence.add("\n "); + + quote.setSymbol("fbnt"); + + sequence.add("\n "); + + quote.setCompanyName("FlyByNightTechnology"); + + sequence.add("\n some text\n "); + + List quotes = quote.getQuotes(); + MixedQuote child = SequencesFactory.INSTANCE.createMixedQuote(); + quotes.add(child); + + child.setPrice(new BigDecimal("2000.0")); + + sequence.add("\n more text\n "); + + sequence.add("price", new BigDecimal("1000.0")); + + sequence.add("\n"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + XMLHelper.INSTANCE.save((DataObject)quote, TEST_NAMESPACE, "mixedStockQuote", baos); + assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(MQ_TEST_DATA))); + } + + public void testRepeatingChoice() throws IOException + { + RepeatingChoice rc = SequencesFactory.INSTANCE.createRepeatingChoice(); + + List as = rc.getA(); + List bs = rc.getB(); + + bs.add(new Integer(1)); + as.add("foo"); + as.add("bar"); + bs.add(new Integer(2)); + + + + + Sequence group = rc.getGroup(); + assertEquals(group.size(), 4); + assertEquals(rc.getA().size(), 2); + assertEquals(rc.getB().size(),2); + + assertEquals(group.getValue(0), new Integer(1)); + assertEquals(group.getValue(1), "foo"); + assertEquals(group.getValue(2), "bar"); + assertEquals(group.getValue(3), new Integer(2)); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + XMLHelper.INSTANCE.save((DataObject)rc, TEST_NAMESPACE, "rc", baos); + assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC_TEST_DATA))); + } + + public void testMixedRepeatingChoice() throws Exception { + MixedRepeatingChoice mrc = SequencesFactory.INSTANCE.createMixedRepeatingChoice(); + + List as = mrc.getA(); + List bs = mrc.getB(); + + + bs.add(new Integer(1)); + as.add("foo"); + Sequence mixed = mrc.getMixed(); + mixed.add("some mixed text"); + as.add("bar"); + bs.add(new Integer(2)); + + // FIXME reintroduce check + // assertEquals(4, mrc.getGroup().size()); + assertEquals(5, mrc.getMixed().size()); + assertEquals(2, mrc.getA().size()); + assertEquals(2, mrc.getB().size(),2); + + + + assertEquals(mixed.getValue(0), new Integer(1)); + assertEquals(mixed.getValue(1), "foo"); + assertEquals(mixed.getValue(3), "bar"); + assertEquals(mixed.getValue(4), new Integer(2)); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + + XMLHelper.INSTANCE.save((DataObject)mrc, TEST_NAMESPACE, "mrc", baos); + assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(MRC_TEST_DATA))); + } + + public void test2RepeatingChoices() throws Exception { + TwoRCs rc2 = SequencesFactory.INSTANCE.createTwoRCs(); + + List as = rc2.getA(); + List bs = rc2.getB(); + List ys = rc2.getY(); + List zs = rc2.getZ(); + + zs.add(new Integer(99)); + bs.add(new Integer(1)); + ys.add("fred"); + as.add("foo"); + as.add("bar"); + bs.add(new Integer(2)); + rc2.setSplit("banana"); + + + Sequence group = rc2.getGroup(); + assertEquals(group.size(), 4); + assertEquals(rc2.getA().size(), 2); + assertEquals(rc2.getB().size(),2); + + Sequence group1 = rc2.getGroup1(); + assertEquals(group1.size(), 2); + assertEquals(rc2.getY().size(), 1); + assertEquals(rc2.getZ().size(),1); + + assertEquals(group.getValue(0), new Integer(1)); + assertEquals(group.getValue(1), "foo"); + assertEquals(group.getValue(2), "bar"); + assertEquals(group.getValue(3), new Integer(2)); + assertEquals(group1.getValue(0), new Integer(99)); + assertEquals(group1.getValue(1), "fred"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + + XMLHelper.INSTANCE.save((DataObject)rc2, TEST_NAMESPACE, "rc2", baos); + assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC2_TEST_DATA))); + } + + protected void setUp() throws Exception { + super.setUp(); + SDOUtil.registerStaticTypes(SequencesFactory.class); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void test2RepeatingChoicesMixed() throws Exception { + TwoRCsMixed rc2m = SequencesFactory.INSTANCE.createTwoRCsMixed(); + + List as = rc2m.getA(); + List bs = rc2m.getB(); + List ys = rc2m.getY(); + List zs = rc2m.getZ(); + Sequence mixed = rc2m.getMixed(); + + bs.add(new Integer(1)); + mixed.add("where will this appear?"); + as.add("foo"); + as.add("bar"); + bs.add(new Integer(2)); + rc2m.setSplit("pea"); + zs.add(new Integer(99)); + ys.add("fred"); + + + assertEquals(8, mixed.size()); + + Sequence group = rc2m.getGroup(); + // FIXME reintroduce test assertEquals(4, group.size()); + assertEquals(rc2m.getA().size(), 2); + assertEquals(rc2m.getB().size(),2); + + Sequence group1 = rc2m.getGroup1(); + // FIXME ditto assertEquals(group1.size(), 2); + assertEquals(rc2m.getY().size(), 1); + assertEquals(rc2m.getZ().size(),1); + + int i = 0; + assertEquals(mixed.getValue(i++), new Integer(1)); + assertEquals(mixed.getValue(i++), "where will this appear?"); + assertEquals(mixed.getValue(i++), "foo"); + assertEquals(mixed.getValue(i++), "bar"); + assertEquals(mixed.getValue(i++), new Integer(2)); + assertEquals(mixed.getValue(i++), "pea"); + assertEquals(mixed.getValue(i++), new Integer(99)); + assertEquals(mixed.getValue(i++), "fred"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + XMLHelper.INSTANCE.save((DataObject)rc2m, TEST_NAMESPACE, "rc2m", baos); + assertTrue(baos.toString(), TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(RC2M_TEST_DATA))); + } + + +} diff --git a/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java b/sandbox/jboynes/sdo/tools/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java new file mode 100644 index 0000000000..0ca49b6122 --- /dev/null +++ b/sandbox/jboynes/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); + } +} |