View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/introspection/TestXMLIntrospector.java,v 1.4 2003/01/11 10:38:55 dion Exp $ 3 * $Revision: 1.4 $ 4 * $Date: 2003/01/11 10:38:55 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>;. 59 * 60 * $Id: TestXMLIntrospector.java,v 1.4 2003/01/11 10:38:55 dion Exp $ 61 */ 62 package org.apache.commons.betwixt.introspection; 63 64 import java.io.StringWriter; 65 66 import java.beans.Introspector; 67 import java.beans.PropertyDescriptor; 68 import java.beans.BeanInfo; 69 70 import junit.framework.Test; 71 import junit.framework.TestSuite; 72 import junit.textui.TestRunner; 73 74 import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry; 75 import org.apache.commons.betwixt.registry.NoCacheRegistry; 76 77 import org.apache.commons.betwixt.XMLBeanInfo; 78 import org.apache.commons.betwixt.XMLIntrospector; 79 import org.apache.commons.betwixt.AbstractTestCase; 80 import org.apache.commons.betwixt.ElementDescriptor; 81 import org.apache.commons.betwixt.AttributeDescriptor; 82 83 import org.apache.commons.betwixt.io.BeanWriter; 84 85 86 /*** Test harness for the XMLIntrospector 87 * 88 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 89 * @version $Revision: 1.4 $ 90 */ 91 public class TestXMLIntrospector extends AbstractTestCase { 92 93 public static void main( String[] args ) { 94 TestRunner.run( suite() ); 95 } 96 97 public static Test suite() { 98 return new TestSuite(TestXMLIntrospector.class); 99 } 100 101 public TestXMLIntrospector(String testName) { 102 super(testName); 103 } 104 105 public void testIntrospector() throws Exception { 106 XMLIntrospector introspector = new XMLIntrospector(); 107 introspector.setAttributesForPrimitives(true); 108 109 Object bean = createBean(); 110 111 XMLBeanInfo info = introspector.introspect( bean ); 112 113 assertTrue( "Found XMLBeanInfo", info != null ); 114 115 ElementDescriptor descriptor = info.getElementDescriptor(); 116 117 assertTrue( "Found root element descriptor", descriptor != null ); 118 119 AttributeDescriptor[] attributes = descriptor.getAttributeDescriptors(); 120 121 assertTrue( "Found attributes", attributes != null && attributes.length > 0 ); 122 123 // test second introspection with caching on 124 info = introspector.introspect( bean ); 125 126 assertTrue( "Found XMLBeanInfo", info != null ); 127 128 descriptor = info.getElementDescriptor(); 129 130 assertTrue( "Found root element descriptor", descriptor != null ); 131 132 attributes = descriptor.getAttributeDescriptors(); 133 134 assertTrue( "Found attributes", attributes != null && attributes.length > 0 ); 135 136 137 // test introspection with caching off 138 //introspector.setCachingEnabled(false); 139 introspector.setRegistry(new NoCacheRegistry()); 140 info = introspector.introspect( bean ); 141 142 assertTrue( "Found XMLBeanInfo", info != null ); 143 144 descriptor = info.getElementDescriptor(); 145 146 assertTrue( "Found root element descriptor", descriptor != null ); 147 148 attributes = descriptor.getAttributeDescriptors(); 149 150 assertTrue( "Found attributes", attributes != null && attributes.length > 0 ); 151 152 153 // test introspection after flushing cache 154 // introspector.setCachingEnabled(true); 155 introspector.setRegistry(new DefaultXMLBeanInfoRegistry()); 156 //introspector.flushCache(); 157 info = introspector.introspect( bean ); 158 159 assertTrue( "Found XMLBeanInfo", info != null ); 160 161 descriptor = info.getElementDescriptor(); 162 163 assertTrue( "Found root element descriptor", descriptor != null ); 164 165 attributes = descriptor.getAttributeDescriptors(); 166 167 assertTrue( "Found attributes", attributes != null && attributes.length > 0 ); 168 169 } 170 171 public void testBeanWithBeanInfo() throws Exception { 172 173 // let's check that bean info's ok 174 BeanInfo bwbiBeanInfo = Introspector.getBeanInfo(BeanWithBeanInfoBean.class); 175 176 PropertyDescriptor[] propertyDescriptors = bwbiBeanInfo.getPropertyDescriptors(); 177 178 assertEquals("Wrong number of properties", 2 , propertyDescriptors.length); 179 180 // order of properties isn't guarenteed 181 if ("alpha".equals(propertyDescriptors[0].getName())) { 182 183 assertEquals("Second property name", "gamma" , propertyDescriptors[1].getName()); 184 185 } else { 186 187 assertEquals("First property name", "gamma" , propertyDescriptors[0].getName()); 188 assertEquals("Second property name", "alpha" , propertyDescriptors[1].getName()); 189 } 190 191 // finished with the descriptors 192 propertyDescriptors = null; 193 194 // SimpleLog log = new SimpleLog("[testBeanWithBeanInfo:XMLIntrospector]"); 195 // log.setLevel(SimpleLog.LOG_LEVEL_TRACE); 196 197 XMLIntrospector introspector = new XMLIntrospector(); 198 introspector.setAttributesForPrimitives(false); 199 // introspector.setLog(log); 200 201 XMLBeanInfo xmlBeanInfo = introspector.introspect(BeanWithBeanInfoBean.class); 202 203 ElementDescriptor[] elementDescriptors = xmlBeanInfo.getElementDescriptor().getElementDescriptors(); 204 205 // log = new SimpleLog("[testBeanWithBeanInfo]"); 206 // log.setLevel(SimpleLog.LOG_LEVEL_DEBUG); 207 208 // log.debug("XMLBeanInfo:"); 209 // log.debug(xmlBeanInfo); 210 // log.debug("Elements:"); 211 // log.debug(elementDescriptors[0].getPropertyName()); 212 // log.debug(elementDescriptors[1].getPropertyName()); 213 214 assertEquals("Wrong number of elements", 2 , elementDescriptors.length); 215 216 // order of properties isn't guarenteed 217 boolean alphaFirst = true; 218 if ("alpha".equals(elementDescriptors[0].getPropertyName())) { 219 220 assertEquals("Second element name", "gamma" , elementDescriptors[1].getPropertyName()); 221 222 } else { 223 alphaFirst = false; 224 assertEquals("First element name", "gamma" , elementDescriptors[0].getPropertyName()); 225 assertEquals("Second element name", "alpha" , elementDescriptors[1].getPropertyName()); 226 } 227 228 // might as well give test output 229 StringWriter out = new StringWriter(); 230 BeanWriter writer = new BeanWriter(out); 231 writer.setWriteIDs(false); 232 BeanWithBeanInfoBean bean = new BeanWithBeanInfoBean("alpha value","beta value","gamma value"); 233 writer.write(bean); 234 235 if (alphaFirst) { 236 237 xmlAssertIsomorphicContent( 238 parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-a.xml"), 239 parseString(out.toString())); 240 241 } else { 242 xmlAssertIsomorphicContent( 243 parseFile("src/test/org/apache/commons/betwixt/introspection/test-bwbi-output-g.xml"), 244 parseString(out.toString())); 245 } 246 } 247 } 248

This page was automatically generated by Maven