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/07 02:26:52 UTC
svn commit: r440912 - in /incubator/tuscany/java/sdo/impl/src:
main/java/org/apache/tuscany/sdo/impl/ main/java/org/apache/tuscany/sdo/util/
test/java/org/apache/tuscany/sdo/test/
Author: frankb
Date: Wed Sep 6 17:26:51 2006
New Revision: 440912
URL: http://svn.apache.org/viewvc?view=rev&rev=440912
Log:
Fix for TUSCANY-670
Added:
incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java
Modified:
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/DataGraphImpl.java
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java
incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/DataGraphImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/DataGraphImpl.java?view=diff&rev=440912&r1=440911&r2=440912
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/DataGraphImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/impl/DataGraphImpl.java Wed Sep 6 17:26:51 2006
@@ -24,6 +24,7 @@
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
+import java.util.Iterator;
import java.util.List;
import org.apache.tuscany.sdo.SDOFactory;
@@ -774,7 +775,6 @@
return getWriteReplacement();
}
- /*
public EObject eObjectForURIFragmentSegment(String uriFragmentSegment)
{
if (uriFragmentSegment.startsWith("@models."))
@@ -800,7 +800,6 @@
return super.eObjectForURIFragmentSegment(uriFragmentSegment);
}
}
- */
/**
* <!-- begin-user-doc -->
Modified: incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java?view=diff&rev=440912&r1=440911&r2=440912
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java (original)
+++ incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/DataGraphResourceFactoryImpl.java Wed Sep 6 17:26:51 2006
@@ -543,6 +543,11 @@
}
else if (eDataGraph.getERootObject() == null)
{
+ if (useNewMethods)
+ {
+ handleSchemaLocation();
+ }
+ processSchemaLocations(prefix, name);
if (processAnyXML)
{
// Ensure that anything can be handled, even if it's not recognized.
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=440912&r1=440911&r2=440912
==============================================================================
--- 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 Wed Sep 6 17:26:51 2006
@@ -24,12 +24,15 @@
import java.math.BigInteger;
import java.security.AccessController;
import java.security.PrivilegedExceptionAction;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
-import java.util.Map;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
-import java.util.ArrayList;
+import java.util.Map;
+import java.util.Set;
import org.apache.tuscany.sdo.SDOExtendedMetaData;
import org.apache.tuscany.sdo.SDOFactory;
@@ -67,8 +70,8 @@
import commonj.sdo.DataGraph;
import commonj.sdo.DataObject;
import commonj.sdo.Property;
-import commonj.sdo.Type;
import commonj.sdo.Sequence;
+import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLHelper;
@@ -229,6 +232,36 @@
((DataGraphImpl)dataGraph).getDataGraphResource().save(outputStream, options);
}
+ /**
+ * Registers the specified {@link Type type}(s) to be serialized along with
+ * the {@link DataObject data object}(s) in the graph.
+ * @param dataGraph The DataGraph in which to register the specified type(s)
+ * @param types A list containing the type(s) to be registered or null to automatically register all
+ * types used by the objects in the DataGraph.
+ */
+ public static void registerDataGraphTypes(DataGraph dataGraph, List/*Type*/ types)
+ {
+ ResourceSet resourceSet = ((DataGraphImpl)dataGraph).getResourceSet();
+
+ Set/*EPackage*/ packages = new HashSet();
+ for (final Iterator iterator = types.iterator(); iterator.hasNext(); ) {
+ EClassifier type = (EClassifier)iterator.next();
+ packages.add(type.getEPackage());
+ }
+
+ for (Iterator iterator = packages.iterator(); iterator.hasNext(); ) {
+ EPackage typePackage = (EPackage)iterator.next();
+ Resource resource = typePackage.eResource();
+ if (resource == null) {
+ resource = resourceSet.createResource(URI.createURI(".ecore"));
+ resource.setURI(URI.createURI(typePackage.getNsURI()));
+ resource.getContents().add(typePackage);
+ }
+ else if (resource.getResourceSet() != resourceSet)
+ resourceSet.getResources().add(resource);
+ }
+ }
+
/**
* Create a new TypeHelper instance. The returned type helper will have visibility of types registered
* directly by calling a define method on it or by calling define on an associated XSDHelper. It will
Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java?view=auto&rev=440912
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/SerializeTypesTestCase.java Wed Sep 6 17:26:51 2006
@@ -0,0 +1,130 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tuscany.sdo.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sdo.helper.TypeHelperImpl;
+import org.apache.tuscany.sdo.util.SDOUtil;
+import org.eclipse.emf.ecore.xmi.XMLResource;
+
+import commonj.sdo.DataGraph;
+import commonj.sdo.DataObject;
+import commonj.sdo.Type;
+import commonj.sdo.helper.DataFactory;
+import commonj.sdo.helper.TypeHelper;
+
+public class SerializeTypesTestCase extends TestCase {
+
+ public void testSerializeTypesRoundTrip() throws Exception {
+ TypeHelper types = SDOUtil.createTypeHelper();
+ DataFactory factory = SDOUtil.createDataFactory(types);
+
+ 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);
+
+ TypeHelper deserializingTypeHelper = SDOUtil.createTypeHelper();
+
+ // The following is a kludge to force deserialization of metadata into a different TypeHelper (scope)
+ // TBD figure out a proper non-EMF way to do this.
+ Map options = new HashMap();
+ Object differentFromSerializing = ((TypeHelperImpl) deserializingTypeHelper).getExtendedMetaData();
+ options.put(XMLResource.OPTION_EXTENDED_META_DATA, differentFromSerializing);
+
+ byte[] serialized = baos.toByteArray();
+ ByteArrayInputStream bais = new ByteArrayInputStream(serialized);
+ DataGraph loadedDataGraph = SDOUtil.loadDataGraph(bais, options);
+
+ 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"));
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org