From 195774c489a1a671aca514b0afa88332bf9c6ee3 Mon Sep 17 00:00:00 2001 From: lresende Date: Tue, 10 Nov 2009 19:20:12 +0000 Subject: Moving SDO tags git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@834617 13f79535-47bb-0310-9956-ffa450edef68 --- .../test/java/org/apache/tuscany/sdo/AllTests.java | 41 + .../org/apache/tuscany/sdo/codegen/AllTests.java | 47 ++ .../java/org/apache/tuscany/sdo/codegen/Bar1.java | 26 + .../java/org/apache/tuscany/sdo/codegen/Bar2.java | 26 + .../BytecodeInterfaceGeneratorTestCase.java | 167 ++++ .../java/org/apache/tuscany/sdo/codegen/Foo.java | 26 + .../codegen/JavaInterfaceGeneratorTestCase.java | 129 +++ .../apache/tuscany/sdo/codegen/MockProperty.java | 94 +++ .../org/apache/tuscany/sdo/codegen/MockType.java | 104 +++ .../java/org/apache/tuscany/sdo/test/AllTests.java | 75 ++ .../org/apache/tuscany/sdo/test/AnyTypeTest.java | 112 +++ .../test/ChangeSummaryOnDataObjectTestCase.java | 216 +++++ .../sdo/test/ChangeSummaryPropertyTestCase.java | 219 +++++ .../tuscany/sdo/test/ChangeSummaryTestCase.java | 92 +++ .../tuscany/sdo/test/CrossScopeCopyTestCase.java | 474 +++++++++++ .../apache/tuscany/sdo/test/DataGraphTestCase.java | 270 +++++++ .../tuscany/sdo/test/DataTypeBaseTypeTestCase.java | 68 ++ .../tuscany/sdo/test/DateConversionTestCase.java | 374 +++++++++ .../test/DefineOpenContentPropertyTestCase.java | 124 +++ .../tuscany/sdo/test/DefineTypeTestCase.java | 684 ++++++++++++++++ .../sdo/test/DeserializationNoSchemaTestCase.java | 108 +++ .../org/apache/tuscany/sdo/test/DotNameTest.java | 78 ++ .../sdo/test/DynamicTypesComparisonTestCase.java | 256 ++++++ .../org/apache/tuscany/sdo/test/FormTestCase.java | 112 +++ .../tuscany/sdo/test/HelperContextTestCase.java | 75 ++ .../apache/tuscany/sdo/test/IsManyTestCase.java | 65 ++ .../sdo/test/JavaSerializeDeserializeTestCase.java | 297 +++++++ .../org/apache/tuscany/sdo/test/JiraTestCases.java | 184 +++++ .../test/MetadataInstancePropertiesTestCase.java | 219 +++++ .../apache/tuscany/sdo/test/MixedTypeTestCase.java | 89 +++ .../sdo/test/NeverStaleChangeSummaryTestCase.java | 76 ++ .../apache/tuscany/sdo/test/OpenTypeTestCase.java | 110 +++ .../tuscany/sdo/test/SchemaLocationTestCase.java | 48 ++ .../tuscany/sdo/test/SerializeTypesTestCase.java | 129 +++ .../tuscany/sdo/test/SimpleCopyTestCase.java | 71 ++ .../tuscany/sdo/test/SimpleDynamicTestCase.java | 123 +++ .../tuscany/sdo/test/SimpleEqualityTestCase.java | 63 ++ .../sdo/test/SubstitutionValuesTestCase.java | 47 ++ .../java/org/apache/tuscany/sdo/test/TestUtil.java | 285 +++++++ .../tuscany/sdo/test/TypeConversionTestCase.java | 889 +++++++++++++++++++++ .../tuscany/sdo/test/TypeRoundTripTestCase.java | 148 ++++ .../tuscany/sdo/test/XMLDocumentTestCase.java | 110 +++ .../apache/tuscany/sdo/test/XMLHelperTestCase.java | 161 ++++ .../test/XMLStreamHelperPerformanceTestCase.java | 91 +++ .../tuscany/sdo/test/XMLStreamHelperTestCase.java | 169 ++++ .../org/apache/tuscany/sdo/test/XPathTestCase.java | 125 +++ .../apache/tuscany/sdo/test/XSDHelperTestCase.java | 224 ++++++ 47 files changed, 7720 insertions(+) create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/AllTests.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/AllTests.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryOnDataObjectTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DataGraphTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DataTypeBaseTypeTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DateConversionTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DefineOpenContentPropertyTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DefineTypeTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DeserializationNoSchemaTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DotNameTest.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/DynamicTypesComparisonTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/HelperContextTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/IsManyTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JavaSerializeDeserializeTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MetadataInstancePropertiesTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MixedTypeTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/NeverStaleChangeSummaryTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/OpenTypeTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SchemaLocationTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleCopyTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleDynamicTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleEqualityTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SubstitutionValuesTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeConversionTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeRoundTripTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XPathTestCase.java create mode 100644 sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XSDHelperTestCase.java (limited to 'sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org') diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/AllTests.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/AllTests.java new file mode 100644 index 0000000000..7d91456137 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/AllTests.java @@ -0,0 +1,41 @@ +/** + * + * 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; +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + + public static Test suite() { + + TestSuite suite = new TestSuite(); + + suite.addTest(org.apache.tuscany.sdo.codegen.AllTests.suite()); + suite.addTest(org.apache.tuscany.sdo.test.AllTests.suite()); + + return suite; + } + + + public static void main(String[] args) { + junit.textui.TestRunner.run(suite()); + } +} + diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/AllTests.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/AllTests.java new file mode 100644 index 0000000000..d67b53fb7c --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/AllTests.java @@ -0,0 +1,47 @@ +/** + * + * 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.codegen; +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + + + public static TestSuite suite() { + + TestSuite suite = new TestSuite(); + + suite.addTestSuite(BytecodeInterfaceGeneratorTestCase.class); + suite.addTestSuite(JavaInterfaceGeneratorTestCase.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-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java new file mode 100644 index 0000000000..4a51310cd0 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar1.java @@ -0,0 +1,26 @@ +/** + * + * 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.codegen; + +/** + * @version $Rev$ $Date$ + */ +public interface Bar1 { +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java new file mode 100644 index 0000000000..e9151b9d46 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Bar2.java @@ -0,0 +1,26 @@ +/** + * + * 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.codegen; + +/** + * @version $Rev$ $Date$ + */ +public interface Bar2 { +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java new file mode 100644 index 0000000000..218a725226 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/BytecodeInterfaceGeneratorTestCase.java @@ -0,0 +1,167 @@ +/** + * + * 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.codegen; + +import java.lang.reflect.Method; +import java.util.List; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class BytecodeInterfaceGeneratorTestCase extends TestCase { + private MockType foo; + private BytecodeInterfaceGenerator gen; + private TestClassLoader cl; + + public void testHeaderNoBaseClass() { + gen.visitType(foo); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals("Foo", c.getName()); + assertTrue(c.isInterface()); + assertEquals(0, c.getInterfaces().length); + assertEquals(0, c.getMethods().length); + } + + public void testHeaderWithPackage() { + gen.visitType(new MockType("org.apache.Foo", null)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals("org.apache.Foo", c.getName()); + assertEquals(0, c.getInterfaces().length); + assertEquals(0, c.getMethods().length); + } + + public void testHeaderOneBaseClass() throws NoJavaImplementationException { + foo.addBaseType(new MockType("bar1", Bar1.class)); + gen.visitType(foo); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals("Foo", c.getName()); + assertEquals(1, c.getInterfaces().length); + assertEquals(Bar1.class, c.getInterfaces()[0]); + assertEquals(0, c.getMethods().length); + } + + public void testHeaderMultipleBaseClass() throws NoJavaImplementationException { + foo.addBaseType(new MockType("bar1", Bar1.class)); + foo.addBaseType(new MockType("bar2", Bar2.class)); + gen.visitType(foo); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals("Foo", c.getName()); + assertEquals(2, c.getInterfaces().length); + assertEquals(Bar1.class, c.getInterfaces()[0]); + assertEquals(Bar2.class, c.getInterfaces()[1]); + assertEquals(0, c.getMethods().length); + } + + public void testBooleanProperty() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(2, c.getMethods().length); + Method getter = c.getMethod("isTrue", new Class[0]); + assertEquals(boolean.class, getter.getReturnType()); + Method setter = c.getMethod("setTrue", new Class[] {boolean.class}); + assertEquals(void.class, setter.getReturnType()); + } + + public void testByteArrayProperty() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("bytes", byte[].class, false, false)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(2, c.getMethods().length); + Method getter = c.getMethod("getBytes", new Class[0]); + assertEquals(byte[].class, getter.getReturnType()); + Method setter = c.getMethod("setBytes", new Class[] {byte[].class}); + assertEquals(void.class, setter.getReturnType()); + } + + public void testObjectProperty() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("bar", Bar1.class, false, false)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(2, c.getMethods().length); + Method getter = c.getMethod("getBar", new Class[0]); + assertEquals(Bar1.class, getter.getReturnType()); + Method setter = c.getMethod("setBar", new Class[] {Bar1.class}); + assertEquals(void.class, setter.getReturnType()); + } + + public void testReadOnlyProperty() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("int", Integer.TYPE, false, true)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(1, c.getMethods().length); + Method getter = c.getMethod("getInt", new Class[0]); + assertEquals(int.class, getter.getReturnType()); + } + + public void testManyProperty() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("list", Integer.TYPE, true, false)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(1, c.getMethods().length); + Method getter = c.getMethod("getList", new Class[0]); + assertEquals(List.class, getter.getReturnType()); + } + + public void testTwoProperties() throws NoJavaImplementationException, NoSuchMethodException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false)); + gen.visitProperty(new MockProperty("int", Integer.TYPE, false, false)); + gen.visitEnd(); + Class c = cl.addClass(gen.getClassData()); + assertEquals(4, c.getMethods().length); + Method getter = c.getMethod("isTrue", new Class[0]); + assertEquals(boolean.class, getter.getReturnType()); + Method setter = c.getMethod("setTrue", new Class[] {boolean.class}); + assertEquals(void.class, setter.getReturnType()); + getter = c.getMethod("getInt", new Class[0]); + assertEquals(int.class, getter.getReturnType()); + setter = c.getMethod("setInt", new Class[] {int.class}); + assertEquals(void.class, setter.getReturnType()); + } + + protected void setUp() throws Exception { + super.setUp(); + foo = new MockType("foo", null); + gen = new BytecodeInterfaceGenerator(); + cl = new TestClassLoader(); + } + + private class TestClassLoader extends ClassLoader { + public TestClassLoader() { + super(TestClassLoader.class.getClassLoader()); + } + + Class addClass(byte[] bytes) { + return defineClass(null, bytes, 0, bytes.length); + } + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java new file mode 100644 index 0000000000..56bf6af9b4 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/Foo.java @@ -0,0 +1,26 @@ +/** + * + * 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.codegen; + +/** + * @version $Rev$ $Date$ + */ +public interface Foo { +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java new file mode 100644 index 0000000000..6cc0529c5f --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/JavaInterfaceGeneratorTestCase.java @@ -0,0 +1,129 @@ +/** + * + * 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.codegen; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class JavaInterfaceGeneratorTestCase extends TestCase { + private static final String SEP = System.getProperty("line.separator"); + + private JavaInterfaceGenerator gen; + private StringWriter writer; + private MockType foo; + + public void testHeaderNoBaseClass() { + gen.visitType(foo); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n}\n"), writer.toString()); + } + + public void testHeaderWithPackage() { + gen.visitType(new MockType("org.apache.Foo", null)); + gen.visitEnd(); + assertEquals(localize("package org.apache;\n\npublic interface Foo {\n}\n"), writer.toString()); + } + + public void testHeaderOneBaseClass() throws NoJavaImplementationException { + foo.addBaseType(new MockType("bar1", Bar1.class)); + gen.visitType(foo); + gen.visitEnd(); + assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1 {\n}\n"), writer.toString()); + } + + public void testHeaderMultipleBaseClass() throws NoJavaImplementationException { + foo.addBaseType(new MockType("bar1", Bar1.class)); + foo.addBaseType(new MockType("bar2", Bar2.class)); + gen.visitType(foo); + gen.visitEnd(); + assertEquals(localize("public interface Foo extends org.apache.tuscany.sdo.codegen.Bar1, org.apache.tuscany.sdo.codegen.Bar2 {\n}\n"), writer.toString()); + } + + public void testBooleanProperty() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean value);\n}\n"), writer.toString()); + } + + public void testByteArrayProperty() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("bytes", byte[].class, false, false)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\tbyte[] getBytes();\n\tvoid setBytes(byte[] value);\n}\n"), writer.toString()); + } + + public void testObjectProperty() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("bar", Bar1.class, false, false)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\torg.apache.tuscany.sdo.codegen.Bar1 getBar();\n\tvoid setBar(org.apache.tuscany.sdo.codegen.Bar1 value);\n}\n"), writer.toString()); + } + + public void testReadOnlyProperty() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("int", Integer.TYPE, false, true)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\tint getInt();\n}\n"), writer.toString()); + } + + public void testManyProperty() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("list", Integer.TYPE, true, false)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\tjava.util.List getList();\n}\n"), writer.toString()); + } + + public void testTwoProperties() throws NoJavaImplementationException { + gen.visitType(foo); + gen.visitProperty(new MockProperty("true", Boolean.TYPE, false, false)); + gen.visitProperty(new MockProperty("int", Integer.TYPE, false, false)); + gen.visitEnd(); + assertEquals(localize("public interface Foo {\n\tboolean isTrue();\n\tvoid setTrue(boolean value);\n\tint getInt();\n\tvoid setInt(int value);\n}\n"), writer.toString()); + } + + protected void setUp() throws Exception { + super.setUp(); + writer = new StringWriter(); + gen = new JavaInterfaceGenerator(new PrintWriter(writer)); + foo = new MockType("foo", null); + } + + private String localize(String s) { + StringBuffer b = new StringBuffer(s.length() + s.length()/10); + for (int i = 0; i < s.length(); i++) { + char ch = s.charAt(i); + if (ch == '\t') { + b.append(" "); + } else if (ch == '\n') { + b.append(SEP); + } else { + b.append(ch); + } + } + return b.toString(); + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java new file mode 100644 index 0000000000..3e3e87e47b --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockProperty.java @@ -0,0 +1,94 @@ +/** + * + * 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.codegen; + +import java.util.List; + +import commonj.sdo.Property; +import commonj.sdo.Type; + +/** + * @version $Rev$ $Date$ + */ +class MockProperty implements Property { + private final String name; + private final Type type; + private final boolean many; + private final boolean readOnly; + + public MockProperty(String name, Class type, boolean many, boolean readOnly) { + this.name = name; + this.type = new MockType(null, type); + this.many = many; + this.readOnly = readOnly; + } + + public String getName() { + return name; + } + + public Type getType() { + return type; + } + + public boolean isMany() { + return many; + } + + public boolean isContainment() { + throw new UnsupportedOperationException(); + } + + public Type getContainingType() { + throw new UnsupportedOperationException(); + } + + public Object getDefault() { + throw new UnsupportedOperationException(); + } + + public boolean isReadOnly() { + return readOnly; + } + + public Property getOpposite() { + throw new UnsupportedOperationException(); + } + + public List /*String*/ getAliasNames() { + throw new UnsupportedOperationException(); + } + + public boolean isNullable() { + throw new UnsupportedOperationException(); + } + + public boolean isOpenContent() { + throw new UnsupportedOperationException(); + } + + public List getInstanceProperties() { + throw new UnsupportedOperationException(); + } + + public Object get(Property property) { + throw new UnsupportedOperationException(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java new file mode 100644 index 0000000000..ec848de4fa --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/codegen/MockType.java @@ -0,0 +1,104 @@ +/** + * + * 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.codegen; + +import java.util.List; +import java.util.ArrayList; + +import commonj.sdo.Type; +import commonj.sdo.Property; + +/** + * @version $Rev$ $Date$ + */ +class MockType implements Type { + private final String name; + private final Class javaClass; + private final List baseTypes = new ArrayList(); + + public MockType(String name, Class javaClass) { + this.javaClass = javaClass; + this.name = name; + } + + public void addBaseType(Type baseType) { + baseTypes.add(baseType); + } + + public String getName() { + return name; + } + + public String getURI() { + throw new UnsupportedOperationException(); + } + + public Class getInstanceClass() { + return javaClass; + } + + public boolean isInstance(Object object) { + throw new UnsupportedOperationException(); + } + + public List /*Property*/ getProperties() { + throw new UnsupportedOperationException(); + } + + public Property getProperty(String propertyName) { + throw new UnsupportedOperationException(); + } + + public boolean isDataType() { + throw new UnsupportedOperationException(); + } + + public boolean isOpen() { + throw new UnsupportedOperationException(); + } + + public boolean isSequenced() { + throw new UnsupportedOperationException(); + } + + public boolean isAbstract() { + throw new UnsupportedOperationException(); + } + + public List /*Type*/ getBaseTypes() { + return baseTypes; + } + + public List /*Property*/ getDeclaredProperties() { + throw new UnsupportedOperationException(); + } + + public List /*String*/ getAliasNames() { + throw new UnsupportedOperationException(); + } + + public List getInstanceProperties() { + throw new UnsupportedOperationException(); + } + + public Object get(Property property) { + throw new UnsupportedOperationException(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java new file mode 100644 index 0000000000..963fc59e2a --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AllTests.java @@ -0,0 +1,75 @@ + +/** + * + * 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(ChangeSummaryOnDataObjectTestCase.class); + suite.addTestSuite(ChangeSummaryPropertyTestCase.class); + suite.addTestSuite(ChangeSummaryTestCase.class); + suite.addTestSuite(CrossScopeCopyTestCase.class); + suite.addTestSuite(DataGraphTestCase.class); + suite.addTestSuite(DataTypeBaseTypeTestCase.class); + suite.addTestSuite(DateConversionTestCase.class); + suite.addTestSuite(DefineOpenContentPropertyTestCase.class); + suite.addTestSuite(DefineTypeTestCase.class); + suite.addTestSuite(FormTestCase.class); + suite.addTestSuite(HelperContextTestCase.class); + suite.addTestSuite(IsManyTestCase.class); + suite.addTestSuite(JavaSerializeDeserializeTestCase.class); + suite.addTestSuite(MixedTypeTestCase.class); + suite.addTestSuite(NeverStaleChangeSummaryTestCase.class); + suite.addTestSuite(OpenTypeTestCase.class); + suite.addTestSuite(SchemaLocationTestCase.class); + suite.addTestSuite(SerializeTypesTestCase.class); + suite.addTestSuite(SimpleCopyTestCase.class); + suite.addTestSuite(SimpleDynamicTestCase.class); + suite.addTestSuite(SimpleEqualityTestCase.class); + suite.addTestSuite(SubstitutionValuesTestCase.class); + suite.addTestSuite(TypeConversionTestCase.class); + suite.addTestSuite(TypeRoundTripTestCase.class); + suite.addTestSuite(XMLDocumentTestCase.class); + suite.addTestSuite(XMLHelperTestCase.class); + suite.addTestSuite(XMLStreamHelperTestCase.class); + suite.addTestSuite(XPathTestCase.class); + suite.addTestSuite(XSDHelperTestCase.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-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java new file mode 100644 index 0000000000..46cd2202b3 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/AnyTypeTest.java @@ -0,0 +1,112 @@ +/** + * + * 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.InputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.URL; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XSDHelper; + +public class AnyTypeTest extends TestCase { + private static TypeHelper typeHelper; + private static DataFactory dataFactory; + private static XMLStreamHelper streamHelper; + private static XSDHelper xsdHelper; + + private static final String TEST_MODEL = "/anytype.xsd"; + private static final String TEST_NAMESPACE = "http://www.example.com/anytype"; + + public void testAnySimpleType() throws Exception { + Property property = typeHelper.getOpenContentProperty(TEST_NAMESPACE, "globalElement"); + Type propertyType = property.getType(); + + DataObject dataObject = dataFactory.create(TEST_NAMESPACE, "Person"); + dataObject.set("firstName", "Fuhwei"); + + DataObject rootObject = dataFactory.create(propertyType); + rootObject.set("anyTypeElement", dataObject); + + dataObject = dataFactory.create(TEST_NAMESPACE, "Person"); + dataObject.set("firstName", "Mindy"); + rootObject.set("personElement", dataObject); + + // XMLStreamHelper.saveObject has a problem to serialize the any type + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + streamHelper.saveObject(rootObject, streamWriter); + streamWriter.flush(); + // System.out.println(writer.toString()); + + XMLInputFactory inputFactory = XMLInputFactory.newInstance(); + StringReader reader = new StringReader(writer.toString()); + XMLStreamReader streamReader = inputFactory.createXMLStreamReader(reader); + XMLDocument doc = streamHelper.load(streamReader); + rootObject = doc.getRootObject(); + DataObject testObject = rootObject.getDataObject("anyTypeElement"); + // System.out.println("anyTypeElement dataobject: " + testObject); + testObject = rootObject.getDataObject("personElement"); + // System.out.println("personElement dataobject: " + testObject); + } + + public void testAbstractTypeFails() { + try { + DataObject abstractObj = dataFactory.create("commonj.sdo","DataObject"); + assertTrue("Should not succeed", false); + } + catch ( IllegalArgumentException e) { + // expected result + } + } + + protected void setUp() throws Exception { + HelperContext hc = SDOUtil.createHelperContext(); + typeHelper = hc.getTypeHelper(); + dataFactory = hc.getDataFactory(); + xsdHelper = hc.getXSDHelper(); + streamHelper = SDOUtil.createXMLStreamHelper(typeHelper); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + xsdHelper.define(inputStream, url.toString()); + inputStream.close(); + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryOnDataObjectTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryOnDataObjectTestCase.java new file mode 100644 index 0000000000..384244eb18 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryOnDataObjectTestCase.java @@ -0,0 +1,216 @@ +/** + * + * 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.InputStream; +import java.math.BigDecimal; +import java.net.URL; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XSDHelper; + + +public class ChangeSummaryOnDataObjectTestCase extends TestCase { + + + private final String TEST_DATA = "/simplechangesummary.xml"; + HelperContext hc; + XSDHelper xh; + TypeHelper th; + + public void testBasicsDO() { + Type cst = th.getType("commonj.sdo","ChangeSummaryType"); + Type strt = th.getType("commonj.sdo", "String"); + + Type newt = SDOUtil.createType(th, "testcases.changesummary", "simpleCS", false); + Property strProp = SDOUtil.createProperty(newt, "strElem", strt); + SDOUtil.createProperty(newt, "changeSummary", cst); + + DataObject iNewt = hc.getDataFactory().create(newt); + + testBasicsBody(strProp, iNewt); +} + + public void testBasicsDG() { + + Type strt = th.getType("commonj.sdo", "String"); + + + Type newt = SDOUtil.createType(th, "testcases.changesummary", "simpleNOCS", false); + Property strProp = SDOUtil.createProperty(newt, "strElem", strt); + + DataGraph graph = SDOUtil.createDataGraph(); + DataObject iNewt = graph.createRootObject(newt); + + testBasicsBody(strProp, iNewt); +} + + + /** + * @param strProp + * @param iNewt + */ + private void testBasicsBody(Property strProp, DataObject iNewt) { + ChangeSummary cs = iNewt.getChangeSummary(); + cs.beginLogging(); + + List co = cs.getChangedDataObjects(); + assertEquals(0, co.size()); + iNewt.set(strProp, "some text"); + assertEquals(0, co.size()); + co = cs.getChangedDataObjects(); + assertEquals(1, co.size()); + + List oldValues = cs.getOldValues((DataObject)co.get(0)); + + ChangeSummary.Setting ov1 = (ChangeSummary.Setting)oldValues.get(0); + Property p = ov1.getProperty(); + assertEquals("strElem", p.getName()); + Object v = ov1.getValue(); + assertEquals(null, v); + + assertTrue(cs.isLogging()); + cs.endLogging(); + assertFalse(cs.isLogging()); + + } + + + + public void testDynamicNestedDataObjectsDG() throws Exception { + DataGraph dataGraph = SDOUtil.createDataGraph(); + DataObject quote = dataGraph.createRootObject(th.getType("http://www.example.com/simple", "Quote")); + + testDynamicNestedDOBody(quote); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + SDOUtil.saveDataGraph(dataGraph, baos, null); + // SDOUtil.saveDataGraph(dataGraph, System.out, null); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA))); + + + assertEquals(1, quote.getList("quotes").size()); + assertEquals("fbnt", quote.getString("symbol")); + dataGraph.getChangeSummary().undoChanges(); + // SDOUtil.saveDataGraph(dataGraph, System.out, null); + assertEquals(0, quote.getList("quotes").size()); + assertNull(quote.getString("symbol")); + + + } + + public void testDynamicNestedDataObjectsDO() throws Exception { + Type quoteType = th.getType("http://www.example.com/simpleCS", "RootQuote"); + DataObject quote = hc.getDataFactory().create(quoteType); + + testDynamicNestedDOBody(quote); + //hc.getXMLHelper().save(quote, "http://www.example.com/simpleCS", "stockQuote", System.out); + assertEquals(1, quote.getList("quotes").size()); + assertEquals("fbnt", quote.getString("symbol")); + + quote.getChangeSummary().undoChanges(); + + assertEquals(0, quote.getList("quotes").size()); + assertNull(quote.getString("symbol")); + + + + } + + /** + * @param quote + */ + private void testDynamicNestedDOBody(DataObject quote) { + // Begin logging changes + // + ChangeSummary changeSummary = quote.getChangeSummary(); + assertNotNull(changeSummary); + assertFalse(changeSummary.isLogging()); + changeSummary.beginLogging(); + + + // Modify the data graph in various fun and interesting ways + // + quote.setString("symbol", "fbnt"); + quote.setString("companyName", "FlyByNightTechnology"); + quote.setBigDecimal("price", new BigDecimal("1000.0")); + quote.setBigDecimal("open1", new BigDecimal("1000.0")); + quote.setBigDecimal("high", new BigDecimal("1000.0")); + quote.setBigDecimal("low", new BigDecimal("1000.0")); + quote.setDouble("volume", 1000); + quote.setDouble("change1", 1000); + + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + changeSummary.endLogging(); + assertEquals(2, changeSummary.getChangedDataObjects().size()); // 2 DataObjects + assertTrue(changeSummary.getChangedDataObjects().contains(quote)); + assertTrue(changeSummary.getChangedDataObjects().contains(child)); + assertFalse(changeSummary.isCreated(quote)); + assertTrue(changeSummary.isCreated(child)); + + ChangeSummary.Setting ov = changeSummary.getOldValue(quote, quote.getType().getProperty("symbol")); + assertNull(ov.getValue()); + + + } + + protected void setUp() throws Exception { + super.setUp(); + +// uncomment these lines for sending aspect trace to a file +// tracing.lib.TraceMyClasses tmc = (TraceMyClasses)Aspects.aspectOf(TraceMyClasses.class); +// tmc.initStream(new PrintStream("c:\\temp\\trace.log")); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource("/simple.xsd"); + InputStream inputStream = url.openStream(); + hc = SDOUtil.createHelperContext(); + th = hc.getTypeHelper(); + xh = hc.getXSDHelper(); + xh.define(inputStream, url.toString()); + inputStream.close(); + + URL url2 = getClass().getResource("/simpleWithChangeSummary.xsd"); + InputStream inputStream2 = url2.openStream(); + xh.define(inputStream2, url2.toString()); + inputStream.close(); + + + +} + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java new file mode 100644 index 0000000000..4a62ec6135 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryPropertyTestCase.java @@ -0,0 +1,219 @@ +/** + * + * 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.net.URL; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +public class ChangeSummaryPropertyTestCase extends TestCase { + private final String TEST_MODEL = "/simpleWithChangeSummary.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/simpleCS"; + + private final String TEST_DATA_BEFORE_UNDO = "/simpleWithChangeSummary.xml"; + private final String TEST_DATA_AFTER_UNDO = "/simpleWithChangeSummaryUndone.xml"; + + HelperContext hc; + DataFactory dataFactory; + XMLHelper xmlHelper; + + void verify(ChangeSummary cs, DataObject quote, String nameSpace, String element, String beforeUndo, String afterUndo) throws IOException { + // Stop logging changes and serialize the resulting data graph + // + cs.endLogging(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + xmlHelper.save(quote, nameSpace, element, baos); + //xmlHelper.save(quote, nameSpace, element, System.out); + + byte[] bytes = baos.toByteArray(); + URL url = getClass().getResource(beforeUndo); + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(bytes), url)); + + DataObject loaded = xmlHelper.load(new ByteArrayInputStream(bytes)).getRootObject(); + baos.reset(); + xmlHelper.save(loaded, nameSpace, element, baos); + //xmlHelper.save(loaded, nameSpace, element, System.out); + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), url)); + + // Undo all changes and then serialize the resulting data graph again + // + cs.undoChanges(); + + baos.reset(); + xmlHelper.save(quote, nameSpace, element, baos); + //xmlHelper.save(quote, nameSpace, element, System.out); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(afterUndo))); + } + + /** + * ChangeSummary property test. + */ + protected final void change(String nameSpace, String beforeUndo, String afterUndo) throws Exception { + Type quoteType = hc.getTypeHelper().getType(nameSpace, "RootQuote"); + DataObject quote = dataFactory.create(quoteType); + + ChangeSummary cs = quote.getChangeSummary(); + ChangeSummary csp = (ChangeSummary)quote.get("changes"); + + assertSame(cs, csp); + + quote.setString("symbol", "fbnt"); + quote.setString("companyName", "FlyByNightTechnology"); + quote.setBigDecimal("price", new BigDecimal("1000.0")); + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("1500.0")); + child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + child = child.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.99")); + child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2500.0")); + + // Begin logging changes + // + cs.beginLogging(); + + // Modify the data graph in various ways + // + quote.setString("symbol", "FBNT"); + quote.setBigDecimal("price", new BigDecimal("999.0")); + quote.setDouble("volume", 1000); + + child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("3000.0")); + child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("4000.0")); + + quote.getDataObject("quotes[2]").delete(); + + verify(cs, quote, nameSpace, "stockQuote", beforeUndo, afterUndo); + } + + /** + * Simple ChangeSummary property test. + */ + public void testChangeSummary() throws Exception { + change(TEST_NAMESPACE, TEST_DATA_BEFORE_UNDO, TEST_DATA_AFTER_UNDO); + } + + static final String SequenceTest_NameSpace = "http://www.example.com/sequenceCS"; + + /** + * Mixed ChangeSummary property test. + */ + public void testMixedChangeSummary() throws Exception { + change(SequenceTest_NameSpace, "/mixedChangeSummary.xml", "/mixedChangeSummaryUndone.xml"); + } + + protected final DataObject createDataObject(Type quoteType, Object value, Sequence sequence, Property property) { + DataObject child = dataFactory.create(quoteType); + child.set("symbol", value); + sequence.add(property, child); + return child; + } + + static final String SequenceTest_ELEMENT = "openQuote"; + + private XSDHelper xsdHelper; + + /** + * Open/any ChangeSummary property test. + */ + public void testOpenChangeSummary() throws Exception { + DataObject quote = dataFactory.create(xsdHelper.getGlobalProperty(SequenceTest_NameSpace, SequenceTest_ELEMENT, true).getType()); + + ChangeSummary cs = quote.getChangeSummary(); + ChangeSummary csp = (ChangeSummary)quote.get("changes"); + + assertSame(cs, csp); + + quote.set("symbol", "fbnt"); + Property property = xsdHelper.getGlobalProperty("http://www.example.com/open", "openStockQuote", true); + Type quoteType = property.getType(); + List openStockQuotes = ((DataObject)quote).getList(property); + + addQuote(openStockQuotes, quoteType, "1500.0"); + DataObject osq2 = addQuote(openStockQuotes, quoteType, "2000.0"); + addQuote(osq2.getList(property), quoteType, "2000.99"); + addQuote(openStockQuotes, quoteType, "2500.0"); + + // Begin logging changes + // + cs.beginLogging(); + + // Modify the data graph in various ways + // + quote.set("symbol", "FBNT"); + + addQuote(openStockQuotes, quoteType, "3000.0"); + addQuote(openStockQuotes, quoteType, "4000.0"); + + openStockQuotes.remove(osq2); + + + verify(cs, quote, SequenceTest_NameSpace, SequenceTest_ELEMENT, "/openChangeSummary.xml", "/openChangeSummaryUndone.xml"); + } + + private DataObject addQuote(List openStockQuotes, Type quoteType, + Object value) { + DataObject osq = dataFactory.create(quoteType); + osq.set("symbol", value); + openStockQuotes.add(osq); + return osq; + } + + void define(String model) throws Exception { + // Populate the meta data for the test model + URL url = getClass().getResource(model); + xsdHelper.define(url.openStream(), url.toString()); + } + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + xsdHelper = hc.getXSDHelper(); + dataFactory = hc.getDataFactory(); + xmlHelper = hc.getXMLHelper(); + + // Populate the meta data for the test (Stock Quote) model + define(TEST_MODEL); + + define("/SequenceChangeSummary.xsd"); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryTestCase.java new file mode 100644 index 0000000000..e956720117 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/ChangeSummaryTestCase.java @@ -0,0 +1,92 @@ +/** + * + * 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.io.InputStream; +import java.math.BigDecimal; +import java.net.URL; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.helper.XSDHelper; + + +public class ChangeSummaryTestCase extends TestCase { + private final String TEST_MODEL = "/simple.xsd"; + private final String TEST_DATA = "/simplechangesummary.xml"; + + /** + * Simple ChangeSummary test. + */ + public void testChangeSummary() throws IOException { + // Create an empty data graph and add a root object, an instance of type Quote + // + + DataGraph dataGraph = SDOUtil.createDataGraph(); + DataObject quote = dataGraph.createRootObject("http://www.example.com/simple", "Quote"); + + // Begin logging changes + // + ChangeSummary changeSummary = dataGraph.getChangeSummary(); + changeSummary.beginLogging(); + + // Modify the data graph in various fun and interesting ways + // + quote.setString("symbol", "fbnt"); + quote.setString("companyName", "FlyByNightTechnology"); + quote.setBigDecimal("price", new BigDecimal("1000.0")); + quote.setBigDecimal("open1", new BigDecimal("1000.0")); + quote.setBigDecimal("high", new BigDecimal("1000.0")); + quote.setBigDecimal("low", new BigDecimal("1000.0")); + quote.setDouble("volume", 1000); + quote.setDouble("change1", 1000); + + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + // Stop logging changes and print the resulting data graph to stdout + // + changeSummary.endLogging(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + SDOUtil.saveDataGraph(dataGraph, baos, null); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA))); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java new file mode 100644 index 0000000000..2ec7e1b389 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java @@ -0,0 +1,474 @@ +/** + * + * 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.math.BigDecimal; +import java.net.URL; +import java.util.Iterator; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +public class CrossScopeCopyTestCase extends TestCase +{ + // Literals + private static final String TEST_NAMESPACE = "http://www.example.com/bank"; + private static final String BANK_MODEL = "/bank.xsd"; + private static final String BANK_TYPE = "bankType"; + private static final String BRANCH_TYPE = "branchType"; + private static final String SERVICE_TYPE = "serviceType"; + private static final String ACCOUNT_TYPE = "accountType"; + private static final String CUSTOMER_TYPE = "customerType"; + private static final String ADDRESS_TYPE = "addressType"; + private static final String DYNAMIC_TYPE = "dynamicType"; + + // SDO model objects + private HelperContext hca; + private HelperContext hcb; + private TypeHelper scopeA; + private TypeHelper scopeB; + + // SDO instance objects + private DataObject bankSDO; + private DataObject branchSDO1; + private DataObject branchSDO2; + private DataObject serviceSDO1; + private DataObject serviceSDO2; + private DataObject serviceSDO3; + private DataObject customerSDO1; + private DataObject customerSDO2; + private DataObject customerSDO3; + private DataObject customerSDO4; + + private int indent = 0; + + public void testCrossScopeCopy() throws IOException + { + CopyHelper copyHelperB = SDOUtil.createCrossScopeCopyHelper(scopeB); + + // Perform Shallow Copy Test + DataObject copiedSDO = copyHelperB.copyShallow(bankSDO); + shallowCopyAssertions(bankSDO, copiedSDO); + + // Perform Deep Copy Test + copiedSDO = copyHelperB.copy(bankSDO); + deepCopyAssertions(bankSDO, copiedSDO); + + // Inter-Reference Copy + copiedSDO = copyHelperB.copy(customerSDO1); + DataObject prop = (DataObject)copiedSDO.get("HomeBranch"); + assertTrue(prop==null); + + // Perform invalid namespace test + DataObject sdo = hca.getDataFactory().create(TEST_NAMESPACE, DYNAMIC_TYPE ); + sdo.set("custNum", "099" ); + sdo.set("firstName", "John"); + sdo.set("lastName", "Doe"); + boolean failed = false; + try + { + // In this case, we are copying an object to a scope + // where the object's type has not been defined. That + // will generate a null pointer exception what we will + // catch. + copyHelperB.copy(sdo); + } + catch(java.lang.NullPointerException ex) + { + failed = true; + } + assertTrue(failed); + } + + protected void setUp() throws Exception + { + super.setUp(); + + // Create Two Scopes + hca = SDOUtil.createHelperContext(); + hcb = SDOUtil.createHelperContext(); + scopeA = hca.getTypeHelper(); + scopeB = hcb.getTypeHelper(); + + // Populate scopes with bank model now + URL url = getClass().getResource(BANK_MODEL); + InputStream inputStream = url.openStream(); + hca.getXSDHelper().define(inputStream, url.toString()); + inputStream.close(); + inputStream = url.openStream(); + hcb.getXSDHelper().define(inputStream, url.toString()); + inputStream.close(); + + // Now Populate scopeA with some dynamic models + populateScopeWithDynamicTypes(scopeA); + + // Construct Source Tree + constructSourceTree(hca.getDataFactory()); + } + + private void shallowCopyAssertions(DataObject sdo, DataObject copiedSdo) + { + assertEquals(sdo.getType().getName(), copiedSdo.getType().getName()); + assertEquals(sdo.getType().getURI(), copiedSdo.getType().getURI()); + assertNotSame(sdo.getType(), copiedSdo.getType()); + assertEquals(sdo.getInstanceProperties().size(), copiedSdo + .getInstanceProperties().size()); + + for(Iterator it = sdo.getInstanceProperties().iterator(), it2 = copiedSdo + .getInstanceProperties().iterator(); it.hasNext();) + { + Property p1 = (Property) it.next(), p2 = (Property) it2.next(); + assertEquals(p1.getName(), p2.getName()); + Object o1 = sdo.get(p1), o2 = copiedSdo.get(p2); + if(p1.getType().isDataType()) + { + assertEquals(o1, o2); + // TODO is there a way I can distinguish between mutable and + // immutable types + // so that I can do some "same object" tests + } + else + { + assertNotSame(p1, p2); + if(p2.isMany()) + { + assertEquals(copiedSdo.getList(p2).size(), 0); + } + else + { + assertNull(copiedSdo.get(p2)); + } + } + try + { + sdo.get(p2); + assertTrue(false); + } + catch(Exception e) + { + // expected route + } + try + { + copiedSdo.get(p1); + assertTrue(false); + } + catch(Exception e2) + { + // expected route + } + } + } + + private void deepCopyAssertions(DataObject sdo, DataObject copiedSdo) + { + //indent(); + + //System.out.println("checking objects of types: " + // + sdo.getType().getName() + ", " + // + copiedSdo.getType().getName()); + indent++; + + assertEquals(sdo.getType().getName(), copiedSdo.getType().getName()); + assertEquals(sdo.getType().getURI(), copiedSdo.getType().getURI()); + assertNotSame(sdo.getType(), copiedSdo.getType()); + assertEquals(sdo.getInstanceProperties().size(), copiedSdo + .getInstanceProperties().size()); + + for(Iterator it = sdo.getInstanceProperties().iterator(), it2 = copiedSdo + .getInstanceProperties().iterator(); it.hasNext();) + { + Property p1 = (Property) it.next(), p2 = (Property) it2.next(); + assertEquals(p1.getName(), p2.getName()); + Object o1 = sdo.get(p1), o2 = copiedSdo.get(p2); + if(p1.getType().isDataType()) + { + assertEquals(o1, o2); + // TODO is there a way I can distinguish between mutable and + // immutable types + // so that I can do some "same object" tests + } + else + { + assertNotSame(p1, p2); + if(p2.isMany()) + { + assertEquals(sdo.getList(p1).size(), copiedSdo.getList(p2) + .size()); + for(Iterator it3 = sdo.getList(p1).iterator(), it4 = copiedSdo + .getList(p2).iterator(); it3.hasNext();) + { + deepCopyAssertions((DataObject) it3.next(), + (DataObject) it4.next()); + } + } + else + { + deepCopyAssertions(sdo.getDataObject(p1), copiedSdo + .getDataObject(p2)); + } + } + try + { + sdo.get(p2); + assertTrue(false); + } + catch(Exception e) + { + // expected route + } + try + { + copiedSdo.get(p1); + assertTrue(false); + } + catch(Exception e2) + { + // expected route + } + } + + indent--; + } + + /* + private void indent() + { + for(int i=0; i \n" + "\n" + " \n" + + " \n" + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + " \n" + " \n" + + " \n" + " \n" + "\n"; + + private final String DYNAMIC_TYPES_URI = "http://www.example.com/dynamicTypesFromSchemaSimple"; + + private final String SDO_FROM_API_AND_DYN = "object created with API with dynamic type "; + + private final String SDO_FROM_API_AND_REF = "object created with API with type from XSD"; + + private final String SDO_FROM_XML_AND_REF = "object created with XML with type from XSD"; + + private final String TEST_XML_DOC_0_STRING = "\n" + + "\n" + + " This is a repeated string.\n" + " This is a repeated string.\n" + + " true\n" + " false\n" + " true\n" + + " 1\n" + " 0\n" + " 0\n" + + " 12.5\n" + " 0\n" + "\n"; + + private void comparePropertyObjects(String propName, String name1, String name2, Object propObj1, Object propObj2) { + if (propObj1 == null) { + if (propObj2 != null) { + localFail("property " + propName + " for " + name1 + " was null but " + name2 + " was '" + propObj2 + + "'"); + } + } else if (propObj2 == null) { + localFail("property " + propName + " for " + name2 + " was null but " + name1 + " was '" + propObj1 + "'"); + } else { + if (!propObj1.equals(propObj2)) { + localFail("property " + propName + " for " + name1 + " was '" + propObj1 + "' but " + name2 + " was '" + + propObj2 + "'"); + } + } + } + + private void compareSequencesIgnoringWhitespace(String name1, String name2, Sequence sequence1, Sequence sequence2) { + if (sequence1 == null) { + if (sequence2 != null) { + localFail("sequence for " + name1 + " was null but " + name2 + " was not null"); + } + } else if (sequence2 == null) { + localFail("sequence for " + name2 + " was null but " + name1 + " was not null"); + } else { + List trimmedSeq1 = new ArrayList(); + List trimmedSeq2 = new ArrayList(); + trimWhitespaceFromSequence(trimmedSeq1, sequence1); + trimWhitespaceFromSequence(trimmedSeq2, sequence2); + int size1 = trimmedSeq1.size(); + int size2 = trimmedSeq2.size(); + if (size1 != size2) { + localFail("lengths of trimmed sequences differ: " + name1 + " was " + size1 + ", " + name2 + " was " + + size2); + } else { + Object[] objArr1 = trimmedSeq1.toArray(); + Object[] objArr2 = trimmedSeq2.toArray(); + for (int i = 0; i < size1; i++) { + Object obj1 = objArr1[i]; + Object obj2 = objArr2[i]; + if (obj1 == null) { + if (obj2 != null) { + localFail("sequence for " + name1 + " had null element for which " + name2 + " had '" + obj2 + + "'"); + } + } else if (obj2 == null) { + localFail("sequence for " + name2 + " had null element for which " + name1 + " had '" + obj1 + "'"); + } else { + if (!obj1.equals(obj2)) { + localFail("sequences did not match: " + name1 + " had '" + obj1 + "' but " + name2 + " had '" + + obj2 + "'"); + } + } + } + } + } + } + + private void getAndCompareProperties(String propName, DataObject xmlAndRefTypDO, DataObject apiAndDynTypDO, + DataObject apiAndRefTypDO) { + Object xmlRefProperty = xmlAndRefTypDO.get(propName); + Object apiDynProperty = apiAndDynTypDO.get(propName); + Object apiRefProperty = apiAndRefTypDO.get(propName); + comparePropertyObjects(propName, SDO_FROM_XML_AND_REF, SDO_FROM_API_AND_DYN, xmlRefProperty, apiDynProperty); + comparePropertyObjects(propName, SDO_FROM_XML_AND_REF, SDO_FROM_API_AND_REF, xmlRefProperty, apiRefProperty); + } + + private void localFail(String message) { + // System.err.println(message); + fail(message); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + private void specifyProperty(DataObject containingTypeDO, String nameString, Type typ, boolean isMany) { + DataObject subordinateProperty = containingTypeDO.createDataObject("property"); + subordinateProperty.set("name", nameString); + subordinateProperty.set("type", typ); + subordinateProperty.setBoolean("many", isMany); + } + + /** + * test #0 of Data Object primitive datatypes + */ + public void testDynamicTypesGroup0DO() throws IOException { + HelperContext hcDO = SDOUtil.createHelperContext(); + + TypeHelper thDO = hcDO.getTypeHelper(); + DataFactory dfDO = hcDO.getDataFactory(); + + // create a container object type + DataObject containerTypeDO = dfDO.create("commonj.sdo", "Type"); + containerTypeDO.set("uri", DYNAMIC_TYPES_URI); + containerTypeDO.set("name", DYNAMIC_ROOT_TYPE_0); + containerTypeDO.set("sequenced", Boolean.TRUE); + + specifyProperty(containerTypeDO, "aString", thDO.getType(COMMONJ_SDO, "String"), true); + specifyProperty(containerTypeDO, "aBoolean", thDO.getType(COMMONJ_SDO, "Boolean"), true); + specifyProperty(containerTypeDO, "aFloat", thDO.getType(COMMONJ_SDO, "Float"), true); + + Type containerType = thDO.define(containerTypeDO); + assertNotNull(containerType); + + DataObject doFromApiAndDynTyp = dfDO.create(containerType); + assertNotNull(doFromApiAndDynTyp); + doFromApiAndDynTyp.getList("aString").add("This is a repeated string."); + doFromApiAndDynTyp.getList("aString").add("This is a repeated string."); + doFromApiAndDynTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndDynTyp.getList("aBoolean").add(new Boolean(false)); + doFromApiAndDynTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndDynTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndDynTyp.getList("aBoolean").add(new Boolean(false)); + doFromApiAndDynTyp.getList("aFloat").add(new Float(0)); + doFromApiAndDynTyp.getList("aFloat").add(new Float(12.5)); + doFromApiAndDynTyp.getList("aFloat").add(new Float(0)); + + Type rootType = thDO.getType(DYNAMIC_TYPES_URI, DYNAMIC_ROOT_TYPE_0); + assertNotNull(rootType); + assertSame(containerType, rootType); + + // now load xml to get a reference data object using schema + HelperContext hcRef = SDOUtil.createHelperContext(); + XSDHelper xsdHelper = hcRef.getXSDHelper(); + List typeList = xsdHelper.define(DYNAMIC_TYPES_SCHEMA_STRING); + assertNotNull(typeList); + TypeHelper thRef = hcRef.getTypeHelper(); + Type rootTypeRef = thRef.getType(DYNAMIC_TYPES_URI, DYNAMIC_ROOT_TYPE_0); + + assertNotNull(rootTypeRef); + + XMLHelper xhRef = hcRef.getXMLHelper(); + // XMLDocument docRef = + // xhRef.load(getClass().getResourceAsStream(TEST_XML_DOC_0)); + XMLDocument docRef = xhRef.load(TEST_XML_DOC_0_STRING); + DataObject doFromXmlAndRefTyp = docRef.getRootObject(); + + assertNotNull(doFromXmlAndRefTyp); + + // create a data object using dynamic API from reference type + DataFactory dfRef = hcRef.getDataFactory(); + DataObject doFromApiAndRefTyp = dfRef.create(rootTypeRef); + assertNotNull(doFromApiAndRefTyp); + doFromApiAndRefTyp.getList("aString").add("This is a repeated string."); + doFromApiAndRefTyp.getList("aString").add("This is a repeated string."); + doFromApiAndRefTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndRefTyp.getList("aBoolean").add(new Boolean(false)); + doFromApiAndRefTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndRefTyp.getList("aBoolean").add(new Boolean(true)); + doFromApiAndRefTyp.getList("aBoolean").add(new Boolean(false)); + doFromApiAndRefTyp.getList("aFloat").add(new Float(0)); + doFromApiAndRefTyp.getList("aFloat").add(new Float(12.5)); + doFromApiAndRefTyp.getList("aFloat").add(new Float(0)); + + getAndCompareProperties("aString", doFromXmlAndRefTyp, doFromApiAndDynTyp, doFromApiAndRefTyp); + getAndCompareProperties("aBoolean", doFromXmlAndRefTyp, doFromApiAndDynTyp, doFromApiAndRefTyp); + getAndCompareProperties("aFloat", doFromXmlAndRefTyp, doFromApiAndDynTyp, doFromApiAndRefTyp); + Sequence seqFromXmlAndRefTyp = doFromXmlAndRefTyp.getSequence(); + Sequence seqFromApiAndDynTyp = doFromApiAndDynTyp.getSequence(); + Sequence seqFromApiAndRefTyp = doFromApiAndRefTyp.getSequence(); + compareSequencesIgnoringWhitespace(SDO_FROM_XML_AND_REF, SDO_FROM_API_AND_DYN, seqFromXmlAndRefTyp, + seqFromApiAndDynTyp); + compareSequencesIgnoringWhitespace(SDO_FROM_XML_AND_REF, SDO_FROM_API_AND_REF, seqFromXmlAndRefTyp, + seqFromApiAndRefTyp); + } + + private void trimWhitespaceFromSequence(List trimmedSeq, Sequence sequence) { + if (sequence != null) { + for (int i = 0; i < sequence.size(); i++) { + Object obj = sequence.getValue(i); + if (obj instanceof String) { + String str = ((String) obj).trim(); + if (str.length() > 0) { + trimmedSeq.add(str); + } + } else { + trimmedSeq.add(obj); + } + } + } + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java new file mode 100644 index 0000000000..e630a8f903 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/FormTestCase.java @@ -0,0 +1,112 @@ +/** + * + * 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.StringReader; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; +import org.eclipse.emf.ecore.resource.Resource; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLHelper; + +public class FormTestCase extends TestCase { + XMLHelper xmlHelper; + Map options; + + protected void setUp() throws Exception { + super.setUp(); + HelperContext hc = SDOUtil.createHelperContext(); + hc.getXSDHelper().define( +""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ + ""+ +""); + xmlHelper = hc.getXMLHelper(); + options = new HashMap(); + /* + * turn off default behaviour of tolerating malformed xml + * tests using this option and bad xml should demonstrate failure to load + */ + options.put(SDOUtil.XML_LOAD_LaxForm, new Integer(0)); + } + + void load(String xml, Object options) throws IOException { + assertNotNull(xmlHelper.load(new StringReader(xml), null, options).getRootObject()); + } + + /* + * this malformed xml will load, as it uses the default lax approach + */ + public void testLaxForm() throws IOException { + load( +""+ + ""+ + ""+ +"", null); + } + + /* + * any malformed xml through this method will fail to load + * since the options instance member disables lax processing + */ + protected final void load(String xml) throws IOException { + try { + load(xml, options); + fail(); + } catch (Resource.IOWrappedException featureNotFound) { + } + } + + public void testUnqualifiedAttribute() throws IOException { + load(""); + } + + public void testQualifiedAttribute() throws IOException { + load(""); + } + + public void testUnqualifiedElement() throws IOException { + load( +""+ + ""+ +""); + } + + public void testQualifiedElement() throws IOException { + load( +""+ + ""+ +""); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/HelperContextTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/HelperContextTestCase.java new file mode 100644 index 0000000000..14e81b6a32 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/HelperContextTestCase.java @@ -0,0 +1,75 @@ +/** + * + * 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 org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.DataHelper; +import commonj.sdo.helper.EqualityHelper; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import commonj.sdo.impl.HelperProvider; + +public class HelperContextTestCase extends TestCase { + + public void testDefaultHelperContext() { + HelperContext defaultHelperContext = HelperProvider.getDefaultContext(); + + assertNotNull(defaultHelperContext.getCopyHelper()); + assertEquals(defaultHelperContext.getCopyHelper(), CopyHelper.INSTANCE); + assertNotNull(defaultHelperContext.getDataFactory()); + assertEquals(defaultHelperContext.getDataFactory(), DataFactory.INSTANCE); + assertNotNull(defaultHelperContext.getDataHelper()); + assertEquals(defaultHelperContext.getDataHelper(), DataHelper.INSTANCE); + assertNotNull(defaultHelperContext.getEqualityHelper()); + assertEquals(defaultHelperContext.getEqualityHelper(), EqualityHelper.INSTANCE); + assertNotNull(defaultHelperContext.getTypeHelper()); + assertEquals(defaultHelperContext.getTypeHelper(), TypeHelper.INSTANCE); + assertNotNull(defaultHelperContext.getXMLHelper()); + assertEquals(defaultHelperContext.getXMLHelper(), XMLHelper.INSTANCE); + assertNotNull(defaultHelperContext.getXSDHelper()); + assertEquals(defaultHelperContext.getXSDHelper(), XSDHelper.INSTANCE); + + } + + public void testCreateHelperContext() { + HelperContext hc = SDOUtil.createHelperContext(); + assertNotNull(hc.getCopyHelper()); + assertNotNull(hc.getDataFactory()); + assertNotNull(hc.getDataHelper()); + assertNotNull(hc.getEqualityHelper()); + assertNotNull(hc.getTypeHelper()); + assertNotNull(hc.getXMLHelper()); + assertNotNull(hc.getXSDHelper()); + + assertNotSame(hc.getDataFactory(), DataFactory.INSTANCE); + assertNotSame(hc.getTypeHelper(), TypeHelper.INSTANCE); + assertNotSame(hc.getXMLHelper(), XMLHelper.INSTANCE); + assertNotSame(hc.getXSDHelper(), XSDHelper.INSTANCE); + // the other "stateless" helpers may be the same as the singletons + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/IsManyTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/IsManyTestCase.java new file mode 100644 index 0000000000..1eef4ea657 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/IsManyTestCase.java @@ -0,0 +1,65 @@ +/** + * + * 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.InputStream; +import java.net.URL; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import junit.framework.TestCase; + +import commonj.sdo.*; +import commonj.sdo.helper.*; + +public class IsManyTestCase extends TestCase { + private final String TEST_MODEL = "/open.xsd"; + private final String TEST_MODEL1ANY = "/open1any.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/open"; + private final String TEST_NAMESPACE1ANY = "http://www.example.com/open1any"; + + public void testIsMany() { + Property priceProperty = XSDHelper.INSTANCE.getGlobalProperty(TEST_NAMESPACE, "price", true); + + Type quoteType = TypeHelper.INSTANCE.getType(TEST_NAMESPACE, "OpenQuote"); + DataObject quote = DataFactory.INSTANCE.create(quoteType); + assertTrue( SDOUtil.isMany(priceProperty, quote)); + + quoteType = TypeHelper.INSTANCE.getType(TEST_NAMESPACE1ANY, "OpenQuote"); + quote = DataFactory.INSTANCE.create(quoteType); + assertFalse( SDOUtil.isMany(priceProperty, quote)); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + + // Populate the meta data for the test (Stock Quote) model with maxOccurs=1 + url = getClass().getResource(TEST_MODEL1ANY); + inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } +} \ No newline at end of file diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JavaSerializeDeserializeTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JavaSerializeDeserializeTestCase.java new file mode 100644 index 0000000000..56a293cde0 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JavaSerializeDeserializeTestCase.java @@ -0,0 +1,297 @@ +/** + * + * 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.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +public class JavaSerializeDeserializeTestCase extends TestCase +{ + + public void testScopeDefinedSerializeDeserializeOfDataObject() + { + HelperContext hc = SDOUtil.createHelperContext(); + Object originalDataObject = createDynamically(hc,true); + + runSerializeDeserialize((DataObject)originalDataObject, hc); + } + + public void testScopeDefinedSerializeDeserializeOfDataGraph() + { + HelperContext hc = SDOUtil.createHelperContext(); + DataGraph testDO = (DataGraph)createDynamically(hc,false); + + runSerializeDeserializeWithDataGraph(testDO, hc); + } + + private String xsdString = "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + "" + + + "" + + "" + + "" + + "" + + "" + + "" + + ""; + + public void testLargePayload() + { + HelperContext hc = SDOUtil.createHelperContext(); + hc.getXSDHelper().define(xsdString); + DataObject company = hc.getDataFactory().create("http://www.example.com/simple", "Company"); + company.setString("symbol", "EXAMPLE"); + company.setString("companyName", "Example Inc."); + List employees = company.getList("employees"); + DataObject employee; + for (int i=0; i<1000; i++) { + employee = hc.getDataFactory().create("http://www.example.com/simple", "Employee"); + employee.setString("employeeID", "ID #" + i); + employee.setString("employeeName", "Empoyee #" + i); + employees.add(employee); + } + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = SDOUtil.createObjectOutputStream(bos, hc); + oos.writeObject(company); + oos.flush(); + byte[] bytes = bos.toByteArray(); + oos.close(); + bos.close(); + + ByteArrayInputStream bis = new ByteArrayInputStream(bytes); + ObjectInputStream ois = SDOUtil.createObjectInputStream(bis, hc); + ois.readObject(); + ois.close(); + bis.close(); + } + catch (Exception e) { + e.printStackTrace(); + fail("An Exception occurred while deserializing the output of the serialization: " + e.toString()); + } + } + + + /** + * Serialize the DataObject then Deserialize the output. + * to testDO. + * @param testDO + * @param scope + */ + + public void runSerializeDeserialize(DataObject originalDataObject, HelperContext hc) + { + + populateFields(originalDataObject); + DataObject tempDO = null; + ByteArrayOutputStream baos = null; + + try + { + baos = serialize(originalDataObject, hc); + + } + catch (Exception e) + { + e.printStackTrace(); + fail("An Exception occurred while serializing the DataObject: " + e.toString()); + } + + try + { + tempDO = deserialize(baos, hc); + + } + catch (Exception e) + { + e.printStackTrace(); + fail("An Exception occurred while deserializing the output of the serialization: " + e.toString()); + } + + assertNotNull("Deserialization returned a null value.", tempDO); + + assertSame(tempDO.getType(), originalDataObject.getType()); + + + } + + /** + * Serialize the DataGraph + * @param dataGraph + * @param scope + */ + public void runSerializeDeserializeWithDataGraph(DataGraph dataGraph, HelperContext hc) + { + DataObject originalDataObject = dataGraph.getRootObject(); + populateFields(originalDataObject); + DataObject tempDO = null; + ByteArrayOutputStream baos = null; + + try + { + baos = serialize(dataGraph, hc); + + } + catch (Exception e) + { + e.printStackTrace(); + fail("An Exception occurred while serializing the DataObject: " + e.toString()); + } + + try + { + tempDO = deserialize(baos, hc); + + } + catch (Exception e) + { + e.printStackTrace(); + fail("An Exception occurred while deserializing the output of the serialization: " + e.toString()); + } + + assertNotNull("Deserialization returned a null value.", tempDO); + + assertSame(tempDO.getType(), originalDataObject.getType()); + + + + } + + /** + * serializeDataObject is a private method to be called by the other methods + * in the ScrenarioLibrary + * + * @param dataObject + * @param fileName + * @throws IOException + */ + public ByteArrayOutputStream serialize(Object object, HelperContext hc) throws IOException + { + //FileOutputStream fos = new FileOutputStream("temp"); + ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream(); + ObjectOutputStream out = SDOUtil.createObjectOutputStream(byteArrayOutput, hc); + out.writeObject(object); + out.close(); + return byteArrayOutput; + } + + /** + * deserializeDataObject is a private method to be called by the other + * methods in the ScrenarioLibrary + * + * @param fileName + * @return + * @throws IOException + * @throws ClassNotFoundException + */ + public DataObject deserialize(ByteArrayOutputStream baos, HelperContext hc) throws IOException, ClassNotFoundException + { + //FileInputStream fis = new FileInputStream("temp"); + ObjectInputStream input = null; + ByteArrayInputStream byteArrayInput = new ByteArrayInputStream(baos.toByteArray()); + + input = SDOUtil.createObjectInputStream(byteArrayInput, hc); + + Object object = input.readObject(); + input.close(); + if(object instanceof DataGraph) + return ((DataGraph)object).getRootObject(); + else + return (DataObject)object; + } + + /** + * populateFields uses set to set each of the fields in the + * DataObject. It is used to ensure a known set of expected values that are + * not other than the default values for the various fields. + * + * @param testDO + * @throws ExpectedConditionError + */ + public static void populateFields(DataObject testDO) + { + + testDO.setString("stringVal", "Testing"); + + } + /** + * createDynamically() creates the SDO Types using the TypeHelper. This method should be kept in + * synch with the XSD used for createDynamicallyWithStaticResources. The same XSD is used for + * the static generation of SDO Types using XSD2JavaGenerator. + */ + public Object createDynamically(HelperContext hc, boolean createDataObject) + { + + TypeHelper types = hc.getTypeHelper(); + DataFactory dataFactory = hc.getDataFactory(); + + Type stringType = types.getType("commonj.sdo", "String"); + + DataObject testType = dataFactory.create("commonj.sdo", "Type"); + testType.set("uri", "http://www.example.com/api_test"); + testType.set("name", "APITest"); + + DataObject stringProperty = testType.createDataObject("property"); + stringProperty.set("name", "stringVal"); + stringProperty.set("type", stringType); + + + List types2Define = new ArrayList(); + types2Define.add(testType); + List apiXSD = types.define(types2Define); + Type apiXSDType = (Type) apiXSD.get(0); + + if(createDataObject) + return dataFactory.create(apiXSDType);; + + // Create an empty DataGraph and attach the document root to it. Otherwise, where is the documentRoot ? + DataGraph dataGraph = SDOUtil.createDataGraph(); + /*DataObject testDO =*/ dataGraph.createRootObject(apiXSDType); + + + return dataGraph; + + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java new file mode 100644 index 0000000000..66138495ce --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java @@ -0,0 +1,184 @@ +/** + * + * 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.util.List; + +import junit.framework.TestCase; +import commonj.sdo.*; +import commonj.sdo.helper.*; + +import org.apache.tuscany.sdo.util.SDOUtil; +import java.util.HashMap; + +/** + * The follow test cases are based upon code snipets in the SDO specification. + * They are also implemented as working samples in the sample-sdo artifact + * + * @author Robbie Minshall + * + */ +public class JiraTestCases extends TestCase { + + /** + * Bogus company namespace + */ + public static final String COMPANY_NAMESPACE = "company.xsd"; + + /** + * previously defined XSD file used + */ + public static final String COMPANY_XSD = "/company.xsd"; + + /** + * XML file containing DataGraph representing a company. This xml file + * conforms to the company model defined in 'Complete DataGraph for Company + * Example' section of the SDO specification + */ + public static final String COMPANY_DATAGRAPH_XML = "/companyDataGraphGenerated.xml"; + + /** + * Generated DataGraph + * {@link org.apache.tuscany.samples.sdo.company.CreateCompany} + */ + public static final String COMPANY_DATAOBJECT_XML = "/companyGenerated.xml"; + + /** + * Defines xsd resource contained within jar file for PurchaseOrder + * DataObject + */ + public static final String PO_XSD_RESOURCE = "/po.xsd"; + + /** + * Defines xml resource contained within jar file that is used to populate + * PurchaseOrder DataObjects + */ + public static final String PO_XML_RESOURCE = "/po.xml"; + + /** + * previously created XSD file used + */ + public static final String LETTER_XSD = "/letter.xsd"; + + /** + * JIRA Details : Add a method to SDOUtil to return all Types associated + * with a specific URI + */ + public void test_TUSCANY583() { + + // define some types + try { + XSDHelper.INSTANCE.define(getClass().getResourceAsStream( + PO_XSD_RESOURCE), null); + + XSDHelper.INSTANCE.define(getClass().getResourceAsStream( + COMPANY_XSD), null); + + createDynamicType(); + + String[] expectedPoTypeNames = {"item", "Items", + "PurchaseOrderType", "quantity", "SKU", "USAddress"}; + + String[] expectedCompanyTypeNames = {"EmployeeType", "DepartmentType", "CompanyType"}; + + String[] expectedCustomerTypeNames = {"Customer"}; + + confirmTypes("http://www.example.com/PO", expectedPoTypeNames); + confirmTypes("company.xsd", expectedCompanyTypeNames); + confirmTypes("http://example.com/customer", + expectedCustomerTypeNames); + + } catch (Exception e) { + fail("Unexpected error " + e.toString()); + e.printStackTrace(); + } + + } + + /** + * Dynamically define customer Type + */ + public static void createDynamicType() { + // get an instance of the type helper + TypeHelper typeH = TypeHelper.INSTANCE; + Type intType = typeH.getType("commonj.sdo", "Int"); + Type stringType = typeH.getType("commonj.sdo", "String"); + + // create a new Type for Customers + DataObject customerType = DataFactory.INSTANCE.create("commonj.sdo", + "Type"); + + customerType.set("uri", "http://example.com/customer"); + customerType.set("name", "Customer"); + + // create a customer number property + DataObject custNumProperty = customerType.createDataObject("property"); + custNumProperty.set("name", "custNum"); + custNumProperty.set("type", intType); + + // create a last name property + DataObject lastNameProperty = customerType.createDataObject("property"); + lastNameProperty.set("name", "lastName"); + lastNameProperty.set("type", stringType); + + // create a first name property + DataObject firstNameProperty = customerType + .createDataObject("property"); + firstNameProperty.set("name", "firstName"); + firstNameProperty.set("type", stringType); + + // now define the Customer type so that customers can be made + typeH.define(customerType); + } + + private void confirmTypes(String uri, String[] expectedTypeNames) { + + try { + + List actualTypes = SDOUtil.getTypes(TypeHelper.INSTANCE, uri); + assertNotNull("Testing that list of types for " + uri + + " is not null", actualTypes); + + // test the number of Types + assertEquals("Testing number of types for " + uri, + expectedTypeNames.length, actualTypes.size()); + + // put into a HashMap for easy lookup + HashMap typeLookup = new HashMap(); + for (int i = 0; i < actualTypes.size(); i++) { + Type type = (Type) actualTypes.get(i); + typeLookup.put(type.getName(), type); + } + + // check that we have the same types + for (int i = 0; i < expectedTypeNames.length; i++) { + assertTrue("testing that type " + expectedTypeNames[i] + + "was returned for " + uri, typeLookup + .containsKey(expectedTypeNames[i])); + } + + } catch (Exception e) { + fail("Exception caught comparing expected types to actual types for uri " + + uri + ":" + e.toString()); + e.printStackTrace(); + } + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MetadataInstancePropertiesTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MetadataInstancePropertiesTestCase.java new file mode 100644 index 0000000000..fa3e62946b --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MetadataInstancePropertiesTestCase.java @@ -0,0 +1,219 @@ +/** + * + * 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.InputStream; +import java.net.URL; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XSDHelper; + + +public class MetadataInstancePropertiesTestCase extends TestCase +{ + private final String TEST_MODEL = "/TypePropertyMetadataInfo.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/metadata/retrievaltest"; + private final String DYNAMIC_TEST_NAMESPACE = "http://www.example.com/metadata/dynamicmetadata"; + + private TypeHelper typeHelper; + private XSDHelper xsdHelper; + private DataFactory dataFactory; + + public void testXSDTypeProperties() + { + // Note: We won't check the actual value of XSD instance properties yet since they're not defined + // in the SDO spec yet. + + //displayInstanceProperties(TEST_NAMESPACE); + + Type type = typeHelper.getType(TEST_NAMESPACE, "Example"); + assertTrue(getInstanceProperty(type, "appinfo") != null); + assertTrue(getInstanceProperty(type, "foo") == null); + + Property property = type.getProperty("number"); + assertTrue(getInstanceProperty(property, "appinfo") != null); + assertTrue(getInstanceProperty(property, "foo") == null); + + property = type.getProperty("Exampletype"); + assertTrue(getInstanceProperty(property, "appinfo") != null); + assertTrue(getInstanceProperty(property, "foo") == null); + + type = typeHelper.getType(TEST_NAMESPACE, "ExampleNumber"); + assertTrue(getInstanceProperty(type, "appinfo") != null); + assertTrue(getInstanceProperty(type, "minInclusive") != null); + assertTrue(getInstanceProperty(type, "maxInclusive") != null); + assertTrue(getInstanceProperty(type, "foo") == null); + + type = typeHelper.getType(TEST_NAMESPACE, "ExampleRating"); + assertTrue(getInstanceProperty(type, "enumeration") != null); + assertTrue(getInstanceProperty(type, "foo") == null); + + type = typeHelper.getType(TEST_NAMESPACE, "PhoneNumber"); + assertTrue(getInstanceProperty(type, "appinfo") != null); + assertTrue(getInstanceProperty(type, "length") != null); + assertTrue(getInstanceProperty(type, "pattern") != null); + assertTrue(getInstanceProperty(type, "foo") == null); + } + + public void testDynamicTypeProperties() + { + // Create dynamic model + Type intType = typeHelper.getType("commonj.sdo", "Int"); + Type stringType = typeHelper.getType("commonj.sdo", "String"); + + Property openContentProperty = typeHelper.getOpenContentProperty("commonj.sdo/xml", "xmlElement"); + + // create a new Type for Customers + DataObject customerType = dataFactory.create("commonj.sdo", "Type"); + customerType.set("uri", DYNAMIC_TEST_NAMESPACE); + customerType.set("name", "Customer"); + customerType.set(openContentProperty, Boolean.TRUE); + + // create a customer number property + DataObject custNumProperty = customerType.createDataObject("property"); + custNumProperty.set("name", "custNum"); + custNumProperty.set("type", intType); + + // create a first name property + DataObject firstNameProperty = + customerType.createDataObject("property"); + firstNameProperty.set("name", "firstName"); + firstNameProperty.set("type", stringType); + firstNameProperty.set(openContentProperty, Boolean.FALSE); + + // create a last name property + DataObject lastNameProperty = customerType.createDataObject("property"); + lastNameProperty.set("name", "lastName"); + lastNameProperty.set("type", stringType); + + // now define the Customer type so that customers can be made + typeHelper.define(customerType); + + //displayInstanceProperties(DYNAMIC_TEST_NAMESPACE); + + Type type = typeHelper.getType(DYNAMIC_TEST_NAMESPACE, "Customer"); + assertTrue(getInstanceProperty(type, "xmlElement") == openContentProperty); + assertTrue(type.get(openContentProperty) == Boolean.TRUE); + assertTrue(getInstanceProperty(type, "foo") == null); + + Property property = type.getProperty("custNum"); + assertTrue(getInstanceProperty(property, "xmlElement") == null); + assertTrue(getInstanceProperty(property, "foo") == null); + + property = type.getProperty("firstName"); + assertTrue(getInstanceProperty(property, "xmlElement") == openContentProperty); + assertTrue(property.get(openContentProperty) == Boolean.FALSE); + assertTrue(getInstanceProperty(property, "foo") == null); + + property = type.getProperty("lastName"); + assertTrue(getInstanceProperty(property, "xmlElement") == null); + assertTrue(getInstanceProperty(property, "foo") == null); + } + + public void setUp() throws Exception + { + super.setUp(); + + HelperContext helperContext = SDOUtil.createHelperContext(); + + typeHelper = helperContext.getTypeHelper(); + xsdHelper = helperContext.getXSDHelper(); + dataFactory = helperContext.getDataFactory(); + + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + + xsdHelper.define(inputStream, url.toString()); + inputStream.close(); + } + + public Property getInstanceProperty(Type type, String propertyName) + { + return getInstanceProperty(type.getInstanceProperties(), propertyName); + } + + public Property getInstanceProperty(Property property, String propertyName) + { + return getInstanceProperty(property.getInstanceProperties(), propertyName); + } + + private Property getInstanceProperty(List instProps, String propertyName) + { + for (int i = 0; i < instProps.size(); i++) + { + Property prop = (Property)instProps.get(i); + if (propertyName.equals(prop.getName())) + return prop; + } + return null; + } + + private void displayInstanceProperties(Type type) + { + System.out.println("Type's name: " + type.getName()); + List instProps = type.getInstanceProperties(); + for (int i = 0; i < instProps.size(); i++) + { + Property prop = (Property)instProps.get(i); + System.out.println("\tType's instance property name: " + prop.getName()); + System.out.println("\tType's instance property value: \"" + type.get(prop) + "\""); + } + } + + private void displayInstanceProperties(Property prop) + { + System.out.println("\tProperty's name: " + prop.getName()); + List propsInstProps = prop.getInstanceProperties(); + for (int j = 0; j < propsInstProps.size(); j++) + { + Property propsInstProp = (Property)propsInstProps.get(j); + System.out.println("\t\tProperty's instance property name: " + propsInstProp.getName()); + System.out.println("\t\tProperty's instance property value: \"" + prop.get(propsInstProp) + "\""); + } + } + + public void displayInstanceProperties(String namespace) + { + List types = SDOUtil.getTypes(typeHelper, namespace); + for (int i = 0; i < types.size(); i++) + { + Type type = (Type)types.get(i); + displayInstanceProperties(type); + List properties = type.getProperties(); + for (int j = 0; j < properties.size(); j++) + { + Property property = (Property)properties.get(j); + displayInstanceProperties(property); + } + System.out.println("**************************************"); + } + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MixedTypeTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MixedTypeTestCase.java new file mode 100644 index 0000000000..806662a83c --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/MixedTypeTestCase.java @@ -0,0 +1,89 @@ +/** + * + * 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.io.InputStream; +import java.math.BigDecimal; +import java.net.URL; + +import commonj.sdo.DataObject; +import commonj.sdo.Sequence; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import junit.framework.TestCase; + + +public class MixedTypeTestCase extends TestCase { + private final String TEST_MODEL = "/mixed.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/mixed"; + private final String TEST_DATA = "/mixed.xml"; + + /** + * Sequenced type SDO 2 test. + */ + public void testSequencedType() throws IOException { + Type quoteType = TypeHelper.INSTANCE.getType(TEST_NAMESPACE, "MixedQuote"); + DataObject quote = DataFactory.INSTANCE.create(quoteType); + + Sequence sequence = quote.getSequence(); + + sequence.add("\n "); + + quote.setString("symbol", "fbnt"); + + sequence.add("\n "); + + quote.setString("companyName", "FlyByNightTechnology"); + + sequence.add("\n some text\n "); + + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + sequence.add("\n more text\n "); + + // quote.setBigDecimal("price", new BigDecimal("1000.0")); + sequence.add("price", new BigDecimal("1000.0")); + + sequence.add("\n"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + XMLHelper.INSTANCE.save(quote, TEST_NAMESPACE, "mixedStockQuote", baos); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA))); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/NeverStaleChangeSummaryTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/NeverStaleChangeSummaryTestCase.java new file mode 100644 index 0000000000..69c8e447df --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/NeverStaleChangeSummaryTestCase.java @@ -0,0 +1,76 @@ +/** + * + * 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.InputStream; +import java.net.URL; + +import junit.framework.TestCase; + +import commonj.sdo.ChangeSummary; +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XSDHelper; + +public class NeverStaleChangeSummaryTestCase extends TestCase { + private final String TEST_MODEL = "/simpleWithChangeSummary.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/simpleCS"; + + /** + * Simple ChangeSummary test. + */ + public void testChangeSummary() throws Exception { + Type quoteType = TypeHelper.INSTANCE.getType(TEST_NAMESPACE, "RootQuote"); + DataObject quote = DataFactory.INSTANCE.create(quoteType); + + ChangeSummary cs = quote.getChangeSummary(); + + // Begin logging changes + // + cs.beginLogging(); + + DataObject child = quote.createDataObject("quotes"); + cs.isCreated(child); + child = quote.createDataObject("quotes"); + assertTrue(cs.isCreated(child)); + + // Stop logging changes and print the resulting data graph to stdout + // + cs.endLogging(); + + cs.isCreated(child); + + cs.undoChanges(); + + assertFalse(cs.isCreated(child)); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } +} \ No newline at end of file diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/OpenTypeTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/OpenTypeTestCase.java new file mode 100644 index 0000000000..74570d3b43 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/OpenTypeTestCase.java @@ -0,0 +1,110 @@ +/** + * + * 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.io.InputStream; +import java.math.BigDecimal; +import java.net.URL; +import java.util.Iterator; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Property; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import junit.framework.TestCase; + + +public class OpenTypeTestCase extends TestCase { + private HelperContext hc; + private TypeHelper th; + private XSDHelper xsdh; + private XMLHelper xmlh; + private DataFactory df; + private final String TEST_MODEL = "/open.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/open"; + private final String TEST_DATA = "/open.xml"; + + /** + * Open content SDO 2 test. + */ + public void testOpenType() throws IOException { + Type quoteType = th.getType(TEST_NAMESPACE, "OpenQuote"); + DataObject quote = df.create(quoteType); + + quote.setString("symbol", "s1"); + + for (Iterator iter = quote.getInstanceProperties().iterator(); iter.hasNext();) { + Property property = (Property) iter.next(); + } + + Property companyProperty = xsdh.getGlobalProperty(TEST_NAMESPACE, "company", true); + DataObject company = quote.createDataObject(companyProperty); + company.setString("name", "FlyByNightTechnology"); + + Property priceProperty = xsdh.getGlobalProperty(TEST_NAMESPACE, "price", true); + quote.getList(priceProperty).add(new BigDecimal("1000.0")); + + for (Iterator iter = quote.getInstanceProperties().iterator(); iter.hasNext();) { + Property property = (Property) iter.next(); + } + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + xmlh.save(quote, TEST_NAMESPACE, "openStockQuote", baos); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(TEST_DATA))); + } + + + public void testOpenType_Tuscany_396() throws IOException { + Type quoteType = th.getType(TEST_NAMESPACE, "OpenQuote2"); + DataObject quote = df.create(quoteType); + + Property priceProperty = xsdh.getGlobalProperty(TEST_NAMESPACE, "price", true); + // the importance of this is that the maxOccurs=1 attribute on the xsd:any + // allows is to use the !isMany getters and setters for open content + quote.set(priceProperty, new BigDecimal("1000.0")); + + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + hc = SDOUtil.createHelperContext(); + th = hc.getTypeHelper(); + xsdh = hc.getXSDHelper(); + df = hc.getDataFactory(); + xmlh = hc.getXMLHelper(); + hc.getXSDHelper().define(inputStream, url.toString()); + inputStream.close(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SchemaLocationTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SchemaLocationTestCase.java new file mode 100644 index 0000000000..eaec327007 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SchemaLocationTestCase.java @@ -0,0 +1,48 @@ +/** + * + * 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.util.*; + +import org.apache.tuscany.sdo.SDOPackage; +import org.apache.tuscany.sdo.util.SDOUtil; + +import junit.framework.TestCase; + +import commonj.sdo.helper.*; + +public class SchemaLocationTestCase extends TestCase { + private final String TEST_XML_DOCUMENT = "/SchemaLocationTestCase.xml"; + + /** + * This method will load an xml document consisting of a xsi:schemaLocation and xsi:noNamespaceSchemaLocation defined. It will then use the + * XMLDocument API to get and set the schemaLocation property. + * + * @throws IOException + */ + public void testSchemaLocation() throws IOException { + // load the xml document which has xsi:noNamespaceSchemaLocation and xsi:schemaLocation defined + Map options = new HashMap(); + options.put(SDOUtil.XML_LOAD_SCHEMA, Boolean.TRUE); + XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(TEST_XML_DOCUMENT), "whatever", options); + assertNotSame(doc.getRootObject().getType(), SDOPackage.eINSTANCE.getAnyTypeDataObject()); + } +} \ No newline at end of file diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java new file mode 100644 index 0000000000..592745e04e --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java @@ -0,0 +1,129 @@ +/** + * + * 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.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataGraph; +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + +public class SerializeTypesTestCase extends TestCase { + + HelperContext hc; + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + } + + public void testSerializeTypesRoundTrip() throws Exception { + TypeHelper types = hc.getTypeHelper(); + DataFactory factory = hc.getDataFactory(); + + Type intType = types.getType("commonj.sdo", "Int"); + Type stringType = types.getType("commonj.sdo", "String"); + + // create a new Type for Addresses + DataObject addressType = factory.create("commonj.sdo", "Type"); + addressType.set("uri", "http://example.com/address"); + addressType.set("name", "Address"); + + // create a address street property + DataObject addrStProperty = addressType.createDataObject("property"); + addrStProperty.set("name", "addrSt"); + addrStProperty.set("type", stringType); + + // create a new Type for Customers + DataObject customerType = factory.create("commonj.sdo", "Type"); + customerType.set("uri", "http://example.com/customer"); + customerType.set("name", "Customer"); + + // create a customer number property + DataObject custNumProperty = customerType.createDataObject("property"); + custNumProperty.set("name", "custNum"); + custNumProperty.set("type", intType); + + // create a first name property + DataObject firstNameProperty = customerType.createDataObject("property"); + firstNameProperty.set("name", "firstName"); + firstNameProperty.set("type", stringType); + + // create a last name property + DataObject lastNameProperty = customerType.createDataObject("property"); + lastNameProperty.set("name", "lastName"); + lastNameProperty.set("type", stringType); + + // create an address property + DataObject addressProperty = customerType.createDataObject("property"); + addressProperty.set("name", "address"); + addressProperty.set("type", addressType); + addressProperty.setBoolean("containment", true); + + // now define the Address and Customer type so that addresses and customers can be made + List types2define = new ArrayList(); + types2define.add(addressType); + types2define.add(customerType); + List typesDefined = types.define(types2define); + + // Create an empty data graph and add a root object, an instance of customerType + // + + DataGraph dataGraph = SDOUtil.createDataGraph(); + Type customerTypeDefined = (Type) typesDefined.get(1); + DataObject customer1 = dataGraph.createRootObject(customerTypeDefined); + + customer1.setInt("custNum", 1); + customer1.set("firstName", "John"); + customer1.set("lastName", "Adams"); + DataObject address = customer1.createDataObject("address"); + address.set("addrSt", "577 Airport Blvd"); + + SDOUtil.registerDataGraphTypes(dataGraph, typesDefined); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + SDOUtil.saveDataGraph(dataGraph, baos, null); + //SDOUtil.saveDataGraph(dataGraph, System.out, null); + + byte[] serialized = baos.toByteArray(); + ByteArrayInputStream bais = new ByteArrayInputStream(serialized); + DataGraph loadedDataGraph = SDOUtil.loadDataGraph(bais, null, SDOUtil.createTypeHelper()); + + DataObject loadedRootObject = loadedDataGraph.getRootObject(); + assertNotSame(loadedRootObject.getType(), customer1.getType()); + + // EqualityHelper requires same Type + assertEquals(loadedRootObject.getInt("custNum"), customer1.getInt("custNum")); + assertEquals(loadedRootObject.get("firstName"), customer1.get("firstName")); + assertEquals(loadedRootObject.get("lastName"), customer1.get("lastName")); + DataObject loadedAddress = loadedRootObject.getDataObject("address"); + assertEquals(loadedAddress.get("addrSt"), address.get("addrSt")); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleCopyTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleCopyTestCase.java new file mode 100644 index 0000000000..36f038d779 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleCopyTestCase.java @@ -0,0 +1,71 @@ +/** + * + * 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.io.InputStream; +import java.net.URL; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.CopyHelper; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import junit.framework.TestCase; + +public class SimpleCopyTestCase extends TestCase { + + private final String TEST_MODEL = "/simple.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/simple"; + private final String QUOTE_XML = "/quote.xml"; + private final String SHALLOW_QUOTE_XML = "/shallowquote.xml"; + + public void testSimpleCopy() throws IOException { + XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(QUOTE_XML)); + DataObject sdo = doc.getRootObject(); + + DataObject copiedSdo = CopyHelper.INSTANCE.copyShallow(sdo); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + XMLHelper.INSTANCE.save(copiedSdo, TEST_NAMESPACE, "stockQuote", baos); + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(SHALLOW_QUOTE_XML))); + assertFalse(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(QUOTE_XML))); + + copiedSdo = CopyHelper.INSTANCE.copy(sdo); + + baos.reset(); + XMLHelper.INSTANCE.save(copiedSdo, TEST_NAMESPACE, "stockQuote", baos); + assertFalse(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(SHALLOW_QUOTE_XML))); + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(QUOTE_XML))); + } + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleDynamicTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleDynamicTestCase.java new file mode 100644 index 0000000000..55aae9d396 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleDynamicTestCase.java @@ -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.tuscany.sdo.test; + + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.net.URL; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; + + +public class SimpleDynamicTestCase extends TestCase { + private final String TEST_MODEL = "/simple.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/simple"; + private final String QUOTE_XML = "/quote.xml"; + + //private final String TEST_MODEL2 = "/simple2.xsd"; + //private final String TEST_NAMESPACE2 = "http://www.example.com/simple2"; + //private final String QUOTE_XML2 = "/quote2.xml"; + + HelperContext hc; + TypeHelper th; + + /** + * Simple Dynamic SDO 2 test. + */ + public void testDynamic() throws IOException { + Type quoteType = th.getType(TEST_NAMESPACE, "Quote"); + DataObject quote = hc.getDataFactory().create(quoteType); + + quote.setString("symbol", "fbnt"); + quote.setString("companyName", "FlyByNightTechnology"); + quote.setBigDecimal("price", new BigDecimal("1000.0")); + quote.setBigDecimal("open1", new BigDecimal("1000.0")); + quote.setBigDecimal("high", new BigDecimal("1000.0")); + quote.setBigDecimal("low", new BigDecimal("1000.0")); + quote.setDouble("volume", 1000); + quote.setDouble("change1", 1000); + + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + hc.getXMLHelper().save(quote, TEST_NAMESPACE, "stockQuote", baos); + + assertFalse(quote.isSet("undefined")); + assertSame(quote.get("undefined"), null); + + assertTrue(TestUtil.equalXmlFiles(new ByteArrayInputStream(baos.toByteArray()), getClass().getResource(QUOTE_XML))); + } + + /* + public void dontTestResolveXSDWithoutSchemaLocation() throws IOException { + + URL url = getClass().getResource(TEST_MODEL2); + InputStream inputStream = url.openStream(); + hc.getXSDHelper().define(inputStream, null); + inputStream.close(); + + Type quote2Type = th.getType(TEST_NAMESPACE2, "Quote2"); + DataObject quote2 = hc.getDataFactory().create(quote2Type); + + quote2.setString("symbol", "fbnt"); + quote2.setString("companyName", "FlyByNightTechnology"); + quote2.setBigDecimal("price", new BigDecimal("1000.0")); + quote2.setBigDecimal("open1", new BigDecimal("1000.0")); + quote2.setBigDecimal("high", new BigDecimal("1000.0")); + quote2.setBigDecimal("low", new BigDecimal("1000.0")); + quote2.setDouble("volume", 1000); + quote2.setDouble("change1", 1000); + + DataObject child = quote2.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + hc.getXMLHelper().save(quote2, TEST_NAMESPACE2, "stockQuote", System.out); + } + */ + + protected void setUp() throws Exception { + super.setUp(); + + hc = SDOUtil.createHelperContext(); + th = hc.getTypeHelper(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + hc.getXSDHelper().define(inputStream, url.toString()); + inputStream.close(); + + + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleEqualityTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleEqualityTestCase.java new file mode 100644 index 0000000000..c3fffa4f10 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SimpleEqualityTestCase.java @@ -0,0 +1,63 @@ +/** + * + * 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 commonj.sdo.DataObject; +import commonj.sdo.helper.EqualityHelper; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; +import junit.framework.TestCase; + +public class SimpleEqualityTestCase extends TestCase { + private static final String TEST_MODEL = "/simple.xsd"; + private static final String QUOTE_XML = "/quote.xml"; + private static final String SHALLOW_QUOTE_XML = "/shallowquote.xml"; + + public void testEquality() throws IOException { + + XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(QUOTE_XML)); + DataObject sdo = doc.getRootObject(); + + doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(SHALLOW_QUOTE_XML)); + DataObject shallowSdo = doc.getRootObject(); + + boolean result = EqualityHelper.INSTANCE.equalShallow(sdo, shallowSdo); + assertTrue(result); + + result = EqualityHelper.INSTANCE.equal(sdo, shallowSdo); + assertFalse(result); + } + + + protected void setUp() throws Exception { + super.setUp(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SubstitutionValuesTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SubstitutionValuesTestCase.java new file mode 100644 index 0000000000..b473503122 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SubstitutionValuesTestCase.java @@ -0,0 +1,47 @@ +/** + * + * 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.net.URL; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.XSDHelper; + +public final class SubstitutionValuesTestCase extends TestCase +{ + public void test() throws IOException + { + URL url = getClass().getResource("/SubstitutionValues.xsd"); + XSDHelper.INSTANCE.define(url.openStream(), url.toString()); + + final DataObject object = DataFactory.INSTANCE.create("http://www.apache.org/tuscany/SubstitutionValues", "TestObject"); + final Type type = object.getType(); + + assertNotNull( SDOUtil.getSubstitutionValues(object, type.getProperty("groupHead"))); + assertNull( SDOUtil.getSubstitutionValues(object, type.getProperty("nonGroupHead"))); + } +} \ No newline at end of file diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java new file mode 100644 index 0000000000..a7ab3f954a --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TestUtil.java @@ -0,0 +1,285 @@ +/** + * + * 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, but within + // the array exist the get(index), get(property), and get(path). Rather than + // referring to each of the three in every circumstance, the more compact array appears. + + private static ConversionType TO_BOOLEAN = new ConversionType("getBoolean"); + private static ConversionType TO_BYTE = new ConversionType("getByte"); + private static ConversionType TO_CHAR = new ConversionType("getChar"); + private static ConversionType TO_DOUBLE = new ConversionType("getDouble"); + private static ConversionType TO_FLOAT = new ConversionType("getFloat"); + private static ConversionType TO_INT = new ConversionType("getInt"); + private static ConversionType TO_LONG = new ConversionType("getLong"); + private static ConversionType TO_SHORT = new ConversionType("getShort"); + private static ConversionType TO_BYTES = new ConversionType("getBytes"); + private static ConversionType TO_BIGDECIMAL = new ConversionType("getBigDecimal"); + private static ConversionType TO_BIGINTEGER = new ConversionType("getBigInteger"); + private static ConversionType TO_DATAOBJECT = new ConversionType("getDataObject"); + private static ConversionType TO_DATE = new ConversionType("getDate"); + private static ConversionType TO_STRING = new ConversionType("getString"); + private static ConversionType TO_LIST = new ConversionType("getList"); + private static ConversionType TO_SEQUENCE = new ConversionType("getSequence"); + + private static GeneralComparator COMPARE_ANY; + + // There will be several instances where a Property must be passed as a parameter. Have available the Type + // to call getProperty() as needed. + + private static Type API_TEST_TYPE; + + // The default constructor establishes each of the Method and Method[] variables. + + public TypeConversionTestCase() throws Exception + { + COMPARE_ANY = new GeneralComparator(); + + // Populate the meta data for the test model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + XSDHelper.INSTANCE.define(inputStream, url.toString()); + inputStream.close(); + + API_TEST_TYPE = TypeHelper.INSTANCE.getType(TEST_NAMESPACE, "APITest"); + } + + private static class ConversionType + { + // The following constants are used because the getMethod function requires an Class + // array describing the parameters to the functions. + + private static final Class[] INT_CLASS_ARRAY = {int.class}; + private static final Class[] PROPERTY_CLASS_ARRAY = {Property.class}; + private static final Class[] STRING_CLASS_ARRAY = {String.class}; + + Method index_method; + Method property_method; + Method path_method; + + public ConversionType (String method_name) + { + try + { + this.index_method = DataObject.class.getMethod(method_name, INT_CLASS_ARRAY); + this.property_method = DataObject.class.getMethod(method_name, PROPERTY_CLASS_ARRAY); + this.path_method = DataObject.class.getMethod(method_name, STRING_CLASS_ARRAY); + } + catch (NoSuchMethodException e) + { + this.index_method = null; + this.property_method = null; + this.path_method = null; + } + } + + public Method getIndexMethod() + { + return this.index_method; + } + + public Method getPropertyMethod() + { + return this.property_method; + } + + public Method getPathMethod() + { + return this.path_method; + } + } + + // Each instance of Test describes a convert-from type. The index, property and path parms + // will refer to the same field, which is a field of the convert-from type. + + private static class Test + { + DataObject test_obj; + Object[] index_parm; + Object[] property_parm; + Object[] path_parm; + Object expected_value; + String from_type; + + // The constructor prepares a test DataObject and determines how to access the field + // in three different ways - index, property, and path. + + Test(String path, int index) + { + this.test_obj = DataFactory.INSTANCE.create(API_TEST_TYPE); + this.index_parm = new Object[] {new Integer(index)}; + this.property_parm = new Object[] {API_TEST_TYPE.getProperty(path)}; + this.path_parm = new Object[] {path}; + this.expected_value = null; + } + + // The initialize() function establishes the initial value of the test field. + + public void initialize(Class type, String type_name, Object initial_value) throws Exception + { + Class[] classArray = {int.class, type}; + Object[] initValueArray = new Object[] {this.index_parm[0], initial_value}; + + Method setter = DataObject.class.getMethod("set" + type_name, classArray); + setter.invoke(test_obj, initValueArray); + this.expected_value = initial_value; + this.from_type = type_name; + } + + // Attempts the conversion to the specified type, using DataObject.get____(). + // The get___() function can be called with an index, path, and property. attemptConversion() + // calls each of those three. + + public void attemptConversion(ConversionType to_type) throws Exception + { + performConversion(to_type.getIndexMethod(), this.index_parm); + performConversion(to_type.getPathMethod(), this.path_parm); + performConversion(to_type.getPropertyMethod(), this.property_parm); + } + + public void checkConversionException(ConversionType to_type, Class expected_exception) throws Exception + { + boolean index_err, path_err, property_err, consistency_err = false; + + index_err = executeExceptionCase(to_type.getIndexMethod(), this.index_parm, expected_exception); + path_err = executeExceptionCase(to_type.getPathMethod(), this.path_parm, expected_exception); + property_err = executeExceptionCase(to_type.getPropertyMethod(), this.property_parm, expected_exception); + + if (index_err != path_err || path_err != property_err) + consistency_err = true; + else if (index_err == false) + attemptConversion(to_type); + + assertFalse("An exception inconsistency exists for " + to_type.getPathMethod().getName() + " when called " + + "for a " + this.from_type + " property.", consistency_err); + } + + private void performConversion (Method convert, Object[] parm) throws Exception + { + try + { + assertTrue("Conversion did not yield expected value for " + convert.getName() + " on a " + this.from_type + " property.", + COMPARE_ANY.compare(convert.invoke(test_obj, parm), this.expected_value) == 0); + } + catch (Exception e) + { + Throwable cause = e.getCause(); + if (cause == null) + { + System.err.println("An exception of type " + e.getClass() + " occurred while performing " + convert.getName() + + " on a " + this.from_type + " property."); + } + else + { + System.err.println("An exception of type " + cause.getClass() + " occurred while performing " + convert.getName() + + " on a " + this.from_type + " property."); + } + + throw e; + } + + } + + private boolean executeExceptionCase (Method convert, Object[] parm, Class expected_exception) throws Exception + { + boolean exception_thrown = false; + try + { + convert.invoke(test_obj, parm); + } + catch (Exception e) + { + exception_thrown = true; + Throwable cause = e.getCause(); + if (cause == null) + { + assertEquals("An unexpected exception occurred while performing " + convert.getName() + + " on a " + this.from_type + " property.", expected_exception, e.getClass()); + } + else + { + assertEquals("An unexpected exception occurred while performing " + convert.getName() + + " on a " + this.from_type + " property.", expected_exception, cause.getClass()); + } + } + + return exception_thrown; + } + } + + private static class GeneralComparator implements Comparator + { + public int compare(Object obj1, Object obj2) + { + if (obj1.getClass() == obj2.getClass()) + { + if (obj1.equals(obj2)) + return 0; + else + return 1; + } + + else if (obj1.getClass() == Date.class) + { + if (obj2.getClass() == String.class) + { + try + { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'MM'-'dd'T'H':'mm':'ss.S"); + + sdf.setTimeZone(TimeZone.getTimeZone("UTC")); + obj2 = sdf.parse((String) obj2); + + if (obj1.equals(obj2)) + return 0; + } + catch (Exception e) + { + System.out.println(e.getMessage()); + } + + return 1; + } + + else + { + Date temp = (Date) obj1; + + return compare(new Long(temp.getTime()), obj2); + } + + } + + else if (obj2.getClass() == Date.class) + { + return compare(obj2, obj1); + } + + else if (obj1.getClass() == Boolean.class) + { + Boolean temp = (Boolean) obj1; + + if (temp.booleanValue()) + { + if (obj2.toString().equalsIgnoreCase("true")) + return 0; + else + return 1; + } + + else + { + if (obj2.toString().equalsIgnoreCase("true")) + return 1; + else + return 0; + } + } + + else if (obj2.getClass() == Boolean.class) + return compare(obj2, obj1); + + else if (obj1.getClass() == Byte.class || obj2.getClass() == Byte.class) + { + byte b1 = (Double.valueOf(obj1.toString())).byteValue(); + byte b2 = (Double.valueOf(obj2.toString())).byteValue(); + + if (b1 == b2) + return 0; + else if (b1 < b2) + return -1; + else + return 1; + } + + else if (obj1.getClass().toString().charAt(6) == '[') + { + long result = 0; + long multiplier = 1; + + byte[] array = (byte[]) obj1; + for (int i = 0; i < array.length; i++) + { + result += array[array.length - i - 1] * multiplier; + multiplier *= 256; + } + + return compare(obj2, new Long(result)); + } + + else if (obj2.getClass().toString().charAt(6) == '[') + { + return compare(obj2, obj1); + } + + else if (obj1.getClass() == Short.class || obj2.getClass() == Short.class) + { + short s1 = (Double.valueOf(obj1.toString())).shortValue(); + short s2 = (Double.valueOf(obj2.toString())).shortValue(); + + if (s1 == s2) + return 0; + else if (s1 < s2) + return -1; + else + return 1; + } + + else if (obj1.getClass() == Integer.class || obj2.getClass() == Integer.class) + { + int i1 = (Double.valueOf(obj1.toString())).intValue(); + int i2 = (Double.valueOf(obj2.toString())).intValue(); + + if (i1 == i2) + return 0; + else if (i1 < i2) + return -1; + else + return 1; + } + + else if ( obj1.getClass() == Long.class || obj2.getClass() == Long.class + || obj1.getClass() == BigInteger.class || obj2.getClass() == BigInteger.class) + { + long l1 = (Double.valueOf(obj1.toString())).longValue(); + long l2 = (Double.valueOf(obj2.toString())).longValue(); + + if (l1 == l2) + return 0; + else if (l1 < l2) + return -1; + else + return 1; + } + + else if (obj1.getClass() == Float.class || obj2.getClass() == Float.class) + { + float f1 = (Double.valueOf(obj1.toString())).floatValue(); + float f2 = (Double.valueOf(obj2.toString())).floatValue(); + + if (f1 == f2) + return 0; + else if (f1 < f2) + return -1; + else + return 1; + } + + else if (obj1.getClass() == Double.class || obj2.getClass() == Double.class) + { + Double b1 = Double.valueOf(obj1.toString()); + Double b2 = Double.valueOf(obj2.toString()); + + return b1.compareTo(b2); + } + + else if (obj1.getClass() == BigDecimal.class || obj2.getClass() == BigDecimal.class) + { + BigDecimal b1 = new BigDecimal(obj1.toString()); + BigDecimal b2 = new BigDecimal(obj2.toString()); + + return b1.compareTo(b2); + } + + else + { + if (obj1.toString().equals(obj2.toString())) + return 0; + else + return 1; + } + } + + } + + /********************************************************** + * In the following test cases, several instances are commented out. + * For these cases, the test case currently fails. A JIRA issue (TUSCANY-581) has + * been opened to either correct the behavior (then uncomment the lines) or to + * alter the specification against which the test cases were designed (and then + * remove the lines - assuming the alteration is to remove stating the + * nature of the exception). + */ + + public void testBooleanConversion() throws Exception + { + Test FromBoolean = new Test("booleanVal", BOOLEAN_VAL_INDEX); + + FromBoolean.initialize(boolean.class, "Boolean", Boolean.valueOf(true)); + + FromBoolean.attemptConversion(TO_BOOLEAN); + FromBoolean.attemptConversion(TO_STRING); + } + + public void testBooleanExceptions() throws Exception + { + Test FromBoolean = new Test("booleanVal", BOOLEAN_VAL_INDEX); + + FromBoolean.initialize(boolean.class, "Boolean", Boolean.valueOf(true)); + +// FromBoolean.checkConversionException(TO_BYTE, ClassCastException.class); +// FromBoolean.checkConversionException(TO_CHAR, ClassCastException.class); +// FromBoolean.checkConversionException(TO_DOUBLE, ClassCastException.class); +// FromBoolean.checkConversionException(TO_FLOAT, ClassCastException.class); +// FromBoolean.checkConversionException(TO_INT, ClassCastException.class); +// FromBoolean.checkConversionException(TO_LONG, ClassCastException.class); +// FromBoolean.checkConversionException(TO_SHORT, ClassCastException.class); +// FromBoolean.checkConversionException(TO_BYTES, ClassCastException.class); +// FromBoolean.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); +// FromBoolean.checkConversionException(TO_BIGINTEGER, ClassCastException.class); + FromBoolean.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromBoolean.checkConversionException(TO_DATE, ClassCastException.class); + FromBoolean.checkConversionException(TO_LIST, ClassCastException.class); + FromBoolean.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testByteConversion() throws Exception + { + Test FromByte = new Test("byteVal", BYTE_VAL_INDEX); + + FromByte.initialize(byte.class, "Byte", Byte.valueOf("-127")); + + FromByte.attemptConversion(TO_BYTE); + FromByte.attemptConversion(TO_DOUBLE); + FromByte.attemptConversion(TO_FLOAT); + FromByte.attemptConversion(TO_INT); + FromByte.attemptConversion(TO_LONG); + FromByte.attemptConversion(TO_SHORT); + FromByte.attemptConversion(TO_STRING); + } + + public void testByteExceptions() throws Exception + { + Test FromByte = new Test("byteVal", BYTE_VAL_INDEX); + + FromByte.initialize(byte.class, "Byte", Byte.valueOf("-127")); + +// FromByte.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromByte.checkConversionException(TO_CHAR, ClassCastException.class); +// FromByte.checkConversionException(TO_BYTES, ClassCastException.class); + FromByte.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); + FromByte.checkConversionException(TO_BIGINTEGER, ClassCastException.class); + FromByte.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromByte.checkConversionException(TO_DATE, ClassCastException.class); + FromByte.checkConversionException(TO_LIST, ClassCastException.class); + FromByte.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testCharConversion() throws Exception + { + Test FromChar = new Test("charVal", CHAR_VAL_INDEX); + + FromChar.initialize(char.class, "Char", new Character('?')); + + FromChar.attemptConversion(TO_CHAR); + FromChar.attemptConversion(TO_STRING); + } + + public void testCharExceptions() throws Exception + { + Test FromChar = new Test("charVal", CHAR_VAL_INDEX); + + FromChar.initialize(char.class, "Char", new Character('?')); + +// FromChar.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromChar.checkConversionException(TO_BYTE, ClassCastException.class); +// FromChar.checkConversionException(TO_DOUBLE, ClassCastException.class); +// FromChar.checkConversionException(TO_FLOAT, ClassCastException.class); +// FromChar.checkConversionException(TO_INT, ClassCastException.class); +// FromChar.checkConversionException(TO_LONG, ClassCastException.class); +// FromChar.checkConversionException(TO_SHORT, ClassCastException.class); +// FromChar.checkConversionException(TO_BYTES, ClassCastException.class); +// FromChar.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); +// FromChar.checkConversionException(TO_BIGINTEGER, ClassCastException.class); + FromChar.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromChar.checkConversionException(TO_DATE, ClassCastException.class); + FromChar.checkConversionException(TO_LIST, ClassCastException.class); + FromChar.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testDoubleConversion() throws Exception + { + Test FromDouble = new Test("doubleVal", DOUBLE_VAL_INDEX); + + FromDouble.initialize(double.class, "Double", new Double(Double.MAX_VALUE)); + + FromDouble.attemptConversion(TO_BYTE); + FromDouble.attemptConversion(TO_DOUBLE); + FromDouble.attemptConversion(TO_FLOAT); + FromDouble.attemptConversion(TO_INT); + FromDouble.attemptConversion(TO_LONG); + FromDouble.attemptConversion(TO_SHORT); + FromDouble.attemptConversion(TO_BIGDECIMAL); + FromDouble.attemptConversion(TO_BIGINTEGER); + FromDouble.attemptConversion(TO_STRING); + } + + public void testDoubleExceptions() throws Exception + { + Test FromDouble = new Test("doubleVal", DOUBLE_VAL_INDEX); + + FromDouble.initialize(double.class, "Double", new Double(Double.MAX_VALUE)); + +// FromDouble.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromDouble.checkConversionException(TO_CHAR, ClassCastException.class); +// FromDouble.checkConversionException(TO_BYTES, ClassCastException.class); + FromDouble.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromDouble.checkConversionException(TO_DATE, ClassCastException.class); + FromDouble.checkConversionException(TO_LIST, ClassCastException.class); + FromDouble.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testFloatConversion() throws Exception + { + Test FromFloat = new Test("floatVal", FLOAT_VAL_INDEX); + + FromFloat.initialize(float.class, "Float", new Float(Float.MIN_VALUE)); + + FromFloat.attemptConversion(TO_BYTE); + FromFloat.attemptConversion(TO_DOUBLE); + FromFloat.attemptConversion(TO_FLOAT); + FromFloat.attemptConversion(TO_INT); + FromFloat.attemptConversion(TO_LONG); + FromFloat.attemptConversion(TO_SHORT); + FromFloat.attemptConversion(TO_BIGDECIMAL); + FromFloat.attemptConversion(TO_BIGINTEGER); + FromFloat.attemptConversion(TO_STRING); + } + + public void testFloatExceptions() throws Exception + { + Test FromFloat = new Test("floatVal", FLOAT_VAL_INDEX); + + FromFloat.initialize(float.class, "Float", new Float(Float.MIN_VALUE)); + +// FromFloat.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromFloat.checkConversionException(TO_CHAR, ClassCastException.class); +// FromFloat.checkConversionException(TO_BYTES, ClassCastException.class); + FromFloat.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromFloat.checkConversionException(TO_DATE, ClassCastException.class); + FromFloat.checkConversionException(TO_LIST, ClassCastException.class); + FromFloat.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testIntConversion() throws Exception + { + Test FromInt = new Test("intVal", INT_VAL_INDEX); + + FromInt.initialize(int.class, "Int", new Integer(5)); + + FromInt.attemptConversion(TO_BYTE); + FromInt.attemptConversion(TO_DOUBLE); + FromInt.attemptConversion(TO_FLOAT); + FromInt.attemptConversion(TO_INT); + FromInt.attemptConversion(TO_LONG); + FromInt.attemptConversion(TO_SHORT); + FromInt.attemptConversion(TO_BIGDECIMAL); + FromInt.attemptConversion(TO_BIGINTEGER); + FromInt.attemptConversion(TO_STRING); + } + + public void testIntExceptions() throws Exception + { + Test FromInt = new Test("intVal", INT_VAL_INDEX); + + FromInt.initialize(int.class, "Int", new Integer(5)); + +// FromInt.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromInt.checkConversionException(TO_CHAR, ClassCastException.class); +// FromInt.checkConversionException(TO_BYTES, ClassCastException.class); + FromInt.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromInt.checkConversionException(TO_DATE, ClassCastException.class); + FromInt.checkConversionException(TO_LIST, ClassCastException.class); + FromInt.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testLongConversion() throws Exception + { + Test FromLong = new Test("longVal", LONG_VAL_INDEX); + + FromLong.initialize(long.class, "Long", new Long(7000L)); + + FromLong.attemptConversion(TO_BYTE); + FromLong.attemptConversion(TO_DOUBLE); + FromLong.attemptConversion(TO_FLOAT); + FromLong.attemptConversion(TO_INT); + FromLong.attemptConversion(TO_LONG); + FromLong.attemptConversion(TO_SHORT); + FromLong.attemptConversion(TO_BIGDECIMAL); + FromLong.attemptConversion(TO_BIGINTEGER); + FromLong.attemptConversion(TO_DATE); + FromLong.attemptConversion(TO_STRING); + } + + public void testLongExceptions() throws Exception + { + Test FromLong = new Test("longVal", LONG_VAL_INDEX); + + FromLong.initialize(long.class, "Long", new Long(7000L)); + +// FromLong.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromLong.checkConversionException(TO_CHAR, ClassCastException.class); +// FromLong.checkConversionException(TO_BYTES, ClassCastException.class); + FromLong.checkConversionException(TO_DATAOBJECT, ClassCastException.class); + FromLong.checkConversionException(TO_LIST, ClassCastException.class); + FromLong.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testShortConversion() throws Exception + { + Test FromShort = new Test("shortVal", SHORT_VAL_INDEX); + + FromShort.initialize(short.class, "Short", new Short("-8000")); + + FromShort.attemptConversion(TO_BYTE); + FromShort.attemptConversion(TO_DOUBLE); + FromShort.attemptConversion(TO_FLOAT); + FromShort.attemptConversion(TO_INT); + FromShort.attemptConversion(TO_LONG); + FromShort.attemptConversion(TO_SHORT); + FromShort.attemptConversion(TO_STRING); + } + + public void testShortExceptions() throws Exception + { + Test FromShort = new Test("shortVal", SHORT_VAL_INDEX); + + FromShort.initialize(short.class, "Short", new Short("-8000")); + +// FromShort.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromShort.checkConversionException(TO_CHAR, ClassCastException.class); +// FromShort.checkConversionException(TO_BYTES, ClassCastException.class); + FromShort.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); + FromShort.checkConversionException(TO_BIGINTEGER, ClassCastException.class); + FromShort.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromShort.checkConversionException(TO_DATE, ClassCastException.class); + FromShort.checkConversionException(TO_LIST, ClassCastException.class); + FromShort.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testStringConversion() throws Exception + { + Test FromString = new Test("stringVal", STRING_VAL_INDEX); + + FromString.initialize(String.class, "String", "5"); + + FromString.attemptConversion(TO_BOOLEAN); + FromString.attemptConversion(TO_BYTE); + FromString.attemptConversion(TO_CHAR); + FromString.attemptConversion(TO_DOUBLE); + FromString.attemptConversion(TO_FLOAT); + FromString.attemptConversion(TO_INT); + FromString.attemptConversion(TO_LONG); + FromString.attemptConversion(TO_SHORT); + FromString.attemptConversion(TO_BIGDECIMAL); + FromString.attemptConversion(TO_BIGINTEGER); + FromString.attemptConversion(TO_STRING); + + FromString.initialize(String.class, "String", "1999-07-25T8:50:14.33Z"); + FromString.attemptConversion(TO_DATE); + } + + public void testStringExceptions() throws Exception + { + Test FromString = new Test("stringVal", STRING_VAL_INDEX); + + FromString.initialize(String.class, "String", "5"); + +// FromString.checkConversionException(TO_BYTES, ClassCastException.class); + FromString.checkConversionException(TO_DATAOBJECT, ClassCastException.class); + FromString.checkConversionException(TO_LIST, ClassCastException.class); + FromString.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testBytesConversion() throws Exception + { + Test FromBytes = new Test("bytesVal", BYTES_VAL_INDEX); + + FromBytes.initialize(byte[].class, "Bytes", new byte[] {10,100}); + + FromBytes.attemptConversion(TO_BYTES); + FromBytes.attemptConversion(TO_BIGINTEGER); + } + + public void testBytesExceptions() throws Exception + { + Test FromBytes = new Test("bytesVal", BYTES_VAL_INDEX); + + FromBytes.initialize(byte[].class, "Bytes", new byte[] {10,100}); + +// FromBytes.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromBytes.checkConversionException(TO_BYTE, ClassCastException.class); +// FromBytes.checkConversionException(TO_CHAR, ClassCastException.class); +// FromBytes.checkConversionException(TO_DOUBLE, ClassCastException.class); +// FromBytes.checkConversionException(TO_FLOAT, ClassCastException.class); +// FromBytes.checkConversionException(TO_INT, ClassCastException.class); +// FromBytes.checkConversionException(TO_LONG, ClassCastException.class); +// FromBytes.checkConversionException(TO_SHORT, ClassCastException.class); +// FromBytes.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); + FromBytes.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromBytes.checkConversionException(TO_DATE, ClassCastException.class); +// FromBytes.checkConversionException(TO_STRING, ClassCastException.class); + FromBytes.checkConversionException(TO_LIST, ClassCastException.class); + FromBytes.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testBigDecimalConversion() throws Exception + { + Test FromBigDecimal = new Test("decimalVal", DECIMAL_VAL_INDEX); + + FromBigDecimal.initialize(BigDecimal.class, "BigDecimal", new BigDecimal("-3")); + + FromBigDecimal.attemptConversion(TO_DOUBLE); + FromBigDecimal.attemptConversion(TO_FLOAT); + FromBigDecimal.attemptConversion(TO_INT); + FromBigDecimal.attemptConversion(TO_LONG); + FromBigDecimal.attemptConversion(TO_BIGDECIMAL); + FromBigDecimal.attemptConversion(TO_BIGINTEGER); + FromBigDecimal.attemptConversion(TO_STRING); + } + + public void testBigDecimalExceptions() throws Exception + { + Test FromBigDecimal = new Test("decimalVal", DECIMAL_VAL_INDEX); + + FromBigDecimal.initialize(BigDecimal.class, "BigDecimal", new BigDecimal("-3")); + +// FromBigDecimal.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromBigDecimal.checkConversionException(TO_BYTE, ClassCastException.class); +// FromBigDecimal.checkConversionException(TO_CHAR, ClassCastException.class); +// FromBigDecimal.checkConversionException(TO_SHORT, ClassCastException.class); +// FromBigDecimal.checkConversionException(TO_BYTES, ClassCastException.class); + FromBigDecimal.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromBigDecimal.checkConversionException(TO_DATE, ClassCastException.class); + FromBigDecimal.checkConversionException(TO_LIST, ClassCastException.class); + FromBigDecimal.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testBigIntegerConversion() throws Exception + { + Test FromBigInteger = new Test("integerVal", INTEGER_VAL_INDEX); + + FromBigInteger.initialize(BigInteger.class, "BigInteger", new BigInteger("31500")); + + FromBigInteger.attemptConversion(TO_DOUBLE); + FromBigInteger.attemptConversion(TO_FLOAT); + FromBigInteger.attemptConversion(TO_INT); + FromBigInteger.attemptConversion(TO_LONG); + FromBigInteger.attemptConversion(TO_SHORT); + FromBigInteger.attemptConversion(TO_BYTES); + FromBigInteger.attemptConversion(TO_BIGDECIMAL); + FromBigInteger.attemptConversion(TO_BIGINTEGER); + FromBigInteger.attemptConversion(TO_STRING); + } + + public void testBigIntegerExceptions() throws Exception + { + Test FromBigInteger = new Test("integerVal", INTEGER_VAL_INDEX); + + FromBigInteger.initialize(BigInteger.class, "BigInteger", new BigInteger("31500")); + +// FromBigInteger.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromBigInteger.checkConversionException(TO_BYTE, ClassCastException.class); +// FromBigInteger.checkConversionException(TO_CHAR, ClassCastException.class); + FromBigInteger.checkConversionException(TO_DATAOBJECT, ClassCastException.class); +// FromBigInteger.checkConversionException(TO_DATE, ClassCastException.class); + FromBigInteger.checkConversionException(TO_LIST, ClassCastException.class); + FromBigInteger.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testDateConversion() throws Exception + { + Test FromDate = new Test("dateVal", DATE_VAL_INDEX); + + FromDate.initialize(Date.class, "Date", new Date(System.currentTimeMillis())); + + FromDate.attemptConversion(TO_LONG); + FromDate.attemptConversion(TO_DATE); + FromDate.attemptConversion(TO_STRING); + } + + public void testDateExceptions() throws Exception + { + Test FromDate = new Test("dateVal", DATE_VAL_INDEX); + + FromDate.initialize(Date.class, "Date", new Date(System.currentTimeMillis())); + +// FromDate.checkConversionException(TO_BOOLEAN, ClassCastException.class); +// FromDate.checkConversionException(TO_BYTE, ClassCastException.class); +// FromDate.checkConversionException(TO_CHAR, ClassCastException.class); +// FromDate.checkConversionException(TO_DOUBLE, ClassCastException.class); +// FromDate.checkConversionException(TO_FLOAT, ClassCastException.class); +// FromDate.checkConversionException(TO_INT, ClassCastException.class); +// FromDate.checkConversionException(TO_SHORT, ClassCastException.class); +// FromDate.checkConversionException(TO_BYTES, ClassCastException.class); +// FromDate.checkConversionException(TO_BIGDECIMAL, ClassCastException.class); +// FromDate.checkConversionException(TO_BIGINTEGER, ClassCastException.class); + FromDate.checkConversionException(TO_DATAOBJECT, ClassCastException.class); + FromDate.checkConversionException(TO_LIST, ClassCastException.class); + FromDate.checkConversionException(TO_SEQUENCE, ClassCastException.class); + } + + public void testTuscany_836() { + assertEquals(DataObjectUtil.getBigDecimal(new Long(Long.MAX_VALUE)).longValue(), Long.MAX_VALUE); + } +} \ No newline at end of file diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeRoundTripTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeRoundTripTestCase.java new file mode 100644 index 0000000000..52bc78345b --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/TypeRoundTripTestCase.java @@ -0,0 +1,148 @@ +/** + * + * 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 java.math.BigInteger; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.Type; +import commonj.sdo.helper.DataHelper; +import commonj.sdo.helper.TypeHelper; + +public class TypeRoundTripTestCase extends TestCase { + + private static class Test { + Type type; + String value; + Object obj; + Comparator comp; + + Test(Type type, String value, Object obj) { + this.type = type; + this.value = value; + this.obj = obj; + } + + Test(Type type, String value, Object obj, Comparator comp) { + this.type = type; + this.value = value; + this.obj = obj; + this.comp = comp; + } + } + + private static class ListComparator implements Comparator { + public int compare(Object list1, Object list2) { + int answer = 1; + if (list1 instanceof List && list2 instanceof List) { + List l1 = (List) list1; + List l2 = (List) list2; + if (l1.size() == l2.size()) { + for (int n = 0; n < l1.size(); n++) { + if (!l1.get(n).equals(l2.get(n))) { + answer = 0; + break; + } + } + } else { + answer = 0; + } + } else { + answer = 0; + } + return answer; + } + } + + private static class BytesComparator implements Comparator { + public int compare(Object o1, Object o2) { + o2 = new String((byte[])o2); + if (o1.equals(o2)) return 1; + else return 0; + } + } + + public void testTypeRoundTrips() throws Exception { + String URI = "commonj.sdo"; + TypeHelper types = TypeHelper.INSTANCE; + + List list = new ArrayList(); + list.add("foo"); + list.add("bar"); + list.add("test"); + Test[] tests = { + new Test(types.getType(URI, "Base64Bytes"), "SGVsbG8gV29ybGQ=", "Hello World", new BytesComparator()), + new Test(types.getType(URI, "Boolean"), "true", new Boolean(true)), + new Test(types.getType(URI, "Byte"), "49", new Byte((byte)49)), + new Test(types.getType(URI, "Bytes"), "666F6F", "foo", new BytesComparator()), + new Test(types.getType(URI, "Character"), "a", new Character('a')), + new Test(types.getType(URI, "Date"), "2005-12-12T12:12:12.012Z", DataHelper.INSTANCE.toDate("2005-12-12T12:12:12.012Z")), + new Test(types.getType(URI, "DateTime"), "2005-12-12T12:12:12zz", "2005-12-12T12:12:12zz"), + new Test(types.getType(URI, "Day"), "---12", "---12"), + new Test(types.getType(URI, "Decimal"), "12.12", new BigDecimal("12.12")), + new Test(types.getType(URI, "Double"), "12.12", new Double(12.12)), + new Test(types.getType(URI, "Duration"), "P5Y2M10D", "P5Y2M10D"), + new Test(types.getType(URI, "Float"), "12.12", new Float(12.12f)), + new Test(types.getType(URI, "Int"), "12", new Integer(12)), + new Test(types.getType(URI, "Integer"), "12", new BigInteger("12")), + new Test(types.getType(URI, "Long"), "12", new Long(12l)), + new Test(types.getType(URI, "Month"), "--12", "--12"), + new Test(types.getType(URI, "MonthDay"), "--12-12", "--12-12"), + new Test(types.getType(URI, "Object"), "test", "test"), + new Test(types.getType(URI, "Short"), "12", new Short((short)12)), + new Test(types.getType(URI, "String"), "test", "test"), + new Test(types.getType(URI, "Strings"), "foo bar test", list, new ListComparator()), + new Test(types.getType(URI, "Time"), "12:12:12.12", "12:12:12.12"), + new Test(types.getType(URI, "URI"), "http://example.org", "http://example.org"), + new Test(types.getType(URI, "Year"), "2005", "2005"), + new Test(types.getType(URI, "YearMonth"), "2005-12", "2005-12"), + new Test(types.getType(URI, "YearMonthDay"), "2005-12-12", "2005-12-12") + }; + + for (int n = 0; n < tests.length; n++) { + assertEquals( + SDOUtil.convertToString(tests[n].type, SDOUtil.createFromString(tests[n].type, tests[n].value)), + tests[n].value + ); + + //System.out.print("."); + + if (tests[n].comp == null) { + assertEquals( + SDOUtil.createFromString(tests[n].type, SDOUtil.convertToString(tests[n].type, tests[n].obj)), + tests[n].obj + ); + } else { + String o1 = SDOUtil.convertToString(tests[n].type, tests[n].obj); + Object o2 = SDOUtil.createFromString(tests[n].type, o1); + assertEquals(tests[n].comp.compare(tests[n].obj, o2), 1); + } + } + + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java new file mode 100644 index 0000000000..1cf59757ef --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLDocumentTestCase.java @@ -0,0 +1,110 @@ +/** + * + * 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 commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; + + +public class XMLDocumentTestCase extends TestCase +{ + private final String TEST_XML_DOCUMENT = "/XMLDocumentTestCase.xml"; + + // remember that NS1... and NS2... items are expected in sorted order by name + private final String NS1_SCHEMA_NAME = "http://www.example.com/open"; + + private final String NS1_SCHEMA_LOCATION = "/open.xsd"; + + private final String NS2_SCHEMA_NAME = "http://www.example.com/xmlDocumentSchemaLocation"; + + private final String NS2_SCHEMA_LOCATION = "/XMLDocumentSchemaLocation.xsd"; + + private final String NS_SET_NAME_LOCATION = "namespace schemaLocation"; + + //private final String NNS_SCHEMA_LOCATION = "http://www.example.com/XMLDocumentNoNamespaceSchemaLocation.xsd"; + private final String NNS_SCHEMA_LOCATION = "/XMLDocumentNoNamespaceSchemaLocation.xsd"; + + private final String NNS_SET_LOCATION = "noNamespaceSchemaLocation"; + + /** + * This method will load an xml document consisting of a xsi:schemaLocation and + * xsi:noNamespaceSchemaLocation defined. It will then use the XMLDocument API to get and + * set the schemaLocation property. + * + * @throws IOException + */ + public void testSchemaLocation() throws IOException + { + // load the xml document which has xsi:noNamespaceSchemaLocation and xsi:schemaLocation defined + XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(TEST_XML_DOCUMENT)); + + // get the schemaLocation + assertEquals(NS1_SCHEMA_NAME + " " + NS1_SCHEMA_LOCATION + " " + NS2_SCHEMA_NAME + " " + NS2_SCHEMA_LOCATION, doc.getSchemaLocation()); + + // set the schemaLocation to another value and test to see if the value was set + doc.setSchemaLocation(NS_SET_NAME_LOCATION); + assertEquals(NS_SET_NAME_LOCATION, doc.getSchemaLocation()); + + // remove the schemaLocation and ensure it returns null + doc.setSchemaLocation(null); + assertNull(doc.getSchemaLocation()); + + // ensure changes to schemaLocation have not changed noNamespaceSchemaLocation + assertEquals(NNS_SCHEMA_LOCATION, doc.getNoNamespaceSchemaLocation()); + } + + /** + * This method will load an xml document consisting of a xsi:schemaLocation and + * xsi:noNamespaceSchemaLocation defined. It will then use the XMLDocument API to get and + * set the noNamespaceSchemaLocation property. + * + * @throws IOException + */ + public void testNoNamespaceSchemaLocation() throws IOException + { + // load the xml document which has xsi:noNamespaceSchemaLocation and xsi:schemaLocation defined + XMLDocument doc = XMLHelper.INSTANCE.load(getClass().getResourceAsStream(TEST_XML_DOCUMENT)); + + // get the noNamespaceSchemaLocation + assertEquals(NNS_SCHEMA_LOCATION, doc.getNoNamespaceSchemaLocation()); + + // set the noNameSpaceSchemaLocation to another value and test to see if the value was set + doc.setNoNamespaceSchemaLocation(NNS_SET_LOCATION); + assertEquals(NNS_SET_LOCATION, doc.getNoNamespaceSchemaLocation()); + + // remove the noNameSpaceSchemaLocation and ensure it returns null + doc.setNoNamespaceSchemaLocation(null); + assertNull(doc.getNoNamespaceSchemaLocation()); + + // ensure changes to noNameSpaceSchemaLocation have not changed schemaLocation + assertEquals(NS1_SCHEMA_NAME + " " + NS1_SCHEMA_LOCATION + " " + NS2_SCHEMA_NAME + " " + NS2_SCHEMA_LOCATION, doc.getSchemaLocation()); + } + + protected void setUp() throws Exception + { + super.setUp(); + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java new file mode 100644 index 0000000000..ffa9b5ed94 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLHelperTestCase.java @@ -0,0 +1,161 @@ +/** + * + * 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.ByteArrayOutputStream; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +public class XMLHelperTestCase extends TestCase { + + HelperContext hc; + private XSDHelper xsdHelper; + XMLHelper xmlh; + static final String INDENT = " ", MARGIN = " ", LINE_BREAK = "\n\n"; + + void define(String model) throws Exception { + // Populate the meta data for the test model + URL url = getClass().getResource(model); + xsdHelper.define(url.openStream(), url.toString()); + } + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + xsdHelper = hc.getXSDHelper(); + xmlh = hc.getXMLHelper(); + + // Populate the meta data for the test (Stock Quote) model + define("/simpleWithChangeSummary.xsd"); + + define("/SequenceChangeSummary.xsd"); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testLoadInputStreamStringObject() throws IOException { + + } + + protected final void format(String xml,String formatted) throws IOException { + XMLDocument doc = xmlh.load(getClass().getResource(xml).openStream()); + Map options = new HashMap(); + + options.put(SDOUtil.XML_SAVE_INDENT, INDENT); + options.put(SDOUtil.XML_SAVE_MARGIN, MARGIN); + options.put(SDOUtil.XML_SAVE_LineBreak, LINE_BREAK); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + // doc declares NameSpaces at root + xmlh.save(xmlh.createDocument(doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName()), baos, options); + //xmlh.save(xmlh.createDocument(doc.getRootObject(), doc.getRootElementURI(), doc.getRootElementName()), System.out, options); + + assertEquals(formatted, baos.toString()); + } + + public void testSaveXMLDocumentOutputStreamObject() throws IOException { + format("/simpleWithChangeSummary.xml", +MARGIN+ "" +LINE_BREAK+ +MARGIN+ "" +LINE_BREAK+ +MARGIN+INDENT+ "FBNT" +LINE_BREAK+ +MARGIN+INDENT+ "FlyByNightTechnology" +LINE_BREAK+ +MARGIN+INDENT+ "999.0" +LINE_BREAK+ +MARGIN+INDENT+ "1000.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "1500.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "2500.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "3000.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "4000.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "fbnt" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "1000.0" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+ ""); + } + + public void testSaveMixedOutputStreamObject() throws IOException { + format("/mixedChangeSummary.xml", +MARGIN+ "" +LINE_BREAK+ +MARGIN+ "" +LINE_BREAK+ +INDENT+ "" +LINE_BREAK+ +INDENT+INDENT+ "fbnt" +LINE_BREAK+ +INDENT+INDENT+ "FlyByNightTechnology" +LINE_BREAK+ +INDENT+INDENT+ "1000.0" +LINE_BREAK+ +INDENT+INDENT+ "" +LINE_BREAK+ +INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+ +INDENT+INDENT+ "" +LINE_BREAK+ +INDENT+ "" +LINE_BREAK+ +"FBNTFlyByNightTechnology999.01500.02500.01000.03000.04000.0"); + } + + public void testOpenMixedOutputStreamObject() throws IOException { + format("/openChangeSummary.xml", +MARGIN+ "" +LINE_BREAK+ +MARGIN+ "" +LINE_BREAK+ +MARGIN+INDENT+ "FBNT" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "1500.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "2500.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "3000.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "4000.0" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "fbnt" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "2000.02000.99" +LINE_BREAK+ +MARGIN+INDENT+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+INDENT+ "" +LINE_BREAK+ +MARGIN+INDENT+ "" +LINE_BREAK+ +MARGIN+ ""); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java new file mode 100644 index 0000000000..5ebdee2f3e --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperPerformanceTestCase.java @@ -0,0 +1,91 @@ +/** + * + * 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.*; +import java.math.BigDecimal; +import java.net.URL; + +import javax.xml.stream.*; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; + +import junit.framework.TestCase; + +import commonj.sdo.*; +import commonj.sdo.helper.*; + + +public class XMLStreamHelperPerformanceTestCase extends TestCase { + private final String TEST_MODEL = "/XMLStreamHelper.xsd"; + private final String TEST_NAMESPACE = "http://www.example.com/simple"; + + HelperContext hc; + + /** + * Simple Dynamic SDO 2 test. + */ + public void testDynamic() throws Exception { + TypeHelper typeHelper = hc.getTypeHelper(); + Type quoteType = typeHelper.getType(TEST_NAMESPACE, "Quote"); + DataObject quote = hc.getDataFactory().create(quoteType); + + quote.setString("symbol", "fbnt"); + quote.setString("companyName", "FlyByNightTechnology"); + quote.setBigDecimal("price", new BigDecimal("1000.0")); + quote.setBigDecimal("open1", new BigDecimal("1000.0")); + quote.setBigDecimal("high", new BigDecimal("1000.0")); + quote.setBigDecimal("low", new BigDecimal("1000.0")); + quote.setDouble("volume", 1000); + quote.setDouble("change1", 1000); + + DataObject child = quote.createDataObject("quotes"); + child.setBigDecimal("price", new BigDecimal("2000.0")); + + DataObject quote3 = quote.createDataObject("quotes3"); + quote3.setString("symbol3", "IBM"); + quote3.setString("company3", "IBM Corp."); + + XMLStreamHelper streamHelper = SDOUtil.createXMLStreamHelper(typeHelper); + XMLOutputFactory outputFactory = XMLOutputFactory.newInstance(); + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + + XMLDocument doc = hc.getXMLHelper().createDocument(quote, TEST_NAMESPACE, "stockQuote"); + streamHelper.save(doc, streamWriter); + streamWriter.flush(); + assertEquals("fbntFlyByNightTechnology1000.01000.01000.01000.01000.01000.02000.0IBMIBM Corp.",writer.toString()); + } + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + + // Populate the meta data for the test (Stock Quote) model + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + hc.getXSDHelper().define(inputStream, url.toString()); + inputStream.close(); + + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java new file mode 100644 index 0000000000..921aedea9e --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XMLStreamHelperTestCase.java @@ -0,0 +1,169 @@ +/** + * + * 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.*; +import java.net.URL; +import java.util.*; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.helper.XMLStreamHelper; +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.*; + +public class XMLStreamHelperTestCase extends TestCase { + + private HelperContext hc; + + private XMLStreamHelper streamHelper; + + private XMLInputFactory inputFactory; + + private XMLOutputFactory outputFactory; + + private QName module = new QName("http://foo", "module"); + + private QName name = new QName("http://bar", "implementation.mock"); + + private String testName = "foo-ext"; + // private String testName = "complex"; + + private String xml; + + // = "1Dummy2"; + + protected void setUp() throws Exception { + super.setUp(); + + hc = SDOUtil.createHelperContext(); + streamHelper = SDOUtil.createXMLStreamHelper(hc.getTypeHelper()); + + URL url = getClass().getClassLoader().getResource(testName + ".xsd"); + hc.getXSDHelper().define(url.openStream(), url.toExternalForm()); + url = getClass().getResource("/mixed.xsd"); + hc.getXSDHelper().define(url.openStream(), url.toString()); + + inputFactory = XMLInputFactory.newInstance(); + outputFactory = XMLOutputFactory.newInstance(); + + url = getClass().getClassLoader().getResource(testName + ".xml"); + InputStreamReader reader = new InputStreamReader(url.openStream()); + StringBuffer stringBuffer = new StringBuffer(); + char buf[] = new char[1024]; + int size; + while ((size = reader.read(buf)) != -1) + stringBuffer.append(buf, 0, size); + xml = stringBuffer.toString(); + reader.close(); + } + + public void testLoadObject() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); + int event = reader.getEventType(); + while (!(event == XMLStreamConstants.START_ELEMENT && reader.getName().equals(name)) && reader.hasNext()) { + event = reader.next(); + } + DataObject dataObject = streamHelper.loadObject(reader); + Assert.assertNotNull(dataObject); + Assert.assertTrue(dataObject.getString("myAttr").equals("helloworld.HelloWorldImpl")); + } + + public void testLoadUnqualifiedObject() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml.replaceAll("bar:", ""))); + int event = reader.getEventType(); + while (!(event == XMLStreamConstants.START_ELEMENT && reader.getName().getLocalPart().equals(name.getLocalPart())) && reader.hasNext()) { + event = reader.next(); + } + Map options = new HashMap(); + options.put(XMLStreamHelper.OPTION_DEFAULT_ROOT_TYPE, hc.getTypeHelper().getType(name.getNamespaceURI(), "MockImplementation")); + DataObject dataObject = streamHelper.loadObject(reader, options); + Assert.assertNotNull(dataObject); + Assert.assertTrue(dataObject.getString("myAttr").equals("helloworld.HelloWorldImpl")); + } + + public void testLoad() throws Exception { + XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader(xml)); + XMLDocument document = streamHelper.load(reader); + Assert.assertNotNull(document); + Assert.assertEquals(document.getRootElementURI(), module.getNamespaceURI()); + Assert.assertEquals(document.getRootElementName(), module.getLocalPart()); + DataObject moduleObject = document.getRootObject(); + List components = moduleObject.getList("component"); + DataObject componentObject = (DataObject) components.get(0); + DataObject implObject = componentObject.getDataObject("implementation.mock"); + Assert.assertTrue(implObject.getString("myAttr").equals("helloworld.HelloWorldImpl")); + } + + public void testSave() throws XMLStreamException { + XMLDocument document = hc.getXMLHelper().load(xml); + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + streamHelper.save(document, streamWriter); + streamWriter.flush(); + String xmlStr = writer.toString(); + //System.out.println(xmlStr); + Assert.assertTrue(xmlStr.indexOf("myAttr=\"helloworld.HelloWorldImpl\"")!=-1); + } + + public void testSaveObject() throws XMLStreamException { + XMLDocument document = hc.getXMLHelper().load(xml); + DataObject moduleObject = document.getRootObject(); + List components = moduleObject.getList("component"); + DataObject componentObject = (DataObject) components.get(0); + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + streamHelper.saveObject(componentObject, streamWriter); + streamWriter.flush(); + Assert.assertTrue(writer.toString().indexOf("myAttr=\"helloworld.HelloWorldImpl\"")!=-1); + } + + public void testSaveSequence() throws IOException, XMLStreamException { + DataObject quote = hc.getDataFactory().create("http://www.example.com/mixed", "MixedQuote"); + quote.setString("symbol", "fbnt"); + quote.getSequence().addText(0, "testing"); + quote.getSequence().addText("more testing"); + + StringWriter writer = new StringWriter(); + XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer); + streamHelper.saveObject(quote, streamWriter); + streamWriter.flush(); + //System.out.println(writer); + assertTrue(writer.toString().indexOf("fbnt") != -1); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XPathTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XPathTestCase.java new file mode 100644 index 0000000000..45f92a73b2 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XPathTestCase.java @@ -0,0 +1,125 @@ +/** + * + * 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 junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import commonj.sdo.DataObject; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.XMLDocument; +import commonj.sdo.helper.XMLHelper; +import commonj.sdo.helper.XSDHelper; + +public class XPathTestCase extends TestCase { + + HelperContext hc; + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + } + + private final String TEST_MODEL = "/xpath.xsd"; + private final String XPATH_XML = "/xpath.xml"; + + /** + * The presence or absence of the @ sign in a path has no meaning. + * Properties are always matched by name independent of their XML representation. + * @throws IOException + */ + public void testAtSignProperty() throws IOException { + XSDHelper xsdHelper = hc.getXSDHelper(); + XMLHelper xmlHelper = hc.getXMLHelper(); + + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + xsdHelper.define(inputStream, url.toString()); + + inputStream.close(); + + XMLDocument doc = xmlHelper.load(getClass().getResourceAsStream(XPATH_XML)); + + DataObject drive = doc.getRootObject(); + DataObject folder1 = (DataObject) drive.get("Folder.1"); + String value = folder1.getString("@creation_date"); + + assertEquals(value, "2000-03-23"); + } + + public void testListIndexing() throws Exception { + XSDHelper xsdHelper = hc.getXSDHelper(); + XMLHelper xmlHelper = hc.getXMLHelper(); + + URL url = getClass().getResource(TEST_MODEL); + InputStream inputStream = url.openStream(); + xsdHelper.define(inputStream, url.toString()); + + inputStream.close(); + + XMLDocument doc = xmlHelper.load(getClass().getResourceAsStream(XPATH_XML)); + + DataObject root = doc.getRootObject(); + DataObject folder1 = root.getDataObject("Folder[1]"); + assertNotNull(folder1); + DataObject folder1a = root.getDataObject("Folder.0"); + assertEquals(folder1, folder1a); + folder1a = root.getDataObject("Folder[FolderName=Folder00000000000]"); + assertEquals(folder1, folder1a); + + DataObject noFolder = null; + + try { + noFolder = root.getDataObject("Folder[3]"); + assertNull(noFolder); + } catch (Exception e) { + assertFalse("bad indexing generated exception" + e, true); + } + + try { + noFolder = root.getDataObject("Folder[0]"); + assertNull(noFolder); + } catch (Exception e) { + assertFalse("bad indexing generated exception" + e, true); + } + + try { + noFolder = root.getDataObject("Folder.2"); + assertNull(noFolder); + } catch (Exception e) { + assertFalse("bad indexing generated exception" + e, true); + } + + try { + noFolder = root.getDataObject("Folder.-1"); + assertNull(noFolder); + } catch (Exception e) { + assertFalse("bad indexing generated exception" + e, true); + } + + noFolder = root.getDataObject("Folder[FolderName=foo]"); + assertNull(noFolder); + } +} diff --git a/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XSDHelperTestCase.java b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XSDHelperTestCase.java new file mode 100644 index 0000000000..b8d3402172 --- /dev/null +++ b/sdo-java/tags/1.0-incubator-M3/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/XSDHelperTestCase.java @@ -0,0 +1,224 @@ +/** + * + * 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.net.URL; +import java.util.Hashtable; +import java.util.List; +import java.util.Vector; + +import junit.framework.TestCase; + +import org.apache.tuscany.sdo.util.SDOUtil; + +import com.example.simple.SimpleFactory; +import commonj.sdo.DataObject; +import commonj.sdo.Type; +import commonj.sdo.helper.DataFactory; +import commonj.sdo.helper.HelperContext; +import commonj.sdo.helper.TypeHelper; +import commonj.sdo.helper.XSDHelper; + +/** + * @version $Rev$ $Date$ + */ +public class XSDHelperTestCase extends TestCase { + private static final String TEST_MODEL = "/simple.xsd"; + private static final String TEST_MODEL2 = "/xsdCorners.xsd"; + private URL modelURL; + private URL xsdCornersURL; + + HelperContext hc; + + protected void setUp() throws Exception { + super.setUp(); + hc = SDOUtil.createHelperContext(); + modelURL = getClass().getResource(TEST_MODEL); + xsdCornersURL = getClass().getResource(TEST_MODEL2); + } + + public void testDefineWithLocation() throws IOException { + XSDHelper xsdHelper = hc.getXSDHelper(); + List types = xsdHelper.define(modelURL.openStream(), modelURL.toString()); + assertEquals(2, types.size()); + } + + public void testDefineWithNoLocation() { + XSDHelper xsdHelper = hc.getXSDHelper(); + List types = xsdHelper.define(getClass().getResourceAsStream(TEST_MODEL), null); + assertEquals(2, types.size()); + } + + public void testDuplicateDefineWithLocation() throws IOException { + XSDHelper xsdHelper = hc.getXSDHelper(); + List types = xsdHelper.define(modelURL.openStream(), modelURL.toString()); + assertEquals(2, types.size()); + + List types2 = xsdHelper.define(modelURL.openStream(), modelURL.toString()); + assertEquals(0, types2.size()); + } + + public void testXSDGeneration_staticSDOType() throws IOException + { + //test for static sdo type. The test succeeds if the IllegalArgumentException is thrown + //by XSDHelper.generate method in which case the string xsd must be null; + + SimpleFactory.INSTANCE.register(hc); + XSDHelper xsdHelper = hc.getXSDHelper(); + DataObject quoteSDO = (DataObject)SimpleFactory.INSTANCE.createQuote(); + List typeList = new Vector(); + typeList.add(quoteSDO.getType()); + String xsd = null; + + try + { + xsd = xsdHelper.generate(typeList); + xsd = ""; + } + catch ( IllegalArgumentException e ) + { + } + assertNull(xsd); + } + + public void testXSDGeneration_DynamicSDOType() throws IOException + { + //test for dynamic SDOs that have no XSD model. Here the testcase succeeds only if the + //xsd is generated by XSDHelper in which case xsd must not be null + XSDHelper xsdHelper = hc.getXSDHelper(); + DataObject quoteType = DataFactory.INSTANCE.create("commonj.sdo", "Type"); + quoteType.set("uri", "http://www.example.com/dynamic"); + quoteType.set("name", "DynamicQuote"); + + DataObject aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "symbol"); + aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "String")); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "price"); + aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "Decimal")); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "volume"); + aProperty.set("type", TypeHelper.INSTANCE.getType("commonj.sdo", "Double")); + + TypeHelper.INSTANCE.define(quoteType); + + Type dynamicQuoteType = + TypeHelper.INSTANCE.getType("http://www.example.com/dynamic", "DynamicQuote"); + + Vector types = new Vector(); + types.add(dynamicQuoteType); + String xsd = null; + + try + { + xsd = xsdHelper.generate(types); + //System.out.println(xsd); + } + catch ( IllegalArgumentException e ) + { + } + assertNotNull(xsd); + + } + + public void testXSDGeneration_DynamicWithNestedStaticSDOType() throws IOException + { + //testing static SDO with XSD Model being contained in a Dynamic SDO not having an XSD Model. + //the schema must be generated with imports / includes for the XSD corresponding to the static + //sdo types. + TypeHelper typeHelper = hc.getTypeHelper(); + XSDHelper xsdHelper = hc.getXSDHelper(); + + SimpleFactory.INSTANCE.register(hc); + DataObject quoteSDO = (DataObject)SimpleFactory.INSTANCE.createQuote(); + + + DataObject quoteType = DataFactory.INSTANCE.create("commonj.sdo", "Type"); + quoteType.set("uri", "http://www.example.com/dynamic"); + quoteType.set("name", "DynamicQuote"); + + DataObject aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "symbol"); + aProperty.set("type", typeHelper.getType("commonj.sdo", "String")); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "price"); + aProperty.set("type", typeHelper.getType("commonj.sdo", "Decimal")); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "volume"); + aProperty.set("type", typeHelper.getType("commonj.sdo", "Double")); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "containedQuotes"); + aProperty.set("type", typeHelper.getType(quoteSDO.getType().getURI(), quoteSDO.getType().getName())); + aProperty.set("containment", new Boolean(true)); + + aProperty = quoteType.createDataObject("property"); + aProperty.set("name", "referredQuotes"); + aProperty.set("type", typeHelper.getType(quoteSDO.getType().getURI(), quoteSDO.getType().getName())); + + typeHelper.define(quoteType); + + Type dynamicQuoteType = + typeHelper.getType("http://www.example.com/dynamic", "DynamicQuote"); + Vector types = new Vector(); + types.add(dynamicQuoteType); + String xsd = null; + + try + { + Hashtable schemaLocationMap = new Hashtable(); + schemaLocationMap.put("http://www.example.com/simple", "http://www.example.com/simple/xsd"); + xsd = xsdHelper.generate(types, schemaLocationMap); + //System.out.println(xsd); + } + catch ( IllegalArgumentException e ) + { + } + assertNotNull(xsd); + + } + + public void testPrefixFromNSWithHyphenNumber() throws IOException { + XSDHelper xsdHelper = hc.getXSDHelper(); + xsdHelper.define(xsdCornersURL.openStream(), xsdCornersURL.toString()); + DataFactory df = hc.getDataFactory(); + DataObject root = df.create("http://www.example.com/simple-1", "A"); + root.setString("a1", "a1s"); + root.setString("a2", "a2s"); + + String doc = hc.getXMLHelper().save(root, "http://www.example.com/simple-1", "a"); + assertTrue(doc.indexOf("xmlns:s1=\"http://www.example.com/simple-1\"") != -1); + } + + public void testShortPrefix() throws IOException { + XSDHelper xsdHelper = hc.getXSDHelper(); + URL url = getClass().getResource("/prefix.xsd"); + xsdHelper.define(url.openStream(), url.toString()); + DataObject cmd = hc.getDataFactory().create("http://soaassureservice.soabench.ibm.com", "CreateClaim"); + cmd.setString("requestInfo", "cost"); + String doc = hc.getXMLHelper().save(cmd, "http://soaassureservice.soabench.ibm.com", "createClaim"); + assertTrue(doc.indexOf("xmlns:as=\"http://soaassureservice.soabench.ibm.com\"") != -1); + } +} -- cgit v1.2.3