Getting "HttpSession is invalid" error while using JSFUnit
lakshmikr Apr 7, 2015 8:41 AMHi,
I am trying to use JSFUnit to test my ADF Faces pages (.jspx pages). Since my UI page is a protected page, I need to authenticate using username and password (weblogic credentials). After giving the credentials through JSFUnit API, looks like the HTTP session is invalidated. Since the HTTP session is invalidated, I am not able to test my UI pages after login (pasted the exception stack trace below). Can you please provide me the solution to resolve this issue.
I am using jboss-jsfunit-core-1.3.0.Final.jar, jboss-jsfunit-richfaces-1.3.0.Final.jar, htmlunit-2.8 and htmlunit-core-js-2.8.jar.
JSFUnitTest.java
package view.test;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.BrowserVersionFeatures;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import com.gargoylesoftware.htmlunit.util.Cookie;
import java.io.IOException;
import java.net.URL;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.jboss.jsfunit.context.JSFUnitFacesContext;
import org.jboss.jsfunit.framework.WebClientSpec;
import org.jboss.jsfunit.framework.WebConversationFactory;
import org.jboss.jsfunit.jsfsession.JSFClientSession;
import org.jboss.jsfunit.jsfsession.JSFServerSession;
import org.jboss.jsfunit.jsfsession.JSFSession;
public class JSFUnitTest
extends org.apache.cactus.ServletTestCase
{
public JSFUnitTest() {
super();
}
public static Test suite()
{
return new TestSuite(JSFUnitTest.class);
}
public void testInput()
throws IOException
{
String applicationName = "Netscape";
String applicationVersion = "5.0 (Windows)";
String userAgent =
"Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0";
int browserVersionNumeric = 17;
BrowserVersion browser =
new BrowserVersion(applicationName, applicationVersion, userAgent,
browserVersionNumeric)
{
public boolean hasFeature(BrowserVersionFeatures property)
{
// change features here
return BrowserVersion.FIREFOX_3_6.hasFeature(property);
}
};
WebClientSpec webSpec = new WebClientSpec("/faces/pageTest.jspx", browser);
WebClient webClient = webSpec.getWebClient();
TestInitialRequestStrategy testStrategy = new TestInitialRequestStrategy();
webSpec.setInitialRequestStrategy(testStrategy);
JSFSession jsfSession1 = new JSFSession(webSpec);
// A JSFServerSession gives you access to JSF state
JSFServerSession server = jsfSession1.getJSFServerSession();
JSFClientSession client = jsfSession1.getJSFClientSession();
System.out.println(server.getFacesContext());
System.out.println(server.getCurrentViewID());
}
}
TestInitialRequestStrategy.java
package view.test;
import com.gargoylesoftware.htmlunit.MockWebConnection;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebRequestSettings;
import com.gargoylesoftware.htmlunit.WebResponse;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.jboss.jsfunit.context.JSFUnitFacesContext;
import org.jboss.jsfunit.framework.JSFUnitWebConnection;
import org.jboss.jsfunit.framework.RequestListener;
import org.jboss.jsfunit.framework.SimpleInitialRequestStrategy;
import org.jboss.jsfunit.framework.WebClientSpec;
import org.jboss.jsfunit.framework.WebConversationFactory;
public class TestInitialRequestStrategy
extends SimpleInitialRequestStrategy
{
@Override
public Page doInitialRequest(WebClientSpec wcSpec)
throws IOException
{
WebClient webClient = wcSpec.getWebClient();
webClient.setActiveXNative(true);
System.out.println(WebConversationFactory.getWARURL());
URL url =
new URL(WebConversationFactory.getWARURL() + "/faces/pageTest.jspx");
webClient.setJavaScriptEnabled(true);
HtmlPage page = (HtmlPage) webClient.getPage(url);
HtmlForm form1 = (HtmlForm) page.getForms().get(0);
((HtmlInput) page.getElementByName("j_username")).setValueAttribute("weblogic");
((HtmlInput) page.getElementByName("j_password")).setValueAttribute("welcome1");
HtmlPage newPage = (HtmlPage) page.getElementByName("submit").click();
return super.doInitialRequest(wcSpec);
}
}
Before Login:
HTTPSession Object: weblogic.servlet.internal.session.MemorySessionData@ebb616
HTTPSession ID: G4TYVv3JvdF0F2WB93qv8T22pWbLdTT7m308ljdHTrkrtpSX2fPJ!-2118563431!1428338569820
JSESSIONID : null ( webClient.getCookieManager().getCookie("JSESSIONID"))
JSESSIONID wcSpec: G4TYVv3JvdF0F2WB93qv8T22pWbLdTT7m308ljdHTrkrtpSX2fPJ!-2118563431!1428338569820 (wcSpec.getCookies().get("JSESSIONID"))
hashcode 15447574
After Login:
HttpSession is invalid: G4TYVv3JvdF0F2WB93qv8T22pWbLdTT7m308ljdHTrkrtpSX2fPJ!-2118563431!1428338569820
HTTPSession ID G4TYVv3JvdF0F2WB93qv8T22pWbLdTT7m308ljdHTrkrtpSX2fPJ!-2118563431!1428338569820
JSESSIONID : JSESSIONID=pnv5Vv3Nzh0wWCWT3Qhn9C4910XsP1ZK4CRzzLbQvSk1VdMXCgFB!-2118563431;domain=slc05kol.us.oracle.com;path=/ ( webClient.getCookieManager().getCookie("JSESSIONID"))
JSESSIONID wcSpec: G4TYVv3JvdF0F2WB93qv8T22pWbLdTT7m308ljdHTrkrtpSX2fPJ!-2118563431!1428338569820 (wcSpec.getCookies().get("JSESSIONID"))
hashcode 15447574
HttpSession is invalid
java.lang.IllegalStateException: HttpSession is invalid
at weblogic.servlet.internal.session.SessionData.check(SessionData.java:479)
at weblogic.servlet.internal.session.SessionData.getAttribute(SessionData.java:392)
at org.jboss.jsfunit.framework.FacesContextBridge.getCurrentInstance(FacesContextBridge.java:56)
at org.jboss.jsfunit.jsfsession.JSFServerSession.pageCreated(JSFServerSession.java:214)
at org.jboss.jsfunit.jsfsession.JSFServerSession.<init>(JSFServerSession.java:57)
at org.jboss.jsfunit.jsfsession.JSFSession.<init>(JSFSession.java:83)
at view.test.JSFUnitTest.testInput(JSFUnitTest.java:132)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.apache.cactus.internal.AbstractCactusTestCase.runBareServer(AbstractCactusTestCase.java:153)
at org.apache.cactus.internal.server.AbstractWebTestCaller.doTest(AbstractWebTestCaller.java:119)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody0(AbstractWebTestController.java:93)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest_aroundBody1$advice(AbstractWebTestController.java:224)
at org.apache.cactus.internal.server.AbstractWebTestController.handleRequest(AbstractWebTestController.java)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody2(ServletTestRedirector.java:101)
at org.apache.cactus.server.ServletTestRedirector.doPost_aroundBody3$advice(ServletTestRedirector.java:224)
at org.apache.cactus.server.ServletTestRedirector.doPost(ServletTestRedirector.java)
at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doPost(JSFUnitServletRedirector.java:46)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody0(ServletTestRedirector.java:72)
at org.apache.cactus.server.ServletTestRedirector.doGet_aroundBody1$advice(ServletTestRedirector.java:224)
at org.apache.cactus.server.ServletTestRedirector.doGet(ServletTestRedirector.java)
at org.jboss.jsfunit.framework.JSFUnitServletRedirector.doGet(JSFUnitServletRedirector.java:52)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at org.jboss.jsfunit.framework.JSFUnitFilter.doFilter(JSFUnitFilter.java:119)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.adf.library.webapp.LibraryFilter.doFilter(LibraryFilter.java:180)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.security.jps.ee.http.JpsAbsFilter$1.run(JpsAbsFilter.java:138)
at java.security.AccessController.doPrivileged(Native Method)
at oracle.security.jps.util.JpsSubject.doAsPrivileged(JpsSubject.java:324)
at oracle.security.jps.ee.util.JpsPlatformUtil.runJaasMode(JpsPlatformUtil.java:464)
at oracle.security.jps.ee.http.JpsAbsFilter.runJaasMode(JpsAbsFilter.java:121)
at oracle.security.jps.ee.http.JpsAbsFilter.doFilter(JpsAbsFilter.java:211)
at oracle.security.jps.ee.http.JpsFilter.doFilter(JpsFilter.java:71)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at oracle.dms.servlet.DMSServletFilter.doFilter(DMSServletFilter.java:163)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3730)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)