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.union;
020:
021: import junit.framework.TestCase;
022: import org.tempuri.union.*;
023: import org.apache.axiom.om.OMElement;
024: import org.apache.axiom.om.OMAbstractFactory;
025: import org.apache.axiom.om.util.StAXUtils;
026: import org.apache.axis2.databinding.types.URI;
027:
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.stream.XMLStreamException;
030: import javax.xml.namespace.QName;
031: import java.io.ByteArrayInputStream;
032:
033: public class UnionTest extends TestCase {
034:
035: public void testRecord2() {
036: Object result;
037: result = testRecord2(new Integer(10));
038: assertEquals(result, new Integer(10));
039: result = testRecord2(new Boolean(true));
040: assertEquals(result, new Boolean(true));
041: }
042:
043: private Object testRecord2(Object testObject) {
044: Record2 record2 = new Record2();
045: DateOrDateTimeType dateOrDateTimeType = new DateOrDateTimeType();
046: record2.setElem1(dateOrDateTimeType);
047: dateOrDateTimeType.setObject(testObject);
048:
049: try {
050: OMElement omElement = record2.getOMElement(
051: Record2.MY_QNAME, OMAbstractFactory.getOMFactory());
052: String omElementString = omElement.toStringWithConsume();
053: System.out.println("OM String ==> " + omElementString);
054: XMLStreamReader xmlReader = StAXUtils
055: .createXMLStreamReader(new ByteArrayInputStream(
056: omElementString.getBytes()));
057: Record2 newRecord2 = Record2.Factory.parse(xmlReader);
058: return newRecord2.getElem1().getObject();
059: } catch (Exception e) {
060: assertFalse(true);
061: }
062: return null;
063: }
064:
065: public void testRecord1() {
066: Object result;
067: try {
068: result = testRecord1(new URI("http://www.google.com"));
069: assertEquals(result, new URI("http://www.google.com"));
070: result = testRecord1(FooEnum._value1);
071: assertEquals(result, FooEnum._value1);
072: } catch (URI.MalformedURIException e) {
073: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
074: }
075: }
076:
077: private Object testRecord1(Object testObject) {
078: Record1 record1 = new Record1();
079: FooOpenEnum fooOpenEnum = new FooOpenEnum();
080: record1.setElem1(fooOpenEnum);
081: try {
082: fooOpenEnum.setObject(testObject);
083: OMElement omElement = record1.getOMElement(
084: Record1.MY_QNAME, OMAbstractFactory.getOMFactory());
085: String omElementString = omElement.toStringWithConsume();
086: System.out.println("OM String ==> " + omElementString);
087: XMLStreamReader xmlReader = StAXUtils
088: .createXMLStreamReader(new ByteArrayInputStream(
089: omElementString.getBytes()));
090: Record1 newRecord1 = Record1.Factory.parse(xmlReader);
091: return newRecord1.getElem1().getObject();
092: } catch (Exception e) {
093: assertFalse(true);
094: }
095: return null;
096: }
097:
098: public void testUnionQName() {
099: UnionQNameTestElement unionQNameTestElement = new UnionQNameTestElement();
100: UnionQNameTest unionQNameTest = new UnionQNameTest();
101: unionQNameTestElement.setUnionQNameTestElement(unionQNameTest);
102: unionQNameTest.setObject(new QName("http://www.google.com",
103: "test"));
104:
105: try {
106: OMElement omElement = unionQNameTestElement.getOMElement(
107: UnionQNameTestElement.MY_QNAME, OMAbstractFactory
108: .getOMFactory());
109: String omElementString = omElement.toStringWithConsume();
110: System.out.println("OM Element ==> " + omElementString);
111: XMLStreamReader xmlReader = StAXUtils
112: .createXMLStreamReader(new ByteArrayInputStream(
113: omElementString.getBytes()));
114: UnionQNameTestElement result = UnionQNameTestElement.Factory
115: .parse(xmlReader);
116: assertEquals(unionQNameTest.getObject(), result
117: .getUnionQNameTestElement().getObject());
118: } catch (Exception e) {
119: assertTrue(false);
120: }
121: }
122:
123: public void testInnerSimpleTypes() {
124: TestInnerUnionType testInnerUnionType = new TestInnerUnionType();
125: PackingType_T packingType_t = new PackingType_T();
126: testInnerUnionType.setTestInnerUnionType(packingType_t);
127: PackingType_T_type0 packingType_t_type0 = new PackingType_T_type0();
128: packingType_t_type0.setPackingType_T_type0("MINOR_a");
129: packingType_t.setObject(packingType_t_type0);
130: OMElement omElement;
131: try {
132: omElement = testInnerUnionType.getOMElement(
133: TestInnerUnionType.MY_QNAME, OMAbstractFactory
134: .getOMFactory());
135: String omElementString = omElement.toStringWithConsume();
136: System.out.println("OM Element ==> " + omElementString);
137: XMLStreamReader xmlReader = StAXUtils
138: .createXMLStreamReader(new ByteArrayInputStream(
139: omElementString.getBytes()));
140: TestInnerUnionType result = TestInnerUnionType.Factory
141: .parse(xmlReader);
142: assertEquals(packingType_t_type0.toString(), result
143: .getTestInnerUnionType().getObject().toString());
144: } catch (Exception e) {
145: assertTrue(false);
146: }
147:
148: testInnerUnionType = new TestInnerUnionType();
149: packingType_t = new PackingType_T();
150: testInnerUnionType.setTestInnerUnionType(packingType_t);
151: PackingType_T_type1 packingType_t_type1 = new PackingType_T_type1();
152: packingType_t_type1.setPackingType_T_type1("PROP_a");
153: packingType_t.setObject(packingType_t_type1);
154:
155: try {
156: omElement = testInnerUnionType.getOMElement(
157: TestInnerUnionType.MY_QNAME, OMAbstractFactory
158: .getOMFactory());
159: String omElementString = omElement.toStringWithConsume();
160: System.out.println("OM Element ==> " + omElementString);
161: XMLStreamReader xmlReader = StAXUtils
162: .createXMLStreamReader(new ByteArrayInputStream(
163: omElementString.getBytes()));
164: TestInnerUnionType result = TestInnerUnionType.Factory
165: .parse(xmlReader);
166: assertEquals(packingType_t_type1.toString(), result
167: .getTestInnerUnionType().getObject().toString());
168: } catch (Exception e) {
169: assertTrue(false);
170: }
171:
172: testInnerUnionType = new TestInnerUnionType();
173: packingType_t = new PackingType_T();
174: testInnerUnionType.setTestInnerUnionType(packingType_t);
175: packingType_t.setObject(PackingType_T_type2.TAR);
176:
177: try {
178: omElement = testInnerUnionType.getOMElement(
179: TestInnerUnionType.MY_QNAME, OMAbstractFactory
180: .getOMFactory());
181: String omElementString = omElement.toStringWithConsume();
182: System.out.println("OM Element ==> " + omElementString);
183: XMLStreamReader xmlReader = StAXUtils
184: .createXMLStreamReader(new ByteArrayInputStream(
185: omElementString.getBytes()));
186: TestInnerUnionType result = TestInnerUnionType.Factory
187: .parse(xmlReader);
188: assertEquals(PackingType_T_type2.TAR, result
189: .getTestInnerUnionType().getObject());
190: } catch (Exception e) {
191: assertTrue(false);
192: }
193:
194: }
195: }
|