001: /*
002: * MetadataTest.java
003: *
004: * Created on June 13, 2007, 11:59 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package com.icesoft.jsfmeta;
011:
012: import com.sun.rave.jsfmeta.beans.ComponentBean;
013: import com.sun.rave.jsfmeta.beans.FacesConfigBean;
014: import com.sun.rave.jsfmeta.beans.PropertyBean;
015: import java.io.File;
016: import java.io.IOException;
017: import java.net.URL;
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import org.xml.sax.SAXException;
022:
023: /**
024: *
025: * @author frank
026: */
027: public class MetadataTest extends TestCase {
028:
029: private String METADATA_XML = "extended-faces-config.xml";
030: private FacesConfigBean facesConfigBean;
031:
032: public static Test suite() {
033: return new TestSuite(MetadataTest.class);
034: }
035:
036: public static void main() {
037: junit.textui.TestRunner.run(MetadataTest.suite());
038: }
039:
040: protected void setUp() {
041:
042: File confFile = getConfDir();
043: boolean isConfFile = confFile.isDirectory();
044: if (!isConfFile) {
045: System.out
046: .println("no conf directory in the build directory: "
047: + confFile);
048: if (!confFile.mkdirs())
049: System.out
050: .println("conf directory could not be created");
051: }
052: MetadataXmlParser jsfMetaParser = new MetadataXmlParser();
053: String filePath = confFile.getPath() + File.separatorChar
054: + METADATA_XML;
055: try {
056: facesConfigBean = jsfMetaParser.parse(new File(filePath));
057:
058: } catch (SAXException ex) {
059: ex.printStackTrace();
060: } catch (IOException ex) {
061: ex.printStackTrace();
062: }
063: }
064:
065: public void testMetadata() {
066:
067: ComponentBean[] componentBeans = facesConfigBean
068: .getComponents();
069: for (int i = 0; i < componentBeans.length; i++) {
070: PropertyBean[] propertyBeans = componentBeans[i]
071: .getProperties();
072: for (int j = 0; j < propertyBeans.length; j++) {
073: if (propertyBeans[j].getPropertyClass() != null
074: && propertyBeans[j].getPropertyClass().trim()
075: .equalsIgnoreCase("boolean")) {
076: if (propertyBeans[j].getEditorClass() != null) {
077: boolean flag = propertyBeans[j]
078: .getEditorClass().trim().length() > 0;
079: assertFalse("\ncomponent class="
080: + componentBeans[i].getComponentClass()
081: + "\ncomponent type="
082: + componentBeans[i].getComponentType()
083: + " has boolean property named="
084: + propertyBeans[j].getPropertyName()
085: + " \nusing wrong editor="
086: + propertyBeans[j].getEditorClass(),
087: flag);
088: }
089: }
090: }
091: ;
092: }
093: }
094:
095: private File getConfDir() {
096:
097: ClassLoader classLoader = Thread.currentThread()
098: .getContextClassLoader();
099: URL url = classLoader.getResource(".");
100:
101: File buildDir = new File(convertFileUrlToPath(url));
102:
103: if (!buildDir.isDirectory()) {
104: System.out.println("test build directory does not exist: "
105: + buildDir);
106: System.exit(1);
107: }
108:
109: File confFile = new File(buildDir.getParent()
110: + File.separatorChar + "classes" + File.separator
111: + "conf");
112:
113: return confFile;
114: }
115:
116: /**
117: * Kind of hack-ish attempt at solving problem that if the directory,
118: * where we're building the component-metadata in, has special
119: * characters in its path, like spaces, then the URL to it will be
120: * escaped, which will be interpretted as a different directory,
121: * unless we unescape it.
122: */
123: private static String convertFileUrlToPath(URL url) {
124:
125: String path = url.getPath();
126: if (url.toExternalForm().startsWith("file:")) {
127: StringBuffer sb = new StringBuffer(path.length());
128: int pathLength = path.length();
129: for (int i = 0; i < pathLength;) {
130: char c = path.charAt(i);
131: if (c == '%') {
132: if ((i + 1) < pathLength
133: && isHexDigit(path.charAt(i + 1))) {
134: int increment = 2;
135: if ((i + 2) < pathLength
136: && isHexDigit(path.charAt(i + 2)))
137: increment++;
138: try {
139: char unescaped = (char) Integer.parseInt(
140: path
141: .substring(i + 1, i
142: + increment), 16);
143:
144: sb.append(unescaped);
145: i += increment;
146: continue;
147: } catch (NumberFormatException nfe) {
148: // Not a valid hex escape, so just fall through,
149: // and append it to the path
150: }
151: }
152: }
153: sb.append(c);
154: i++;
155: }
156: path = sb.toString();
157: }
158: return path;
159: }
160:
161: private static boolean isHexDigit(char c) {
162: return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
163: }
164: }
|