1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class AttributeGroupTest extends TestCase {
49
50
51
52
53
54
55 public void testAttributeGroup() throws Exception {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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 }