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.attribute;
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.attribute.*;
026:
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamException;
029: import java.io.ByteArrayInputStream;
030:
031: public class AttributeTest extends TestCase {
032:
033: public void testElement1() {
034:
035: TestElement1 testElement = new TestElement1();
036: testElement.setAttribute1(1);
037: OMElement omElement;
038: try {
039: omElement = testElement.getOMElement(TestElement1.MY_QNAME,
040: OMAbstractFactory.getOMFactory());
041: String omElementString = omElement.toStringWithConsume();
042: System.out.println("OMElement ==> " + omElementString);
043: XMLStreamReader xmlReader = StAXUtils
044: .createXMLStreamReader(new ByteArrayInputStream(
045: omElementString.getBytes()));
046: TestElement1 result = TestElement1.Factory.parse(xmlReader);
047: assertEquals(result.getAttribute1(), 1);
048: } catch (Exception e) {
049: fail();
050: }
051:
052: testElement = new TestElement1();
053: testElement.setAttribute1(Integer.MIN_VALUE);
054:
055: try {
056: omElement = testElement.getOMElement(TestElement1.MY_QNAME,
057: OMAbstractFactory.getOMFactory());
058: String omElementString = omElement.toStringWithConsume();
059: System.out.println("OMElement ==> " + omElementString);
060: XMLStreamReader xmlReader = StAXUtils
061: .createXMLStreamReader(new ByteArrayInputStream(
062: omElementString.getBytes()));
063: TestElement1 result = TestElement1.Factory.parse(xmlReader);
064: assertEquals(result.getAttribute1(), Integer.MIN_VALUE);
065: } catch (Exception e) {
066: fail();
067: }
068: }
069:
070: public void testElement2() {
071: TestElement2 testElement = new TestElement2();
072: testElement.setAttribute1(1);
073: OMElement omElement;
074: try {
075: omElement = testElement.getOMElement(TestElement2.MY_QNAME,
076: OMAbstractFactory.getOMFactory());
077: String omElementString = omElement.toStringWithConsume();
078: System.out.println("OMElement ==> " + omElementString);
079: XMLStreamReader xmlReader = StAXUtils
080: .createXMLStreamReader(new ByteArrayInputStream(
081: omElementString.getBytes()));
082: TestElement2 result = TestElement2.Factory.parse(xmlReader);
083: assertEquals(result.getAttribute1(), 1);
084: } catch (Exception e) {
085: fail();
086: }
087:
088: testElement = new TestElement2();
089: testElement.setAttribute1(Integer.MIN_VALUE);
090:
091: try {
092: omElement = testElement.getOMElement(TestElement2.MY_QNAME,
093: OMAbstractFactory.getOMFactory());
094: String omElementString = omElement.toStringWithConsume();
095: System.out.println("OMElement ==> " + omElementString);
096: fail();
097: } catch (Exception e) {
098: assertTrue(true);
099: }
100: }
101:
102: public void testElement3() {
103: TestElement3 testElement = new TestElement3();
104: testElement.setAttribute1("test");
105: OMElement omElement;
106: try {
107: omElement = testElement.getOMElement(TestElement3.MY_QNAME,
108: OMAbstractFactory.getOMFactory());
109: String omElementString = omElement.toStringWithConsume();
110: System.out.println("OMElement ==> " + omElementString);
111: XMLStreamReader xmlReader = StAXUtils
112: .createXMLStreamReader(new ByteArrayInputStream(
113: omElementString.getBytes()));
114: TestElement3 result = TestElement3.Factory.parse(xmlReader);
115: assertEquals(result.getAttribute1(), "test");
116: } catch (Exception e) {
117: fail();
118: }
119:
120: testElement = new TestElement3();
121:
122: try {
123: omElement = testElement.getOMElement(TestElement3.MY_QNAME,
124: OMAbstractFactory.getOMFactory());
125: String omElementString = omElement.toStringWithConsume();
126: System.out.println("OMElement ==> " + omElementString);
127: XMLStreamReader xmlReader = StAXUtils
128: .createXMLStreamReader(new ByteArrayInputStream(
129: omElementString.getBytes()));
130: TestElement3 result = TestElement3.Factory.parse(xmlReader);
131: assertEquals(result.getAttribute1(), null);
132: } catch (Exception e) {
133: fail();
134: }
135: }
136:
137: public void testElement4() {
138: TestElement4 testElement = new TestElement4();
139: testElement.setAttribute1("test");
140: OMElement omElement;
141: try {
142: omElement = testElement.getOMElement(TestElement4.MY_QNAME,
143: OMAbstractFactory.getOMFactory());
144: String omElementString = omElement.toStringWithConsume();
145: System.out.println("OMElement ==> " + omElementString);
146: XMLStreamReader xmlReader = StAXUtils
147: .createXMLStreamReader(new ByteArrayInputStream(
148: omElementString.getBytes()));
149: TestElement4 result = TestElement4.Factory.parse(xmlReader);
150: assertEquals(result.getAttribute1(), "test");
151: } catch (Exception e) {
152: fail();
153: }
154:
155: testElement = new TestElement4();
156:
157: try {
158: omElement = testElement.getOMElement(TestElement4.MY_QNAME,
159: OMAbstractFactory.getOMFactory());
160: String omElementString = omElement.toStringWithConsume();
161: System.out.println("OMElement ==> " + omElementString);
162: fail();
163: } catch (Exception e) {
164: assertTrue(true);
165: }
166: }
167:
168: public void testAttributeSimpleType() {
169: TestAttributeSimpleType testAttributeSimpleType = new TestAttributeSimpleType();
170: Attribute1_type0 attribute1_type0 = new Attribute1_type0();
171: attribute1_type0.setAttribute1_type0("test attribute");
172: testAttributeSimpleType.setAttribute1(attribute1_type0);
173:
174: try {
175: OMElement omElement = testAttributeSimpleType.getOMElement(
176: TestAttributeSimpleType.MY_QNAME, OMAbstractFactory
177: .getOMFactory());
178: String omElementString = omElement.toStringWithConsume();
179: System.out.println("OMString ==> " + omElementString);
180: XMLStreamReader xmlReader = StAXUtils
181: .createXMLStreamReader(new ByteArrayInputStream(
182: omElementString.getBytes()));
183: TestAttributeSimpleType result = TestAttributeSimpleType.Factory
184: .parse(xmlReader);
185: assertEquals(result.getAttribute1().getAttribute1_type0(),
186: "test attribute");
187: } catch (Exception e) {
188: fail();
189: }
190: }
191:
192: public void testAttributeReferenceElement() {
193: TestAttributeReferenceElement testAttributeReferenceElement = new TestAttributeReferenceElement();
194: TestAttributeReferenceType testAttributeReferenceType = new TestAttributeReferenceType();
195: testAttributeReferenceType.setParam1("param1");
196: testAttributeReferenceType.setParam2("param2");
197: testAttributeReferenceType.setTestAttribute1("attribute1");
198:
199: testAttributeReferenceElement
200: .setTestAttributeReferenceElement(testAttributeReferenceType);
201:
202: try {
203: OMElement omElement = testAttributeReferenceElement
204: .getOMElement(
205: TestAttributeReferenceElement.MY_QNAME,
206: OMAbstractFactory.getOMFactory());
207: String omElementString = omElement.toStringWithConsume();
208: System.out.println("OM Element ==> " + omElementString);
209: XMLStreamReader xmlReader = StAXUtils
210: .createXMLStreamReader(new ByteArrayInputStream(
211: omElementString.getBytes()));
212: TestAttributeReferenceElement result = TestAttributeReferenceElement.Factory
213: .parse(xmlReader);
214:
215: assertEquals(result.getTestAttributeReferenceElement()
216: .getParam1(), "param1");
217: assertEquals(result.getTestAttributeReferenceElement()
218: .getParam2(), "param2");
219: assertEquals(result.getTestAttributeReferenceElement()
220: .getTestAttribute1(), "attribute1");
221: } catch (XMLStreamException e) {
222: fail();
223: } catch (Exception e) {
224: fail();
225: }
226: }
227: }
|