1 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 ; 16 import java.io.IOException ; 17 import java.net.URL ; 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import org.xml.sax.SAXException ; 22 23 27 public class MetadataTest extends TestCase{ 28 29 private String 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 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 filePath = confFile.getPath() + File.separatorChar 51 + METADATA_XML; 52 try { 53 facesConfigBean = jsfMetaParser.parse(new File (filePath)); 54 55 } catch (SAXException ex) { 56 ex.printStackTrace(); 57 } catch (IOException 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 getConfDir() { 81 82 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 83 URL url = classLoader.getResource("."); 84 85 File buildDir = new File ( 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 confFile = new File (buildDir.getParent() + File.separatorChar 93 + "classes" + File.separator + "conf"); 94 95 return confFile; 96 } 97 104 private static String convertFileUrlToPath(URL url) { 105 106 String path = url.getPath(); 107 if( url.toExternalForm().startsWith("file:") ) { 108 StringBuffer sb = new StringBuffer ( 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 nfe) { 125 } 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 |