001: package com.icesoft.faces.metadata;
002:
003: import com.icesoft.jsfmeta.MetadataXmlParser;
004: import java.beans.BeanInfo;
005: import java.beans.IntrospectionException;
006: import java.beans.Introspector;
007: import java.beans.MethodDescriptor;
008: import java.beans.PropertyDescriptor;
009: import java.beans.SimpleBeanInfo;
010: import java.io.IOException;
011: import java.net.URL;
012:
013: import org.xml.sax.SAXException;
014: import com.sun.rave.jsfmeta.beans.ComponentBean;
015: import com.sun.rave.jsfmeta.beans.FacesConfigBean;
016:
017: import junit.framework.Test;
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: public class BeanPropertiesTest extends TestCase {
022:
023: public static Test suite() {
024: return new TestSuite(BeanPropertiesTest.class);
025: }
026:
027: public static void main() {
028: junit.textui.TestRunner.run(BeanPropertiesTest.suite());
029: }
030:
031: /*TODO: test argument signature */
032:
033: public void testComponentProperties() {
034:
035: String[] components = getComponentBeanInfo();
036: for (int j = 0; j < components.length; j++) {
037:
038: try {
039: Class beanInfoClass = Class.forName(components[j]
040: + "BeanInfo");
041: Object object = beanInfoClass.newInstance();
042: if (object instanceof SimpleBeanInfo) {
043: SimpleBeanInfo new_name = (SimpleBeanInfo) object;
044: testSimpleBeanInfo(new_name);
045: }
046: } catch (NullPointerException e) {
047: e.printStackTrace();
048: fail(e.getMessage());
049: } catch (ClassNotFoundException e) {
050: e.printStackTrace();
051: fail(e.getMessage());
052: } catch (InstantiationException e) {
053: e.printStackTrace();
054: fail(e.getMessage());
055: } catch (IllegalAccessException e) {
056: e.printStackTrace();
057: fail(e.getMessage());
058: } catch (Exception e) {
059: e.printStackTrace();
060: fail(e.getMessage());
061: }
062:
063: }
064: }
065:
066: public String[] getComponentBeanInfo() {
067:
068: String[] cb = null;
069: MetadataXmlParser jsfMetaParser = new MetadataXmlParser();
070:
071: try {
072: ClassLoader classLoader = Thread.currentThread()
073: .getContextClassLoader();
074: URL localUrl = classLoader.getResource(".");
075: String newPath = "file:"
076: + localUrl.getPath()
077: + "../../../component/conf/META-INF/faces-config.xml";
078: URL url = new URL(newPath);
079:
080: FacesConfigBean facesConfigBean = jsfMetaParser.parse(url);
081: ComponentBean[] componentbeans = facesConfigBean
082: .getComponents();
083: cb = new String[componentbeans.length];
084:
085: for (int i = 0; i < componentbeans.length; i++) {
086: cb[i] = componentbeans[i].getComponentClass();
087: }
088: } catch (IOException e) {
089: e.printStackTrace();
090: fail(e.getMessage());
091: } catch (SAXException e) {
092: e.printStackTrace();
093: fail(e.getMessage());
094: }
095:
096: return cb;
097: }
098:
099: public void testSimpleBeanInfo(SimpleBeanInfo simpleBeanInfo) {
100:
101: PropertyDescriptor[] pds = null;
102: Object newObject = null;
103: try {
104: pds = simpleBeanInfo.getPropertyDescriptors();
105: String className = simpleBeanInfo.getClass().getName();
106:
107: Class namedClass = Class.forName(className.substring(0,
108: className.indexOf("BeanInfo")));
109: newObject = namedClass.newInstance();
110:
111: } catch (NullPointerException e) {
112: e.printStackTrace();
113: fail(e.getMessage());
114: } catch (ClassNotFoundException e) {
115: e.printStackTrace();
116: fail(e.getMessage());
117: } catch (InstantiationException e) {
118: e.printStackTrace();
119: fail(e.getMessage());
120: } catch (IllegalAccessException e) {
121: e.printStackTrace();
122: fail(e.getMessage());
123: } catch (Exception e) {
124: e.printStackTrace();
125: fail(e.getMessage());
126: }
127:
128: String[] names = getMethodArray(newObject);
129: for (int i = 0; i < pds.length; i++) {
130: String propertyName = pds[i].getName();
131:
132: String tmp = "\tmethod name="
133: + pds[i].getReadMethod().getName() + " type"
134: + pds[i].getPropertyType();
135: boolean methodIndexBoolean = getMethodIndex(pds[i]
136: .getReadMethod().getName(), names) == -1;
137: String message = " failed class name= "
138: + newObject.getClass().getName() + " method name= "
139: + propertyName + "\n" + tmp;
140: assertEquals(" " + message + "", false, methodIndexBoolean);
141: }
142: }
143:
144: private int getMethodIndex(String methodName, String[] names) {
145:
146: int index = -1;
147: for (int j = 0; j < names.length; j++) {
148: if (methodName.equalsIgnoreCase(names[j])) {
149: index = j;
150: return index;
151: }
152: }
153: return index;
154: }
155:
156: private String[] getMethodArray(Object object) {
157:
158: String[] methodArray = null;
159: try {
160: BeanInfo beanInfo = Introspector.getBeanInfo(object
161: .getClass());
162: MethodDescriptor[] mds = beanInfo.getMethodDescriptors();
163: methodArray = new String[mds.length];
164: for (int j = 0; j < mds.length; j++) {
165: methodArray[j] = mds[j].getMethod().getName();
166: }
167: } catch (IntrospectionException e) {
168: e.printStackTrace();
169: }
170: return methodArray;
171: }
172: }
|