/*
 *  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;

import junit.framework.TestCase;
import test.sdo21.framework.TestHelper;
import test.sdo21.framework.junit3_8.CTSTestCase;
import test.sdo21.CTSSuite;
import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Sequence;
import commonj.sdo.Type;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.DataFactory;

/**
 * Tests adding objects to a sequence. Note: the tests in this test case must be run sequentially as each test
 * depends on state from the previous test.
 */
public class SequenceAddOpenTest extends CTSTestCase {

    protected TestHelper testHelper;
    protected DataFactory dataFactory;
    protected TypeHelper typeHelper;
    protected DataObject testOpenDataObject;

    public SequenceAddOpenTest(String string) {
        super(string);
    }

    public void setUp() throws Exception {
        super.setUp();

        HelperContext scope = getScope();
        testHelper = getTestHelper();
        dataFactory = scope.getDataFactory();
        typeHelper = scope.getTypeHelper();

        // define an open type if it is not already defined in the HelperContext provided
        // by the implementation being tested
        String typeURI = "http://www.example.com/cts/SequenceAddOpenTest";
        String typeName = "OpenDataObject";
        Type openType = typeHelper.getType(typeURI, typeName);
        if (openType == null) {
          DataObject typeDef = dataFactory.create("commonj.sdo", "Type");
          typeDef.set("uri", typeURI);
          typeDef.set("name", typeName);
          typeDef.set("open", true);
          typeDef.set("sequenced", true);
          openType = typeHelper.define(typeDef);
        }

        testOpenDataObject = dataFactory.create( openType );
    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(String propertyName, Object value)
     */
    public void testAddBooleanByName() throws Exception {

        //Get a sequence to add to
        Sequence testSequence = testOpenDataObject.getSequence();

        //Add 3 proprerties
        testSequence.add("testBoolean1", Boolean.TRUE);
        testSequence.add("testBoolean2", Boolean.FALSE);
        testSequence.add("testBoolean3", Boolean.TRUE);

        //Check that the size is reported correctly
        //with the size of 3.
        assertEquals(3, testSequence.size());

        //Check the values that were added
        assertEquals(Boolean.TRUE, testSequence.getValue(0));
        assertEquals(Boolean.FALSE, testSequence.getValue(1));
        assertEquals(Boolean.TRUE, testSequence.getValue(2));

        Property prop = testSequence.getProperty(0);

        //OnDemand property should be a string
        //Checking only the first property
        assertEquals("testBoolean1", prop.getName());
        assertEquals("BooleanObject", prop.getType().getName());

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(Property property, Object value)
     */
    public void testAddBooleanByProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "Boolean"));
        prop.set("name", "testBoolean1");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add(openProp, new Boolean(false));
        assertEquals(Boolean.FALSE, testSequence.getValue(0));

        openProp = testSequence.getProperty(0);

        //OnDemand property should be a string
        //Checking only the first property
        assertEquals("testBoolean1", openProp.getName());
        assertEquals("Boolean", openProp.getType().getName());

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(int propertyIndex, Object value)
     */
    public void testAddBooleanByPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add("testBoolean", new Boolean(true));
        testSequence.add("testBoolean", new Boolean(false));

        //Actual value to test from sequence
        testSequence.add(0, new Boolean(false));
        assertEquals(Boolean.FALSE, testSequence.getValue(2));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(int index, String propertyName, Object value)
     */
    public void testAddBooleanByIndexAndPropertyName() throws Exception {

        //Get a sequence from the test fixture DataObject
        Sequence testSequence = testOpenDataObject.getSequence();

        testSequence.add(0, "testBoolean", new Boolean(true));
        assertEquals(Boolean.TRUE, testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(int index, Property property, Object value)
     */
    public void testAddBooleanByIndexAndProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "Boolean"));
        prop.set("name", "testBoolean");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add(0, openProp, new Boolean(false));
        assertEquals(Boolean.FALSE, testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Boolean
     * <p/>
     * boolean add(int index, int propertyIndex, Object value)
     */
    public void testSequenceAddBooleanByIndexAndPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add("testBoolean", new Boolean(true));
        testSequence.add("testBoolean", new Boolean(false));

        //Actual value to test from sequence
        testSequence.add(0, 0, new Boolean(false));
        assertEquals(Boolean.FALSE, testSequence.getValue(2));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(String propertyName, Object value)
     */
    public void testAddStringByName() throws Exception {

        //Get a sequence to add to
        Sequence testSequence = testOpenDataObject.getSequence();

        testSequence.add("testString", new String("testString"));
        assertEquals("testString", testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(Property property, Object value)
     */
    public void testAddStringByProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "String"));
        prop.set("name", "testString");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(openProp, new String("testString"));
        assertEquals("testString", testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(int propertyIndex, Object value)
     */
    public void testAddStringByPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();

        //Add additional duplicate properties
        testSequence.add("testString", new String("testString"));
        testSequence.add("testString", new String("testString1"));

        //Actual value to check
        testSequence.add(0, (Object)new String("testString2"));
        assertEquals("testString2", testSequence.getValue(2));


    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(int index, String propertyName, Object value)
     */
    public void testAddStringByIndexAndPropertyName() throws Exception {

        //Get a sequence from the test fixture DataObject
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(0, "testString", new String("testString"));
        assertEquals("testString", testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(int index, Property property, Object value)
     */
    public void testAddStringByIndexAndProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "String"));
        prop.set("name", "testString");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(0, openProp, new String("testString"));
        assertEquals("testString", testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of String
     * <p/>
     * boolean add(int index, int propertyIndex, Object value)
     */
    public void testSequenceAddStringByIndexAndPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add("testString", new String("testString1"));
        testSequence.add("testString", new String("testString2"));

        //Actual value to test from the sequence
        testSequence.add(0, 0, new String("testString"));
        assertEquals("testString2", testSequence.getValue(2));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(Character propertyName, Object value)
     */
    public void testAddCharacterByName() throws Exception {

        //Get a sequence to add to
        Sequence testSequence = testOpenDataObject.getSequence();

        testSequence.add("testCharacter", new Character('A'));
        assertEquals('A', testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(Property property, Object value)
     */
    public void testAddCharacterByProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "Character"));
        prop.set("name", "testCharacter");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(openProp, new Character('A'));
        assertEquals('A', testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(int propertyIndex, Object value)
     */
    public void testAddCharacterByPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();

        //Add additional duplicate properties
        testSequence.add("testCharacter", new Character('A'));
        testSequence.add("testCharacter", new Character('B'));

        //Actual value to check
        testSequence.add(0, new Character('C'));
        assertEquals('C', testSequence.getValue(2));


    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(int index, Character propertyName, Object value)
     */
    public void testAddCharacterByIndexAndPropertyName() throws Exception {

        //Get a sequence from the test fixture DataObject
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(0, "testCharacter", new Character('A'));
        assertEquals('A', testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(int index, Property property, Object value)
     */
    public void testAddCharacterByIndexAndProperty() throws Exception {

        DataObject prop = dataFactory.create("commonj.sdo", "Property");
        prop.set("type", typeHelper.getType("commonj.sdo", "Character"));
        prop.set("name", "testCharacter");

        Property openProp = typeHelper.defineOpenContentProperty(null, prop);
        Sequence testSequence = testOpenDataObject.getSequence();

        //Actual value to test
        testSequence.add(0, openProp, new Character('A'));
        assertEquals('A', testSequence.getValue(0));

    }

    /**
     * Tests the SDO 2.1 Sequence method for the
     * java type of Character
     * <p/>
     * boolean add(int index, int propertyIndex, Object value)
     */
    public void testSequenceAddCharacterByIndexAndPropertyIndex() throws Exception {

        Sequence testSequence = testOpenDataObject.getSequence();
        testSequence.add("testCharacter", new Character('A'));
        testSequence.add("testCharacter", new Character('B'));

        //Actual value to test from the sequence
        testSequence.add(0, 0, new Character('C'));
        assertEquals('C', testSequence.getValue(0));
        assertEquals('A', testSequence.getValue(1));
        assertEquals('B', testSequence.getValue(2));

    }
}
