Skip to content

Instantly share code, notes, and snippets.

@lfryc
Created April 23, 2013 14:36
Show Gist options
  • Save lfryc/5444073 to your computer and use it in GitHub Desktop.
Save lfryc/5444073 to your computer and use it in GitHub Desktop.
package org.richfaces.integration.partialViewContext;
import static org.jboss.arquillian.warp.jsf.Phase.RENDER_RESPONSE;
import java.io.IOException;
import java.net.URL;
import javax.faces.application.Application;
import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.xml.parsers.ParserConfigurationException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.warp.Activity;
import org.jboss.arquillian.warp.Inspection;
import org.jboss.arquillian.warp.Warp;
import org.jboss.arquillian.warp.WarpTest;
import org.jboss.arquillian.warp.jsf.AfterPhase;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.facesconfig20.WebFacesConfigDescriptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.richfaces.integration.CoreDeployment;
import org.richfaces.shrinkwrap.descriptor.FaceletAsset;
import org.xml.sax.SAXException;
import com.google.common.base.Function;
/**
* Tests that the exception in ajax requests ends partial-response correctly (RF-12893)
*/
@RunAsClient
@RunWith(Arquillian.class)
@WarpTest
public class AjaxRenderingFailurePropagationTest {
@Drone
private WebDriver browser;
@ArquillianResource
private URL contextPath;
@FindBy(id = "button1")
private WebElement button1;
@FindBy(id = "button2")
private WebElement button2;
@FindBy(tagName = "body")
private WebElement body;
@Deployment
public static WebArchive createDeployment() {
CoreDeployment deployment = new CoreDeployment(AjaxRenderingFailurePropagationTest.class);
// deployment.withWholeCore();
deployment.archive().addClasses(FailingPhaseListener.class, FailingComponent.class);
addIndexPage(deployment);
deployment.facesConfig(new Function<WebFacesConfigDescriptor, WebFacesConfigDescriptor>() {
public WebFacesConfigDescriptor apply(WebFacesConfigDescriptor facesConfig) {
facesConfig.getOrCreateLifecycle()
.phaseListener(FailingPhaseListener.class.getName());
return facesConfig;
}
});
return deployment.getFinalArchive();
}
@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
browser.get(contextPath.toExternalForm());
// button2.click();
Warp.initiate(new Activity() {
public void perform() {
button2.click();
}
}).inspect(new Inspection() {
private static final long serialVersionUID = 1L;
@ArquillianResource
private FacesContext context;
@ArquillianResource
private UIViewRoot root;
@ArquillianResource
private Application application;
@AfterPhase(RENDER_RESPONSE)
public void checkFailure() {
// assertEquals(1, context.getMessageList().size());
System.out.println(context.getExceptionHandler().getUnhandledExceptionQueuedEvents());
System.out.println(context.getExceptionHandler().getHandledExceptionQueuedEvent());
}
});
}
private static void addIndexPage(CoreDeployment deployment) {
FaceletAsset p = new FaceletAsset();
p.head("<h:outputScript name='jsf.js' library='javax.faces' />");
// p.head("<h:outputScript name='jquery.js' />");
// p.head("<h:outputScript name='richfaces.js' />");
p.head("<script> function errorHandler(error) { console.log(error); }; </script>");
p.form("<h:panelGroup id='panel'>");
p.form(" <h:commandButton id='button1' render='@form' onclick='RichFaces.ajax(this, event, {\"incId\": \"1\"}); return false;'>");
p.form(" </h:commandButton>");
p.form(" <h:commandButton id='button2'>");
p.form(" <f:ajax render='@form' onerror='errorHandler' />");
p.form(" </h:commandButton>");
p.form("</h:panelGroup>");
deployment.archive().addAsWebResource(p, "index.xhtml");
}
@FacesComponent(FailingComponent.COMPONENT_TYPE)
public static class FailingComponent extends UIOutput {
public static final String COMPONENT_TYPE = "org.richfaces.FailingComponent";
@Override
public void encodeAll(FacesContext context) throws IOException {
if (context.isPostback()) {
throw new IllegalStateException("rendering exception");
}
}
}
public static class FailingPhaseListener implements PhaseListener {
@Override
public void afterPhase(PhaseEvent event) {
FacesContext context = FacesContext.getCurrentInstance();
if (context.isPostback()) {
if (event.getPhaseId() == PhaseId.RESTORE_VIEW) {
UIViewRoot root = context.getViewRoot();
Application application = context.getApplication();
UIComponent form = root.findComponent("form");
UIComponent customComponent = application.createComponent(FailingComponent.COMPONENT_TYPE);
form.getChildren().add(customComponent);
}
}
}
@Override
public void beforePhase(PhaseEvent event) {
// TODO Auto-generated method stub
}
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment