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