KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > xs > junit > ParserTestBase


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.xs.junit;
18
19 import junit.framework.TestCase;
20 import org.apache.ws.jaxme.xs.XSAtomicType;
21 import org.apache.ws.jaxme.xs.XSComplexType;
22 import org.apache.ws.jaxme.xs.XSElement;
23 import org.apache.ws.jaxme.xs.XSGroup;
24 import org.apache.ws.jaxme.xs.XSListType;
25 import org.apache.ws.jaxme.xs.XSModelGroup;
26 import org.apache.ws.jaxme.xs.XSParser;
27 import org.apache.ws.jaxme.xs.XSParticle;
28 import org.apache.ws.jaxme.xs.XSSimpleContentType;
29 import org.apache.ws.jaxme.xs.XSSimpleType;
30 import org.apache.ws.jaxme.xs.XSType;
31 import org.apache.ws.jaxme.xs.XSUnionType;
32 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
33 import org.apache.ws.jaxme.xs.xml.XsComplexContentType;
34 import org.xml.sax.SAXException JavaDoc;
35
36 /**
37  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
38  */

39 public abstract class ParserTestBase extends TestCase {
40   protected ParserTestBase(String JavaDoc pName) {
41     super(pName);
42   }
43
44   protected XSParser newXSParser() {
45     XSParser parser = new XSParser();
46     parser.setValidating(false);
47     return parser;
48   }
49
50   protected JAXBParser newJAXBParser() {
51     JAXBParser parser = new JAXBParser();
52     parser.setValidating(false);
53     return parser;
54   }
55
56   protected XSComplexType assertComplexType(XSType pType) throws SAXException JavaDoc {
57     assertTrue(!pType.isSimple());
58     XSComplexType result = pType.getComplexType();
59     assertNotNull(result);
60     boolean haveException = false;
61     try {
62       pType.getSimpleType();
63     } catch (IllegalStateException JavaDoc e) {
64       haveException = true;
65     }
66     assertTrue(haveException);
67     return result;
68   }
69
70   protected XSSimpleType assertSimpleType(XSType pType) throws SAXException JavaDoc {
71     assertTrue(pType.isSimple());
72     XSSimpleType result = pType.getSimpleType();
73     assertNotNull(result);
74     boolean haveException = false;
75     try {
76       pType.getComplexType();
77     } catch (IllegalStateException JavaDoc e) {
78       haveException = true;
79     }
80     assertTrue(haveException);
81     return result;
82   }
83
84   protected XSAtomicType assertAtomicType(XSSimpleType pType) throws SAXException JavaDoc {
85     assertTrue(pType.isAtomic());
86     assertTrue(!pType.isList());
87     assertTrue(!pType.isUnion());
88     XSAtomicType result = pType.getAtomicType();
89     assertNotNull(result);
90     boolean haveException = false;
91     try {
92       pType.getListType();
93     } catch (IllegalStateException JavaDoc e) {
94       haveException = true;
95     }
96     assertTrue(haveException);
97     haveException = false;
98     try {
99       pType.getUnionType();
100     } catch (IllegalStateException JavaDoc e) {
101       haveException = true;
102     }
103     assertTrue(haveException);
104     return result;
105   }
106
107   protected XSListType assertListType(XSSimpleType pType) throws SAXException JavaDoc {
108     assertTrue(!pType.isAtomic());
109     assertTrue(pType.isList());
110     assertTrue(!pType.isUnion());
111     XSListType result = pType.getListType();
112     assertNotNull(result);
113     boolean haveException = false;
114     try {
115       pType.getAtomicType();
116     } catch (IllegalStateException JavaDoc e) {
117       haveException = true;
118     }
119     assertTrue(haveException);
120     haveException = false;
121     try {
122       pType.getUnionType();
123     } catch (IllegalStateException JavaDoc e) {
124       haveException = true;
125     }
126     assertTrue(haveException);
127     return result;
128   }
129
130   protected XSUnionType assertUnionType(XSSimpleType pType) throws SAXException JavaDoc {
131     assertTrue(!pType.isAtomic());
132     assertTrue(!pType.isList());
133     assertTrue(pType.isUnion());
134     XSUnionType result = pType.getUnionType();
135     assertNotNull(result);
136     boolean haveException = false;
137     try {
138       pType.getListType();
139     } catch (IllegalStateException JavaDoc e) {
140       haveException = true;
141     }
142     assertTrue(haveException);
143     haveException = false;
144     try {
145       pType.getAtomicType();
146     } catch (IllegalStateException JavaDoc e) {
147       haveException = true;
148     }
149     assertTrue(haveException);
150     return result;
151   }
152
153   protected XSType assertRestriction(XSSimpleType pType) throws SAXException JavaDoc {
154     assertTrue(pType.isRestriction());
155     XSType result = pType.getRestrictedType();
156     assertNotNull(result);
157     assertSimpleType(result);
158     return result;
159   }
160
161   protected XSParticle assertComplexContent(XSComplexType pType) {
162     assertTrue(!pType.hasSimpleContent());
163     XSParticle result = pType.getParticle();
164     assertNotNull(result);
165     XsComplexContentType ccType = pType.getComplexContentType();
166     assertNotNull(pType.getComplexContentType());
167     int num = 0;
168     if (pType.isElementOnly()) {
169       ++num;
170       assertEquals(XsComplexContentType.ELEMENT_ONLY, ccType);
171     }
172     if (pType.isEmpty()) {
173       ++num;
174       assertEquals(XsComplexContentType.EMPTY, ccType);
175     }
176     if (pType.isMixed()) {
177       ++num;
178       assertEquals(XsComplexContentType.MIXED, ccType);
179     }
180     assertEquals(1, num);
181   
182     num = 0;
183     if (result.isElement()) {
184       ++num;
185       assertNotNull(result.getElement());
186       assertEquals(XSParticle.ELEMENT, result.getType());
187     }
188     if (result.isGroup()) {
189       ++num;
190       assertNotNull(result.getGroup());
191       assertEquals(XSParticle.GROUP, result.getType());
192     }
193     if (result.isWildcard()) {
194       ++num;
195       assertNotNull(result.getWildcard());
196       assertEquals(XSParticle.WILDCARD, result.getType());
197     }
198     assertEquals(1, num);
199   
200     return result;
201   }
202
203   protected XSGroup assertGroup(XSParticle pParticle) {
204     assertTrue(pParticle.isGroup());
205     assertEquals(XSParticle.GROUP, pParticle.getType());
206     XSGroup result = pParticle.getGroup();
207     assertNotNull(result);
208     return result;
209   }
210
211   protected void assertSequence(XSGroup pGroup) {
212     assertTrue(pGroup.isSequence());
213     assertFalse(pGroup.isChoice());
214     assertFalse(pGroup.isAll());
215     assertEquals(XSModelGroup.SEQUENCE, pGroup.getCompositor());
216   }
217
218   protected void assertChoice(XSGroup pGroup) {
219       assertFalse(pGroup.isSequence());
220       assertTrue(pGroup.isChoice());
221       assertFalse(pGroup.isAll());
222       assertEquals(XSModelGroup.CHOICE, pGroup.getCompositor());
223   }
224
225   protected XSElement assertElement(XSParticle pParticle) {
226     assertTrue(pParticle.isElement());
227     assertEquals(XSParticle.ELEMENT, pParticle.getType());
228     XSElement result = pParticle.getElement();
229     assertNotNull(result);
230     return result;
231   }
232
233   protected XSSimpleContentType assertSimpleContent(XSComplexType pType) {
234     assertTrue(pType.hasSimpleContent());
235     XSSimpleContentType result = pType.getSimpleContent();
236     assertNotNull(result);
237     XSType resultType = result.getType();
238     assertNotNull(resultType);
239     assertTrue(resultType.isSimple());
240     boolean haveException = false;
241     try {
242       pType.getComplexContentType();
243     } catch (IllegalStateException JavaDoc e) {
244       haveException = true;
245     }
246     assertTrue(haveException);
247     assertNotNull(result);
248     return result;
249   }
250 }
251
Popular Tags