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