KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > jsfmeta > MetadataTest


1 /*
2  * MetadataTest.java
3  *
4  * Created on June 13, 2007, 11:59 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package com.icesoft.jsfmeta;
11
12 import com.sun.rave.jsfmeta.beans.ComponentBean;
13 import com.sun.rave.jsfmeta.beans.FacesConfigBean;
14 import com.sun.rave.jsfmeta.beans.PropertyBean;
15 import java.io.File JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.net.URL JavaDoc;
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import org.xml.sax.SAXException JavaDoc;
22
23 /**
24  *
25  * @author frank
26  */

27 public class MetadataTest extends TestCase{
28     
29     private String JavaDoc METADATA_XML = "extended-faces-config.xml";
30     private FacesConfigBean facesConfigBean;
31     
32     public static Test suite() {
33         return new TestSuite(MetadataTest.class);
34     }
35     
36     public static void main() {
37         junit.textui.TestRunner.run(MetadataTest.suite());
38     }
39     
40     protected void setUp(){
41         
42         File JavaDoc confFile = getConfDir();
43         boolean isConfFile = confFile.isDirectory();
44         if(!isConfFile){
45             System.out.println("no conf directory in the build directory: "+ confFile);
46             if( !confFile.mkdirs() )
47                 System.out.println("conf directory could not be created");
48         }
49         MetadataXmlParser jsfMetaParser = new MetadataXmlParser();
50         String JavaDoc filePath = confFile.getPath() + File.separatorChar
51                 + METADATA_XML;
52         try {
53             facesConfigBean = jsfMetaParser.parse(new File JavaDoc(filePath));
54             
55         } catch (SAXException JavaDoc ex) {
56             ex.printStackTrace();
57         } catch (IOException JavaDoc ex) {
58             ex.printStackTrace();
59         }
60     }
61     
62     public void testMetadata(){
63         
64         ComponentBean[] componentBeans = facesConfigBean.getComponents();
65         for(int i=0; i<componentBeans.length; i++){
66             PropertyBean[] propertyBeans = componentBeans[i].getProperties();
67             for(int j=0; j<propertyBeans.length; j++){
68                 if(propertyBeans[j].getPropertyClass() != null && propertyBeans[j].getPropertyClass().trim().equalsIgnoreCase("boolean")){
69                     if(propertyBeans[j].getEditorClass() != null){
70                         boolean flag = propertyBeans[j].getEditorClass().trim().length() > 0;
71                         assertFalse("\ncomponent class="+componentBeans[i].getComponentClass()+
72                                 "\ncomponent type="+componentBeans[i].getComponentType()+" has boolean property named="+propertyBeans[j].getPropertyName()
73                         +" \nusing wrong editor="+ propertyBeans[j].getEditorClass(), flag);
74                     }
75                 }
76             };
77         }
78     }
79     
80     private File JavaDoc getConfDir() {
81         
82         ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
83         URL JavaDoc url = classLoader.getResource(".");
84         
85         File JavaDoc buildDir = new File JavaDoc( convertFileUrlToPath(url) );
86         
87         if(!buildDir.isDirectory()){
88             System.out.println("test build directory does not exist: "+buildDir);
89             System.exit(1);
90         }
91         
92         File JavaDoc confFile = new File JavaDoc(buildDir.getParent() + File.separatorChar
93                 + "classes" + File.separator + "conf");
94         
95         return confFile;
96     }
97     /**
98      * Kind of hack-ish attempt at solving problem that if the directory,
99      * where we're building the component-metadata in, has special
100      * characters in its path, like spaces, then the URL to it will be
101      * escaped, which will be interpretted as a different directory,
102      * unless we unescape it.
103      */

104     private static String JavaDoc convertFileUrlToPath(URL JavaDoc url) {
105         
106         String JavaDoc path = url.getPath();
107         if( url.toExternalForm().startsWith("file:") ) {
108             StringBuffer JavaDoc sb = new StringBuffer JavaDoc( path.length() );
109             int pathLength = path.length();
110             for(int i = 0; i < pathLength;) {
111                 char c = path.charAt(i);
112                 if( c == '%' ) {
113                     if( (i+1) < pathLength && isHexDigit(path.charAt(i+1)) ) {
114                         int increment = 2;
115                         if( (i+2) < pathLength && isHexDigit(path.charAt(i+2)) )
116                             increment++;
117                         try {
118                             char unescaped = (char) Integer.parseInt(
119                                     path.substring(i+1, i+increment), 16);
120                             
121                             sb.append( unescaped );
122                             i += increment;
123                             continue;
124                         } catch(NumberFormatException JavaDoc nfe) {
125                             // Not a valid hex escape, so just fall through,
126
// and append it to the path
127
}
128                     }
129                 }
130                 sb.append( c );
131                 i++;
132             }
133             path = sb.toString();
134         }
135         return path;
136     }
137     
138     private static boolean isHexDigit(char c) {
139         return ( (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
140     }
141 }
142
Popular Tags