/* * 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. * * $Rev: 552500 $ $Date: 2007-07-02 15:18:46 +0100 (Mon, 02 Jul 2007) $ */ package test.sdo21.tests.api.TypeHelper; import commonj.sdo.helper.HelperContext; import commonj.sdo.helper.TypeHelper; import commonj.sdo.DataObject; import commonj.sdo.Type; import test.sdo21.tests.TestData.StandardFactory; import test.sdo21.tests.TestData.TestDataFactory; import test.sdo21.tests.TestData.StandardXSDFactory; import test.sdo21.tests.util.CTSUtil; import test.sdo21.framework.CTSTestCase; import java.util.List; import java.util.ArrayList; import static org.junit.Assert.*; import org.junit.*; public class TypeHelperTest extends CTSTestCase { TestDataFactory factory; public TypeHelperTest() { // Tests on the Property interface should be independent of the metadata creation mechanism // so just pick one Standard Factory factory = new StandardXSDFactory(); } @Before public void setUp() throws Exception { super.setUp(); factory.defineMetaData(getScope()); } @After public void tearDown() throws Exception { super.tearDown(); } /** * Test that newly defined type has correct default values for open, sequenced, abstract, dataType */ @Test public void testNewTypeDefaults() { HelperContext scope = getScope(); TypeHelper typeHelper = scope.getTypeHelper(); DataObject defineDO2 = scope.getDataFactory().create("commonj.sdo", "Type"); defineDO2.set("uri", StandardFactory.TEST_NAMESPACE); defineDO2.set("name", "testNewTypeOpenIsFalse"); Type t = typeHelper.define(defineDO2); assertFalse("Newly created type should have open=false by default", t.isOpen()); assertFalse("Newly created type should have abstract=false by default", t.isAbstract()); assertFalse("Newly created type should have sequenced=false by default", t.isSequenced()); assertFalse("Newly created type should have dataType=false by default", t.isDataType()); } /** * Verify the performance of TypeHelper.getType(URI, Name) * * @throws Exception */ @Test public void getTypeByURI() throws Exception { DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); Type testType = testDO.getType(); TypeHelper typeHelper = getScope().getTypeHelper(); Type returnedType = typeHelper.getType(testType.getURI(), testType.getName()); // TODO I thinkk we can assert that the types are the same instance here? assertTrue("TypeHelper.getType(URI, Name) did not return the expected Type.", CTSUtil.areEqualTypes(returnedType, testType)); } /** * Verify the performance of TypeHelper.getType(Class) * * @throws Exception */ @Test @Ignore("this test currently never reaches the interesting assertion") public void getTypeByClass() throws Exception { DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); Type testType = testDO.getType(); TypeHelper typeHelper = getScope().getTypeHelper(); Type returnedType = typeHelper.getType(testType.getURI(), testType.getName()); if (testType.getInstanceClass() != null) { returnedType = typeHelper.getType(testType.getInstanceClass()); assertTrue("TypeHelper.getType(Class) did not return the expected Type.", CTSUtil.areEqualTypes(returnedType, testType)); } } /** * Verify the proper performance of TypeHelper.getType(Class) when a non-SDO * class is passed to TypeHelper.getType(Class) */ @Test public void getTypeWithNonSDOClass() { TypeHelper typeHelper = getTestHelper().createHelperContext().getTypeHelper(); assertNull("TypeHelper.getType(Class) should return null when no Type was defined for the interface Class.", typeHelper.getType(TypeHelperTest.class)); } /** * Verify the proper performance of TypeHelper.getClass(URI, Name) when the * namespace for URI does not include Name */ @Test public void getTypeByURIWithInvalidName() throws Exception { DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = getScope().getTypeHelper(); assertNull("TypeHelper.getType(URI, Name) should return null when no Type has the indicated Name in the URI namespace.", typeHelper.getType(testDO.getType().getURI(), "UndefinedName")); } /** * Verify the proper performance of TypeHelper.getClass(URI, Name) when the * Name exists but not in the namespace of the URI * * @throws Exception */ @Test public void getTypeByURIWithInvalidURI() throws Exception { DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = getScope().getTypeHelper(); assertNull("TypeHelper.getType(URI, Name) should return null when no Type has the indicated Name in the URI namespace.", typeHelper.getType("UndefinedURI", testDO.getType().getName())); } /** * Verify the performance of TypeHelper.define(DataObject), use * DataFactory.create(URI, name) to verify * * @throws Exception */ @Test public void DefineByDataObjectCreateByURI() throws Exception { HelperContext scope = getScope(); DataObject testDO = factory.createTestData(scope, StandardFactory.API_TYPE); TypeHelper typeHelper = scope.getTypeHelper(); Type stringType = typeHelper.getType("commonj.sdo", "String"); DataObject defineTypeDO = scope.getDataFactory().create("commonj.sdo", "Type"); defineTypeDO.set("uri", StandardFactory.TEST_NAMESPACE); defineTypeDO.set("name", "DefinedType1"); DataObject IDProperty = defineTypeDO.createDataObject("property"); IDProperty.set("name", "ID"); IDProperty.set("type", stringType); DataObject DOProperty = defineTypeDO.createDataObject("property"); DOProperty.set("name", "contained"); DOProperty.set("type", testDO.getType()); typeHelper.define(defineTypeDO); // Verify the Type definition by creating a DataObject of the newly // defined Type via DataFactory.create(URI, name). DataObject result = scope.getDataFactory().create(StandardFactory.TEST_NAMESPACE, defineTypeDO.getString("name")); assertNotNull("CTSSuite.getTestHelper().getDataFactory() returned null", result); assertEquals("CTSSuite.getTestHelper().getDataFactory()e did not create a Type that could be instantiated.", result.getType().getName(), "DefinedType1"); assertNotNull("CTSSuite.getTestHelper().getDataFactory() did not create a Type that could be instantiated, getProperty(ID) was null", result.getInstanceProperty("ID")); } /** * Verify the performance of TypeHelper.define(DataObject), use * DataFactory.create(Type) to verify * * @throws Exception */ @Test public void DefineByDataObjectCreateByType() throws Exception { HelperContext scope = getScope(); DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = scope.getTypeHelper(); Type stringType = typeHelper.getType("commonj.sdo", "String"); DataObject defineTypeDO = scope.getDataFactory().create("commonj.sdo", "Type"); defineTypeDO.set("uri", StandardFactory.TEST_NAMESPACE); defineTypeDO.set("name", "DefinedType2"); DataObject IDProperty = defineTypeDO.createDataObject("property"); IDProperty.set("name", "ID"); IDProperty.set("type", stringType); DataObject DOProperty = defineTypeDO.createDataObject("property"); DOProperty.set("name", "contained"); DOProperty.set("type", testDO.getType()); Type definedType = typeHelper.define(defineTypeDO); // Verify the Type definition by creating a DataObject of the newly // defined Type via DataFactory.create(Type) DataObject result = scope.getDataFactory().create(definedType); assertNotNull("TypeHelper.define(DataObject) returned null", result); assertEquals("TypeHelper.define(DataObject) did not create a Type that could be instantiated.", result .getType().getName(), "DefinedType2"); assertNotNull("TypeHelper.define(DataObject) did not create a Type that could be instantiated, getProperty(ID) was null", result.getInstanceProperty("ID")); } /** * Verify the performance of TypeHelper.define(List) * * @throws Exception */ @Test public void DefineByList() throws Exception { HelperContext scope = getScope(); DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = scope.getTypeHelper(); Type stringType = typeHelper.getType("commonj.sdo", "String"); DataObject define3 = scope.getDataFactory().create("commonj.sdo", "Type"); define3.set("uri", StandardFactory.TEST_NAMESPACE); define3.set("name", "DefinedType3"); DataObject firstNameProperty = define3.createDataObject("property"); firstNameProperty.set("name", "firstName"); firstNameProperty.set("type", stringType); DataObject DOProperty = define3.createDataObject("property"); DOProperty.set("name", "contained"); DOProperty.set("type", testDO.getType()); DataObject define4 = scope.getDataFactory().create("commonj.sdo", "Type"); define4.set("uri", StandardFactory.TEST_NAMESPACE); define4.set("name", "DefinedType4"); DataObject lastNameProperty = define4.createDataObject("property"); lastNameProperty.set("name", "lastName"); lastNameProperty.set("type", stringType); DataObject DOProperty2 = define4.createDataObject("property"); DOProperty2.set("name", "contained"); DOProperty2.set("type", testDO.getType()); List DOList = new ArrayList(); DOList.add(define3); DOList.add(define4); List types = typeHelper.define(DOList); assertEquals("TypeHelper.define(List) should define the same number of Types as is present in List.", 2, types .size()); Type typeInList = (Type) types.get(0); Type define4Type; if (typeInList.getName().equals("DefinedType3")) { typeInList = (Type) types.get(1); assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.", "DefinedType4", typeInList.getName()); define4Type = typeInList; } else { define4Type = typeInList; assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.", "DefinedType4", typeInList.getName()); typeInList = (Type) types.get(1); assertEquals("TypeHelper.define(List) returned a List that did not include Types with the expected names.", "DefinedType3", typeInList.getName()); } // Attempt to create one of the Types using DataFactory.create(URI, // name) DataObject result = scope.getDataFactory().create(StandardFactory.TEST_NAMESPACE, define3.getString("name")); assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(URI, name). result is null.", result); assertEquals("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(URI, name). getType() incorrect", result.getType().getName(), "DefinedType3"); assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(URI, name). firstName property returned null", result.getInstanceProperty("firstName")); // Attempt to create the other type using DataFactory.create(Type) result = scope.getDataFactory().create(define4Type); assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type). result is null.", result); assertEquals("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type). getType() incorrect", result.getType().getName(), "DefinedType4"); assertNotNull("TypeHelper.define(List) did not create a Type that could be instantiated with DataFactory.create(Type). lastName property returned null", result.getInstanceProperty("lastName")); } /** * Verify the error handling of TypeHelper.define(List) when List contains a * member that is not a DataObject * * @throws Exception */ @Test public void DefineByListInvalidListMember() throws Exception { HelperContext scope = getScope(); DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = scope.getTypeHelper(); Type intType = typeHelper.getType("commonj.sdo", "Int"); DataObject define5 = scope.getDataFactory().create("commonj.sdo", "Type"); define5.set("uri", StandardFactory.TEST_NAMESPACE); define5.set("name", "DefinedType5"); DataObject ID1Property = define5.createDataObject("property"); ID1Property.set("name", "ID1"); ID1Property.set("type", intType); DataObject DOProperty = define5.createDataObject("property"); DOProperty.set("name", "contained"); DOProperty.set("type", testDO.getType()); DataObject define6 = scope.getDataFactory().create("commonj.sdo", "Type"); define6.set("uri", StandardFactory.TEST_NAMESPACE); define6.set("name", "DefinedType6"); DataObject ID2Property = define6.createDataObject("property"); ID2Property.set("name", "ID2"); ID2Property.set("type", intType); List DOList = new ArrayList(); DOList.add(define5); DOList.add("A"); DOList.add(define6); try { typeHelper.define(DOList); fail("TypeHelper.define(List) should throw an Exception when List " + "contains a member that is not a DataObject."); } catch (Exception e) { // Do nothing } } /** * Verify the expected behavior of TypeHelper.define(DataObject) when a Type * of the same name has already been defined differently. * * @throws Exception */ @Test @Ignore public void singleRedefinitionDifferent() throws Exception { // TODO complete this test case HelperContext scope = getScope(); DataObject testDO = factory.createTestData(getScope(), StandardFactory.API_TYPE); TypeHelper typeHelper = scope.getTypeHelper(); Type intType = typeHelper.getType("commonj.sdo", "Int"); // Determine what should happen when a Type is redefined (same URI // & Name, but different properties). // When this is known, uncomment the following and supply the // appropriate test. // While it's not clear to what should happen (from the spec), what is // happening is that there is no // notification of the duplication, and the originally defined Type is // returned by the define() in the latter instance. DataObject defineDO = scope.getDataFactory().create("commonj.sdo", "Type"); defineDO.set("uri", StandardFactory.TEST_NAMESPACE); defineDO.set("name", "DefineTypeAgain"); DataObject numProperty = defineDO.createDataObject("property"); numProperty.set("name", "num"); numProperty.set("type", intType); typeHelper.define(defineDO); DataObject defineDO2 = scope.getDataFactory().create("commonj.sdo", "Type"); defineDO2.set("uri", StandardFactory.TEST_NAMESPACE); defineDO2.set("name", "DefineTypeAgain"); DataObject DOProperty = defineDO2.createDataObject("property"); DOProperty.set("name", "contained"); DOProperty.set("type", testDO.getType()); typeHelper.define(defineDO2); } /** * Verify the expected behavior of TypeHelper.define(DataObject) when a Type * of the same name has already been defined, but defined identically. */ @Test @Ignore public void singleRedefinitionSame() { // TODO implement test } /** * Verify the expected behavior of TypeHelper.define(List) when an element * of the List has already been defined differently. */ @Test @Ignore public void listRedefinitionDifferent() { // TODO implement test } /** * Verify the expected behavior of TypeHelper.define(List) when an element * of the List has already been defined identically. */ @Test @Ignore public void listRedefinitionSame() { //TODO implement test } }