KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > metadata > BeanPropertiesTest


1 package com.icesoft.faces.metadata;
2
3 import com.icesoft.jsfmeta.MetadataXmlParser;
4 import java.beans.BeanInfo JavaDoc;
5 import java.beans.IntrospectionException JavaDoc;
6 import java.beans.Introspector JavaDoc;
7 import java.beans.MethodDescriptor JavaDoc;
8 import java.beans.PropertyDescriptor JavaDoc;
9 import java.beans.SimpleBeanInfo JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.net.URL JavaDoc;
12
13 import org.xml.sax.SAXException JavaDoc;
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     /*TODO: test argument signature */
32     
33     public void testComponentProperties() {
34
35         String JavaDoc[] components = getComponentBeanInfo();
36         for (int j = 0; j < components.length; j++) {
37
38             try {
39                 Class JavaDoc beanInfoClass = Class.forName(components[j] + "BeanInfo");
40                 Object JavaDoc object = beanInfoClass.newInstance();
41                 if (object instanceof SimpleBeanInfo JavaDoc) {
42                     SimpleBeanInfo JavaDoc new_name = (SimpleBeanInfo JavaDoc) object;
43                     testSimpleBeanInfo(new_name);
44                 }
45             } catch (NullPointerException JavaDoc e) {
46                 e.printStackTrace();
47                 fail(e.getMessage());
48             } catch (ClassNotFoundException JavaDoc e) {
49                 e.printStackTrace();
50                 fail(e.getMessage());
51             } catch (InstantiationException JavaDoc e) {
52                 e.printStackTrace();
53                 fail(e.getMessage());
54             } catch (IllegalAccessException JavaDoc e) {
55                 e.printStackTrace();
56                 fail(e.getMessage());
57             } catch (Exception JavaDoc e){
58                 e.printStackTrace();
59                 fail(e.getMessage());
60             }
61
62         }
63     }
64
65     public String JavaDoc[] getComponentBeanInfo() {
66
67         String JavaDoc[] cb = null;
68         MetadataXmlParser jsfMetaParser = new MetadataXmlParser();
69
70         try {
71             ClassLoader JavaDoc classLoader = Thread.currentThread()
72                     .getContextClassLoader();
73             URL JavaDoc localUrl = classLoader.getResource(".");
74             String JavaDoc newPath = "file:" + localUrl.getPath()
75                     + "../../../component/conf/META-INF/faces-config.xml";
76             URL JavaDoc url = new URL JavaDoc(newPath);
77
78             FacesConfigBean facesConfigBean = jsfMetaParser.parse(url);
79             ComponentBean[] componentbeans = facesConfigBean.getComponents();
80             cb = new String JavaDoc[componentbeans.length];
81
82             for (int i = 0; i < componentbeans.length; i++) {
83                 cb[i] = componentbeans[i].getComponentClass();
84             }
85         } catch (IOException JavaDoc e) {
86             e.printStackTrace();
87             fail(e.getMessage());
88         } catch (SAXException JavaDoc e) {
89             e.printStackTrace();
90             fail(e.getMessage());
91         }
92
93         return cb;
94     }
95
96     public void testSimpleBeanInfo(SimpleBeanInfo JavaDoc simpleBeanInfo) {
97
98         PropertyDescriptor JavaDoc[] pds = null;
99         Object JavaDoc newObject = null;
100         try {
101             pds = simpleBeanInfo.getPropertyDescriptors();
102             String JavaDoc className = simpleBeanInfo.getClass().getName();
103             
104             Class JavaDoc namedClass = Class.forName(className.substring(0, className
105                     .indexOf("BeanInfo")));
106             newObject = namedClass.newInstance();
107             
108             
109         } catch (NullPointerException JavaDoc e) {
110             e.printStackTrace();
111             fail(e.getMessage());
112         } catch (ClassNotFoundException JavaDoc e) {
113             e.printStackTrace();
114             fail(e.getMessage());
115         } catch (InstantiationException JavaDoc e) {
116             e.printStackTrace();
117             fail(e.getMessage());
118         } catch (IllegalAccessException JavaDoc e) {
119             e.printStackTrace();
120             fail(e.getMessage());
121         } catch (Exception JavaDoc e){
122             e.printStackTrace();
123             fail(e.getMessage());
124         }
125         
126         
127         String JavaDoc[] names = getMethodArray(newObject);
128         for (int i = 0; i < pds.length; i++) {
129             String JavaDoc propertyName = pds[i].getName();
130
131             String JavaDoc 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 JavaDoc 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 JavaDoc methodName, String JavaDoc[] 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 JavaDoc[] getMethodArray(Object JavaDoc object) {
155
156         String JavaDoc[] methodArray = null;
157         try {
158             BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(object.getClass());
159             MethodDescriptor JavaDoc[] mds = beanInfo.getMethodDescriptors();
160             methodArray = new String JavaDoc[mds.length];
161             for (int j = 0; j < mds.length; j++) {
162                 methodArray[j] = mds[j].getMethod().getName();
163             }
164         } catch (IntrospectionException JavaDoc e) {
165             e.printStackTrace();
166         }
167         return methodArray;
168     }
169 }
170
Popular Tags