1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package tests;
21
22 import junit.framework.TestCase;
23 import org.apache.ws.commons.schema.*;
24
25 import javax.xml.namespace.QName;
26 import javax.xml.transform.stream.StreamSource;
27 import java.io.FileInputStream;
28 import java.io.InputStream;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Set;
32
33 /*
34 * Copyright 2004,2007 The Apache Software Foundation.
35 * Copyright 2006 International Business Machines Corp.
36 *
37 * Licensed under the Apache License, Version 2.0 (the "License");
38 * you may not use this file except in compliance with the License.
39 * You may obtain a copy of the License at
40 *
41 * http://www.apache.org/licenses/LICENSE-2.0
42 *
43 * Unless required by applicable law or agreed to in writing, software
44 * distributed under the License is distributed on an "AS IS" BASIS,
45 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
46 * See the License for the specific language governing permissions and
47 * limitations under the License.
48 *
49 */
50 public class ChoiceTest extends TestCase {
51
52 /**
53 * This method will test the choice.
54 *
55 * @throws Exception Any exception encountered
56 */
57 public void testChoice() throws Exception {
58
59 /*
60 <schema xmlns="http://www.w3.org/2001/XMLSchema"
61 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
62 xmlns:tns="http://soapinterop.org/types"
63 targetNamespace="http://soapinterop.org/types">
64
65 <element name="computer">
66 <complexType>
67 <choice>
68 <element name="desktop" type="string"/>
69 <element name="laptop" type="string"/>
70 </choice>
71 </complexType>
72 </element>
73
74 </schema>
75 */
76
77 QName computerElementQname = new QName("http://soapinterop.org/types",
78 "computer");
79 QName mbElementQname = new QName("http://soapinterop.org/types",
80 "motherboard");
81
82 InputStream is = new FileInputStream(Resources.asURI("choice.xsd"));
83 XmlSchemaCollection schemaCol = new XmlSchemaCollection();
84 schemaCol.read(new StreamSource(is), null);
85
86 QName WRONG_QNAME = new QName("http://soapinterop.org/types",
87 "machine");
88 XmlSchemaElement elem = schemaCol.getElementByQName(WRONG_QNAME);
89 assertNull(elem);
90 elem = schemaCol.getElementByQName(computerElementQname);
91 assertEquals("computer", elem.getName());
92 assertEquals(new QName("http://soapinterop.org/types", "computer"),
93 elem.getQName());
94
95 XmlSchemaComplexType cType = (XmlSchemaComplexType)elem.getSchemaType();
96 assertNotNull(cType);
97
98 XmlSchemaChoice choice = (XmlSchemaChoice)cType.getParticle();
99 assertNotNull(choice);
100
101 Set s = new HashSet();
102 s.add("desktop");
103 s.add("laptop");
104 XmlSchemaObjectCollection items = choice.getItems();
105 Iterator iterator = items.getIterator();
106 while (iterator.hasNext()) {
107 XmlSchemaElement e = (XmlSchemaElement)iterator.next();
108 String eName = e.getName();
109 if (eName.equals("desktop")) {
110 assertEquals(new QName("", "desktop"), e.getQName());
111 assertEquals(e.getName(), "desktop");
112 } else if (eName.equals("laptop")) {
113 assertEquals(new QName("", "laptop"), e.getQName());
114 assertEquals(e.getName(), "laptop");
115 } else {
116 fail("Should have had a name of desktop or laptop, but"
117 + " instead had " + eName);
118 }
119 assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
120 "string"), e.getSchemaTypeName());
121 assertTrue(s.remove(e.getName()));
122 }
123 assertTrue("The set should have been empty, but instead contained: "
124 + s + ".",
125 s.isEmpty());
126
127 //test min and max occurs
128 elem = schemaCol.getElementByQName(mbElementQname);
129 assertEquals("motherboard", elem.getName());
130 assertEquals(new QName("http://soapinterop.org/types", "motherboard"),
131 elem.getQName());
132
133 cType = (XmlSchemaComplexType)elem.getSchemaType();
134 assertNotNull(cType);
135
136 choice = (XmlSchemaChoice)cType.getParticle();
137 assertNotNull(choice);
138
139 //values from the XML file
140 assertEquals(choice.getMinOccurs(),1);
141 assertEquals(choice.getMaxOccurs(),6);
142
143 }
144
145 }