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.populate.other;
020:
021: import junit.framework.TestCase;
022:
023: import javax.xml.stream.XMLStreamException;
024: import javax.xml.stream.XMLStreamReader;
025: import javax.xml.stream.XMLInputFactory;
026: import java.lang.reflect.InvocationTargetException;
027: import java.lang.reflect.Method;
028: import java.beans.IntrospectionException;
029: import java.beans.BeanInfo;
030: import java.beans.Introspector;
031: import java.beans.PropertyDescriptor;
032: import java.io.ByteArrayInputStream;
033:
034: import org.apache.axiom.om.util.StAXUtils;
035:
036: public class PopulateMixedMinoccurs0Test extends TestCase {
037:
038: /*
039:
040: <xsd:element name="stringListResponseElement" type="tns:StringListResponseType"/>
041: <xsd:complexType name="StringListResponseType">
042: <xsd:choice>
043: <xsd:element name="stringList" type="tns:StringList"/>
044: <xsd:element name="exception" type="tns:ExceptionType"/>
045: </xsd:choice>
046: </xsd:complexType>
047: <xsd:complexType name="StringList">
048: <xsd:sequence>
049: <xsd:element name="s" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
050: </xsd:sequence>
051: </xsd:complexType>
052:
053: */
054:
055: private String xmlString1 = "<stringListResponseElement "
056: + "xmlns=\"http://recursion1.org\">" + "<stringList>"
057: + "<s>item1</s>" + "<s>item2</s>" + "<s>item3</s>"
058: + "</stringList>" + "</stringListResponseElement>";
059:
060: private String xmlString2 = "<stringListResponseElement "
061: + "xmlns=\"http://recursion1.org\">" + "<stringList>"
062: + "</stringList>" + "</stringListResponseElement>";
063:
064: public void testPopulate1() throws Exception {
065: populateAndAssert(xmlString1, 3);
066: }
067:
068: public void testPopulate2() throws Exception {
069: populateAndAssert(xmlString2, 0);
070: }
071:
072: private void populateAndAssert(String s, int count)
073: throws XMLStreamException, ClassNotFoundException,
074: NoSuchMethodException, IllegalAccessException,
075: InvocationTargetException, IntrospectionException {
076: XMLStreamReader reader = StAXUtils
077: .createXMLStreamReader(new ByteArrayInputStream(s
078: .getBytes()));
079: Class clazz = Class
080: .forName("org.recursion1.StringListResponseElement");
081: Class innerClazz = clazz.getDeclaredClasses()[0];
082: Method parseMethod = innerClazz.getMethod("parse",
083: new Class[] { XMLStreamReader.class });
084: Object obj = parseMethod.invoke(null, new Object[] { reader });
085:
086: assertNotNull(obj);
087:
088: Object stringListResponseElement = null;
089: BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
090: PropertyDescriptor[] propertyDescriptors = beanInfo
091: .getPropertyDescriptors();
092: Method readMethod;
093:
094: for (int i = 0; i < propertyDescriptors.length; i++) {
095: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
096: String displayName = propertyDescriptor.getDisplayName();
097: if ("stringListResponseElement".equals(displayName)) {
098: readMethod = propertyDescriptor.getReadMethod();
099: stringListResponseElement = readMethod
100: .invoke(obj, null);
101: break;
102: }
103: }
104:
105: assertNotNull(stringListResponseElement);
106:
107: beanInfo = Introspector.getBeanInfo(stringListResponseElement
108: .getClass());
109: propertyDescriptors = beanInfo.getPropertyDescriptors();
110: Object stringArray = null;
111: for (int i = 0; i < propertyDescriptors.length; i++) {
112: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
113: String displayName = propertyDescriptor.getDisplayName();
114: if ("stringList".equals(displayName)) {
115: readMethod = propertyDescriptor.getReadMethod();
116: stringArray = readMethod.invoke(
117: stringListResponseElement, null);
118: break;
119: }
120: }
121:
122: assertNotNull(stringArray);
123:
124: beanInfo = Introspector.getBeanInfo(stringArray.getClass());
125: propertyDescriptors = beanInfo.getPropertyDescriptors();
126: Object sArray = null;
127: for (int i = 0; i < propertyDescriptors.length; i++) {
128: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
129: String displayName = propertyDescriptor.getDisplayName();
130: if ("stringList".equals(displayName)) {
131: readMethod = propertyDescriptor.getReadMethod();
132: sArray = readMethod.invoke(stringArray, null);
133: break;
134: }
135: }
136: if (sArray != null) {
137: Object[] array = (Object[]) sArray;
138: assertEquals(count, array.length);
139: }
140:
141: }
142: }
|