You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by fr...@apache.org on 2006/09/12 22:57:00 UTC
svn commit: r442701 - in /incubator/tuscany/java/sdo/impl/src:
main/java/org/apache/tuscany/sdo/helper/
main/java/org/apache/tuscany/sdo/util/
test/java/org/apache/tuscany/sdo/test/ test/resources/
Author: frankb
Date: Tue Sep 12 13:57:00 2006
New Revision: 442701
URL: http://svn.apache.org/viewvc?view=rev&rev=442701
Log:
Fix for TUSCANY-627
Added:
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java
incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd
Modified:
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java?view=auto&rev=442701
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java (added)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/helper/CrossScopeCopyHelperImpl.java Tue Sep 12 13:57:00 2006
@@ -0,0 +1,236 @@
+package org.apache.tuscany.sdo.helper;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.eclipse.emf.ecore.EAttribute;
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EReference;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.emf.ecore.util.FeatureMap;
+import org.eclipse.emf.ecore.util.FeatureMapUtil;
+import org.eclipse.emf.ecore.util.InternalEList;
+import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
+
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.CopyHelper;
+import commonj.sdo.helper.TypeHelper;
+
+/**
+ * A CopyHelper implementation that creates the copy objects in a specific metadata scope.
+ * The target scope must contain a compatible version of the Types needed to create the copy objects.
+ */
+public class CrossScopeCopyHelperImpl implements CopyHelper
+{
+ protected TypeHelper scope;
+
+ public CrossScopeCopyHelperImpl(TypeHelper targetScope)
+ {
+ scope = targetScope;
+ }
+
+ public DataObject copyShallow(DataObject dataObject)
+ {
+ Copier copier = new CrossScopeCopier()
+ {
+ protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
+ {
+ }
+ protected void copyAttribute(EAttribute eAttribute, EObject eObject, EObject copyEObject)
+ {
+ if (eObject.eIsSet(eAttribute) && !FeatureMapUtil.isFeatureMap(eAttribute))
+ {
+ super.copyAttribute(eAttribute,eObject,copyEObject);
+ }
+ }
+ };
+ EObject result = copier.copy((EObject)dataObject);
+ copier.copyReferences();
+ return (DataObject)result;
+ }
+
+ public DataObject copy(DataObject dataObject)
+ {
+ Copier copier = new CrossScopeCopier();
+ DataObject result = (DataObject)copier.copy((EObject)dataObject);
+ copier.copyReferences();
+ return (DataObject)result;
+ }
+
+ protected class CrossScopeCopier extends EcoreUtil.Copier
+ {
+ protected boolean useOriginalReferences = false;
+
+ protected EClass getTarget(EClass eClass)
+ {
+ EClass target = (EClass)get(eClass);
+ if (target == null)
+ {
+ Type type = (Type)eClass;
+ target = (EClass)scope.getType(type.getURI(), type.getName());
+ }
+ return target;
+ }
+
+ protected EStructuralFeature getTarget(EStructuralFeature eStructuralFeature)
+ {
+ EClass eClass = getTarget(eStructuralFeature.getEContainingClass());
+ EStructuralFeature targetEf = eClass.getEStructuralFeature(eStructuralFeature.getName());
+ return targetEf;
+ }
+
+ /**
+ * This Method WILL BE REMOVED when EMF 3.0 is available
+ */
+ public void copyReferences()
+ {
+ for (Iterator i = entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry)i.next();
+ EObject eObject = (EObject)entry.getKey();
+ EObject copyEObject = (EObject)entry.getValue();
+ EClass eClass = eObject.eClass();
+ for (int j = 0, size = eClass.getFeatureCount(); j < size; ++j)
+ {
+ EStructuralFeature eStructuralFeature = eClass.getEStructuralFeature(j);
+ if (eStructuralFeature.isChangeable() && !eStructuralFeature.isDerived())
+ {
+ if (eStructuralFeature instanceof EReference)
+ {
+ EReference eReference = (EReference)eStructuralFeature;
+ if (!eReference.isContainment() && !eReference.isContainer())
+ {
+ copyReference(eReference, eObject, copyEObject);
+ }
+ }
+ else if (FeatureMapUtil.isFeatureMap(eStructuralFeature))
+ {
+ FeatureMap featureMap = (FeatureMap)eObject.eGet(eStructuralFeature);
+ FeatureMap copyFeatureMap = (FeatureMap)copyEObject.eGet(getTarget(eStructuralFeature));
+ int copyFeatureMapSize = copyFeatureMap.size();
+ for (int k = 0, featureMapSize = featureMap.size(); k < featureMapSize; ++k)
+ {
+ EStructuralFeature feature = featureMap.getEStructuralFeature(k);
+ if (feature instanceof EReference)
+ {
+ Object referencedEObject = featureMap.getValue(k);
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null && referencedEObject != null)
+ {
+ EReference reference = (EReference)feature;
+ if (!useOriginalReferences || reference.isContainment() || reference.getEOpposite() != null)
+ {
+ continue;
+ }
+ copyReferencedEObject = referencedEObject;
+ }
+ // If we can't add it, it must aleady be in the list so find it and move it to the end.
+ //
+ if (!copyFeatureMap.add(feature, copyReferencedEObject))
+ {
+ for (int l = 0; l < copyFeatureMapSize; ++l)
+ {
+ if (copyFeatureMap.getEStructuralFeature(l) == feature && copyFeatureMap.getValue(l) == copyReferencedEObject)
+ {
+ copyFeatureMap.move(copyFeatureMap.size() - 1, l);
+ --copyFeatureMapSize;
+ break;
+ }
+ }
+ }
+ }
+ else
+ {
+ copyFeatureMap.add(featureMap.get(k));
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * This Method WILL BE REMOVED when EMF 3.0 is available
+ */
+ protected void copyReference(EReference eReference, EObject eObject, EObject copyEObject)
+ {
+ if (eObject.eIsSet(eReference))
+ {
+ if (eReference.isMany())
+ {
+ InternalEList source = (InternalEList)eObject.eGet(eReference);
+ InternalEList target = (InternalEList)copyEObject.eGet(getTarget(eReference));
+ if (source.isEmpty())
+ {
+ target.clear();
+ }
+ else
+ {
+ boolean isBidirectional = eReference.getEOpposite() != null;
+ int index = 0;
+ for (Iterator k = resolveProxies ? source.iterator() : source.basicIterator(); k.hasNext();)
+ {
+ Object referencedEObject = k.next();
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null)
+ {
+ if (useOriginalReferences && !isBidirectional)
+ {
+ target.addUnique(index, referencedEObject);
+ ++index;
+ }
+ }
+ else
+ {
+ if (isBidirectional)
+ {
+ int position = target.indexOf(copyReferencedEObject);
+ if (position == -1)
+ {
+ target.addUnique(index, copyReferencedEObject);
+ }
+ else if (index != position)
+ {
+ target.move(index, copyReferencedEObject);
+ }
+ }
+ else
+ {
+ target.addUnique(index, copyReferencedEObject);
+ }
+ ++index;
+ }
+ }
+ }
+ }
+ else
+ {
+ Object referencedEObject = eObject.eGet(eReference, resolveProxies);
+ if (referencedEObject == null)
+ {
+ copyEObject.eSet(getTarget(eReference), null);
+ }
+ else
+ {
+ Object copyReferencedEObject = get(referencedEObject);
+ if (copyReferencedEObject == null)
+ {
+ if (useOriginalReferences && eReference.getEOpposite() == null)
+ {
+ copyEObject.eSet(getTarget(eReference), referencedEObject);
+ }
+ }
+ else
+ {
+ copyEObject.eSet(getTarget(eReference), copyReferencedEObject);
+ }
+ }
+ }
+ }
+ }
+ }
+}
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?view=diff&rev=442701&r1=442700&r2=442701
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java Tue Sep 12 13:57:00 2006
@@ -37,6 +37,7 @@
import org.apache.tuscany.sdo.SDOExtendedMetaData;
import org.apache.tuscany.sdo.SDOFactory;
import org.apache.tuscany.sdo.SimpleAnyTypeDataObject;
+import org.apache.tuscany.sdo.helper.CrossScopeCopyHelperImpl;
import org.apache.tuscany.sdo.helper.DataFactoryImpl;
import org.apache.tuscany.sdo.helper.SDOExtendedMetaDataImpl;
import org.apache.tuscany.sdo.helper.TypeHelperImpl;
@@ -72,6 +73,7 @@
import commonj.sdo.Property;
import commonj.sdo.Sequence;
import commonj.sdo.Type;
+import commonj.sdo.helper.CopyHelper;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLHelper;
@@ -319,6 +321,16 @@
public static XMLStreamHelper createXMLStreamHelper(TypeHelper scope)
{
return new XMLStreamHelperImpl(scope);
+ }
+
+ /**
+ * Create a new cross scope CopyHelper.
+ * @param targetScope the TypeHelper containing the Types to use to create the copy objects.
+ * @return the new CopyHelper.
+ */
+ public static CopyHelper createCrossScopeCopyHelper(TypeHelper targetScope)
+ {
+ return new CrossScopeCopyHelperImpl(targetScope);
}
/**
Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java?view=auto&rev=442701
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/CrossScopeCopyTestCase.java Tue Sep 12 13:57:00 2006
@@ -0,0 +1,450 @@
+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.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 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 = SDOUtil.createDataFactory(scopeA).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
+ scopeA = SDOUtil.createTypeHelper();
+ scopeB = SDOUtil.createTypeHelper();
+
+ // Populate scopes with bank model now
+ URL url = getClass().getResource(BANK_MODEL);
+ InputStream inputStream = url.openStream();
+ SDOUtil.createXSDHelper(scopeA).define(inputStream, url.toString());
+ inputStream.close();
+ inputStream = url.openStream();
+ SDOUtil.createXSDHelper(scopeB).define(inputStream, url.toString());
+ inputStream.close();
+
+ // Now Populate scopeA with some dynamic models
+ populateScopeWithDynamicTypes(scopeA);
+
+ // Construct Source Tree
+ constructSourceTree(SDOUtil.createDataFactory(scopeA));
+ }
+
+ 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 <indent; i++) System.out.print(" ");
+ }
+ */
+
+ protected void constructSourceTree(DataFactory df)
+ {
+ // Create Instances
+ bankSDO = df.create(TEST_NAMESPACE, BANK_TYPE );
+ branchSDO1 = df.create(TEST_NAMESPACE, BRANCH_TYPE );
+ branchSDO2 = df.create(TEST_NAMESPACE, BRANCH_TYPE );
+ serviceSDO1 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
+ serviceSDO2 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
+ serviceSDO3 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
+ customerSDO1 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
+ customerSDO2 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
+ customerSDO3 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
+ customerSDO4 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
+
+ // Populate the Bank Instance
+ bankSDO.set("name", "Fourth National");
+ Vector v = new Vector();
+ v.add(branchSDO1);
+ v.add(branchSDO2);
+ bankSDO.set("Branch",v);
+ v.removeAllElements();
+ v.add(serviceSDO1);
+ v.add(serviceSDO2);
+ v.add(serviceSDO3);
+ bankSDO.set("Service",v);
+ v.removeAllElements();
+ v.add(customerSDO1);
+ v.add(customerSDO2);
+ v.add(customerSDO3);
+ v.add(customerSDO4);
+ bankSDO.set("Customer",v);
+ v.removeAllElements();
+
+ // Populate Branch Instances
+ // Branch 1
+ branchSDO1.set("ID", "BR100");
+ DataObject addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "1302 Money Street");
+ addr.set("City", "Apex");
+ addr.set("State", "NC");
+ addr.set("Zip", "27502");
+ branchSDO1.set("Address", addr);
+ v.add("If you are north, head south");
+ v.add("If you are south, head north");
+ branchSDO1.set("Directions", v);
+ v.removeAllElements();
+ DataObject account1 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account1.set("ID", "0000 1200 0001");
+ account1.set("Service", serviceSDO1 );
+ account1.setBigDecimal("Balance", new BigDecimal("3124.12"));
+ v.add(account1);
+ DataObject account2 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account2.set("ID", "0000 8899 0001");
+ account2.set("Service", serviceSDO1 );
+ account2.setBigDecimal("Balance", new BigDecimal("20.00"));
+ v.add(account2);
+ DataObject account3 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account3.set("ID", "0000 3110 0020");
+ account3.set("Service", serviceSDO3 );
+ account3.setBigDecimal("Balance", new BigDecimal("5000.00"));
+ v.add(account3);
+ branchSDO1.set("Account", v);
+ v.removeAllElements();
+ // Branch 2
+ branchSDO2.set("ID", "BR200");
+ addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "1207 Cash Court");
+ addr.set("City", "Raleigh");
+ addr.set("State", "NC");
+ addr.set("Zip", "27701");
+ branchSDO2.set("Address", addr);
+ v.add("If you are east, head west");
+ v.add("If you are west, head east");
+ branchSDO2.set("Directions", v);
+ v.removeAllElements();
+ DataObject account4 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account4.set("ID", "0000 0011 0001");
+ account4.set("Service", serviceSDO1 );
+ account4.setBigDecimal("Balance", new BigDecimal("99.12"));
+ v.add(account4);
+ DataObject account5 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account5.set("ID", "0000 9911 0001");
+ account5.set("Service", serviceSDO2 );
+ account5.setBigDecimal("Balance", new BigDecimal("820.00"));
+ v.add(account5);
+ DataObject account6 = df.create(TEST_NAMESPACE, ACCOUNT_TYPE );
+ account6.set("ID", "0000 0001 0020");
+ account6.set("Service", serviceSDO3 );
+ account6.setBigDecimal("Balance", new BigDecimal("9000.00"));
+ v.add(account6);
+ branchSDO2.set("Account", v);
+ v.removeAllElements();
+
+ // Populate Service Instances
+ serviceSDO1.set("ID", "SRV01");
+ serviceSDO1.set("Name", "Checking");
+ serviceSDO1.setBigDecimal("Fee", new BigDecimal("0.00"));
+ serviceSDO2.set("ID", "SRV02");
+ serviceSDO2.set("Name", "Savings");
+ serviceSDO2.setBigDecimal("Fee", new BigDecimal("0.00"));
+ serviceSDO3.set("ID", "SRV03");
+ serviceSDO3.set("Name", "Loan");
+ serviceSDO3.setBigDecimal("Fee", new BigDecimal("0.00"));
+
+ // Populate Customer Instances
+ // Customer 1
+ customerSDO1.set("ID", "CUST01");
+ customerSDO1.set("First", "James");
+ customerSDO1.set("Last", "Madison");
+ addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "1234 Easy Street");
+ addr.set("City", "New York");
+ addr.set("State", "NY");
+ addr.set("Zip", "27511");
+ customerSDO1.set("Address", addr);
+ customerSDO1.set("HomeBranch", branchSDO1);
+ v.add(account1);
+ customerSDO1.set("Account", v);
+ v.removeAllElements();
+ v.add(customerSDO2);
+ v.add(customerSDO3);
+ customerSDO1.set("Related", v);
+ v.removeAllElements();
+ // Customer 2
+ customerSDO2.set("ID", "CUST02");
+ customerSDO2.set("First", "George");
+ customerSDO2.set("Last", "Washington");
+ addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "1776 Potomac Avenue");
+ addr.set("City", "Washington");
+ addr.set("State", "DC");
+ addr.set("Zip", "50555");
+ customerSDO2.set("Address", addr);
+ customerSDO2.set("HomeBranch", branchSDO1);
+ v.add(account2);
+ v.add(account3);
+ customerSDO2.set("Account", v);
+ v.removeAllElements();
+ // Customer 3
+ customerSDO3.set("ID", "CUST03");
+ customerSDO3.set("First", "Thomas");
+ customerSDO3.set("Last", "Jefferson");
+ addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "1492 Columbus Avenue");
+ addr.set("City", "Charlottesville");
+ addr.set("State", "VA");
+ addr.set("Zip", "20121");
+ customerSDO3.set("Address", addr);
+ customerSDO3.set("HomeBranch", branchSDO2);
+ v.add(account4);
+ customerSDO3.set("Account", v);
+ v.removeAllElements();
+ // Customer 4
+ customerSDO4.set("ID", "CUST04");
+ customerSDO4.set("First", "Benjamin");
+ customerSDO4.set("Last", "Franklin");
+ addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
+ addr.set("Street", "99 Light Street");
+ addr.set("City", "Philadelphia");
+ addr.set("State", "PA");
+ addr.set("Zip", "19251");
+ customerSDO4.set("Address", addr);
+ customerSDO4.set("HomeBranch", branchSDO2);
+ v.add(account5);
+ v.add(account6);
+ customerSDO4.set("Account", v);
+ v.removeAllElements();
+
+ }
+
+ private void populateScopeWithDynamicTypes(TypeHelper scope)
+ {
+ Type stringType = scope.getType("commonj.sdo", "String");
+ DataObject customerType = DataFactory.INSTANCE.create("commonj.sdo", "Type");
+ customerType.set("uri", TEST_NAMESPACE);
+ customerType.set("name", DYNAMIC_TYPE );
+ DataObject custNumProperty = customerType.createDataObject("property");
+ custNumProperty.set("name", "custNum");
+ custNumProperty.set("type", stringType);
+ DataObject firstNameProperty = customerType.createDataObject("property");
+ firstNameProperty.set("name", "firstName");
+ firstNameProperty.set("type", stringType);
+ DataObject lastNameProperty = customerType.createDataObject("property");
+ lastNameProperty.set("name", "lastName");
+ lastNameProperty.set("type", stringType);
+ scope.define(customerType);
+ }
+
+ /*
+ private void dumpObject(DataObject sdo, String node )
+ {
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ SDOUtil.createXMLHelper(scopeA).save(sdo, TEST_NAMESPACE,
+ node, baos);
+ System.out.println(baos.toString());
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ */
+}
Added: incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd?view=auto&rev=442701
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd Tue Sep 12 13:57:00 2006
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ * Copyright (c) 2005-2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ -->
+<xsd:schema targetNamespace="http://www.example.com/bank"
+ xmlns:bank="http://www.example.com/bank"
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+ xmlns:sdoXML="commonj.sdo/xml">
+
+ <xsd:element name="bank" type="bank:bankType"/>
+
+ <xsd:complexType name="bankType">
+ <xsd:sequence>
+ <xsd:element name="Branch" type="bank:branchType" maxOccurs="unbounded" />
+ <xsd:element name="Service" type="bank:serviceType" maxOccurs="unbounded" />
+ <xsd:element name="Customer" type="bank:customerType" maxOccurs="unbounded" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="branchType">
+ <xsd:sequence>
+ <xsd:element name="Address" type="bank:addressType"/>
+ <xsd:element name="Directions" type="xsd:string" maxOccurs="unbounded" />
+ <xsd:element name="Account" type="bank:accountType" maxOccurs="unbounded" />
+ </xsd:sequence>
+ <xsd:attribute name="ID" type="xsd:ID"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="serviceType">
+ <xsd:attribute name="ID" type="xsd:ID"/>
+ <xsd:attribute name="Name" type="xsd:string"/>
+ <xsd:attribute name="Fee" type="xsd:decimal"/>
+ </xsd:complexType>
+
+ <xsd:complexType name="accountType">
+ <xsd:attribute name="ID" type="xsd:ID"/>
+ <xsd:attribute name="Service" type="xsd:IDREF" sdoXML:propertyType="bank:serviceType" />
+ <xsd:attribute name="Balance" type="xsd:decimal" />
+ </xsd:complexType>
+
+ <xsd:complexType name="customerType">
+ <xsd:sequence>
+ <xsd:element name="First" type="xsd:string" />
+ <xsd:element name="Last" type="xsd:string" />
+ <xsd:element name="Address" type="bank:addressType"/>
+ <xsd:element name="HomeBranch" type="xsd:IDREF" sdoXML:propertyType="bank:branchType" />
+ <xsd:element name="Account" type="xsd:IDREF" sdoXML:propertyType="bank:accountType" maxOccurs="unbounded" />
+ </xsd:sequence>
+ <xsd:attribute name="ID" type="xsd:ID"/>
+ <xsd:attribute name="Related" type="xsd:IDREFS" sdoXML:propertyType="bank:customerType" />
+ </xsd:complexType>
+
+ <xsd:complexType name="addressType">
+ <xsd:sequence>
+ <xsd:element name="Street" type="xsd:string"/>
+ <xsd:element name="City" type="xsd:string"/>
+ <xsd:element name="State" type="xsd:string"/>
+ <xsd:element name="Zip" type="xsd:string"/>
+ </xsd:sequence>
+ </xsd:complexType>
+
+ </xsd:schema>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org
Files headers, was: svn commit: r442701
Posted by Jeremy Boynes <jb...@apache.org>.
Please can we make sure the ASF header is included on the source files.
--
Jeremy
On Sep 12, 2006, at 1:57 PM, frankb@apache.org wrote:
> Author: frankb
> Date: Tue Sep 12 13:57:00 2006
> New Revision: 442701
>
> URL: http://svn.apache.org/viewvc?view=rev&rev=442701
> Log:
> Fix for TUSCANY-627
>
> Added:
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
> incubator/tuscany/java/sdo/impl/src/test/java/org/apache/
> tuscany/sdo/test/CrossScopeCopyTestCase.java
> incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd
> Modified:
> incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/util/SDOUtil.java
>
> Added: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/helper/CrossScopeCopyHelperImpl.java
> URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/
> src/main/java/org/apache/tuscany/sdo/helper/
> CrossScopeCopyHelperImpl.java?view=auto&rev=442701
> ======================================================================
> ========
> --- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/helper/CrossScopeCopyHelperImpl.java (added)
> +++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/helper/CrossScopeCopyHelperImpl.java Tue Sep 12
> 13:57:00 2006
> @@ -0,0 +1,236 @@
> +package org.apache.tuscany.sdo.helper;
> +
> +import java.util.Iterator;
> +import java.util.Map;
> +
> +import org.eclipse.emf.ecore.EAttribute;
> +import org.eclipse.emf.ecore.EClass;
> +import org.eclipse.emf.ecore.EObject;
> +import org.eclipse.emf.ecore.EReference;
> +import org.eclipse.emf.ecore.EStructuralFeature;
> +import org.eclipse.emf.ecore.util.EcoreUtil;
> +import org.eclipse.emf.ecore.util.FeatureMap;
> +import org.eclipse.emf.ecore.util.FeatureMapUtil;
> +import org.eclipse.emf.ecore.util.InternalEList;
> +import org.eclipse.emf.ecore.util.EcoreUtil.Copier;
> +
> +import commonj.sdo.DataObject;
> +import commonj.sdo.Type;
> +import commonj.sdo.helper.CopyHelper;
> +import commonj.sdo.helper.TypeHelper;
> +
> +/**
> + * A CopyHelper implementation that creates the copy objects in a
> specific metadata scope.
> + * The target scope must contain a compatible version of the Types
> needed to create the copy objects.
> + */
> +public class CrossScopeCopyHelperImpl implements CopyHelper
> +{
> + protected TypeHelper scope;
> +
> + public CrossScopeCopyHelperImpl(TypeHelper targetScope)
> + {
> + scope = targetScope;
> + }
> +
> + public DataObject copyShallow(DataObject dataObject)
> + {
> + Copier copier = new CrossScopeCopier()
> + {
> + protected void copyContainment(EReference eReference,
> EObject eObject, EObject copyEObject)
> + {
> + }
> + protected void copyAttribute(EAttribute eAttribute, EObject
> eObject, EObject copyEObject)
> + {
> + if (eObject.eIsSet(eAttribute) && !
> FeatureMapUtil.isFeatureMap(eAttribute))
> + {
> + super.copyAttribute(eAttribute,eObject,copyEObject);
> + }
> + }
> + };
> + EObject result = copier.copy((EObject)dataObject);
> + copier.copyReferences();
> + return (DataObject)result;
> + }
> +
> + public DataObject copy(DataObject dataObject)
> + {
> + Copier copier = new CrossScopeCopier();
> + DataObject result = (DataObject)copier.copy((EObject)dataObject);
> + copier.copyReferences();
> + return (DataObject)result;
> + }
> +
> + protected class CrossScopeCopier extends EcoreUtil.Copier
> + {
> + protected boolean useOriginalReferences = false;
> +
> + protected EClass getTarget(EClass eClass)
> + {
> + EClass target = (EClass)get(eClass);
> + if (target == null)
> + {
> + Type type = (Type)eClass;
> + target = (EClass)scope.getType(type.getURI(), type.getName
> ());
> + }
> + return target;
> + }
> +
> + protected EStructuralFeature getTarget(EStructuralFeature
> eStructuralFeature)
> + {
> + EClass eClass = getTarget
> (eStructuralFeature.getEContainingClass());
> + EStructuralFeature targetEf = eClass.getEStructuralFeature
> (eStructuralFeature.getName());
> + return targetEf;
> + }
> +
> + /**
> + * This Method WILL BE REMOVED when EMF 3.0 is available
> + */
> + public void copyReferences()
> + {
> + for (Iterator i = entrySet().iterator(); i.hasNext();)
> + {
> + Map.Entry entry = (Map.Entry)i.next();
> + EObject eObject = (EObject)entry.getKey();
> + EObject copyEObject = (EObject)entry.getValue();
> + EClass eClass = eObject.eClass();
> + for (int j = 0, size = eClass.getFeatureCount(); j < size;
> ++j)
> + {
> + EStructuralFeature eStructuralFeature =
> eClass.getEStructuralFeature(j);
> + if (eStructuralFeature.isChangeable() && !
> eStructuralFeature.isDerived())
> + {
> + if (eStructuralFeature instanceof EReference)
> + {
> + EReference eReference = (EReference)eStructuralFeature;
> + if (!eReference.isContainment() && !
> eReference.isContainer())
> + {
> + copyReference(eReference, eObject, copyEObject);
> + }
> + }
> + else if (FeatureMapUtil.isFeatureMap(eStructuralFeature))
> + {
> + FeatureMap featureMap = (FeatureMap)eObject.eGet
> (eStructuralFeature);
> + FeatureMap copyFeatureMap = (FeatureMap)
> copyEObject.eGet(getTarget(eStructuralFeature));
> + int copyFeatureMapSize = copyFeatureMap.size();
> + for (int k = 0, featureMapSize = featureMap.size();
> k < featureMapSize; ++k)
> + {
> + EStructuralFeature feature =
> featureMap.getEStructuralFeature(k);
> + if (feature instanceof EReference)
> + {
> + Object referencedEObject = featureMap.getValue(k);
> + Object copyReferencedEObject = get
> (referencedEObject);
> + if (copyReferencedEObject == null &&
> referencedEObject != null)
> + {
> + EReference reference = (EReference)feature;
> + if (!useOriginalReferences ||
> reference.isContainment() || reference.getEOpposite() != null)
> + {
> + continue;
> + }
> + copyReferencedEObject = referencedEObject;
> + }
> + // If we can't add it, it must aleady be in the
> list so find it and move it to the end.
> + //
> + if (!copyFeatureMap.add(feature,
> copyReferencedEObject))
> + {
> + for (int l = 0; l < copyFeatureMapSize; ++l)
> + {
> + if (copyFeatureMap.getEStructuralFeature(l)
> == feature && copyFeatureMap.getValue(l) == copyReferencedEObject)
> + {
> + copyFeatureMap.move(copyFeatureMap.size()
> - 1, l);
> + --copyFeatureMapSize;
> + break;
> + }
> + }
> + }
> + }
> + else
> + {
> + copyFeatureMap.add(featureMap.get(k));
> + }
> + }
> + }
> + }
> + }
> + }
> + }
> +
> + /**
> + * This Method WILL BE REMOVED when EMF 3.0 is available
> + */
> + protected void copyReference(EReference eReference, EObject
> eObject, EObject copyEObject)
> + {
> + if (eObject.eIsSet(eReference))
> + {
> + if (eReference.isMany())
> + {
> + InternalEList source = (InternalEList)eObject.eGet
> (eReference);
> + InternalEList target = (InternalEList)copyEObject.eGet
> (getTarget(eReference));
> + if (source.isEmpty())
> + {
> + target.clear();
> + }
> + else
> + {
> + boolean isBidirectional = eReference.getEOpposite() !=
> null;
> + int index = 0;
> + for (Iterator k = resolveProxies ? source.iterator() :
> source.basicIterator(); k.hasNext();)
> + {
> + Object referencedEObject = k.next();
> + Object copyReferencedEObject = get(referencedEObject);
> + if (copyReferencedEObject == null)
> + {
> + if (useOriginalReferences && !isBidirectional)
> + {
> + target.addUnique(index, referencedEObject);
> + ++index;
> + }
> + }
> + else
> + {
> + if (isBidirectional)
> + {
> + int position = target.indexOf
> (copyReferencedEObject);
> + if (position == -1)
> + {
> + target.addUnique(index, copyReferencedEObject);
> + }
> + else if (index != position)
> + {
> + target.move(index, copyReferencedEObject);
> + }
> + }
> + else
> + {
> + target.addUnique(index, copyReferencedEObject);
> + }
> + ++index;
> + }
> + }
> + }
> + }
> + else
> + {
> + Object referencedEObject = eObject.eGet(eReference,
> resolveProxies);
> + if (referencedEObject == null)
> + {
> + copyEObject.eSet(getTarget(eReference), null);
> + }
> + else
> + {
> + Object copyReferencedEObject = get(referencedEObject);
> + if (copyReferencedEObject == null)
> + {
> + if (useOriginalReferences && eReference.getEOpposite
> () == null)
> + {
> + copyEObject.eSet(getTarget(eReference),
> referencedEObject);
> + }
> + }
> + else
> + {
> + copyEObject.eSet(getTarget(eReference),
> copyReferencedEObject);
> + }
> + }
> + }
> + }
> + }
> + }
> +}
>
> Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/util/SDOUtil.java
> URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/
> src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?
> view=diff&rev=442701&r1=442700&r2=442701
> ======================================================================
> ========
> --- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/util/SDOUtil.java (original)
> +++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/
> tuscany/sdo/util/SDOUtil.java Tue Sep 12 13:57:00 2006
> @@ -37,6 +37,7 @@
> import org.apache.tuscany.sdo.SDOExtendedMetaData;
> import org.apache.tuscany.sdo.SDOFactory;
> import org.apache.tuscany.sdo.SimpleAnyTypeDataObject;
> +import org.apache.tuscany.sdo.helper.CrossScopeCopyHelperImpl;
> import org.apache.tuscany.sdo.helper.DataFactoryImpl;
> import org.apache.tuscany.sdo.helper.SDOExtendedMetaDataImpl;
> import org.apache.tuscany.sdo.helper.TypeHelperImpl;
> @@ -72,6 +73,7 @@
> import commonj.sdo.Property;
> import commonj.sdo.Sequence;
> import commonj.sdo.Type;
> +import commonj.sdo.helper.CopyHelper;
> import commonj.sdo.helper.DataFactory;
> import commonj.sdo.helper.TypeHelper;
> import commonj.sdo.helper.XMLHelper;
> @@ -319,6 +321,16 @@
> public static XMLStreamHelper createXMLStreamHelper(TypeHelper
> scope)
> {
> return new XMLStreamHelperImpl(scope);
> + }
> +
> + /**
> + * Create a new cross scope CopyHelper.
> + * @param targetScope the TypeHelper containing the Types to use
> to create the copy objects.
> + * @return the new CopyHelper.
> + */
> + public static CopyHelper createCrossScopeCopyHelper(TypeHelper
> targetScope)
> + {
> + return new CrossScopeCopyHelperImpl(targetScope);
> }
>
> /**
>
> Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/
> tuscany/sdo/test/CrossScopeCopyTestCase.java
> URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/
> src/test/java/org/apache/tuscany/sdo/test/
> CrossScopeCopyTestCase.java?view=auto&rev=442701
> ======================================================================
> ========
> --- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/
> tuscany/sdo/test/CrossScopeCopyTestCase.java (added)
> +++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/
> tuscany/sdo/test/CrossScopeCopyTestCase.java Tue Sep 12 13:57:00 2006
> @@ -0,0 +1,450 @@
> +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.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 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 = SDOUtil.createDataFactory(scopeA).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
> + scopeA = SDOUtil.createTypeHelper();
> + scopeB = SDOUtil.createTypeHelper();
> +
> + // Populate scopes with bank model now
> + URL url = getClass().getResource(BANK_MODEL);
> + InputStream inputStream = url.openStream();
> + SDOUtil.createXSDHelper(scopeA).define(inputStream,
> url.toString());
> + inputStream.close();
> + inputStream = url.openStream();
> + SDOUtil.createXSDHelper(scopeB).define(inputStream,
> url.toString());
> + inputStream.close();
> +
> + // Now Populate scopeA with some dynamic models
> + populateScopeWithDynamicTypes(scopeA);
> +
> + // Construct Source Tree
> + constructSourceTree(SDOUtil.createDataFactory(scopeA));
> + }
> +
> + 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 <indent; i++) System.out.print(" ");
> + }
> + */
> +
> + protected void constructSourceTree(DataFactory df)
> + {
> + // Create Instances
> + bankSDO = df.create(TEST_NAMESPACE, BANK_TYPE );
> + branchSDO1 = df.create(TEST_NAMESPACE, BRANCH_TYPE );
> + branchSDO2 = df.create(TEST_NAMESPACE, BRANCH_TYPE );
> + serviceSDO1 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
> + serviceSDO2 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
> + serviceSDO3 = df.create(TEST_NAMESPACE, SERVICE_TYPE );
> + customerSDO1 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
> + customerSDO2 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
> + customerSDO3 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
> + customerSDO4 = df.create(TEST_NAMESPACE, CUSTOMER_TYPE );
> +
> + // Populate the Bank Instance
> + bankSDO.set("name", "Fourth National");
> + Vector v = new Vector();
> + v.add(branchSDO1);
> + v.add(branchSDO2);
> + bankSDO.set("Branch",v);
> + v.removeAllElements();
> + v.add(serviceSDO1);
> + v.add(serviceSDO2);
> + v.add(serviceSDO3);
> + bankSDO.set("Service",v);
> + v.removeAllElements();
> + v.add(customerSDO1);
> + v.add(customerSDO2);
> + v.add(customerSDO3);
> + v.add(customerSDO4);
> + bankSDO.set("Customer",v);
> + v.removeAllElements();
> +
> + // Populate Branch Instances
> + // Branch 1
> + branchSDO1.set("ID", "BR100");
> + DataObject addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "1302 Money Street");
> + addr.set("City", "Apex");
> + addr.set("State", "NC");
> + addr.set("Zip", "27502");
> + branchSDO1.set("Address", addr);
> + v.add("If you are north, head south");
> + v.add("If you are south, head north");
> + branchSDO1.set("Directions", v);
> + v.removeAllElements();
> + DataObject account1 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account1.set("ID", "0000 1200 0001");
> + account1.set("Service", serviceSDO1 );
> + account1.setBigDecimal("Balance", new BigDecimal("3124.12"));
> + v.add(account1);
> + DataObject account2 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account2.set("ID", "0000 8899 0001");
> + account2.set("Service", serviceSDO1 );
> + account2.setBigDecimal("Balance", new BigDecimal("20.00"));
> + v.add(account2);
> + DataObject account3 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account3.set("ID", "0000 3110 0020");
> + account3.set("Service", serviceSDO3 );
> + account3.setBigDecimal("Balance", new BigDecimal("5000.00"));
> + v.add(account3);
> + branchSDO1.set("Account", v);
> + v.removeAllElements();
> + // Branch 2
> + branchSDO2.set("ID", "BR200");
> + addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "1207 Cash Court");
> + addr.set("City", "Raleigh");
> + addr.set("State", "NC");
> + addr.set("Zip", "27701");
> + branchSDO2.set("Address", addr);
> + v.add("If you are east, head west");
> + v.add("If you are west, head east");
> + branchSDO2.set("Directions", v);
> + v.removeAllElements();
> + DataObject account4 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account4.set("ID", "0000 0011 0001");
> + account4.set("Service", serviceSDO1 );
> + account4.setBigDecimal("Balance", new BigDecimal("99.12"));
> + v.add(account4);
> + DataObject account5 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account5.set("ID", "0000 9911 0001");
> + account5.set("Service", serviceSDO2 );
> + account5.setBigDecimal("Balance", new BigDecimal("820.00"));
> + v.add(account5);
> + DataObject account6 = df.create(TEST_NAMESPACE,
> ACCOUNT_TYPE );
> + account6.set("ID", "0000 0001 0020");
> + account6.set("Service", serviceSDO3 );
> + account6.setBigDecimal("Balance", new BigDecimal("9000.00"));
> + v.add(account6);
> + branchSDO2.set("Account", v);
> + v.removeAllElements();
> +
> + // Populate Service Instances
> + serviceSDO1.set("ID", "SRV01");
> + serviceSDO1.set("Name", "Checking");
> + serviceSDO1.setBigDecimal("Fee", new BigDecimal("0.00"));
> + serviceSDO2.set("ID", "SRV02");
> + serviceSDO2.set("Name", "Savings");
> + serviceSDO2.setBigDecimal("Fee", new BigDecimal("0.00"));
> + serviceSDO3.set("ID", "SRV03");
> + serviceSDO3.set("Name", "Loan");
> + serviceSDO3.setBigDecimal("Fee", new BigDecimal("0.00"));
> +
> + // Populate Customer Instances
> + // Customer 1
> + customerSDO1.set("ID", "CUST01");
> + customerSDO1.set("First", "James");
> + customerSDO1.set("Last", "Madison");
> + addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "1234 Easy Street");
> + addr.set("City", "New York");
> + addr.set("State", "NY");
> + addr.set("Zip", "27511");
> + customerSDO1.set("Address", addr);
> + customerSDO1.set("HomeBranch", branchSDO1);
> + v.add(account1);
> + customerSDO1.set("Account", v);
> + v.removeAllElements();
> + v.add(customerSDO2);
> + v.add(customerSDO3);
> + customerSDO1.set("Related", v);
> + v.removeAllElements();
> + // Customer 2
> + customerSDO2.set("ID", "CUST02");
> + customerSDO2.set("First", "George");
> + customerSDO2.set("Last", "Washington");
> + addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "1776 Potomac Avenue");
> + addr.set("City", "Washington");
> + addr.set("State", "DC");
> + addr.set("Zip", "50555");
> + customerSDO2.set("Address", addr);
> + customerSDO2.set("HomeBranch", branchSDO1);
> + v.add(account2);
> + v.add(account3);
> + customerSDO2.set("Account", v);
> + v.removeAllElements();
> + // Customer 3
> + customerSDO3.set("ID", "CUST03");
> + customerSDO3.set("First", "Thomas");
> + customerSDO3.set("Last", "Jefferson");
> + addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "1492 Columbus Avenue");
> + addr.set("City", "Charlottesville");
> + addr.set("State", "VA");
> + addr.set("Zip", "20121");
> + customerSDO3.set("Address", addr);
> + customerSDO3.set("HomeBranch", branchSDO2);
> + v.add(account4);
> + customerSDO3.set("Account", v);
> + v.removeAllElements();
> + // Customer 4
> + customerSDO4.set("ID", "CUST04");
> + customerSDO4.set("First", "Benjamin");
> + customerSDO4.set("Last", "Franklin");
> + addr = df.create(TEST_NAMESPACE, ADDRESS_TYPE );
> + addr.set("Street", "99 Light Street");
> + addr.set("City", "Philadelphia");
> + addr.set("State", "PA");
> + addr.set("Zip", "19251");
> + customerSDO4.set("Address", addr);
> + customerSDO4.set("HomeBranch", branchSDO2);
> + v.add(account5);
> + v.add(account6);
> + customerSDO4.set("Account", v);
> + v.removeAllElements();
> +
> + }
> +
> + private void populateScopeWithDynamicTypes(TypeHelper scope)
> + {
> + Type stringType = scope.getType("commonj.sdo", "String");
> + DataObject customerType = DataFactory.INSTANCE.create
> ("commonj.sdo", "Type");
> + customerType.set("uri", TEST_NAMESPACE);
> + customerType.set("name", DYNAMIC_TYPE );
> + DataObject custNumProperty = customerType.createDataObject
> ("property");
> + custNumProperty.set("name", "custNum");
> + custNumProperty.set("type", stringType);
> + DataObject firstNameProperty = customerType.createDataObject
> ("property");
> + firstNameProperty.set("name", "firstName");
> + firstNameProperty.set("type", stringType);
> + DataObject lastNameProperty = customerType.createDataObject
> ("property");
> + lastNameProperty.set("name", "lastName");
> + lastNameProperty.set("type", stringType);
> + scope.define(customerType);
> + }
> +
> + /*
> + private void dumpObject(DataObject sdo, String node )
> + {
> + try
> + {
> + ByteArrayOutputStream baos = new ByteArrayOutputStream();
> + SDOUtil.createXMLHelper(scopeA).save(sdo, TEST_NAMESPACE,
> + node, baos);
> + System.out.println(baos.toString());
> + }
> + catch (IOException e)
> + {
> + e.printStackTrace();
> + }
> + }
> + */
> +}
>
> Added: incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd
> URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/
> src/test/resources/bank.xsd?view=auto&rev=442701
> ======================================================================
> ========
> --- incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd
> (added)
> +++ incubator/tuscany/java/sdo/impl/src/test/resources/bank.xsd Tue
> Sep 12 13:57:00 2006
> @@ -0,0 +1,75 @@
> +<?xml version="1.0" encoding="UTF-8"?>
> +<!--
> + * Copyright (c) 2005-2006 The Apache Software Foundation or its
> licensors, as applicable.
> + *
> + * Licensed under the Apache License, Version 2.0 (the "License");
> + * you may not use this file except in compliance with the License.
> + * You may obtain a copy of the License at
> + *
> + * http://www.apache.org/licenses/LICENSE-2.0
> + *
> + * Unless required by applicable law or agreed to in writing,
> software
> + * distributed under the License is distributed on an "AS IS" BASIS,
> + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
> or implied.
> + * See the License for the specific language governing
> permissions and
> + * limitations under the License.
> + -->
> +<xsd:schema targetNamespace="http://www.example.com/bank"
> + xmlns:bank="http://www.example.com/bank"
> + xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> + xmlns:sdoXML="commonj.sdo/xml">
> +
> + <xsd:element name="bank" type="bank:bankType"/>
> +
> + <xsd:complexType name="bankType">
> + <xsd:sequence>
> + <xsd:element name="Branch" type="bank:branchType"
> maxOccurs="unbounded" />
> + <xsd:element name="Service" type="bank:serviceType"
> maxOccurs="unbounded" />
> + <xsd:element name="Customer" type="bank:customerType"
> maxOccurs="unbounded" />
> + </xsd:sequence>
> + <xsd:attribute name="name" type="xsd:string"/>
> + </xsd:complexType>
> +
> + <xsd:complexType name="branchType">
> + <xsd:sequence>
> + <xsd:element name="Address" type="bank:addressType"/>
> + <xsd:element name="Directions" type="xsd:string"
> maxOccurs="unbounded" />
> + <xsd:element name="Account" type="bank:accountType"
> maxOccurs="unbounded" />
> + </xsd:sequence>
> + <xsd:attribute name="ID" type="xsd:ID"/>
> + </xsd:complexType>
> +
> + <xsd:complexType name="serviceType">
> + <xsd:attribute name="ID" type="xsd:ID"/>
> + <xsd:attribute name="Name" type="xsd:string"/>
> + <xsd:attribute name="Fee" type="xsd:decimal"/>
> + </xsd:complexType>
> +
> + <xsd:complexType name="accountType">
> + <xsd:attribute name="ID" type="xsd:ID"/>
> + <xsd:attribute name="Service" type="xsd:IDREF"
> sdoXML:propertyType="bank:serviceType" />
> + <xsd:attribute name="Balance" type="xsd:decimal" />
> + </xsd:complexType>
> +
> + <xsd:complexType name="customerType">
> + <xsd:sequence>
> + <xsd:element name="First" type="xsd:string" />
> + <xsd:element name="Last" type="xsd:string" />
> + <xsd:element name="Address" type="bank:addressType"/>
> + <xsd:element name="HomeBranch" type="xsd:IDREF"
> sdoXML:propertyType="bank:branchType" />
> + <xsd:element name="Account" type="xsd:IDREF"
> sdoXML:propertyType="bank:accountType" maxOccurs="unbounded" />
> + </xsd:sequence>
> + <xsd:attribute name="ID" type="xsd:ID"/>
> + <xsd:attribute name="Related" type="xsd:IDREFS"
> sdoXML:propertyType="bank:customerType" />
> + </xsd:complexType>
> +
> + <xsd:complexType name="addressType">
> + <xsd:sequence>
> + <xsd:element name="Street" type="xsd:string"/>
> + <xsd:element name="City" type="xsd:string"/>
> + <xsd:element name="State" type="xsd:string"/>
> + <xsd:element name="Zip" type="xsd:string"/>
> + </xsd:sequence>
> + </xsd:complexType>
> +
> + </xsd:schema>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-commits-help@ws.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org