1 package com.icesoft.faces.metadata; 2 3 import com.icesoft.jsfmeta.MetadataXmlParser; 4 import java.beans.BeanInfo ; 5 import java.beans.IntrospectionException ; 6 import java.beans.Introspector ; 7 import java.beans.MethodDescriptor ; 8 import java.beans.PropertyDescriptor ; 9 import java.beans.SimpleBeanInfo ; 10 import java.io.IOException ; 11 import java.net.URL ; 12 13 import org.xml.sax.SAXException ; 14 import com.sun.rave.jsfmeta.beans.ComponentBean; 15 import com.sun.rave.jsfmeta.beans.FacesConfigBean; 16 17 import junit.framework.Test; 18 import junit.framework.TestCase; 19 import junit.framework.TestSuite; 20 21 public class BeanPropertiesTest extends TestCase { 22 23 public static Test suite() { 24 return new TestSuite(BeanPropertiesTest.class); 25 } 26 27 public static void main() { 28 junit.textui.TestRunner.run(BeanPropertiesTest.suite()); 29 } 30 31 32 33 public void testComponentProperties() { 34 35 String [] components = getComponentBeanInfo(); 36 for (int j = 0; j < components.length; j++) { 37 38 try { 39 Class beanInfoClass = Class.forName(components[j] + "BeanInfo"); 40 Object object = beanInfoClass.newInstance(); 41 if (object instanceof SimpleBeanInfo ) { 42 SimpleBeanInfo new_name = (SimpleBeanInfo ) object; 43 testSimpleBeanInfo(new_name); 44 } 45 } catch (NullPointerException e) { 46 e.printStackTrace(); 47 fail(e.getMessage()); 48 } catch (ClassNotFoundException e) { 49 e.printStackTrace(); 50 fail(e.getMessage()); 51 } catch (InstantiationException e) { 52 e.printStackTrace(); 53 fail(e.getMessage()); 54 } catch (IllegalAccessException e) { 55 e.printStackTrace(); 56 fail(e.getMessage()); 57 } catch (Exception e){ 58 e.printStackTrace(); 59 fail(e.getMessage()); 60 } 61 62 } 63 } 64 65 public String [] getComponentBeanInfo() { 66 67 String [] cb = null; 68 MetadataXmlParser jsfMetaParser = new MetadataXmlParser(); 69 70 try { 71 ClassLoader classLoader = Thread.currentThread() 72 .getContextClassLoader(); 73 URL localUrl = classLoader.getResource("."); 74 String newPath = "file:" + localUrl.getPath() 75 + "../../../component/conf/META-INF/faces-config.xml"; 76 URL url = new URL (newPath); 77 78 FacesConfigBean facesConfigBean = jsfMetaParser.parse(url); 79 ComponentBean[] componentbeans = facesConfigBean.getComponents(); 80 cb = new String [componentbeans.length]; 81 82 for (int i = 0; i < componentbeans.length; i++) { 83 cb[i] = componentbeans[i].getComponentClass(); 84 } 85 } catch (IOException e) { 86 e.printStackTrace(); 87 fail(e.getMessage()); 88 } catch (SAXException e) { 89 e.printStackTrace(); 90 fail(e.getMessage()); 91 } 92 93 return cb; 94 } 95 96 public void testSimpleBeanInfo(SimpleBeanInfo simpleBeanInfo) { 97 98 PropertyDescriptor [] pds = null; 99 Object newObject = null; 100 try { 101 pds = simpleBeanInfo.getPropertyDescriptors(); 102 String className = simpleBeanInfo.getClass().getName(); 103 104 Class namedClass = Class.forName(className.substring(0, className 105 .indexOf("BeanInfo"))); 106 newObject = namedClass.newInstance(); 107 108 109 } catch (NullPointerException e) { 110 e.printStackTrace(); 111 fail(e.getMessage()); 112 } catch (ClassNotFoundException e) { 113 e.printStackTrace(); 114 fail(e.getMessage()); 115 } catch (InstantiationException e) { 116 e.printStackTrace(); 117 fail(e.getMessage()); 118 } catch (IllegalAccessException e) { 119 e.printStackTrace(); 120 fail(e.getMessage()); 121 } catch (Exception e){ 122 e.printStackTrace(); 123 fail(e.getMessage()); 124 } 125 126 127 String [] names = getMethodArray(newObject); 128 for (int i = 0; i < pds.length; i++) { 129 String propertyName = pds[i].getName(); 130 131 String tmp = "\tmethod name=" + pds[i].getReadMethod().getName() 132 + " type" + pds[i].getPropertyType(); 133 boolean methodIndexBoolean = getMethodIndex(pds[i].getReadMethod() 134 .getName(), names) == -1; 135 String message = " failed class name= " 136 + newObject.getClass().getName() + " method name= " 137 + propertyName +"\n"+ tmp; 138 assertEquals(" " + message + "", false, methodIndexBoolean); 139 } 140 } 141 142 private int getMethodIndex(String methodName, String [] names) { 143 144 int index = -1; 145 for (int j = 0; j < names.length; j++) { 146 if (methodName.equalsIgnoreCase(names[j])) { 147 index = j; 148 return index; 149 } 150 } 151 return index; 152 } 153 154 private String [] getMethodArray(Object object) { 155 156 String [] methodArray = null; 157 try { 158 BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass()); 159 MethodDescriptor [] mds = beanInfo.getMethodDescriptors(); 160 methodArray = new String [mds.length]; 161 for (int j = 0; j < mds.length; j++) { 162 methodArray[j] = mds[j].getMethod().getName(); 163 } 164 } catch (IntrospectionException e) { 165 e.printStackTrace(); 166 } 167 return methodArray; 168 } 169 } 170 | Popular Tags |