1 package com.icesoft.faces.metadata; 2 3 import java.io.IOException ; 4 import java.net.URL ; 5 6 import javax.faces.component.UIComponentBase; 7 import javax.faces.render.RenderKitFactory; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 13 import org.xml.sax.SAXException ; 14 15 import com.icesoft.jsfmeta.MetadataXmlParser; 16 import com.sun.rave.jsfmeta.beans.ComponentBean; 17 import com.sun.rave.jsfmeta.beans.FacesConfigBean; 18 import com.sun.rave.jsfmeta.beans.RendererBean; 19 20 public class RendererTypeTest extends TestCase { 21 22 private ComponentBean[] componentBeans; 23 24 private UIComponentBase[] uiComponentBases; 25 26 private RendererBean[] rendererBeans; 27 28 public static Test suite() { 29 return new TestSuite(RendererTypeTest.class); 30 } 31 32 public static void main() { 33 junit.textui.TestRunner.run(RendererTypeTest.suite()); 34 } 35 36 protected void setUp() { 37 38 if (componentBeans == null && uiComponentBases == null) { 39 componentBeans = getComponentBeanInfo(); 40 rendererBeans = getRendererBean(); 41 uiComponentBases = new UIComponentBase[componentBeans.length]; 42 43 for (int j = 0; j < componentBeans.length; j++) { 44 Object newObject = null; 45 46 try { 47 Class namedClass = Class.forName(componentBeans[j] 48 .getComponentClass()); 49 newObject = namedClass.newInstance(); 50 51 if (newObject instanceof UIComponentBase) { 52 uiComponentBases[j] = (UIComponentBase) newObject; 53 54 } 55 } catch (NullPointerException e) { 56 e.printStackTrace(); 57 fail(e.getMessage()); 58 } catch (ClassNotFoundException e) { 59 e.printStackTrace(); 60 fail(e.getMessage()); 61 } catch (InstantiationException e) { 62 e.printStackTrace(); 63 fail(e.getMessage()); 64 } catch (IllegalAccessException e) { 65 e.printStackTrace(); 66 fail(e.getMessage()); 67 } 68 } 69 } 70 } 71 72 public void testRendererType() { 73 74 for (int i = 0; i < componentBeans.length; i++) { 75 String renderTypeUIComponent = uiComponentBases[i] 76 .getRendererType(); 77 String renderTypeComponentBean = componentBeans[i] 78 .getRendererType(); 79 80 boolean notSameRenderType = renderTypeUIComponent != null 81 && renderTypeComponentBean != null 82 && !renderTypeUIComponent.trim().equalsIgnoreCase( 83 renderTypeComponentBean.trim()); 84 85 String message = "RenderType not the same for Component Class=" 86 + componentBeans[i].getComponentClass() 87 + "\n component renderType=" + renderTypeUIComponent 88 + "\n faces-config declared renderType=" 89 + renderTypeComponentBean + "\n\n"; 90 assertFalse(message, notSameRenderType); 91 } 92 } 93 94 public void testRendererClass() { 95 96 for (int i = 0; i < rendererBeans.length; i++) { 97 98 String message = ""; 99 try { 100 String rendererClass = rendererBeans[i].getRendererClass(); 101 System.out.println("rendererClass=" + rendererClass); 102 Class namedClass = Class.forName(rendererClass); 103 String packageName = namedClass.getPackage().getName(); 104 105 message = "RenderType not the same for Component Class=" 106 + componentBeans[i].getComponentClass() 107 + "\n component renderType=" 108 + rendererBeans[i].getRendererType() 109 + "\n renderer class=" + rendererClass + "\n\n"; 110 } catch (Exception e) { 111 fail(message); 112 } 113 } 114 } 115 116 public void testFamilyType() { 117 118 for (int i = 0; i < componentBeans.length; i++) { 119 String familyTypeUIComponent = uiComponentBases[i].getFamily(); 120 String familyTypeComponentBean = componentBeans[i] 121 .getComponentFamily(); 122 123 boolean notSameRenderType = familyTypeUIComponent != null 124 && familyTypeComponentBean != null 125 && !familyTypeUIComponent.trim().equalsIgnoreCase( 126 familyTypeComponentBean.trim()); 127 128 String message = "FamilyType not the same for Component Class=" 129 + componentBeans[i].getComponentClass() 130 + "\n component familyType=" + familyTypeUIComponent 131 + "\n faces-config declared familyType=" 132 + familyTypeComponentBean + "\n\n"; 133 assertFalse(message, notSameRenderType); 134 } 135 } 136 137 139 private FacesConfigBean getFacesConfigBean() { 140 141 FacesConfigBean facesConfigBean = null; 142 MetadataXmlParser jsfMetaParser = new MetadataXmlParser(); 143 try { 144 ClassLoader classLoader = Thread.currentThread() 145 .getContextClassLoader(); 146 URL localUrl = classLoader.getResource("."); 147 String newPath = "file:" + localUrl.getPath() 148 + "../../../component/conf/META-INF/faces-config.xml"; 149 URL url = new URL (newPath); 150 151 facesConfigBean = jsfMetaParser.parse(url); 152 } catch (IOException e) { 153 e.printStackTrace(); 154 fail(e.getMessage()); 155 } catch (SAXException e) { 156 e.printStackTrace(); 157 fail(e.getMessage()); 158 } 159 return facesConfigBean; 160 } 161 162 private ComponentBean[] getComponentBeanInfo() { 163 164 ComponentBean[] cb = getFacesConfigBean().getComponents(); 165 return cb; 166 } 167 168 private RendererBean[] getRendererBean() { 169 170 RendererBean[] rb = getFacesConfigBean().getRenderKit( 171 RenderKitFactory.HTML_BASIC_RENDER_KIT).getRenderers(); 172 return rb; 173 } 174 } 175 | Popular Tags |