001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.schema.testsuite;
020:
021: import org.apache.axiom.om.OMAbstractFactory;
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.util.StAXUtils;
024: import services.echo.types.*;
025:
026: import javax.xml.stream.XMLInputFactory;
027: import javax.xml.stream.XMLStreamReader;
028: import java.io.ByteArrayInputStream;
029:
030: public class CustomElementsTest extends AbstractTest {
031:
032: public static final int MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST = 1;
033: public static final int MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST = 2;
034: public static final int MIN_EQUALS_ONE_NILLABLE_TRUE_TEST = 3;
035: public static final int MIN_EQUALS_ONE_NILLABLE_FALSE_TEST = 4;
036:
037: public void testCustomArray() {
038:
039: BookInformation[] returnObject;
040: System.out.println("minOccurs = 0 and nillable true");
041: try {
042: returnObject = testCustomArray(null,
043: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
044: assertTrue(assertArrayEqual(returnObject,
045: new BookInformation[] { null }));
046: returnObject = testCustomArray(
047: new BookInformation[] { null },
048: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
049: assertTrue(assertArrayEqual(returnObject,
050: new BookInformation[] { null }));
051: returnObject = testCustomArray(
052: new BookInformation[] { getBookInformation() },
053: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
054: assertTrue(assertArrayEqual(returnObject,
055: new BookInformation[] { getBookInformation() }));
056: returnObject = testCustomArray(new BookInformation[] {
057: getBookInformation(), null },
058: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
059: assertTrue(assertArrayEqual(
060: returnObject,
061: new BookInformation[] { getBookInformation(), null }));
062: } catch (Exception e) {
063: fail();
064: }
065: System.out.println("minOccurs = 0 and nillable false");
066: try {
067: returnObject = testCustomArray(null,
068: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
069: assertTrue(assertArrayEqual(returnObject, null));
070: returnObject = testCustomArray(
071: new BookInformation[] { null },
072: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
073: assertTrue(assertArrayEqual(returnObject, null));
074: returnObject = testCustomArray(
075: new BookInformation[] { getBookInformation() },
076: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
077: assertTrue(assertArrayEqual(returnObject,
078: new BookInformation[] { getBookInformation() }));
079: returnObject = testCustomArray(new BookInformation[] {
080: getBookInformation(), null },
081: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
082: assertTrue(assertArrayEqual(returnObject,
083: new BookInformation[] { getBookInformation() }));
084: } catch (Exception e) {
085: e.printStackTrace();
086: fail();
087: }
088: System.out.println("minOccurs = 1 and nillable true");
089: try {
090: returnObject = testCustomArray(null,
091: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
092: assertTrue(assertArrayEqual(returnObject,
093: new BookInformation[] { null }));
094: returnObject = testCustomArray(
095: new BookInformation[] { null },
096: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
097: assertTrue(assertArrayEqual(returnObject,
098: new BookInformation[] { null }));
099: returnObject = testCustomArray(
100: new BookInformation[] { getBookInformation() },
101: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
102: assertTrue(assertArrayEqual(returnObject,
103: new BookInformation[] { getBookInformation() }));
104: returnObject = testCustomArray(new BookInformation[] {
105: getBookInformation(), null },
106: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
107: assertTrue(assertArrayEqual(
108: returnObject,
109: new BookInformation[] { getBookInformation(), null }));
110: } catch (Exception e) {
111: fail();
112: }
113:
114: System.out.println("minOccurs = 1 and nillable false");
115: try {
116: returnObject = testCustomArray(null,
117: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
118: fail();
119: } catch (Exception e) {
120: assertTrue(true);
121: }
122:
123: try {
124: returnObject = testCustomArray(
125: new BookInformation[] { null },
126: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
127: fail();
128: } catch (Exception e) {
129: assertTrue(true);
130: }
131:
132: try {
133: returnObject = testCustomArray(new BookInformation[] {
134: getBookInformation(), null },
135: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
136: fail();
137: } catch (Exception e) {
138: assertTrue(true);
139: }
140:
141: try {
142: returnObject = testCustomArray(
143: new BookInformation[] { getBookInformation() },
144: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
145: assertTrue(assertArrayEqual(returnObject,
146: new BookInformation[] { getBookInformation() }));
147: } catch (Exception e) {
148: fail();
149: }
150:
151: }
152:
153: private BookInformation[] testCustomArray(
154: BookInformation[] innerElement, int type) throws Exception {
155: OMElement omElement;
156: BookInformation[] returnObject = null;
157: String omElementString;
158: switch (type) {
159: case MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST: {
160: TestBookInformation1 testBookInformation = new TestBookInformation1();
161: testBookInformation.setTestValue(innerElement);
162: omElement = testBookInformation.getOMElement(
163: TestBookInformation1.MY_QNAME, OMAbstractFactory
164: .getSOAP12Factory());
165: omElementString = omElement.toStringWithConsume();
166: System.out.println("OMElement ==> " + omElementString);
167: XMLStreamReader xmlReader = StAXUtils
168: .createXMLStreamReader(new ByteArrayInputStream(
169: omElementString.getBytes()));
170: returnObject = TestBookInformation1.Factory
171: .parse(xmlReader).getTestValue();
172: break;
173: }
174: case MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST: {
175: TestBookInformation3 testBookInformation = new TestBookInformation3();
176: testBookInformation.setTestValue(innerElement);
177: omElement = testBookInformation.getOMElement(
178: TestBookInformation3.MY_QNAME, OMAbstractFactory
179: .getSOAP12Factory());
180: omElementString = omElement.toStringWithConsume();
181: System.out.println("OMElement ==> " + omElementString);
182: XMLStreamReader xmlReader = StAXUtils
183: .createXMLStreamReader(new ByteArrayInputStream(
184: omElementString.getBytes()));
185: returnObject = TestBookInformation3.Factory
186: .parse(xmlReader).getTestValue();
187: break;
188: }
189: case MIN_EQUALS_ONE_NILLABLE_TRUE_TEST: {
190: TestBookInformation5 testBookInformation = new TestBookInformation5();
191: testBookInformation.setTestValue(innerElement);
192: omElement = testBookInformation.getOMElement(
193: TestBookInformation5.MY_QNAME, OMAbstractFactory
194: .getSOAP12Factory());
195: omElementString = omElement.toStringWithConsume();
196: System.out.println("OMElement ==> " + omElementString);
197: XMLStreamReader xmlReader = StAXUtils
198: .createXMLStreamReader(new ByteArrayInputStream(
199: omElementString.getBytes()));
200: returnObject = TestBookInformation5.Factory
201: .parse(xmlReader).getTestValue();
202: break;
203: }
204: case MIN_EQUALS_ONE_NILLABLE_FALSE_TEST: {
205: TestBookInformation7 testBookInformation = new TestBookInformation7();
206: testBookInformation.setTestValue(innerElement);
207: omElement = testBookInformation.getOMElement(
208: TestBookInformation7.MY_QNAME, OMAbstractFactory
209: .getSOAP12Factory());
210: omElementString = omElement.toStringWithConsume();
211: System.out.println("OMElement ==> " + omElementString);
212: XMLStreamReader xmlReader = StAXUtils
213: .createXMLStreamReader(new ByteArrayInputStream(
214: omElementString.getBytes()));
215: returnObject = TestBookInformation7.Factory
216: .parse(xmlReader).getTestValue();
217: break;
218: }
219: }
220: return returnObject;
221: }
222:
223: public void testCustom() {
224: BookInformation returnObject;
225: System.out.println("minOccurs = 0 and nillable true");
226: try {
227: returnObject = testCustom(null,
228: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
229: assertTrue(isBookInformationObjectsEquals(returnObject,
230: null));
231: returnObject = testCustom(getBookInformation(),
232: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
233: assertTrue(isBookInformationObjectsEquals(returnObject,
234: getBookInformation()));
235: } catch (Exception e) {
236: fail();
237: }
238: System.out.println("minOccurs = 0 and nillable false");
239: try {
240: returnObject = testCustom(null,
241: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
242: assertTrue(isBookInformationObjectsEquals(returnObject,
243: null));
244: returnObject = testCustom(getBookInformation(),
245: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
246: assertTrue(isBookInformationObjectsEquals(returnObject,
247: getBookInformation()));
248: } catch (Exception e) {
249: fail();
250: }
251: System.out.println("minOccurs = 1 and nillable true");
252: try {
253: returnObject = testCustom(null,
254: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
255: assertTrue(isBookInformationObjectsEquals(returnObject,
256: null));
257: returnObject = testCustom(getBookInformation(),
258: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
259: assertTrue(isBookInformationObjectsEquals(returnObject,
260: getBookInformation()));
261: } catch (Exception e) {
262: fail();
263: }
264: System.out.println("minOccurs = 1 and nillable false");
265: try {
266: returnObject = testCustom(null,
267: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
268: fail();
269: } catch (Exception e) {
270: assertTrue(true);
271: }
272:
273: try {
274: returnObject = testCustom(getBookInformation(),
275: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
276: assertTrue(isBookInformationObjectsEquals(returnObject,
277: getBookInformation()));
278: } catch (Exception e) {
279: fail();
280: }
281:
282: }
283:
284: private BookInformation testCustom(BookInformation innerElement,
285: int type) throws Exception {
286: OMElement omElement;
287: BookInformation returnObject = null;
288: String omElementString;
289: switch (type) {
290: case MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST: {
291: TestBookInformation2 testBookInformation = new TestBookInformation2();
292: testBookInformation.setTestValue(innerElement);
293: omElement = testBookInformation.getOMElement(
294: TestBookInformation2.MY_QNAME, OMAbstractFactory
295: .getSOAP12Factory());
296: omElementString = omElement.toStringWithConsume();
297: System.out.println("OMElement ==> " + omElementString);
298: XMLStreamReader xmlReader = StAXUtils
299: .createXMLStreamReader(new ByteArrayInputStream(
300: omElementString.getBytes()));
301: returnObject = TestBookInformation2.Factory
302: .parse(xmlReader).getTestValue();
303: break;
304: }
305: case MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST: {
306: TestBookInformation4 testBookInformation = new TestBookInformation4();
307: testBookInformation.setTestValue(innerElement);
308: omElement = testBookInformation.getOMElement(
309: TestBookInformation4.MY_QNAME, OMAbstractFactory
310: .getSOAP12Factory());
311: omElementString = omElement.toStringWithConsume();
312: System.out.println("OMElement ==> " + omElementString);
313: XMLStreamReader xmlReader = StAXUtils
314: .createXMLStreamReader(new ByteArrayInputStream(
315: omElementString.getBytes()));
316: returnObject = TestBookInformation4.Factory
317: .parse(xmlReader).getTestValue();
318: break;
319: }
320: case MIN_EQUALS_ONE_NILLABLE_TRUE_TEST: {
321: TestBookInformation6 testBookInformation = new TestBookInformation6();
322: testBookInformation.setTestValue(innerElement);
323: omElement = testBookInformation.getOMElement(
324: TestBookInformation6.MY_QNAME, OMAbstractFactory
325: .getSOAP12Factory());
326: omElementString = omElement.toStringWithConsume();
327: System.out.println("OMElement ==> " + omElementString);
328: XMLStreamReader xmlReader = StAXUtils
329: .createXMLStreamReader(new ByteArrayInputStream(
330: omElementString.getBytes()));
331: returnObject = TestBookInformation6.Factory
332: .parse(xmlReader).getTestValue();
333: break;
334: }
335: case MIN_EQUALS_ONE_NILLABLE_FALSE_TEST: {
336: TestBookInformation8 testBookInformation = new TestBookInformation8();
337: testBookInformation.setTestValue(innerElement);
338: omElement = testBookInformation.getOMElement(
339: TestBookInformation8.MY_QNAME, OMAbstractFactory
340: .getSOAP12Factory());
341: omElementString = omElement.toStringWithConsume();
342: System.out.println("OMElement ==> " + omElementString);
343: XMLStreamReader xmlReader = StAXUtils
344: .createXMLStreamReader(new ByteArrayInputStream(
345: omElementString.getBytes()));
346: returnObject = TestBookInformation8.Factory
347: .parse(xmlReader).getTestValue();
348: break;
349: }
350: }
351: return returnObject;
352: }
353:
354: }
|