Skip to content

Commit

Permalink
WELD-29 test case
Browse files Browse the repository at this point in the history
  • Loading branch information
drallen committed Jan 8, 2010
1 parent 8deab01 commit 98623e3
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 0 deletions.
@@ -0,0 +1,104 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc. and/or its affiliates, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.weld.tests.contexts.errorpage;

import java.util.HashSet;
import java.util.Set;

import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.Classes;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.testharness.impl.packaging.Resource;
import org.jboss.testharness.impl.packaging.Resources;
import org.jboss.testharness.impl.packaging.war.WarArtifactDescriptor;
import org.jboss.weld.test.AbstractWeldTest;
import org.testng.annotations.Test;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;

/**
* <p>This test was mostly developed to test the scenario related to WELD-29. Essentially
* a JSF action throws an exception, and the error page is then rendered during which
* all relevant scopes for CDI are tested.</p>
*
* @author David Allen
*
*/
@Artifact(addCurrentPackage=false)
@Classes({Storm.class})
@IntegrationTest(runLocally=true)
@Resources({
@Resource(destination=WarArtifactDescriptor.WEB_XML_DESTINATION, source="web.xml"),
@Resource(destination="storm.jspx", source="storm.jsf"),
@Resource(destination="error.jspx", source="error.jsf"),
@Resource(destination="/WEB-INF/faces-config.xml", source="faces-config.xml")
})
public class ErrorPageTest extends AbstractWeldTest
{
@Test(description = "WELD-29", groups = { "broken" })
public void testActionMethodExceptionDoesNotDestroyContext() throws Exception
{
WebClient client = new WebClient();
client.setThrowExceptionOnFailingStatusCode(false);

HtmlPage page = client.getPage(getPath("/storm.jsf"));
HtmlSubmitInput disasterButton = getFirstMatchingElement(page, HtmlSubmitInput.class, "disasterButton");
page = disasterButton.click();
assert "Application Error".equals(page.getTitleText());
HtmlDivision conversationValue = getFirstMatchingElement(page, HtmlDivision.class, "conversation");
assert conversationValue.asText().equals("0");
HtmlDivision requestValue = getFirstMatchingElement(page, HtmlDivision.class, "request");
assert requestValue.asText().equals("medium");
}

protected <T> Set<T> getElements(HtmlElement rootElement, Class<T> elementClass)
{
Set<T> result = new HashSet<T>();

for (HtmlElement element : rootElement.getAllHtmlChildElements())
{
result.addAll(getElements(element, elementClass));
}

if (elementClass.isInstance(rootElement))
{
result.add(elementClass.cast(rootElement));
}
return result;

}

protected <T extends HtmlElement> T getFirstMatchingElement(HtmlPage page, Class<T> elementClass, String id)
{

Set<T> inputs = getElements(page.getBody(), elementClass);
for (T input : inputs)
{
if (input.getId().contains(id))
{
return input;
}
}
return null;
}

}
@@ -0,0 +1,12 @@
package org.jboss.weld.tests.contexts.errorpage;

import javax.enterprise.context.RequestScoped;

@RequestScoped
public class Rain
{
public String getSeverityLevel()
{
return "medium";
}
}
@@ -0,0 +1,44 @@
package org.jboss.weld.tests.contexts.errorpage;

import java.io.Serializable;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@ConversationScoped
public class Storm implements Serializable
{

/**
*
*/
private static final long serialVersionUID = -1513633490356967202L;

@Inject Conversation conversation;

private String strength = "0";

public void beginConversation()
{
conversation.begin();
}

public void disaster()
{
throw new RuntimeException("Storm is a disaster");
}

public String getStrength()
{
return strength;
}

public void setStrength(String strength)
{
this.strength = strength;
}

}
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<PROCESS model-entity="JSFProcess"/>
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns="http://www.w3.org/1999/xhtml"
version="2.0">
<jsp:output doctype-root-element="html"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<jsp:directive.page contentType="text/html"/>
<html>
<head>
<title>Application Error</title>
</head>
<body>
<h1>Application Error</h1>
<p>The following error occurred while processing the request:</p>

<div id="request">${rain.serverityLevel}</div>
<div id="conversation">${storm.strength}</div>

</body>
</html>
</jsp:root>

@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">

</faces-config>
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns="http://www.w3.org/1999/xhtml"
version="2.0">
<jsp:output doctype-root-element="html"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3c.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<jsp:directive.page contentType="text/html"/>
<html>
<head>
</head>
<body>
<f:view>
<h:form id="form">
<h:inputText value="#{storm.strength}" id="stormStrength" />
<h:commandButton action="#{storm.beginConversation}" value="Begin" id="beginConversationButton"/>
<h:commandButton action="#{storm.disaster}" value="Disaster" id="disasterButton"/>
</h:form>
</f:view>
</body>
</html>
</jsp:root>
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>JSR-299 RI Core Tests</display-name>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jspx</param-value>
</context-param>
<!-- JSF -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Conversation Status Servlet</servlet-name>
<servlet-class>org.jboss.jsr299.tck.tests.context.conversation.client.ConversationStatusServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Conversation Status Servlet</servlet-name>
<url-pattern>/conversation-status</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jspx</location>
</error-page>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

0 comments on commit 98623e3

Please sign in to comment.