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.Iterator;
30  
31  /*
32   * Copyright 2004,2007 The Apache Software Foundation.
33   * Copyright 2006 International Business Machines Corp.
34   *
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
46   *
47   */
48  public class AttributeGroupTest extends TestCase {
49  
50      /**
51       * This method will test the list.
52       *
53       * @throws Exception Any exception encountered
54       */
55      public void testAttributeGroup() throws Exception {
56  
57          /*
58           <schema xmlns="http://www.w3.org/2001/XMLSchema"
59                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
60                   xmlns:tns="http://soapinterop.org/types"
61                   targetNamespace="http://soapinterop.org/types"
62                   attributeFormDefault="qualified" >
63    
64             <attributeGroup name="department">
65               <attribute name="name" type="string"/>
66               <attribute name="id" type="integer"/>
67             </attributeGroup>
68    
69             <element name="member">
70               <complexType>
71                 <attributeGroup ref="tns:department"/>
72               </complexType>
73             </element>
74  
75           </schema>
76          */
77  
78          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
79                                          "member");
80          InputStream is = new FileInputStream(Resources.asURI("attributegroup.xsd"));
81          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
82          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
83  
84          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
85          assertNotNull(elem);
86          assertEquals("member", elem.getName());
87          assertEquals(new QName("http://soapinterop.org/types", "member"),
88                       elem.getQName());
89          
90          XmlSchemaComplexType t = (XmlSchemaComplexType)elem.getSchemaType();
91          assertNotNull(t);
92  
93          XmlSchemaObjectCollection c = t.getAttributes();
94          for (Iterator i = c.getIterator(); i.hasNext(); ) {
95              XmlSchemaAttributeGroupRef agrn = (XmlSchemaAttributeGroupRef)i.next();
96              assertEquals(new QName("http://soapinterop.org/types",
97                                     "department"), agrn.getRefName()); 
98          }
99  
100         XmlSchemaObjectTable attG = schema.getAttributeGroups();
101         assertNotNull(attG);
102         assertEquals(1, attG.getCount());
103         
104         for (Iterator i = attG.getNames(); i.hasNext(); ) {
105             assertEquals("department", ((QName)i.next()).getLocalPart());
106         }
107 
108         for (Iterator i = attG.getValues(); i.hasNext(); ) {
109             Object obj1 = i.next();
110             if (obj1 instanceof XmlSchemaAttributeGroup) {
111                 assertEquals("department", ((XmlSchemaAttributeGroup)obj1).getName().getLocalPart());
112                 XmlSchemaObjectCollection attributes =
113                     ((XmlSchemaAttributeGroup)obj1).getAttributes();
114                 assertNotNull(attributes);
115                 assertEquals(2, attributes.getCount());
116                 for (Iterator j = attributes.getIterator(); j.hasNext(); ) {
117                     XmlSchemaAttribute obj2 = (XmlSchemaAttribute)j.next();
118                     String name = obj2.getName();
119                     if (name.equals("id")) {
120                         assertEquals(new QName("http://soapinterop.org/types", "id"),
121                                      obj2.getQName());
122                         assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
123                                                "integer"), obj2.getSchemaTypeName());
124                     } else if (name.equals("name")) {
125                         assertEquals(new QName("http://soapinterop.org/types", "name"),
126                                      obj2.getQName());
127                         assertEquals(new QName("http://www.w3.org/2001/XMLSchema",
128                                                "string"), obj2.getSchemaTypeName());
129                     } else {
130                         fail("The name \"" + name + "\" should not have been found "
131                              + "for an attribute.");
132 
133                     }
134                 }
135             } else {
136                 fail("There should have been one instance of the "
137                      + "class " + XmlSchemaAttributeGroup.class.getName()
138                      + " , but instead " + obj1.getClass().getName() + " was"
139                      + " found.");
140             }
141         }
142 
143     }
144 
145 }