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 org.apache.ws.commons.schema.*;
23  import org.apache.ws.commons.schema.constants.Constants;
24  import org.w3c.dom.Attr;
25  
26  import java.io.FileInputStream;
27  import java.io.InputStream;
28  import java.util.Map;
29  
30  import javax.xml.namespace.QName;
31  import javax.xml.transform.stream.StreamSource;
32  
33  import junit.framework.TestCase;
34  
35  /**
36   * Test case for <a
37   * href="https://issues.apache.org/jira/browse/WSCOMMONS-378">WSCOMMONS-378</a>
38   * 
39   * @author Sergey Vladimirov (vlsergey at gmail dot com)
40   */
41  public class WSCommons378Test extends TestCase {
42  
43      /**
44       * Tests that {@link SchemaBuilder} correctly reads additional enumeration
45       * facet attributes
46       * 
47       * @throws Exception
48       *             Any exception encountered
49       */
50      public void test() throws Exception {
51  	InputStream is = new FileInputStream(Resources
52  		.asURI("wscommons-378.xsd"));
53  	XmlSchemaCollection schemaCol = new XmlSchemaCollection();
54  	schemaCol.read(new StreamSource(is), null);
55  
56  	XmlSchemaSimpleType type = (XmlSchemaSimpleType) schemaCol
57  		.getTypeByQName(new QName("foo"));
58  	XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction) type
59  		.getContent();
60  	XmlSchemaObjectCollection facets = restriction.getFacets();
61  
62  	assertEquals(2, facets.getCount());
63  
64  	XmlSchemaEnumerationFacet facet1 = (XmlSchemaEnumerationFacet) facets
65  		.getItem(0);
66  	XmlSchemaEnumerationFacet facet2 = (XmlSchemaEnumerationFacet) facets
67  		.getItem(1);
68  
69  	final Map externalAttributes1 = (Map) facet1.getMetaInfoMap().get(
70  		Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES);
71  	final Map externalAttributes2 = (Map) facet2.getMetaInfoMap().get(
72  		Constants.MetaDataConstants.EXTERNAL_ATTRIBUTES);
73  
74  	assertNotNull(externalAttributes1);
75  	assertNotNull(externalAttributes2);
76  
77  	assertEquals(1, externalAttributes1.size());
78  	assertEquals(1, externalAttributes2.size());
79  
80  	Attr attr1 = (Attr) externalAttributes1.values().iterator().next();
81  	Attr attr2 = (Attr) externalAttributes2.values().iterator().next();
82  
83  	assertNotNull(attr1);
84  	assertNotNull(attr2);
85  
86  	assertEquals("http://testuri.org/", attr1.getNamespaceURI());
87  	assertEquals("http://testuri.org/", attr2.getNamespaceURI());
88  
89  	assertEquals("test", attr1.getPrefix());
90  	assertEquals("test", attr2.getPrefix());
91  
92  	assertEquals("attr1", attr1.getLocalName());
93  	assertEquals("attr2", attr2.getLocalName());
94  
95  	assertEquals("attr1-value", attr1.getValue());
96  	assertEquals("attr2-value", attr2.getValue());
97      }
98  }