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.innerChoice;
020:
021: import junit.framework.TestCase;
022: import org.apache.axiom.om.OMAbstractFactory;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axiom.om.util.StAXUtils;
025: import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter;
026: import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLSerializer;
027: import test.axis2.apache.org.*;
028:
029: import javax.xml.stream.XMLStreamReader;
030: import javax.xml.stream.XMLStreamWriter;
031: import java.io.ByteArrayInputStream;
032:
033: import com.mynamespace.testinnerparticle.TestInnerParticle1Sequence_type0;
034:
035: public class InnerChoiceTest extends TestCase {
036:
037: public void testInnerChoice1() {
038: TestInnerSequence1 testSequence = new TestInnerSequence1();
039: testSequence.setSequence1("test sequence");
040: testSequence.setSequence2(3);
041:
042: TestInnerSequence1Choice_type0 TestInnerSequence1Choice_type0 = new TestInnerSequence1Choice_type0();
043: TestInnerSequence1Choice_type0.setChoice1("test choice");
044: TestInnerSequence1Choice_type0.setChoice2(5);
045: testSequence
046: .setTestInnerSequence1Choice_type0(TestInnerSequence1Choice_type0);
047:
048: try {
049: OMElement omElement = testSequence.getOMElement(
050: TestInnerSequence1.MY_QNAME, OMAbstractFactory
051: .getSOAP12Factory());
052: String omElementString = omElement.toStringWithConsume();
053: System.out.println("OMelement ==> " + omElementString);
054: XMLStreamReader xmlReader = StAXUtils
055: .createXMLStreamReader(new ByteArrayInputStream(
056: omElementString.getBytes()));
057: TestInnerSequence1 result = TestInnerSequence1.Factory
058: .parse(xmlReader);
059: assertEquals(result.getSequence1(), "test sequence");
060: assertEquals(result.getSequence2(), 3);
061: assertEquals(result.getTestInnerSequence1Choice_type0()
062: .getChoice2(), 5);
063:
064: // writtig result back
065: XMLStreamWriter xmlStreamWriter = StAXUtils
066: .createXMLStreamWriter(System.out);
067: MTOMAwareXMLStreamWriter mtomAwareXMLStreamWriter = new MTOMAwareXMLSerializer(
068: xmlStreamWriter);
069: result.serialize(new javax.xml.namespace.QName(
070: "org.apache.axis2.test", "TestInnerSequence1",
071: "foo"), OMAbstractFactory.getSOAP11Factory(),
072: mtomAwareXMLStreamWriter);
073: xmlStreamWriter.flush();
074:
075: } catch (Exception e) {
076: fail();
077: }
078: }
079:
080: public void testInnerChoice21() {
081: TestInnerSequence2 testSequence = new TestInnerSequence2();
082: testSequence.setSequence1("sequence");
083: testSequence.setSequence2(3);
084: TestInnerSequence2Choice_type0 testInnerSequence2Choice_type1 = new TestInnerSequence2Choice_type0();
085: testInnerSequence2Choice_type1.setChoice1(new String[] {
086: "choice1", "choice2" });
087: testSequence
088: .setTestInnerSequence2Choice_type0(testInnerSequence2Choice_type1);
089:
090: try {
091: OMElement omElement = testSequence.getOMElement(
092: TestInnerSequence2.MY_QNAME, OMAbstractFactory
093: .getSOAP12Factory());
094: String omElementString = omElement.toStringWithConsume();
095: System.out.println("OMelement ==> " + omElementString);
096: XMLStreamReader xmlReader = StAXUtils
097: .createXMLStreamReader(new ByteArrayInputStream(
098: omElementString.getBytes()));
099: TestInnerSequence2 result = TestInnerSequence2.Factory
100: .parse(xmlReader);
101: assertEquals(result.getSequence1(), "sequence");
102: assertEquals(result.getSequence2(), 3);
103: assertTrue(isEqual(result
104: .getTestInnerSequence2Choice_type0().getChoice1(),
105: new String[] { "choice1", "choice2" }));
106: } catch (Exception e) {
107: fail();
108: }
109: }
110:
111: public void testInnerChoice22() {
112: TestInnerSequence2 testSequence = new TestInnerSequence2();
113: testSequence.setSequence1("sequence");
114: testSequence.setSequence2(3);
115: TestInnerSequence2Choice_type0 testInnerSequence2Choice_type1 = new TestInnerSequence2Choice_type0();
116: testInnerSequence2Choice_type1.setChoice2(new int[] { 2, 4 });
117: testSequence
118: .setTestInnerSequence2Choice_type0(testInnerSequence2Choice_type1);
119:
120: try {
121: OMElement omElement = testSequence.getOMElement(
122: TestInnerSequence2.MY_QNAME, OMAbstractFactory
123: .getSOAP12Factory());
124: String omElementString = omElement.toStringWithConsume();
125: System.out.println("OMelement ==> " + omElementString);
126: XMLStreamReader xmlReader = StAXUtils
127: .createXMLStreamReader(new ByteArrayInputStream(
128: omElementString.getBytes()));
129: TestInnerSequence2 result = TestInnerSequence2.Factory
130: .parse(xmlReader);
131: assertEquals(result.getSequence1(), "sequence");
132: assertEquals(result.getSequence2(), 3);
133: assertTrue(isEqual(result
134: .getTestInnerSequence2Choice_type0().getChoice2(),
135: new int[] { 2, 4 }));
136: } catch (Exception e) {
137: fail();
138: }
139: }
140:
141: public void testInnerChoice31() {
142: TestInnerSequence3 testSequence = new TestInnerSequence3();
143: testSequence.setSequence1("sequence");
144: testSequence.setSequence2(3);
145: TestInnerSequence3Choice_type0 testInnerSequence3Choice_type0 = new TestInnerSequence3Choice_type0();
146: testInnerSequence3Choice_type0.setChoice1(new String[] {
147: "choice1", null, "choice2" });
148: testSequence
149: .setTestInnerSequence3Choice_type0(testInnerSequence3Choice_type0);
150:
151: try {
152: OMElement omElement = testSequence.getOMElement(
153: TestInnerSequence3.MY_QNAME, OMAbstractFactory
154: .getSOAP12Factory());
155: String omElementString = omElement.toStringWithConsume();
156: System.out.println("OMelement ==> " + omElementString);
157: XMLStreamReader xmlReader = StAXUtils
158: .createXMLStreamReader(new ByteArrayInputStream(
159: omElementString.getBytes()));
160: TestInnerSequence3 result = TestInnerSequence3.Factory
161: .parse(xmlReader);
162: assertEquals(result.getSequence1(), "sequence");
163: assertEquals(result.getSequence2(), 3);
164: assertTrue(isEqual(result
165: .getTestInnerSequence3Choice_type0().getChoice1(),
166: new String[] { "choice1", null, "choice2" }));
167: } catch (Exception e) {
168: fail();
169: }
170: }
171:
172: public void testInnerChoice32() {
173: TestInnerSequence3 testSequence = new TestInnerSequence3();
174: testSequence.setSequence1("sequence");
175: testSequence.setSequence2(3);
176: TestInnerSequence3Choice_type0 testInnerSequence3Choice_type0 = new TestInnerSequence3Choice_type0();
177: testInnerSequence3Choice_type0.setChoice2(new int[] { 2,
178: Integer.MIN_VALUE, 6 });
179: testSequence
180: .setTestInnerSequence3Choice_type0(testInnerSequence3Choice_type0);
181:
182: try {
183: OMElement omElement = testSequence.getOMElement(
184: TestInnerSequence3.MY_QNAME, OMAbstractFactory
185: .getSOAP12Factory());
186: String omElementString = omElement.toStringWithConsume();
187: System.out.println("OMelement ==> " + omElementString);
188: XMLStreamReader xmlReader = StAXUtils
189: .createXMLStreamReader(new ByteArrayInputStream(
190: omElementString.getBytes()));
191: TestInnerSequence3 result = TestInnerSequence3.Factory
192: .parse(xmlReader);
193: assertEquals(result.getSequence1(), "sequence");
194: assertEquals(result.getSequence2(), 3);
195: assertTrue(isEqual(result
196: .getTestInnerSequence3Choice_type0().getChoice2(),
197: new int[] { 2, Integer.MIN_VALUE, 6 }));
198: } catch (Exception e) {
199: fail();
200: }
201: }
202:
203: private boolean isEqual(String[] test1, String[] test2) {
204: boolean isEqual = true;
205: if (test1.length != test2.length) {
206: isEqual = false;
207: } else {
208: for (int i = 0; i < test1.length; i++) {
209: if (test1[i] != null) {
210: if (!test1[i].equals(test2[i])) {
211: isEqual = false;
212: break;
213: }
214: } else {
215: if (test2[i] != null) {
216: isEqual = false;
217: break;
218: }
219: }
220:
221: }
222: }
223:
224: return isEqual;
225: }
226:
227: private boolean isEqual(int[] test1, int[] test2) {
228: boolean isEqual = true;
229: if (test1.length != test2.length) {
230: isEqual = false;
231: } else {
232: for (int i = 0; i < test1.length; i++) {
233: if (test1[i] != test2[i]) {
234: isEqual = false;
235: break;
236: }
237: }
238: }
239:
240: return isEqual;
241: }
242:
243: }
|