01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.schema.populate.other;
20:
21: import junit.framework.TestCase;
22:
23: import javax.xml.stream.XMLStreamReader;
24: import javax.xml.stream.XMLInputFactory;
25: import java.io.ByteArrayInputStream;
26: import java.lang.reflect.Method;
27: import java.beans.BeanInfo;
28: import java.beans.Introspector;
29: import java.beans.PropertyDescriptor;
30:
31: import org.apache.axiom.om.util.StAXUtils;
32:
33: public class PopulateChoiceTest extends TestCase {
34: private String xmlString = "<myElement xmlns=\"http://soapinterop.org/types\">"
35: + "<varFloat>3.3</varFloat>" + "</myElement>";
36:
37: public void testPopulate() throws Exception {
38:
39: XMLStreamReader reader = StAXUtils
40: .createXMLStreamReader(new ByteArrayInputStream(
41: xmlString.getBytes()));
42: Class clazz = Class.forName("org.soapinterop.types.MyElement");
43: Class innerClazz = clazz.getDeclaredClasses()[0];
44: Method parseMethod = innerClazz.getMethod("parse",
45: new Class[] { XMLStreamReader.class });
46: Object obj = parseMethod.invoke(null, new Object[] { reader });
47:
48: Object myElement = null;
49: BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
50: PropertyDescriptor[] propertyDescriptors = beanInfo
51: .getPropertyDescriptors();
52: Method readMethod;
53: for (int i = 0; i < propertyDescriptors.length; i++) {
54: PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
55: if ("myElement".equals(propertyDescriptor.getDisplayName())) {
56: readMethod = propertyDescriptor.getReadMethod();
57: myElement = readMethod.invoke(obj, (Object[]) null);
58: break;
59: }
60: }
61: assertNotNull(myElement);
62:
63: BeanInfo structBeanInfo = Introspector.getBeanInfo(myElement
64: .getClass());
65: PropertyDescriptor[] structPropertyDescriptors = structBeanInfo
66: .getPropertyDescriptors();
67: for (int i = 0; i < structPropertyDescriptors.length; i++) {
68: PropertyDescriptor propertyDescriptor = structPropertyDescriptors[i];
69: if ("varFloat".equals(propertyDescriptor.getDisplayName())) {
70: readMethod = propertyDescriptor.getReadMethod();
71: assertEquals("varFloat is not properly set", new Float(
72: 3.3), readMethod.invoke(myElement,
73: (Object[]) null));
74:
75: } else if ("varInt".equals(propertyDescriptor
76: .getDisplayName())) {
77: readMethod = propertyDescriptor.getReadMethod();
78: //this should not be set ! - it should return zero
79: assertEquals("varInt is not properly set", new Integer(
80: 0), readMethod.invoke(myElement,
81: (Object[]) null));
82: }
83:
84: }
85: }
86: }
|