View Javadoc

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  package tests;
20  
21  import junit.framework.TestCase;
22  import org.apache.ws.commons.schema.*;
23  
24  import javax.xml.namespace.QName;
25  import javax.xml.transform.stream.StreamSource;
26  import java.io.FileInputStream;
27  import java.io.InputStream;
28  
29  /**
30   * TestElementForm
31   */
32  public class TestElementForm extends TestCase {
33      String NS = "http://unqualified-elements.example.com";
34      QName UNQUAL = new QName(NS, "unQualifiedLocals");
35      private XmlSchemaCollection schema;
36  
37      protected void setUp() throws Exception {
38          InputStream is = new FileInputStream(Resources.asURI("elementForm.xsd"));
39          schema = new XmlSchemaCollection();
40          schema.read(new StreamSource(is), null);
41      }
42  
43      public void testLocalElements() throws Exception {
44          XmlSchemaElement element = schema.getElementByQName(UNQUAL);
45          assertNotNull("Couldn't find unQualifiedLocals element", element);
46          XmlSchemaComplexType type = (XmlSchemaComplexType)element.getSchemaType();
47          XmlSchemaSequence seq = (XmlSchemaSequence)type.getParticle();
48          XmlSchemaObjectCollection items = seq.getItems();
49          XmlSchemaElement subElement;
50          subElement = (XmlSchemaElement)items.getItem(0);
51          QName qname = subElement.getQName();
52          assertEquals("Namespace on unqualified element", "", qname.getNamespaceURI());
53          subElement = (XmlSchemaElement)items.getItem(1);
54          qname = subElement.getQName();
55          assertEquals("Bad namespace on qualified element", NS, qname.getNamespaceURI());
56      }
57  }