summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk-cts/sdo2.1/src/main/java/test/sdo21/tests/general/XSDHelperTest.java
blob: 1705f00e8a7909bd7d9b35574401673f6c41e888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 *  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.general;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

//import org.junit.Before;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

import org.apache.ws.commons.schema.XmlSchema;
import org.apache.ws.commons.schema.XmlSchemaCollection;
import org.apache.ws.commons.schema.XmlSchemaComplexType;
import org.apache.ws.commons.schema.XmlSchemaObject;
import org.apache.ws.commons.schema.XmlSchemaObjectTable;
import org.apache.ws.commons.schema.XmlSchemaType;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.Ignore;

import test.sdo21.CTSSuite;
import test.sdo21.framework.CTSTestCase;

import commonj.sdo.DataObject;
import commonj.sdo.Type;
import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XSDHelper;

/**
 * Tests XSD serialization/deserialization.<p/>
 * This tests requires extension by import or creation of further tests.
 * It currently only tests one flavour of {@link XSDHelper#define(java.io.InputStream, String)} and {@link XSDHelper#generate(List)})
 * 
 * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=52">2.1 spec section 3.13</a>
 * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=56">2.1 spec section 4</a>
 * 
 */
public class XSDHelperTest extends CTSTestCase {
    private static final String TEST_MODEL = "/simple.xsd";
    private XmlSchemaCollection col = new XmlSchemaCollection();
    private static URL modelURL;

    /**
     * Obtains test model resource. Runs once before any of the test methods.
     */
    @BeforeClass
    public static void obtainResource() {
        modelURL = XSDHelperTest.class.getResource(TEST_MODEL);
    }
    
    @Before public void setUp() throws Exception
    {
      super.setUp();
    }
    
    @After
    public void tearDown() throws Exception {
      super.tearDown();
    }

    /**
     * Verifies the performance of XSDHelper.define() when a SchemaLocation is provided.
     * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=52">2.1 spec section 3.13</a>
     * @see commonj.sdo.XSDHelper#define(InputStream, String)
     */
    @Test
    public void testDefineWithLocation() {
        try {

            XSDHelper xsdHelper = getScope().getXSDHelper();
            List types = xsdHelper.define(modelURL.openStream(), modelURL.toString());
            checkTypes(modelURL, getScope().getTypeHelper(), types);
         } catch (Exception e) {
            fail("Exception calling xsdHelper.define" + e.toString());
        }
    }

    
    /**
     * Utility method to ensure that the set of types in the list includes all those explicity
     * defined in the schema<p>
     * Checking xsd type definition post condition ---<br/>
     * <i>for all t in schemaTypes there exists a t' in types such that QName(t) == QName(t')</i>
     * @param modelUrl location of the schema related to the set of types
     * @param typeHelper associated with the scope for the types in the list
     * @param types
     */
    private void checkTypes(URL modelUrl, TypeHelper typeHelper, List types) {
      try {
          XmlSchema schema = col.read(new StreamSource(modelUrl.openStream()), null);
          XmlSchemaObjectTable schemaTypes = schema.getSchemaTypes();
          
          Iterator<XmlSchemaType> it = schemaTypes.getValues();
          XmlSchemaType schemaType;
          while (it.hasNext()){
              schemaType = it.next();
              QName qname = schemaType.getQName();
              Type sdoType = typeHelper.getType(qname.getNamespaceURI(), qname.getLocalPart());
              assertNotNull("Type not known to SDO environment: "+ qname, sdoType);
              assertTrue("Sdo type not created from this invocation of type definition", types.contains(sdoType));
          }
      } catch (IOException e) {

          fail("Exception parsing schema" + e);
      }
  }

    /**
     * Verifies the performance of XSDHelper.define() when a SchemaLocation is not provided.
     * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=52">2.1 spec section 3.13</a>
     * @see commonj.sdo.XSDHelper#define(InputStream, String)
     */
    @Test
    public void testDefineWithNoLocation() {
        try {
            XSDHelper xsdHelper = getScope().getXSDHelper();
            List types = xsdHelper.define(XSDHelperTest.class.getResourceAsStream(TEST_MODEL), null);
            checkTypes(modelURL, getScope().getTypeHelper(), types);
            //assertEquals("XSDHelper.define() did not create the expected number of Types", 2, types.size());

        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception caught" + e.toString());
        }
    }

    /**
     * Verifies that duplicate Types are not redefined.
     * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=52">2.1 spec section 3.13</a>
     * @see commonj.sdo.XSDHelper#define(InputStream, String)
     */
    @Test
    public void testDuplicateDefineWithLocation() {
        try {
            XSDHelper xsdHelper = getScope().getXSDHelper();
            List types = xsdHelper.define(modelURL.openStream(), modelURL.toString());
            assertTrue("XSDHelper.define() did not create the expected number of Types", types.size() > 0);
            // redefine type
            List duplicateTypes = xsdHelper.define(modelURL.openStream(), modelURL.toString());
            assertEquals("XSDHelper.define() did not create the expected number of Types", 0, duplicateTypes.size());

        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception caught" + e.toString());
        }
    }

    /**
     * Verifies the performance of XSDHelper.generate for dynamic SDOs with no XSD model.<p/>
     * Could improve this by postconditions on generated schema. 
     * @see commonj.sdo.XSDHelper#generate(InputStream, String)
     * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=53">2.1 spec section 3.13.2</a>
     * @see <a href="http://osoa.org/download/attachments/36/Java-SDO-Spec-v2.1.0-FINAL.pdf?version=1#page=56">2.1 spec section 4</a>
     */
    @Test
    public void testXSDGeneration_DynamicSDOType() {
        try {
            boolean exceptionCaught = false;

            // test for dynamic SDOs that have no XSD model. Here the testcase
            // succeeds only if the
            // xsd is generated by XSDHelper in which case xsd must not be null
            XSDHelper xsdHelper = getScope().getXSDHelper();
            DataObject quoteType = getScope().getDataFactory().create("commonj.sdo", "Type");
            quoteType.set("uri", "http://www.example.com/dynamic");
            quoteType.set("name", "DynamicQuote");

            TypeHelper th = getScope().getTypeHelper();
            DataObject aProperty = quoteType.createDataObject("property");
            aProperty.set("name", "symbol");
            aProperty.set("type", th.getType("commonj.sdo", "String"));

            aProperty = quoteType.createDataObject("property");
            aProperty.set("name", "price");
            aProperty.set("type", th.getType("commonj.sdo", "Decimal"));

            aProperty = quoteType.createDataObject("property");
            aProperty.set("name", "volume");
            aProperty.set("type", th.getType("commonj.sdo", "Double"));

            th.define(quoteType);

            Type dynamicQuoteType = th.getType("http://www.example.com/dynamic", "DynamicQuote");

            Vector types = new Vector();
            types.add(dynamicQuoteType);
            String xsd = null;

            try {
                xsd = xsdHelper.generate(types);
                assertNotNull("XSDHelper.generate() did not complete as expected for dynamic SDOs with no XSD model.  Exception was thrown",
                    xsd);
            } catch (IllegalArgumentException e) {
                fail("XSDHelper.generate() did not complete as expected for dynamic SDOs with no XSD model.  Exception was thrown : " + e
                    .toString());
            } catch (Exception e) {
                fail("Exception caught when generating a schema");
            }

        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception caught" + e.toString());
        }
    }
    
}