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.XMLStreamReader;
024: import javax.xml.stream.XMLInputFactory;
025: import javax.xml.stream.XMLStreamException;
026: import java.io.ByteArrayInputStream;
027: import java.lang.reflect.Method;
028: import java.lang.reflect.Array;
029: import java.lang.reflect.InvocationTargetException;
030: import java.beans.BeanInfo;
031: import java.beans.Introspector;
032: import java.beans.PropertyDescriptor;
033: import java.beans.IntrospectionException;
034:
035: import org.apache.axiom.om.util.StAXUtils;
036:
037: public class PopulateMinOccurs0Test extends TestCase {
038:
039: /*
040: <xs:element name="A" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
041: <xs:element name="B" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
042: <xs:element name="C" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
043: <xs:element name="D" type="xs:string" minOccurs="0" maxOccurs="1"/>
044: */
045:
046: // element D is missing
047: private String xmlString1 = "<root xmlns=\"http://test.org\">"
048: + "<A>I am A</A>" + "<B>I am B1</B>" + "<B>I am B2</B>"
049: + "<C>I am C1</C>" + "<C>I am C2</C>" + "</root>";
050:
051: //B elements are missing
052: private String xmlString2 = "<root xmlns=\"http://test.org\">"
053: + "<A>I am A</A>" + "<C>I am B2</C>" + "<C>I am B2</C>"
054: + "<D>I am D1</D>" + "</root>";
055:
056: //Only A is present
057: private String xmlString3 = "<root xmlns=\"http://test.org\">"
058: + "<A>I am A</A>" + "</root>";
059:
060: public void testPopulate1() throws Exception {
061: populateAndAssert(xmlString1, 2, "b");
062: populateAndAssert(xmlString1, 2, "c");
063: }
064:
065: public void testPopulate2() throws Exception {
066: populateAndAssert(xmlString2, 0, "b");
067: populateAndAssert(xmlString2, 2, "c");
068: }
069:
070: public void testPopulate3() throws Exception {
071: populateAndAssert(xmlString3, 0, "b");
072: populateAndAssert(xmlString3, 1, "a");
073: }
074:
075: private void populateAndAssert(String s, int expectedCount,
076: String itemtoTest) throws XMLStreamException,
077: ClassNotFoundException, NoSuchMethodException,
078: IllegalAccessException, InvocationTargetException,
079: IntrospectionException {
080: XMLStreamReader reader = StAXUtils
081: .createXMLStreamReader(new ByteArrayInputStream(s
082: .getBytes()));
083: Class clazz = Class.forName("org.test.Root");
084: Class innerClazz = clazz.getDeclaredClasses()[0];
085: Method parseMethod = innerClazz.getMethod("parse",
086: new Class[] { XMLStreamReader.class });
087: Object obj = parseMethod.invoke(null, new Object[] { reader });
088:
089: assertNotNull(obj);
090:
091: Object stringArray = null;
092: BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
093: PropertyDescriptor[] propertyDescriptors = beanInfo
094: .getPropertyDescriptors();
095: Method readMethod;
096:
097: for (int i = 0; i < propertyDescriptors.length; i++) {
098: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
099: if (itemtoTest.equals(propertyDescriptor.getDisplayName())) {
100: readMethod = propertyDescriptor.getReadMethod();
101: stringArray = readMethod.invoke(obj, null);
102: break;
103: }
104: }
105:
106: if (expectedCount != 0) {
107: assertNotNull(stringArray);
108: String[] array = (String[]) stringArray;
109: assertEquals(array.length, expectedCount);
110: }
111: }
112:
113: }
|