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.restriction;
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.tempuri.*;
026:
027: import javax.xml.namespace.QName;
028: import javax.xml.stream.XMLStreamReader;
029: import java.io.ByteArrayInputStream;
030: import java.util.Date;
031:
032: public class SimpleRestrictionTest extends TestCase {
033:
034: public void testSimpleAttribute() {
035:
036: TestSimpleAttributeElement testSimpleAttributeElement = new TestSimpleAttributeElement();
037:
038: TestSimpleAttribute testSimpleAttribute = new TestSimpleAttribute();
039: testSimpleAttributeElement
040: .setTestSimpleAttributeElement(testSimpleAttribute);
041: testSimpleAttribute.setTestElement1(new QName(
042: "http://wso2.com", "test1"));
043: testSimpleAttribute.setTestElement2(new QName(
044: "http://wso2.com", "test2"));
045: testSimpleAttribute.setTestElement3(new QName(
046: "http://wso2.com", "test3"));
047:
048: ParentSimpleType parentSimpleType1 = new ParentSimpleType();
049: parentSimpleType1.setChildSimpleType("test simple type 1");
050:
051: ParentSimpleType parentSimpleType2 = new ParentSimpleType();
052: parentSimpleType2.setChildSimpleType("test simple type 2");
053:
054: testSimpleAttribute.setAttrib1(parentSimpleType1);
055: testSimpleAttribute.setAttrib2(parentSimpleType2);
056:
057: try {
058: OMElement omElement = testSimpleAttributeElement
059: .getOMElement(TestSimpleAttributeElement.MY_QNAME,
060: OMAbstractFactory.getOMFactory());
061: String omElementString = omElement.toStringWithConsume();
062: System.out.println("OM String ==> " + omElementString);
063: XMLStreamReader xmlReader = StAXUtils
064: .createXMLStreamReader(new ByteArrayInputStream(
065: omElementString.getBytes()));
066: TestSimpleAttributeElement result = TestSimpleAttributeElement.Factory
067: .parse(xmlReader);
068: assertEquals(result.getTestSimpleAttributeElement()
069: .getTestElement1(), testSimpleAttribute
070: .getTestElement1());
071: assertEquals(result.getTestSimpleAttributeElement()
072: .getTestElement2(), testSimpleAttribute
073: .getTestElement2());
074: assertEquals(result.getTestSimpleAttributeElement()
075: .getTestElement3(), testSimpleAttribute
076: .getTestElement3());
077: assertEquals(result.getTestSimpleAttributeElement()
078: .getAttrib1().getChildSimpleType(),
079: parentSimpleType1.getChildSimpleType());
080: assertEquals(result.getTestSimpleAttributeElement()
081: .getAttrib2().getChildSimpleType(),
082: parentSimpleType2.getChildSimpleType());
083: } catch (Exception e) {
084: assertFalse(true);
085: }
086:
087: }
088:
089: public void testNormalSimpleTypeElement() {
090:
091: NormalSimpleTypeElement normalSimpleTypeElement = new NormalSimpleTypeElement();
092: ParentNormalSimpleType parentNormalSimpleType = new ParentNormalSimpleType();
093: normalSimpleTypeElement
094: .setNormalSimpleTypeElement(parentNormalSimpleType);
095: parentNormalSimpleType.setNormalSimpleType(new QName(
096: "http://wso2.com", "test"));
097:
098: try {
099: OMElement omElement = normalSimpleTypeElement.getOMElement(
100: NormalSimpleTypeElement.MY_QNAME, OMAbstractFactory
101: .getOMFactory());
102: String omElementString = omElement.toStringWithConsume();
103: System.out.println("OM Element ==> " + omElementString);
104: XMLStreamReader xmlReader = StAXUtils
105: .createXMLStreamReader(new ByteArrayInputStream(
106: omElementString.getBytes()));
107: NormalSimpleTypeElement result = NormalSimpleTypeElement.Factory
108: .parse(xmlReader);
109: assertEquals(result.getNormalSimpleTypeElement()
110: .getNormalSimpleType(), parentNormalSimpleType
111: .getNormalSimpleType());
112: } catch (Exception e) {
113: assertFalse(true);
114: }
115:
116: }
117:
118: public void testEnumerationSimpleTypeElement() {
119:
120: EnumerationSimpleTypeElement enumerationSimpleTypeElement = new EnumerationSimpleTypeElement();
121: enumerationSimpleTypeElement
122: .setEnumerationSimpleTypeElement(ParentEnumerationSimpleType.value1);
123:
124: try {
125: OMElement omElement = enumerationSimpleTypeElement
126: .getOMElement(
127: EnumerationSimpleTypeElement.MY_QNAME,
128: OMAbstractFactory.getOMFactory());
129: String omElementString = omElement.toStringWithConsume();
130: System.out.println("OM Element ==> " + omElementString);
131: XMLStreamReader xmlReader = StAXUtils
132: .createXMLStreamReader(new ByteArrayInputStream(
133: omElementString.getBytes()));
134: EnumerationSimpleTypeElement result = EnumerationSimpleTypeElement.Factory
135: .parse(xmlReader);
136: assertEquals(result.getEnumerationSimpleTypeElement()
137: .getValue(), ParentEnumerationSimpleType.value1
138: .getValue());
139: } catch (Exception e) {
140: assertFalse(true);
141: }
142:
143: }
144:
145: public void testComplexRestrictionType() {
146:
147: ComplexRestrictionTypeTestElement complexRestrictionTypeTestElement = new ComplexRestrictionTypeTestElement();
148: ParentRestrictionType parentRestrictionType = new ParentRestrictionType();
149: complexRestrictionTypeTestElement
150: .setComplexRestrictionTypeTestElement(parentRestrictionType);
151: parentRestrictionType.setBaseTypeElement1("test 1");
152: parentRestrictionType.setBaseTypeElement2(5);
153:
154: try {
155: OMElement omElement = complexRestrictionTypeTestElement
156: .getOMElement(
157: ComplexRestrictionTypeTestElement.MY_QNAME,
158: OMAbstractFactory.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: ComplexRestrictionTypeTestElement result = ComplexRestrictionTypeTestElement.Factory
165: .parse(xmlReader);
166: assertEquals(result.getComplexRestrictionTypeTestElement()
167: .getBaseTypeElement1(), parentRestrictionType
168: .getBaseTypeElement1());
169: assertEquals(result.getComplexRestrictionTypeTestElement()
170: .getBaseTypeElement2(), parentRestrictionType
171: .getBaseTypeElement2());
172: } catch (Exception e) {
173: e.printStackTrace();
174: assertFalse(true);
175: }
176:
177: }
178:
179: public void testPersonElement() {
180: PersonElement personElement = new PersonElement();
181: Person person = new Person();
182: personElement.setPersonElement(person);
183: person.setName("amila");
184: person.setAge(23);
185: person.setHairColor(HairColor_type1.black);
186: Date date = new Date();
187: person.setBirthDate(date);
188: Address address = new Address();
189: person.setAddress(address);
190: address.setCity("Galle");
191: address.setLine1("line1");
192: address.setLine2("line2");
193: address.setState("state");
194: Zip_type1 ziptype = new Zip_type1();
195: address.setZip(ziptype);
196: ziptype.setZip_type0("C");
197:
198: try {
199: OMElement omElement = personElement.getOMElement(
200: ComplexRestrictionTypeTestElement.MY_QNAME,
201: OMAbstractFactory.getOMFactory());
202: String omElementString = omElement.toStringWithConsume();
203: System.out.println("OM Element ==> " + omElementString);
204: XMLStreamReader xmlReader = StAXUtils
205: .createXMLStreamReader(new ByteArrayInputStream(
206: omElementString.getBytes()));
207: PersonElement result = PersonElement.Factory
208: .parse(xmlReader);
209: assertEquals(result.getPersonElement().getName(), "amila");
210: assertEquals(result.getPersonElement().getAge(), 23);
211: assertEquals(result.getPersonElement().getHairColor(),
212: HairColor_type1.black);
213: Address resultAddress = result.getPersonElement()
214: .getAddress();
215: assertEquals(resultAddress.getCity(), "Galle");
216: assertEquals(resultAddress.getLine1(), "line1");
217: assertEquals(resultAddress.getLine2(), "line2");
218: assertEquals(resultAddress.getState(), "state");
219: assertEquals(resultAddress.getZip().getZip_type0(), "C");
220: } catch (Exception e) {
221: e.printStackTrace();
222: assertFalse(true);
223: }
224:
225: }
226:
227: }
|