001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.server.axis.assembler;
018:
019: import junit.framework.TestCase;
020: import org.apache.ws.commons.schema.XmlSchemaCollection;
021:
022: import javax.xml.namespace.QName;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025:
026: public class SchemaInfoBuilderTest extends TestCase {
027: public void testSimpleType() throws Exception {
028: XmlSchemaInfo expected = new XmlSchemaInfo();
029: {
030: QName xmlTypeKey = new QName("X", "type");
031:
032: XmlTypeInfo xmlType = new XmlTypeInfo();
033: xmlType.qname = new QName("X", "type");
034: xmlType.anonymous = false;
035: xmlType.simpleBaseType = new QName(
036: "http://www.w3.org/2001/XMLSchema", "integer");
037: xmlType.enumType = false;
038: xmlType.listType = false;
039: xmlType.arrayComponentType = null;
040:
041: expected.types.put(xmlTypeKey, xmlType);
042: }
043:
044: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/SimpleType.xsd");
045: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
046: }
047:
048: public void testNestedElements() throws Exception {
049: XmlSchemaInfo expected = new XmlSchemaInfo();
050: {
051: QName xmlTypeKey = new QName("X", "type");
052:
053: XmlTypeInfo xmlType = new XmlTypeInfo();
054: xmlType.qname = new QName("X", "type");
055: xmlType.anonymous = false;
056: xmlType.simpleBaseType = null;
057: xmlType.enumType = false;
058: xmlType.listType = false;
059: xmlType.arrayComponentType = null;
060: xmlType.attributes.put("stringAttribute", new QName(
061: "http://www.w3.org/2001/XMLSchema", "string"));
062: xmlType.attributes.put("intAttribute", new QName(
063: "http://www.w3.org/2001/XMLSchema", "integer"));
064:
065: {
066: QName key = new QName("X", "string");
067:
068: XmlElementInfo xmlElement = new XmlElementInfo();
069: xmlElement.qname = new QName("X", "string");
070: xmlElement.xmlType = new QName(
071: "http://www.w3.org/2001/XMLSchema", "string");
072: xmlElement.minOccurs = 1;
073: xmlElement.maxOccurs = 1;
074: xmlElement.nillable = false;
075:
076: xmlType.elements.put(key, xmlElement);
077: }
078: {
079: QName key = new QName("X", "int");
080:
081: XmlElementInfo xmlElement = new XmlElementInfo();
082: xmlElement.qname = new QName("X", "int");
083: xmlElement.xmlType = new QName(
084: "http://www.w3.org/2001/XMLSchema", "integer");
085: xmlElement.minOccurs = 1;
086: xmlElement.maxOccurs = 1;
087: xmlElement.nillable = false;
088:
089: xmlType.elements.put(key, xmlElement);
090: }
091: expected.types.put(xmlTypeKey, xmlType);
092: }
093:
094: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/ComplexSequenceType.xsd");
095: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
096:
097: schemaInfo = loadSchemaInfo("schema/ComplexAllType.xsd");
098: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
099: }
100:
101: public void testSoapArrayByAttribute() throws Exception {
102: XmlSchemaInfo expected = new XmlSchemaInfo();
103: {
104: QName xmlTypeKey = new QName("X", "array");
105:
106: XmlTypeInfo xmlType = new XmlTypeInfo();
107: xmlType.qname = new QName("X", "array");
108: xmlType.anonymous = false;
109: xmlType.simpleBaseType = null;
110: xmlType.enumType = false;
111: xmlType.listType = false;
112: xmlType.arrayComponentType = new QName(
113: "http://www.w3.org/2001/XMLSchema", "integer[]");
114:
115: expected.types.put(xmlTypeKey, xmlType);
116: }
117:
118: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/SoapArrayByAttribute.xsd");
119: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
120: }
121:
122: public void testSoapArrayByRestriction() throws Exception {
123: XmlSchemaInfo expected = new XmlSchemaInfo();
124: {
125: QName xmlTypeKey = new QName("X", "array");
126:
127: XmlTypeInfo xmlType = new XmlTypeInfo();
128: xmlType.qname = new QName("X", "array");
129: xmlType.anonymous = false;
130: xmlType.simpleBaseType = null;
131: xmlType.enumType = false;
132: xmlType.listType = false;
133: xmlType.arrayComponentType = new QName(
134: "http://www.w3.org/2001/XMLSchema", "integer");
135:
136: expected.types.put(xmlTypeKey, xmlType);
137: }
138:
139: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/SoapArrayByRestriction.xsd");
140: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
141: }
142:
143: public void testSimpleElement() throws Exception {
144: XmlSchemaInfo expected = new XmlSchemaInfo();
145: {
146: QName xmlElementKey = new QName("X", "element");
147:
148: XmlElementInfo xmlElement = new XmlElementInfo();
149: xmlElement.qname = new QName("X", "element");
150: xmlElement.xmlType = new QName("X", ">element");
151: xmlElement.minOccurs = 1;
152: xmlElement.maxOccurs = 1;
153: xmlElement.nillable = false;
154:
155: expected.elements.put(xmlElementKey, xmlElement);
156: }
157: {
158: QName xmlTypeKey = new QName("X", ">element");
159:
160: XmlTypeInfo xmlType = new XmlTypeInfo();
161: xmlType.qname = new QName("X", ">element");
162: xmlType.anonymous = true;
163: xmlType.simpleBaseType = new QName(
164: "http://www.w3.org/2001/XMLSchema", "integer");
165: xmlType.enumType = false;
166: xmlType.listType = false;
167: xmlType.arrayComponentType = null;
168:
169: expected.types.put(xmlTypeKey, xmlType);
170: }
171:
172: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/SimpleElement.xsd");
173: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
174: }
175:
176: public void testComplexElement() throws Exception {
177: XmlSchemaInfo expected = new XmlSchemaInfo();
178: {
179: QName xmlElementKey = new QName("X", "element");
180:
181: XmlElementInfo xmlElement = new XmlElementInfo();
182: xmlElement.qname = new QName("X", "element");
183: xmlElement.xmlType = new QName("X", ">element");
184: xmlElement.minOccurs = 1;
185: xmlElement.maxOccurs = 1;
186: xmlElement.nillable = false;
187:
188: expected.elements.put(xmlElementKey, xmlElement);
189: }
190: {
191: QName xmlTypeKey = new QName("X", ">element");
192:
193: XmlTypeInfo xmlType = new XmlTypeInfo();
194: xmlType.qname = new QName("X", ">element");
195: xmlType.anonymous = true;
196: xmlType.simpleBaseType = null;
197: xmlType.enumType = false;
198: xmlType.listType = false;
199: xmlType.arrayComponentType = null;
200: xmlType.attributes.put("stringAttribute", new QName(
201: "http://www.w3.org/2001/XMLSchema", "string"));
202: xmlType.attributes.put("intAttribute", new QName(
203: "http://www.w3.org/2001/XMLSchema", "integer"));
204:
205: {
206: QName key = new QName("X", "string");
207:
208: XmlElementInfo xmlElement = new XmlElementInfo();
209: xmlElement.qname = new QName("X", "string");
210: xmlElement.xmlType = new QName(
211: "http://www.w3.org/2001/XMLSchema", "string");
212: xmlElement.minOccurs = 1;
213: xmlElement.maxOccurs = 1;
214: xmlElement.nillable = false;
215:
216: xmlType.elements.put(key, xmlElement);
217: }
218: {
219: QName key = new QName("X", "int");
220:
221: XmlElementInfo xmlElement = new XmlElementInfo();
222: xmlElement.qname = new QName("X", "int");
223: xmlElement.xmlType = new QName(
224: "http://www.w3.org/2001/XMLSchema", "integer");
225: xmlElement.minOccurs = 1;
226: xmlElement.maxOccurs = 1;
227: xmlElement.nillable = false;
228:
229: xmlType.elements.put(key, xmlElement);
230: }
231: expected.types.put(xmlTypeKey, xmlType);
232: }
233:
234: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/ComplexSequenceElement.xsd");
235: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
236:
237: schemaInfo = loadSchemaInfo("schema/ComplexAllElement.xsd");
238: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
239: }
240:
241: public void testJaxRpcSpecExample() throws Exception {
242: XmlSchemaInfo expected = new XmlSchemaInfo();
243:
244: // <complexType name="root">
245: // <sequence>
246: // <element name="data" type="string"/>
247: // </sequence>
248: // </complexType>
249: {
250: QName xmlTypeKey = new QName("X", "root");
251:
252: XmlTypeInfo xmlType = new XmlTypeInfo();
253: xmlType.qname = new QName("X", "root");
254: xmlType.anonymous = false;
255: xmlType.simpleBaseType = null;
256: xmlType.enumType = false;
257: xmlType.listType = false;
258: xmlType.arrayComponentType = null;
259: {
260: QName xmlElementKey = new QName("X", "data");
261:
262: XmlElementInfo xmlElement = new XmlElementInfo();
263: xmlElement.qname = new QName("X", "data");
264: xmlElement.xmlType = new QName(
265: "http://www.w3.org/2001/XMLSchema", "string");
266: xmlElement.minOccurs = 1;
267: xmlElement.maxOccurs = 1;
268: xmlElement.nillable = false;
269:
270: xmlType.elements.put(xmlElementKey, xmlElement);
271: }
272:
273: expected.types.put(xmlTypeKey, xmlType);
274: }
275:
276: // <element name="root">
277: // <complexType>
278: // <sequence>
279: // <element name="inside" maxOccurs="10">
280: // <complexType>
281: // <sequence>
282: // <element name="data2" type="string"/>
283: // </sequence>
284: // </complexType>
285: // </element>
286: // <element ref="tns:someOtherElement" maxOccurs="20"/>
287: // </sequence>
288: // </complexType>
289: // </element>
290: {
291: QName xmlElementKey = new QName("X", "root");
292:
293: XmlElementInfo xmlElement = new XmlElementInfo();
294: xmlElement.qname = new QName("X", "root");
295: xmlElement.xmlType = new QName("X", ">root");
296: xmlElement.minOccurs = 1;
297: xmlElement.maxOccurs = 1;
298: xmlElement.nillable = false;
299:
300: expected.elements.put(xmlElementKey, xmlElement);
301: }
302: {
303: QName xmlTypeKey = new QName("X", ">root");
304:
305: XmlTypeInfo xmlType = new XmlTypeInfo();
306: xmlType.qname = new QName("X", ">root");
307: xmlType.anonymous = true;
308: xmlType.simpleBaseType = null;
309: xmlType.enumType = false;
310: xmlType.listType = false;
311: xmlType.arrayComponentType = null;
312: {
313: QName xmlElementKey = new QName("X", "inside");
314:
315: XmlElementInfo xmlElement = new XmlElementInfo();
316: xmlElement.qname = new QName("X", "inside");
317: xmlElement.xmlType = new QName("X", ">>root>inside");
318: xmlElement.minOccurs = 1;
319: xmlElement.maxOccurs = 10;
320: xmlElement.nillable = false;
321:
322: xmlType.elements.put(xmlElementKey, xmlElement);
323: }
324: {
325: QName xmlElementKey = new QName("X", "someOtherElement");
326:
327: XmlElementInfo xmlElement = new XmlElementInfo();
328: xmlElement.qname = new QName("X", "someOtherElement");
329: xmlElement.xmlType = new QName(
330: "http://www.w3.org/2001/XMLSchema", "int");
331: xmlElement.minOccurs = 1;
332: xmlElement.maxOccurs = 20;
333: xmlElement.nillable = false;
334:
335: xmlType.elements.put(xmlElementKey, xmlElement);
336: }
337:
338: expected.types.put(xmlTypeKey, xmlType);
339: }
340: {
341: QName xmlTypeKey = new QName("X", ">>root>inside");
342:
343: XmlTypeInfo xmlType = new XmlTypeInfo();
344: xmlType.qname = new QName("X", ">>root>inside");
345: xmlType.anonymous = true;
346: xmlType.simpleBaseType = null;
347: xmlType.enumType = false;
348: xmlType.listType = false;
349: xmlType.arrayComponentType = null;
350: {
351: QName xmlElementKey = new QName("X", "data2");
352:
353: XmlElementInfo xmlElement = new XmlElementInfo();
354: xmlElement.qname = new QName("X", "data2");
355: xmlElement.xmlType = new QName(
356: "http://www.w3.org/2001/XMLSchema", "string");
357: xmlElement.minOccurs = 1;
358: xmlElement.maxOccurs = 1;
359: xmlElement.nillable = false;
360:
361: xmlType.elements.put(xmlElementKey, xmlElement);
362: }
363:
364: expected.types.put(xmlTypeKey, xmlType);
365: }
366:
367: // <element name="someOtherElement" type="xsd:int"/>
368: {
369: QName xmlElementKey = new QName("X", "someOtherElement");
370:
371: XmlElementInfo xmlElement = new XmlElementInfo();
372: xmlElement.qname = new QName("X", "someOtherElement");
373: xmlElement.xmlType = new QName(
374: "http://www.w3.org/2001/XMLSchema", "int");
375: xmlElement.minOccurs = 1;
376: xmlElement.maxOccurs = 1;
377: xmlElement.nillable = false;
378:
379: expected.elements.put(xmlElementKey, xmlElement);
380: }
381:
382: XmlSchemaInfo schemaInfo = loadSchemaInfo("schema/JaxRpcSpecExample.xsd");
383: TypeInfoTestUtil.assertEqual(expected, schemaInfo, true);
384: }
385:
386: private XmlSchemaInfo loadSchemaInfo(String fileName)
387: throws Exception {
388: InputStream in = getClass().getClassLoader()
389: .getResourceAsStream(fileName);
390: XmlSchemaCollection xmlSchemaCollection = new XmlSchemaCollection();
391: xmlSchemaCollection.read(new InputStreamReader(in), null);
392: CommonsSchemaInfoBuilder schemaInfoBuilder = new CommonsSchemaInfoBuilder(
393: xmlSchemaCollection);
394: XmlSchemaInfo schemaInfo = schemaInfoBuilder.createSchemaInfo();
395: return schemaInfo;
396: }
397: }
|