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 StringElementsTest 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 testStringArray() {
038: System.out.println("Test minOccurs 0 nillable true");
039: String[] returnObject = null;
040:
041: try {
042: returnObject = testStringArray(null,
043: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
044: assertTrue(assertArrayEqual(returnObject,
045: new String[] { null }));
046: returnObject = testStringArray(new String[] { null },
047: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
048: assertTrue(assertArrayEqual(returnObject,
049: new String[] { null }));
050: returnObject = testStringArray(new String[] { "test" },
051: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
052: assertTrue(assertArrayEqual(returnObject,
053: new String[] { "test" }));
054: returnObject = testStringArray(
055: new String[] { "test", null },
056: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
057: assertTrue(assertArrayEqual(returnObject, new String[] {
058: "test", null }));
059: } catch (Exception e) {
060: fail();
061: }
062:
063: System.out.println("Test minOccurs = 0 nillable false");
064: try {
065: returnObject = testStringArray(null,
066: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
067: assertTrue(assertArrayEqual(returnObject, null));
068: returnObject = testStringArray(new String[] { null },
069: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
070: assertTrue(assertArrayEqual(returnObject, null));
071: returnObject = testStringArray(new String[] { "test" },
072: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
073: assertTrue(assertArrayEqual(returnObject,
074: new String[] { "test" }));
075: returnObject = testStringArray(
076: new String[] { "test", null },
077: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
078: assertTrue(assertArrayEqual(returnObject,
079: new String[] { "test" }));
080: } catch (Exception e) {
081: e.printStackTrace();
082: fail();
083: }
084:
085: System.out.println("Test minOccurs = 1 nillable true");
086: try {
087: returnObject = testStringArray(null,
088: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
089: assertTrue(assertArrayEqual(returnObject,
090: new String[] { null }));
091: returnObject = testStringArray(new String[] { null },
092: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
093: assertTrue(assertArrayEqual(returnObject,
094: new String[] { null }));
095: returnObject = testStringArray(new String[] { "test" },
096: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
097: assertTrue(assertArrayEqual(returnObject,
098: new String[] { "test" }));
099: returnObject = testStringArray(
100: new String[] { "test", null },
101: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
102: assertTrue(assertArrayEqual(returnObject, new String[] {
103: "test", null }));
104: } catch (Exception e) {
105: fail();
106: }
107:
108: System.out.println("Test minOccurs = 1 nillable false");
109:
110: try {
111: returnObject = testStringArray(null,
112: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
113: fail();
114: } catch (Exception e) {
115: assertTrue(true);
116: }
117:
118: try {
119: returnObject = testStringArray(new String[] { null },
120: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
121: fail();
122: } catch (Exception e) {
123: assertTrue(true);
124: }
125:
126: try {
127: returnObject = testStringArray(
128: new String[] { "test", null },
129: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
130: fail();
131: } catch (Exception e) {
132: assertTrue(true);
133: }
134:
135: try {
136: returnObject = testStringArray(new String[] { "test" },
137: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
138: assertTrue(assertArrayEqual(returnObject,
139: new String[] { "test" }));
140: } catch (Exception e) {
141: fail();
142: }
143:
144: }
145:
146: private String[] testStringArray(String[] innerString, int type)
147: throws Exception {
148:
149: String[] returnObject = null;
150: OMElement omElement;
151: String omElementString;
152:
153: switch (type) {
154: case MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST: {
155: TestString1 testString = new TestString1();
156: testString.setTestValue(innerString);
157: omElement = testString.getOMElement(TestString1.MY_QNAME,
158: OMAbstractFactory.getSOAP12Factory());
159: omElementString = omElement.toStringWithConsume();
160: System.out.println("OMElement ==> " + omElementString);
161: XMLStreamReader xmlReader = StAXUtils
162: .createXMLStreamReader(new ByteArrayInputStream(
163: omElementString.getBytes()));
164: returnObject = TestString1.Factory.parse(xmlReader)
165: .getTestValue();
166: break;
167: }
168: case MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST: {
169: TestString3 testString = new TestString3();
170: testString.setTestValue(innerString);
171: omElement = testString.getOMElement(TestString3.MY_QNAME,
172: OMAbstractFactory.getSOAP12Factory());
173: omElementString = omElement.toStringWithConsume();
174: System.out.println("OMElement ==> " + omElementString);
175: XMLStreamReader xmlReader = StAXUtils
176: .createXMLStreamReader(new ByteArrayInputStream(
177: omElementString.getBytes()));
178: returnObject = TestString3.Factory.parse(xmlReader)
179: .getTestValue();
180: break;
181: }
182: case MIN_EQUALS_ONE_NILLABLE_TRUE_TEST: {
183: TestString5 testString = new TestString5();
184: testString.setTestValue(innerString);
185: omElement = testString.getOMElement(TestString5.MY_QNAME,
186: OMAbstractFactory.getSOAP12Factory());
187: omElementString = omElement.toStringWithConsume();
188: System.out.println("OMElement ==> " + omElementString);
189: XMLStreamReader xmlReader = StAXUtils
190: .createXMLStreamReader(new ByteArrayInputStream(
191: omElementString.getBytes()));
192: returnObject = TestString5.Factory.parse(xmlReader)
193: .getTestValue();
194: break;
195: }
196: case MIN_EQUALS_ONE_NILLABLE_FALSE_TEST: {
197: TestString7 testString = new TestString7();
198: testString.setTestValue(innerString);
199: omElement = testString.getOMElement(TestString7.MY_QNAME,
200: OMAbstractFactory.getSOAP12Factory());
201: omElementString = omElement.toStringWithConsume();
202: System.out.println("OMElement ==> " + omElementString);
203: XMLStreamReader xmlReader = StAXUtils
204: .createXMLStreamReader(new ByteArrayInputStream(
205: omElementString.getBytes()));
206: returnObject = TestString7.Factory.parse(xmlReader)
207: .getTestValue();
208: break;
209: }
210:
211: }
212: return returnObject;
213: }
214:
215: public void testString() {
216:
217: String returnObject;
218: System.out.println("Test minOccurs 0 nillable true");
219: try {
220: returnObject = testString(null,
221: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
222: assertEquals(returnObject, null);
223: returnObject = testString("Test",
224: MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST);
225: assertEquals(returnObject, "Test");
226: } catch (Exception e) {
227: fail();
228: }
229:
230: System.out.println("Test minOccurs = 0 nillable false");
231: try {
232: returnObject = testString(null,
233: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
234: assertEquals(returnObject, null);
235: returnObject = testString("Test",
236: MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST);
237: assertEquals(returnObject, "Test");
238: } catch (Exception e) {
239: fail();
240: }
241:
242: System.out.println("Test minOccurs = 1 nillable true");
243: try {
244: returnObject = testString(null,
245: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
246: assertEquals(returnObject, null);
247: returnObject = testString("Test",
248: MIN_EQUALS_ONE_NILLABLE_TRUE_TEST);
249: assertEquals(returnObject, "Test");
250: } catch (Exception e) {
251: fail();
252: }
253: System.out.println("Test minOccurs = 1 nillable false");
254: try {
255: returnObject = testString(null,
256: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
257: fail();
258: } catch (Exception e) {
259: assertTrue(true);
260: }
261:
262: try {
263: returnObject = testString("Test",
264: MIN_EQUALS_ONE_NILLABLE_FALSE_TEST);
265: assertEquals(returnObject, "Test");
266: } catch (Exception e) {
267: fail();
268: }
269:
270: }
271:
272: private String testString(String innerString, int type)
273: throws Exception {
274:
275: String returnObject = null;
276: OMElement omElement;
277: String omElementString;
278:
279: switch (type) {
280: case MIN_EQUALS_ZERO_NILLABLE_TRUE_TEST: {
281: TestString2 testString = new TestString2();
282: testString.setTestValue(innerString);
283: omElement = testString.getOMElement(TestString2.MY_QNAME,
284: OMAbstractFactory.getSOAP12Factory());
285: omElementString = omElement.toStringWithConsume();
286: System.out.println("OMElement ==> " + omElementString);
287: XMLStreamReader xmlReader = StAXUtils
288: .createXMLStreamReader(new ByteArrayInputStream(
289: omElementString.getBytes()));
290: returnObject = TestString2.Factory.parse(xmlReader)
291: .getTestValue();
292: break;
293: }
294: case MIN_EQUALS_ZERO_NILLABLE_FALSE_TEST: {
295: TestString4 testString = new TestString4();
296: testString.setTestValue(innerString);
297: omElement = testString.getOMElement(TestString4.MY_QNAME,
298: OMAbstractFactory.getSOAP12Factory());
299: omElementString = omElement.toStringWithConsume();
300: System.out.println("OMElement ==> " + omElementString);
301: XMLStreamReader xmlReader = StAXUtils
302: .createXMLStreamReader(new ByteArrayInputStream(
303: omElementString.getBytes()));
304: returnObject = TestString4.Factory.parse(xmlReader)
305: .getTestValue();
306: break;
307: }
308: case MIN_EQUALS_ONE_NILLABLE_TRUE_TEST: {
309: TestString6 testString = new TestString6();
310: testString.setTestValue(innerString);
311: omElement = testString.getOMElement(TestString6.MY_QNAME,
312: OMAbstractFactory.getSOAP12Factory());
313: omElementString = omElement.toStringWithConsume();
314: System.out.println("OMElement ==> " + omElementString);
315: XMLStreamReader xmlReader = StAXUtils
316: .createXMLStreamReader(new ByteArrayInputStream(
317: omElementString.getBytes()));
318: returnObject = TestString6.Factory.parse(xmlReader)
319: .getTestValue();
320: break;
321: }
322: case MIN_EQUALS_ONE_NILLABLE_FALSE_TEST: {
323: TestString8 testString = new TestString8();
324: testString.setTestValue(innerString);
325: omElement = testString.getOMElement(TestString8.MY_QNAME,
326: OMAbstractFactory.getSOAP12Factory());
327: omElementString = omElement.toStringWithConsume();
328: System.out.println("OMElement ==> " + omElementString);
329: XMLStreamReader xmlReader = StAXUtils
330: .createXMLStreamReader(new ByteArrayInputStream(
331: omElementString.getBytes()));
332: returnObject = TestString8.Factory.parse(xmlReader)
333: .getTestValue();
334: break;
335: }
336:
337: }
338: return returnObject;
339: }
340:
341: }
|