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.XMLInputFactory;
024: import javax.xml.stream.XMLStreamReader;
025: import java.beans.BeanInfo;
026: import java.beans.Introspector;
027: import java.beans.PropertyDescriptor;
028: import java.io.ByteArrayInputStream;
029: import java.lang.reflect.Array;
030: import java.lang.reflect.Method;
031:
032: import org.apache.axiom.om.util.StAXUtils;
033:
034: public class PopulateArrayTest extends TestCase {
035: private String xmlString = "<myobject xmlns=\"http://soapinterop.org2/xsd\">"
036: + "<varFloat>3.3</varFloat>"
037: + "<varInt>5</varInt>"
038: + "<varString>Hello</varString>"
039: + "<varString>Hello2</varString>"
040: + "<varString>Hello3</varString>" + "</myobject>";
041:
042: public void testPopulate() throws Exception {
043:
044: XMLStreamReader reader = StAXUtils
045: .createXMLStreamReader(new ByteArrayInputStream(
046: xmlString.getBytes()));
047: Class clazz = Class.forName("org2.soapinterop.xsd.Myobject");
048: Class innerClazz = clazz.getDeclaredClasses()[0];
049: Method parseMethod = innerClazz.getMethod("parse",
050: new Class[] { XMLStreamReader.class });
051: Object obj = parseMethod.invoke(null, new Object[] { reader });
052:
053: Object soapStruct = null;
054: BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
055: PropertyDescriptor[] propertyDescriptors = beanInfo
056: .getPropertyDescriptors();
057: Method readMethod;
058: for (int i = 0; i < propertyDescriptors.length; i++) {
059: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
060: if ("myobject".equals(propertyDescriptor.getDisplayName())) {
061: readMethod = propertyDescriptor.getReadMethod();
062: soapStruct = readMethod.invoke(obj, (Object[]) null);
063: break;
064: }
065: }
066:
067: assertNotNull(soapStruct);
068:
069: BeanInfo structBeanInfo = Introspector.getBeanInfo(soapStruct
070: .getClass());
071: PropertyDescriptor[] structPropertyDescriptors = structBeanInfo
072: .getPropertyDescriptors();
073: for (int i = 0; i < structPropertyDescriptors.length; i++) {
074: PropertyDescriptor propertyDescriptor = structPropertyDescriptors[i];
075: if ("varInt".equals(propertyDescriptor.getDisplayName())) {
076: readMethod = propertyDescriptor.getReadMethod();
077: assertEquals("varInt is not properly set", new Integer(
078: 5), readMethod.invoke(soapStruct,
079: (Object[]) null));
080: } else if ("varFloat".equals(propertyDescriptor
081: .getDisplayName())) {
082: readMethod = propertyDescriptor.getReadMethod();
083: assertEquals("varFloat is not properly set", new Float(
084: 3.3), readMethod.invoke(soapStruct,
085: (Object[]) null));
086: } else if ("varString".equals(propertyDescriptor
087: .getDisplayName())) {
088: readMethod = propertyDescriptor.getReadMethod();
089: assertTrue("String array is not set", readMethod
090: .getReturnType().isArray());
091: Object array = readMethod.invoke(soapStruct,
092: (Object[]) null);
093: int length = Array.getLength(array);
094: assertEquals("Array length is not correct", length, 3);
095:
096: //check the items here
097: }
098:
099: }
100:
101: }
102:
103: }
|