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.OMElement;
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.util.StAXUtils;
024: import services.echo.types.TestAny1;
025: import services.echo.types.TestAny2;
026: import services.echo.types.TestAny3;
027: import services.echo.types.TestAny4;
028:
029: import javax.xml.stream.XMLStreamReader;
030: import java.io.ByteArrayInputStream;
031:
032: public class AnyElementsTest extends AbstractTest {
033:
034: public static final int MIN_EQUALS_ZERO = 0;
035: public static final int MIN_EQUALS_ONE = 1;
036:
037: public void testAnyArray() {
038: OMElement[] returnObject;
039: try {
040: returnObject = testAnyArray(null, MIN_EQUALS_ZERO);
041: assertTrue(assertArrayEqual(returnObject, null));
042: returnObject = testAnyArray(new OMElement[] { null },
043: MIN_EQUALS_ZERO);
044: assertTrue(assertArrayEqual(returnObject, null));
045: returnObject = testAnyArray(
046: new OMElement[] { getOMElement() }, MIN_EQUALS_ZERO);
047: assertTrue(assertArrayEqual(returnObject,
048: new OMElement[] { getOMElement() }));
049: returnObject = testAnyArray(new OMElement[] {
050: getOMElement(), getOMElement() }, MIN_EQUALS_ZERO);
051: assertTrue(assertArrayEqual(returnObject, new OMElement[] {
052: getOMElement(), getOMElement() }));
053: } catch (Exception e) {
054: e.printStackTrace();
055: fail();
056: }
057:
058: try {
059: returnObject = testAnyArray(null, MIN_EQUALS_ONE);
060: fail();
061: } catch (Exception e) {
062: assertTrue(true);
063: }
064:
065: try {
066: returnObject = testAnyArray(new OMElement[] { null },
067: MIN_EQUALS_ONE);
068: fail();
069: } catch (Exception e) {
070: assertTrue(true);
071: }
072:
073: try {
074: returnObject = testAnyArray(new OMElement[] {
075: getOMElement(), getOMElement() }, MIN_EQUALS_ONE);
076: assertTrue(assertArrayEqual(returnObject, new OMElement[] {
077: getOMElement(), getOMElement() }));
078: } catch (Exception e) {
079: fail();
080: }
081:
082: }
083:
084: private OMElement[] testAnyArray(OMElement[] innerObject, int type)
085: throws Exception {
086: OMElement omElement;
087: String omElementString;
088: OMElement[] returnObject = null;
089:
090: switch (type) {
091: case MIN_EQUALS_ZERO: {
092: TestAny1 testAny = new TestAny1();
093: testAny.setExtraElement(innerObject);
094: omElement = testAny.getOMElement(TestAny1.MY_QNAME,
095: OMAbstractFactory.getSOAP12Factory());
096: omElementString = omElement.toStringWithConsume();
097: System.out.println("OMElement ==> " + omElementString);
098: XMLStreamReader xmlReader = StAXUtils
099: .createXMLStreamReader(new ByteArrayInputStream(
100: omElementString.getBytes()));
101: returnObject = TestAny1.Factory.parse(xmlReader)
102: .getExtraElement();
103: break;
104: }
105: case MIN_EQUALS_ONE: {
106: TestAny3 testAny = new TestAny3();
107: testAny.setExtraElement(innerObject);
108: omElement = testAny.getOMElement(TestAny3.MY_QNAME,
109: OMAbstractFactory.getSOAP12Factory());
110: omElementString = omElement.toStringWithConsume();
111: System.out.println("OMElement ==> " + omElementString);
112: XMLStreamReader xmlReader = StAXUtils
113: .createXMLStreamReader(new ByteArrayInputStream(
114: omElementString.getBytes()));
115: returnObject = TestAny3.Factory.parse(xmlReader)
116: .getExtraElement();
117: break;
118: }
119: }
120: return returnObject;
121: }
122:
123: public void testAny() {
124: OMElement returnObject;
125: try {
126: returnObject = testAny(null, MIN_EQUALS_ZERO);
127: assertTrue(isOMElementsEqual(returnObject, null));
128: returnObject = testAny(getOMElement(), MIN_EQUALS_ZERO);
129: assertTrue(isOMElementsEqual(returnObject, getOMElement()));
130:
131: } catch (Exception e) {
132: fail();
133: }
134:
135: try {
136: returnObject = testAny(null, MIN_EQUALS_ONE);
137: fail();
138: } catch (Exception e) {
139: assertTrue(true);
140: }
141:
142: try {
143: returnObject = testAny(getOMElement(), MIN_EQUALS_ONE);
144: assertTrue(isOMElementsEqual(returnObject, getOMElement()));
145: } catch (Exception e) {
146: fail();
147: }
148: }
149:
150: private OMElement testAny(OMElement innerObject, int type)
151: throws Exception {
152: OMElement omElement;
153: OMElement returnObject = null;
154: String omElementString;
155:
156: switch (type) {
157: case MIN_EQUALS_ZERO: {
158: TestAny2 testAny = new TestAny2();
159: testAny.setExtraElement(innerObject);
160: omElement = testAny.getOMElement(TestAny2.MY_QNAME,
161: OMAbstractFactory.getSOAP12Factory());
162: omElementString = omElement.toStringWithConsume();
163: System.out.println("OMElement ==> " + omElementString);
164: XMLStreamReader xmlReader = StAXUtils
165: .createXMLStreamReader(new ByteArrayInputStream(
166: omElementString.getBytes()));
167: returnObject = TestAny2.Factory.parse(xmlReader)
168: .getExtraElement();
169: break;
170: }
171: case MIN_EQUALS_ONE: {
172: TestAny4 testAny = new TestAny4();
173: testAny.setExtraElement(innerObject);
174: omElement = testAny.getOMElement(TestAny4.MY_QNAME,
175: OMAbstractFactory.getSOAP12Factory());
176: omElementString = omElement.toStringWithConsume();
177: System.out.println("OMElement ==> " + omElementString);
178: XMLStreamReader xmlReader = StAXUtils
179: .createXMLStreamReader(new ByteArrayInputStream(
180: omElementString.getBytes()));
181: returnObject = TestAny4.Factory.parse(xmlReader)
182: .getExtraElement();
183: break;
184: }
185: }
186: return returnObject;
187: }
188:
189: }
|