/* * 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$ $Date$ */ package test.sdo21.tests.api.DataObject; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import test.sdo21.framework.CTSTestCase; import test.sdo21.tests.util.CTSUtil; import commonj.sdo.DataObject; import commonj.sdo.Property; import commonj.sdo.Type; import commonj.sdo.helper.HelperContext; import commonj.sdo.helper.XMLDocument; import java.util.List; import java.util.ArrayList; /** * Set of tests for DataObject APIs. * Currently limited to a fairly narrow set of tests on the set(Property, value), isSet(), and unSet() * methods. *
* TODO Need to extend the test set or migrate tests from other existing tests as yet unidentified. */ public class DataObjectTest extends CTSTestCase { private boolean debug = false; private static int uniqueNumber = 1; @Before public void setUp() throws Exception { super.setUp(); } /** * This test checks that getInstanceProperties returns all properties * defined by the DataObject's type, regardless of whether they are set or * not. It also checks that open content properties only appear in * getInstanceProperties if they are set. Related sections in the * specification are / 3.1.9 / 3.1.11 Related JIRA SDO-179 * @see 2.1 spec section 3.1.1 * @see 2.1 spec section 3.1.9 * @see 2.1 spec section 3.1.11 * @see SDO Spec JIRA 179 * @throws Exception */ @Test @Ignore("On demand open content property is not yet implemented in Tuscany.") public void testGetInstancePropertiesSize() throws Exception { // define a type with two properties HelperContext helperContext = getScope(); String typeName = getTypeName(); DataObject typeDef = CTSUtil.createTypeDef("", typeName, true, helperContext); CTSUtil.createPropertyDef(typeDef, "p1", "commonj.sdo#String", false, false, helperContext); CTSUtil.createPropertyDef(typeDef, "p2", "commonj.sdo#String", false, false, helperContext); helperContext.getTypeHelper().define(typeDef); // create a DataObject that uses this type DataObject dobj = helperContext.getDataFactory().create("", typeName); // getInstanceProperties() should return p1, p2 even though they are not // set // System.out.println(dobj.getInstanceProperties()); assertEquals(2, dobj.getInstanceProperties().size()); dobj.set("p1", "foo"); // getInstanceProperties() should still return p1, p2 assertEquals(2, dobj.getInstanceProperties().size()); dobj.unset("p1"); // getInstanceProperties() should still return p1, p2 assertEquals(2, dobj.getInstanceProperties().size()); // set an on-demand open content property dobj.set("p3", "foo"); // getInstanceProperties() should now return p1, p2, p3 assertEquals(3, dobj.getInstanceProperties().size()); // unset the on-demand property dobj.unset("p3"); // the spec says that open content properties only appear in // getInstancePropeties if // they are set so we expect the list to be smaller now assertEquals(2, dobj.getInstanceProperties().size()); } /** * Tests an isMany=false Boolean type property in an open type for being set to false/unset. * @see 2.1 spec section 3.1.5 * @see commonj.sdo.DataObject#isSet() * @see commonj.sdo.DataObject#unset() * @see commonj.sdo.DataObject#set(Property, Boolean) * @throws Exception */ @Test public void testIsSet_Boolean_false() throws Exception { // define a type with two properties HelperContext helperContext = getScope(); String typeName = getTypeName(); DataObject typeDef = CTSUtil.createTypeDef("", typeName, true, helperContext); CTSUtil.createPropertyDef(typeDef, "b1", "commonj.sdo#Boolean", false, false, helperContext); helperContext.getTypeHelper().define(typeDef); // create a DataObject that uses this type DataObject testDO = helperContext.getDataFactory().create("", typeName); Property p = testDO.getInstanceProperty("b1"); testDO.unset(p); assertFalse("Property was set", testDO.isSet(p)); testDO.set(p, false); assertTrue("Property was not set ", testDO.isSet(p)); } /** * Tests an isMany=false Boolean type property in an open type for being set to true/unset. * @see 2.1 spec section 3.1.5 * @see commonj.sdo.DataObject#getInstanceProperty(String) * @see commonj.sdo.DataObject#isSet() * @see commonj.sdo.DataObject#unset() * @see commonj.sdo.DataObject#set(Property, Boolean) * @throws Exception */ @Test public void testIsSet_Boolean_true() throws Exception { // define a type with two properties HelperContext helperContext = getScope(); String typeName = getTypeName(); DataObject typeDef = CTSUtil.createTypeDef("", typeName, true, helperContext); CTSUtil.createPropertyDef(typeDef, "b1", "commonj.sdo#Boolean", false, false, helperContext); helperContext.getTypeHelper().define(typeDef); // create a DataObject that uses this type DataObject testDO = helperContext.getDataFactory().create("", typeName); Property p = testDO.getInstanceProperty("b1"); testDO.unset(p); assertFalse("Property was set", testDO.isSet(p)); testDO.set(p, true); assertTrue("Property was not set " + testDO.get(p), testDO .isSet(p)); } /** * Tests isSet() of Integer property where isMany = false in an open type. * @see 2.1 spec section 3.1.5 * @see commonj.sdo.DataObject#isSet() * @see commonj.sdo.DataObject#unset() * @see commonj.sdo.DataObject#set(Property, Integer) * @throws Exception */ @Test public void testIsSet_Integer_0() throws Exception { // define a type with two properties HelperContext helperContext = getScope(); String typeName = getTypeName(); DataObject typeDef = CTSUtil.createTypeDef("", typeName, true, helperContext); CTSUtil.createPropertyDef(typeDef, "i1", "commonj.sdo#Integer", false, false, helperContext); helperContext.getTypeHelper().define(typeDef); // create a DataObject that uses this type DataObject testDO = helperContext.getDataFactory().create("", typeName); Property p = testDO.getInstanceProperty("i1"); testDO.unset(p); assertFalse("Property was set", testDO.isSet(p)); testDO.set(p, java.math.BigInteger.valueOf(0)); assertTrue("Property was not set" + testDO.get(p), testDO.isSet(p)); } /** * Ensures correct behaviour (returns null) on attempting to get a non existent property in an open type.