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.beans.IntrospectionException;
027: import java.beans.BeanInfo;
028: import java.beans.PropertyDescriptor;
029: import java.beans.Introspector;
030: import java.lang.reflect.InvocationTargetException;
031: import java.lang.reflect.Method;
032: import java.io.ByteArrayInputStream;
033:
034: import org.apache.axiom.om.util.StAXUtils;
035:
036: public class PopulateComplexMinOccurs0Test extends TestCase {
037:
038: // all are present
039: private String xmlString1 = "<root xmlns=\"http://test1.org\">"
040: + "<city>Colombo</city>" + "<person>" + "<name>Amy</name>"
041: + "<age>10</age>" + "</person>" + "<address>"
042: + "<streetName>My Street</streetName>"
043: + "<streetNo>1</streetNo>" + "</address>" + "</root>";
044:
045: //person element is missing
046: private String xmlString2 = "<root xmlns=\"http://test1.org\">"
047: + "<city>Colombo</city>" + "<address>"
048: + "<streetName>My Street</streetName>"
049: + "<streetNo>1</streetNo>" + "</address>" + "</root>";
050:
051: //Only city is present
052: private String xmlString3 = "<root xmlns=\"http://test1.org\">"
053: + "<city>Colombo</city>" + "</root>";
054:
055: // all are present with two addresses
056: private String xmlString4 = "<root xmlns=\"http://test1.org\">"
057: + "<city>Colombo</city>" + "<person>" + "<name>Amy</name>"
058: + "<age>10</age>" + "</person>" + "<address>"
059: + "<streetName>My Street</streetName>"
060: + "<streetNo>1</streetNo>" + "</address>" + "<address>"
061: + "<streetName>My Street2</streetName>"
062: + "<streetNo>2</streetNo>" + "</address>" + "</root>";
063:
064: public void testPopulate1() throws Exception {
065: populateAndAssert(xmlString1, 1, true);
066: }
067:
068: public void testPopulate2() throws Exception {
069: populateAndAssert(xmlString2, 1, false);
070: }
071:
072: public void testPopulate3() throws Exception {
073: populateAndAssert(xmlString3, 0, false);
074: }
075:
076: public void testPopulate4() throws Exception {
077: populateAndAssert(xmlString4, 2, true);
078: }
079:
080: private void populateAndAssert(String s, int expectedAddressCount,
081: boolean personPresent) throws XMLStreamException,
082: ClassNotFoundException, NoSuchMethodException,
083: IllegalAccessException, InvocationTargetException,
084: IntrospectionException {
085: XMLStreamReader reader = StAXUtils
086: .createXMLStreamReader(new ByteArrayInputStream(s
087: .getBytes()));
088: Class clazz = Class.forName("org.test1.Root");
089: Class innerClazz = clazz.getDeclaredClasses()[0];
090: Method parseMethod = innerClazz.getMethod("parse",
091: new Class[] { XMLStreamReader.class });
092: Object obj = parseMethod.invoke(null, new Object[] { reader });
093:
094: assertNotNull(obj);
095:
096: Object personObject = null;
097: Object addressObject = null;
098: BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
099: PropertyDescriptor[] propertyDescriptors = beanInfo
100: .getPropertyDescriptors();
101: Method readMethod;
102:
103: for (int i = 0; i < propertyDescriptors.length; i++) {
104: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
105: if ("person".equals(propertyDescriptor.getDisplayName())) {
106: readMethod = propertyDescriptor.getReadMethod();
107: personObject = readMethod.invoke(obj, null);
108:
109: if (personPresent)
110: assertNotNull(personObject);
111: else
112: assertNull(personObject);
113: } else if ("address".equals(propertyDescriptor
114: .getDisplayName())) {
115: readMethod = propertyDescriptor.getReadMethod();
116: addressObject = readMethod.invoke(obj, null);
117: if (expectedAddressCount != 0) {
118: Object[] objArray = (Object[]) addressObject;
119: assertEquals(expectedAddressCount, objArray.length);
120: }
121: }
122: }
123:
124: }
125:
126: }
|