/** * * 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.api.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(hcb); // 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