You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by ro...@apache.org on 2006/08/15 15:14:49 UTC

svn commit: r431597 - in /incubator/tuscany/java/sdo/impl/src: main/java/org/apache/tuscany/sdo/util/SDOUtil.java test/java/org/apache/tuscany/sdo/test/JiraTestCases.java test/resources/company.xsd test/resources/po.xsd

Author: robbinspg
Date: Tue Aug 15 06:14:49 2006
New Revision: 431597

URL: http://svn.apache.org/viewvc?rev=431597&view=rev
Log:
TUSCANY-583 Patch applied at request of Kelvin

Added:
    incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java   (with props)
    incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd   (with props)
    incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd   (with props)
Modified:
    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/util/SDOUtil.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/main/java/org/apache/tuscany/sdo/util/SDOUtil.java?rev=431597&r1=431596&r2=431597&view=diff
==============================================================================
--- 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 Aug 15 06:14:49 2006
@@ -28,6 +28,8 @@
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
 
 import org.apache.tuscany.sdo.SDOExtendedMetaData;
 import org.apache.tuscany.sdo.SDOFactory;
@@ -279,6 +281,31 @@
   public static XMLStreamHelper createXMLStreamHelper(TypeHelper scope)
   {
     return new XMLStreamHelperImpl(scope);
+  }
+  
+  /**
+   * Gets all of the types associated with a uri.
+   * 
+   * @param scope
+   *            the TypeHelper to use for locating types.
+   * @param uri.
+   *            Uri of the Types
+   * @return List. List containing instances of Type, null if uri is not found.
+   */
+  public static List getTypes(TypeHelper scope, String uri) {
+
+      EPackage ePackage = ((TypeHelperImpl) scope).getExtendedMetaData().getPackage(uri);
+      if (ePackage != null) {
+          /**
+           * ePackage.getEclassifiers will return an EList ( simple extension of List ).
+           * 
+           * When a Type is generated from XML EMF will create a DocumentRoot type As this is EMF specific it should be removed
+           */
+          List result = new ArrayList(ePackage.getEClassifiers());
+          result.remove(((TypeHelperImpl) scope).getExtendedMetaData().getDocumentRoot(ePackage));
+          return result;
+      }
+      return null;
   }
   
   public static Type createType(TypeHelper scope, String uri, String name, boolean isDataType)

Added: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java?rev=431597&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java (added)
+++ incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java Tue Aug 15 06:14:49 2006
@@ -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();
+		}
+	}
+}

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/java/org/apache/tuscany/sdo/test/JiraTestCases.java
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd?rev=431597&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd Tue Aug 15 06:14:49 2006
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+ <xsd:schema xmlns:company="company.xsd" 
+  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
+  targetNamespace="company.xsd">
+    <xsd:element name="company" type="company:CompanyType"/>
+    <xsd:complexType name="CompanyType">
+	    <xsd:sequence>
+		    <xsd:element name="departments" type="company:DepartmentType" maxOccurs="unbounded"/>
+	    </xsd:sequence>
+	    <xsd:attribute name="name" type="xsd:string"/>
+	    <xsd:attribute name="employeeOfTheMonth" type="xsd:string"/>
+    </xsd:complexType>
+    <xsd:complexType name="DepartmentType">
+	    <xsd:sequence>
+    		<xsd:element name="employees" type="company:EmployeeType" maxOccurs="unbounded"/>
+	    </xsd:sequence>
+	    <xsd:attribute name="name" type="xsd:string"/>	   
+		<xsd:attribute name="location" type="xsd:string"/>
+    	<xsd:attribute name="number" type="xsd:int"/>		
+    </xsd:complexType>
+    <xsd:complexType name="EmployeeType">
+	    <xsd:attribute name="name" type="xsd:string"/>
+	    <xsd:attribute name="SN" type="xsd:ID"/>
+	    <xsd:attribute name="manager" type="xsd:boolean"/>
+    </xsd:complexType>
+ </xsd:schema>

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/company.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev,Date

Added: incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd
URL: http://svn.apache.org/viewvc/incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd?rev=431597&view=auto
==============================================================================
--- incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd (added)
+++ incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd Tue Aug 15 06:14:49 2006
@@ -0,0 +1,62 @@
+<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    xmlns="http://www.example.com/PO" targetNamespace="http://www.example.com/PO">
+
+    <xsd:element name="purchaseOrder" type="PurchaseOrderType"/>
+    <xsd:element name="comment" type="xsd:string"/>
+
+    <xsd:complexType name="PurchaseOrderType">
+        <xsd:sequence>
+            <xsd:element name="shipTo" type="USAddress"/>
+            <xsd:element name="billTo" type="USAddress"/>
+            <xsd:element ref="comment" minOccurs="0"/>
+            <xsd:element name="items"  type="Items"/>
+
+        </xsd:sequence>
+        <xsd:attribute name="orderDate" type="xsd:date"/>
+    </xsd:complexType>
+
+    <xsd:complexType name="USAddress">
+        <xsd:sequence>
+            <xsd:element name="name"   type="xsd:string"/>
+
+            <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:decimal"/>
+        </xsd:sequence>
+        <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
+
+    </xsd:complexType>
+
+    <xsd:complexType name="Items">
+        <xsd:sequence>
+            <xsd:element name="item" minOccurs="0" maxOccurs="unbounded">
+                <xsd:complexType>
+                    <xsd:sequence>
+
+                        <xsd:element name="productName" type="xsd:string"/>
+                        <xsd:element name="price"  type="xsd:decimal"/>
+                        <xsd:element name="quantity">
+                            <xsd:simpleType>
+                                <xsd:restriction base="xsd:positiveInteger">
+                                    <xsd:maxExclusive value="100"/>
+                                </xsd:restriction>
+
+                            </xsd:simpleType>
+                        </xsd:element>
+                        <xsd:element ref="comment"   minOccurs="0"/>
+                        <xsd:element name="shipDate" type="xsd:date" minOccurs="0"/>
+                    </xsd:sequence>
+
+                    <xsd:attribute name="partNum" type="SKU" use="required"/>
+                </xsd:complexType>
+            </xsd:element>
+        </xsd:sequence>
+    </xsd:complexType>
+    <xsd:simpleType name="SKU">
+
+        <xsd:restriction base="xsd:string">
+            <xsd:pattern value="\d{3}-[A-Z]{2}"/>
+        </xsd:restriction>
+    </xsd:simpleType>
+ </xsd:schema>

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/tuscany/java/sdo/impl/src/test/resources/po.xsd
------------------------------------------------------------------------------
    svn:keywords = Rev,Date



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org