You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Gary VanMatre <gv...@comcast.net> on 2006/08/21 19:18:41 UTC

Re: How to configure the default renderers and component within a Shale-Test based unit test

>From: "Dennis Byrne" <de...@dbyrne.net> 
>
> I would be suprised if you found a quick and easy way to do this. MyFaces core 
> uses digester to unmarshal the config files. It then calls the API you mention. 
> I would start digging around in org.apache.myfaces.config . 
> 

 
Or, another way would be to write a simple helper method to extract the information from a working web application. You could build some code and drop it into an abstract test case. 


Consider the following to extract component's and renderers:




private void captureMetadata() {


FacesContext context = FacesContext.getCurrentInstance();


RenderKitFactory factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
RenderKit defaultRenderKit = factory.getRenderKit(context, context.getViewRoot().getRenderKitId());


Iterator cti = context.getApplication().getComponentTypes();
List componentInfo = new ArrayList();
List rendererInfo = new ArrayList();
while (cti.hasNext()) {
try {
String componentType = (String) cti.next();
UIComponent component = (UIComponent) context.getApplication().createComponent(componentType);


String family = component.getFamily();


String rendererType = component.getRendererType();
Renderer renderer = defaultRenderKit.getRenderer(family, rendererType);


String[] componentRegInfo = new String[2];
componentRegInfo[0] = componentType;
componentRegInfo[1] = component.getClass().getName();
componentInfo.add(componentRegInfo);


String[] rendererRegInfo = new String[3];
rendererRegInfo[0] = family;
rendererRegInfo[1] = rendererType;
rendererRegInfo[2] = renderer.getClass().getName();
rendererInfo.add(rendererRegInfo);


} catch (Exception e) {
e.printStackTrace();
}


}


StringBuffer buff = new StringBuffer();
buff.append("protected static final Object[] COMPONENTS = {\n");
for (int i = 0; i < componentInfo.size(); i++) {
String[] componentRegInfo = (String[]) componentInfo.get(i);


buff.append(" new String[] {\"").append(componentRegInfo[0])
.append("\", \"").append(componentRegInfo[1])
.append("},\n");


}
buff.append("};\n\n\n;");


buff.append("protected static final Object[] RENDERERS = {\n");
for (int i = 0; i < rendererInfo.size(); i++) {
String[] rendererRegInfo = (String[]) rendererInfo.get(i);


buff.append(" new String[] {\"").append(rendererRegInfo[0])
.append("\", \"").append(rendererRegInfo[1])
.append("\", \"").append(rendererRegInfo[2]).append("},\n");


}
buff.append("};\n");


System.out.print(buff.toString());


}






And the following to register with the shale test case framework:








The validators, converters and listeners can be introspected in the same fashion.


for (int i = 0; i < RENDERERS.length; i++) {
Renderer renderer = null;
renderer: for (int j = 2; j < 4; j++) {
try {
Class clazz = Class.forName(RENDERERS[i][j]);
if (clazz != null) {
renderer = (Renderer) clazz.newInstance();
if (renderer != null) {
//System.out.println(RENDERERS[i][j]);
break renderer;
}
}
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
}
if (renderer != null) {
  facesContext.getRenderKit().addRenderer(RENDERERS[i][0], RENDERERS[i][1],renderer);
  }
} 




for (int i = 0; i < COMPONENTS.length; i++) {
application.addComponent(((String[])COMPONENTS[i])[0], ((String[])COMPONENTS[i])[1]);
}

Gary


> Dennis Byrne 
> 
> >-----Original Message----- 
> >From: Paul Spencer [mailto:paulsp@apache.org] 
> >Sent: Sunday, August 20, 2006 11:42 PM 
> >To: 'MyFaces Development' 
> >Subject: How to configure the default renderers and component within a 
> Shale-Test based unit test 
> > 
> >I am writing a unit test based on the Shale Test Framework for a Tomahawk 
> >component. My current problem is the I need to add the default MyFaces and 
> >Tomahawk components and renderers to the FacesContext. To date I have been 
> >using facesContext.getApplication().addComponent(...) and 
> >facesContext.getRenderKit().addRenderer(...). This is becoming very 
> >cumbersome. I know the defaults are out their in various configuration files, 
> >but I do not know how to tell Shale's test framework how to use them. 
> > 
> >Suggestions? 
> > 
> >Paul Spencer 
> > 
> > 
> 
>