Author: alexsmirnov
Date: 2008-11-10 20:39:29 -0500 (Mon, 10 Nov 2008)
New Revision: 11071
Added:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebClient.java
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
Modified:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalServer.java
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/ServerConnection.java
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubHttpSession.java
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubServletContext.java
branches/jsf2.0/framework/jsf-test/src/test/java/org/richfaces/test/FacesServerTest.java
Log:
Session and context attribute listeners implemented
Modified:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalServer.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalServer.java 2008-11-10
17:06:53 UTC (rev 11070)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalServer.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -1,20 +1,24 @@
package org.richfaces.test;
-import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
+import java.util.EventListener;
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
+import javax.servlet.ServletContextAttributeEvent;
+import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionAttributeListener;
+import javax.servlet.http.HttpSessionBindingEvent;
+import javax.servlet.http.HttpSessionEvent;
+import javax.servlet.http.HttpSessionListener;
import org.richfaces.test.stub.ClasspathServerResource;
import org.richfaces.test.stub.ServerResource;
@@ -23,17 +27,9 @@
import org.richfaces.test.stub.ServletContainer;
import org.richfaces.test.stub.StaticServlet;
import org.richfaces.test.stub.StubHttpSession;
-import org.richfaces.test.stub.StubServletConfig;
import org.richfaces.test.stub.StubServletContext;
-import com.gargoylesoftware.htmlunit.WebConnection;
-import com.gargoylesoftware.htmlunit.WebRequestSettings;
-import com.gargoylesoftware.htmlunit.WebResponse;
-import com.gargoylesoftware.htmlunit.WebResponseData;
-import ch.ethz.ssh2.Session;
-
-
/**
* Hello world!
*
@@ -45,12 +41,59 @@
private ServletContainer defaultServlet;
- private List<ServletContextListener> contextListeners = new
ArrayList<ServletContextListener>();
+ private List<EventListener> contextListeners = new
ArrayList<EventListener>();
private Map<String,String> initParameters=new HashMap<String, String>();
private ServerResource serverRoot = new ServerResourcesDirectory();
+ private class LocalContext extends StubServletContext{
+
+ @Override
+ protected void valueBound(ServletContextAttributeEvent event) {
+ // inform listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof ServletContextAttributeListener) {
+ ServletContextAttributeListener contextListener = (ServletContextAttributeListener)
listener;
+ contextListener.attributeAdded(event);
+ }
+ }
+ }
+
+ @Override
+ protected void valueReplaced(ServletContextAttributeEvent event) {
+ // inform listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof ServletContextAttributeListener) {
+ ServletContextAttributeListener contextListener = (ServletContextAttributeListener)
listener;
+ contextListener.attributeReplaced(event);
+ }
+ }
+ }
+
+ @Override
+ protected void valueUnbound(
+ ServletContextAttributeEvent event) {
+ // inform listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof ServletContextAttributeListener) {
+ ServletContextAttributeListener contextListener = (ServletContextAttributeListener)
listener;
+ contextListener.attributeRemoved(event);
+ }
+ }
+ }
+
+ @Override
+ /**
+ * @param path
+ * @return
+ */
+ protected ServerResource getServerResource(String path) {
+ return serverRoot.getResource(new ServerResourcePath(path));
+ }
+
+ }
+
private StubServletContext context;
private class ServerHttpSession extends StubHttpSession {
@@ -58,6 +101,28 @@
public ServletContext getServletContext() {
return context;
}
+
+ @Override
+ protected void valueBound(HttpSessionBindingEvent sessionBindingEvent) {
+ // inform session listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof HttpSessionAttributeListener) {
+ HttpSessionAttributeListener contextListener = (HttpSessionAttributeListener)
listener;
+ contextListener.attributeAdded(sessionBindingEvent);
+ }
+ }
+ }
+
+ @Override
+ protected void valueUnbound(HttpSessionBindingEvent sessionBindingEvent) {
+ // inform session listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof HttpSessionAttributeListener) {
+ HttpSessionAttributeListener contextListener = (HttpSessionAttributeListener)
listener;
+ contextListener.attributeRemoved(sessionBindingEvent);
+ }
+ }
+ }
}
@@ -87,7 +152,7 @@
serverRoot.addResource(new ServerResourcePath(path), new
ClasspathServerResource(resource));
}
- public void addContextListener(ServletContextListener listener) {
+ public void addWebListener(EventListener listener) {
contextListeners.add(listener);
}
@@ -98,19 +163,29 @@
public HttpSession getSession(boolean create){
if(null == this.session && create){
this.session = new ServerHttpSession();
- // TODO - inform session listeners
+ // inform session listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof HttpSessionListener) {
+ HttpSessionListener contextListener = (HttpSessionListener) listener;
+ contextListener.sessionCreated(new HttpSessionEvent(session));
+ }
+ }
}
return session;
}
public void init() throws ServletException {
// Create context.
- this.context = new StubServletContext(serverRoot);
+ this.context = new LocalContext();
// Create init parameters
context.addInitParameters(initParameters);
// Inform listeners
- for (ServletContextListener listener : contextListeners) {
- listener.contextInitialized(new ServletContextEvent(context));
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof ServletContextListener) {
+ ServletContextListener contextListener = (ServletContextListener) listener;
+ contextListener.contextInitialized(new ServletContextEvent(context));
+
+ }
}
// Init servlets
for (ServletContainer servlet : servlets) {
@@ -125,12 +200,22 @@
public void destroy(){
// Destroy session
if (null != this.session) {
- // TODO - inform session listeners.
- session.destroy();
+ // inform session listeners.
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof HttpSessionListener) {
+ HttpSessionListener contextListener = (HttpSessionListener) listener;
+ contextListener.sessionDestroyed(new HttpSessionEvent(session));
+ }
+ }
+ session.destroy();
}
// Inform listeners
- for (ServletContextListener listener : contextListeners) {
- listener.contextDestroyed(new ServletContextEvent(context));
+ for (EventListener listener : contextListeners) {
+ if (listener instanceof ServletContextListener) {
+ ServletContextListener contextListener = (ServletContextListener) listener;
+ contextListener.contextDestroyed(new ServletContextEvent(context));
+
+ }
}
// Destroy servlets
for (ServletContainer servlet : servlets) {
@@ -146,22 +231,4 @@
public StubServletContext getContext() {
return context;
}
-
-
- public WebConnection getWebConnection(){
- return new WebConnection(){
-
- public WebResponse getResponse(WebRequestSettings settings)
- throws IOException {
- ServerConnection connection = getConnection(settings.getUrl());
- try {
- connection.execute();
- } catch (ServletException e) {
- throw new IOException(e.getMessage());
- }
- return connection.getWebResponse(settings);
- }
-
- };
- }
}
Added:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebClient.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebClient.java
(rev 0)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebClient.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -0,0 +1,55 @@
+/**
+ *
+ */
+package org.richfaces.test;
+
+import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.WebClient;
+import com.gargoylesoftware.htmlunit.WebConnection;
+
+/**
+ * @author asmirnov
+ *
+ */
+@SuppressWarnings("serial")
+public class LocalWebClient extends WebClient {
+
+ private final LocalServer server;
+
+ private transient WebConnection webConnection;
+
+ /**
+ *
+ */
+ public LocalWebClient(LocalServer server) {
+ super();
+ this.server = server;
+ }
+
+ /**
+ * @param browserVersion
+ */
+ public LocalWebClient(LocalServer server,BrowserVersion browserVersion) {
+ super(browserVersion);
+ this.server = server;
+ }
+
+ /**
+ * @return the webConnection
+ */
+ public WebConnection getWebConnection() {
+ if (this.webConnection == null) {
+ this.webConnection = new LocalWebConnection(server);
+ }
+
+ return this.webConnection;
+ }
+
+ /**
+ * @param webConnection the webConnection to set
+ */
+ public void setWebConnection(WebConnection webConnection) {
+ this.webConnection = webConnection;
+ }
+
+}
Property changes on:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebClient.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
(rev 0)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -0,0 +1,34 @@
+/**
+ *
+ */
+package org.richfaces.test;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import com.gargoylesoftware.htmlunit.WebConnection;
+import com.gargoylesoftware.htmlunit.WebRequestSettings;
+import com.gargoylesoftware.htmlunit.WebResponse;
+
+public final class LocalWebConnection implements WebConnection {
+ private final LocalServer localServer;
+
+ /**
+ * @param localServer
+ */
+ public LocalWebConnection(LocalServer localServer) {
+ this.localServer = localServer;
+ }
+
+ public WebResponse getResponse(WebRequestSettings settings)
+ throws IOException {
+ ServerConnection connection = localServer.getConnection(settings.getUrl());
+ try {
+ connection.execute();
+ } catch (ServletException e) {
+ throw new IOException(e.getMessage());
+ }
+ return new LocalWebResponse(settings,connection);
+ }
+}
\ No newline at end of file
Property changes on:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebConnection.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
(rev 0)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -0,0 +1,81 @@
+/**
+ *
+ */
+package org.richfaces.test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.commons.httpclient.NameValuePair;
+
+import com.gargoylesoftware.htmlunit.WebRequestSettings;
+import com.gargoylesoftware.htmlunit.WebResponse;
+
+public final class LocalWebResponse implements WebResponse {
+ private final WebRequestSettings settings;
+ private final ServerConnection serverConnection;
+
+ public LocalWebResponse(WebRequestSettings settings,ServerConnection serverConnection)
{
+ this.settings = settings;
+ this.serverConnection = serverConnection;
+ }
+
+ public InputStream getContentAsStream() throws IOException {
+ return new ByteArrayInputStream(getResponseBody());
+ }
+
+ public String getContentAsString() {
+ return serverConnection.getContentAsString();
+ }
+
+ public String getContentCharSet() {
+ return serverConnection.getCharacterEncoding();
+ }
+
+ public String getContentType() {
+ return serverConnection.getContentType();
+ }
+
+ public long getLoadTimeInMilliSeconds() {
+ return 0;
+ }
+
+ public com.gargoylesoftware.htmlunit.HttpMethod getRequestMethod() {
+ return
com.gargoylesoftware.htmlunit.HttpMethod.valueOf(serverConnection.getMethod().toString());
+ }
+
+ public WebRequestSettings getRequestSettings() {
+ return settings;
+ }
+
+ public byte[] getResponseBody() {
+ return serverConnection.getResponseBody();
+ }
+
+ public String getResponseHeaderValue(String headerName) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public int getStatusCode() {
+ return serverConnection.getStatus();
+ }
+
+ public String getStatusMessage() {
+ return serverConnection.getErrorMessage();
+ }
+
+ public URL getUrl() {
+ return serverConnection.getUrl();
+ }
+
+ public List<NameValuePair> getResponseHeaders() {
+ // TODO Auto-generated method stub
+ return Collections.emptyList();
+ }
+}
\ No newline at end of file
Property changes on:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/LocalWebResponse.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/ServerConnection.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/ServerConnection.java 2008-11-10
17:06:53 UTC (rev 11070)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/ServerConnection.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -3,9 +3,7 @@
*/
package org.richfaces.test;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
@@ -18,9 +16,9 @@
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
-import org.apache.commons.httpclient.NameValuePair;
import org.richfaces.test.stub.ServletContainer;
import org.richfaces.test.stub.StubHttpRequest;
import org.richfaces.test.stub.StubHttpResponse;
@@ -31,56 +29,82 @@
/**
* This class represent single connection to the server.
+ *
* @author asmirnov
- *
+ *
*/
public class ServerConnection {
-
-
-
+
private final LocalServer server;
-
- private final URL url;
+ final URL url;
+
private ConnectionRequest request;
-
- private ConnectionResponse response;
+ ConnectionResponse response;
+
private final ServletContainer servlet;
-
- private HttpMethod method= HttpMethod.GET;
-
- private static final Cookie[] COOKIE = new Cookie[]{};
+ private HttpMethod method = HttpMethod.GET;
+
+ private static final Cookie[] COOKIE = new Cookie[] {};
+
private List<Cookie> cookies = new ArrayList<Cookie>();
-
+
private Map<String, String[]> requestParameters = new HashMap<String,
String[]>();
-
private final String pathInfo;
private final String servletPath;
+ private boolean finished = false;
public ServerConnection(LocalServer localServer, URL url) {
this.server = localServer;
this.url = url;
String path = url.getPath();
servlet = localServer.getServlet(path);
- if(null == servlet){
+ if (null == servlet) {
throw new IllegalArgumentException();
}
this.pathInfo = servlet.getPathInfo(path);
this.servletPath = servlet.getServletPath(path);
this.request = new ConnectionRequest();
this.response = new ConnectionResponse();
- this.request.setAttribute("javax.servlet.include.path_info", this.pathInfo);
- this.request.setAttribute("javax.servlet.include.servlet_path",
this.servletPath);
+ this.request.setAttribute("javax.servlet.include.path_info",
+ this.pathInfo);
+ this.request.setAttribute("javax.servlet.include.servlet_path",
+ this.servletPath);
}
-
-
+
+ /**
+ * @return the finished
+ */
+ public boolean isFinished() {
+ return finished;
+ }
+
+ /**
+ * @param finished
+ * the finished to set
+ */
+ public void setFinished(boolean finished) {
+ this.finished = finished;
+ }
+
+ private void checkFinished() {
+ if (!isFinished()) {
+ throw new IllegalStateException("request have not been executed");
+ }
+ }
+
public void execute() throws ServletException, IOException {
- this.servlet.execute(request,response);
+ if (isFinished()) {
+ throw new IllegalStateException(
+ "request already have been executed");
+ }
+ this.servlet.execute(request, response);
+ setFinished(true);
}
/**
@@ -91,34 +115,43 @@
}
/**
- * @param method the method to set
+ * @param method
+ * the method to set
*/
public void setMethod(HttpMethod method) {
this.method = method;
}
-
+
+ /**
+ * @return the url
+ */
+ public URL getUrl() {
+ return url;
+ }
+
public void addRequestParameter(String name, String value) {
String[] values = requestParameters.get(name);
- if(null == values){
+ if (null == values) {
values = new String[1];
} else {
- String[] newValues= new String[values.length+1];
+ String[] newValues = new String[values.length + 1];
System.arraycopy(values, 0, newValues, 0, values.length);
- values=newValues;
+ values = newValues;
}
- values[values.length-1]= value;
+ values[values.length - 1] = value;
requestParameters.put(name, values);
}
- public String getRenderedContent() {
+ public String getContentAsString() {
+ checkFinished();
String content = response.getWriterContent();
- if(null == content){
+ if (null == content) {
byte[] streamContent = response.getStreamContent();
- if(null!= streamContent){
+ if (null != streamContent) {
String encoding = response.getCharacterEncoding();
- if(null != encoding){
+ if (null != encoding) {
try {
- content = new String(streamContent,encoding);
+ content = new String(streamContent, encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
}
@@ -130,6 +163,24 @@
return content;
}
+ public byte[] getResponseBody() {
+ checkFinished();
+ byte[] content = response.getStreamContent();
+ if (null == content) {
+ String writerContent = response.getWriterContent();
+ if (null != writerContent) {
+ try {
+ content = writerContent.getBytes(response
+ .getCharacterEncoding());
+ } catch (UnsupportedEncodingException e) {
+ content = writerContent.getBytes();
+ }
+ } else {
+ content = new byte[0];
+ }
+ }
+ return content;
+ }
/**
* @return the cookies
@@ -137,28 +188,27 @@
public List<Cookie> getCookies() {
return cookies;
}
-
+
/**
* @return the request
*/
- public ConnectionRequest getRequest() {
+ public StubHttpRequest getRequest() {
return request;
}
/**
* @return the response
*/
- public ConnectionResponse getResponse() {
+ public StubHttpResponse getResponse() {
return response;
}
private class ConnectionRequest extends StubHttpRequest {
-
public Cookie[] getCookies() {
- // TODO Auto-generated method stub
return cookies.toArray(COOKIE);
}
+
/*
* (non-Javadoc)
*
@@ -203,7 +253,7 @@
public String getRequestURI() {
return url.getPath();
}
-
+
/*
* (non-Javadoc)
*
@@ -211,9 +261,9 @@
*/
public String getParameter(String name) {
String[] values = requestParameters.get(name);
- if(null != values && values.length>0){
+ if (null != values && values.length > 0) {
return values[0];
- }
+ }
return null;
}
@@ -240,37 +290,40 @@
/*
* (non-Javadoc)
*
- * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
+ * @see
+ * javax.servlet.ServletRequest#getParameterValues(java.lang.String)
*/
public String[] getParameterValues(String name) {
return requestParameters.get(name);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see javax.servlet.http.HttpServletRequest#getSession()
*/
public HttpSession getSession() {
return server.getSession();
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see javax.servlet.http.HttpServletRequest#getSession(boolean)
*/
public HttpSession getSession(boolean create) {
return server.getSession(create);
}
-
-
}
-
+
private class ConnectionResponse extends StubHttpResponse {
/*
* (non-Javadoc)
*
* @see
- * javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie
- * )
+ * javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http
+ * .Cookie )
*/
public void addCookie(Cookie cookie) {
cookies.add(cookie);
@@ -279,78 +332,24 @@
}
- public WebResponse getWebResponse(final WebRequestSettings settings) {
- // TODO Auto-generated method stub
- return new WebResponse(){
+ public String getCharacterEncoding() {
+ checkFinished();
+ return response.getCharacterEncoding();
+ }
- public InputStream getContentAsStream() throws IOException {
- return new ByteArrayInputStream(getResponseBody());
- }
+ public String getContentType() {
+ checkFinished();
+ return response.getContentType();
+ }
- public String getContentAsString() {
- // TODO Auto-generated method stub
- return response.getWriterContent();
- }
+ public int getStatus() {
+ checkFinished();
+ return response.getStatus();
+ }
- public String getContentCharSet() {
- return response.getCharacterEncoding();
- }
-
- public String getContentType() {
- return response.getContentType();
- }
-
- public long getLoadTimeInMilliSeconds() {
- return 0;
- }
-
- public com.gargoylesoftware.htmlunit.HttpMethod getRequestMethod() {
- return com.gargoylesoftware.htmlunit.HttpMethod.valueOf(getMethod().toString());
- }
-
- public WebRequestSettings getRequestSettings() {
- return settings;
- }
-
- public byte[] getResponseBody() {
- byte[] content = response.getStreamContent();
- if(null == content){
- String writerContent = response.getWriterContent();
- if(null != writerContent){
- try {
- content = writerContent.getBytes(response.getCharacterEncoding());
- } catch (UnsupportedEncodingException e) {
- content = writerContent.getBytes();
- }
- } else {
- content = new byte[0];
- }
- }
- return content;
- }
-
- public String getResponseHeaderValue(String headerName) {
- // TODO Auto-generated method stub
- return null;
- }
-
- public int getStatusCode() {
- return response.getStatus();
- }
-
- public String getStatusMessage() {
- return response.getErrorMessage();
- }
-
- public URL getUrl() {
- return url;
- }
-
- public List<NameValuePair> getResponseHeaders() {
- // TODO Auto-generated method stub
- return Collections.emptyList();
- }
-
- };
+ public String getErrorMessage() {
+ checkFinished();
+ return response.getErrorMessage();
}
+
}
Modified:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubHttpSession.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubHttpSession.java 2008-11-10
17:06:53 UTC (rev 11070)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubHttpSession.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -12,6 +12,8 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionBindingEvent;
+import javax.servlet.http.HttpSessionBindingListener;
import javax.servlet.http.HttpSessionContext;
/**
@@ -39,7 +41,11 @@
/**
*
*/
- public void destroy(){
+ public void destroy(){
+ // Destroy all session attributes.
+ for (String name : getValueNames()) {
+ removeAttribute(name);
+ }
}
/* (non-Javadoc)
@@ -92,8 +98,7 @@
*/
@SuppressWarnings("deprecation")
public HttpSessionContext getSessionContext() {
- // TODO Auto-generated method stub
- return null;
+ throw new NotImplementedException("Session context is not implemented");
}
/* (non-Javadoc)
@@ -140,10 +145,37 @@
*/
public void removeAttribute(String name) {
// TODO - inform listeners
- this.attributes.remove(name);
+ Object removed = this.attributes.remove(name);
+ if(null != removed){
+ removedFromSession(name,removed);
+ }
}
+ protected void removedFromSession(String name, Object removed) {
+ HttpSessionBindingEvent sessionBindingEvent = new
HttpSessionBindingEvent(this,name,removed);
+ if (removed instanceof HttpSessionBindingListener) {
+ HttpSessionBindingListener listener = (HttpSessionBindingListener) removed;
+ listener.valueUnbound(sessionBindingEvent);
+ }
+ valueUnbound(sessionBindingEvent);
+ }
+
+
+ protected abstract void valueUnbound(HttpSessionBindingEvent sessionBindingEvent);
+
+ protected void boundToSession(String name, Object value) {
+ HttpSessionBindingEvent sessionBindingEvent = new
HttpSessionBindingEvent(this,name,value);
+ if (value instanceof HttpSessionBindingListener) {
+ HttpSessionBindingListener listener = (HttpSessionBindingListener) value;
+ listener.valueBound(sessionBindingEvent);
+ }
+ valueBound(sessionBindingEvent);
+ }
+
+
+ protected abstract void valueBound(HttpSessionBindingEvent sessionBindingEvent);
+
/* (non-Javadoc)
* @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
*/
@@ -156,7 +188,13 @@
*/
public void setAttribute(String name, Object value) {
// TODO - inform listeners
- attributes.put(name, value);
+ Object oldValue = attributes.put(name, value);
+ if (null != oldValue) {
+ removedFromSession(name, oldValue);
+ }
+ if(null != value){
+ boundToSession(name, value);
+ }
}
/* (non-Javadoc)
Modified:
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubServletContext.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubServletContext.java 2008-11-10
17:06:53 UTC (rev 11070)
+++
branches/jsf2.0/framework/jsf-test/src/main/java/org/richfaces/test/stub/StubServletContext.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -19,6 +19,8 @@
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
+import javax.servlet.ServletContextAttributeEvent;
+import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletException;
import org.richfaces.test.ServerLogger;
@@ -27,7 +29,7 @@
* @author asmirnov
*
*/
-public class StubServletContext implements ServletContext {
+public abstract class StubServletContext implements ServletContext {
private static final Logger log = ServerLogger.SERVER.getLogger();
@@ -35,14 +37,8 @@
private static final String APPLICATION_NAME = "stub";
private final Map<String, Object> attributes = new HashMap<String,
Object>();
- private final ServerResource serverRoot;
-
private Map<String,String> initParameters = new HashMap<String, String>();
- public StubServletContext(ServerResource serverRoot) {
- this.serverRoot = serverRoot;
- }
-
/* (non-Javadoc)
* @see javax.servlet.ServletContext#getAttribute(java.lang.String)
*/
@@ -167,9 +163,7 @@
* @param path
* @return
*/
- protected ServerResource getServerResource(String path) {
- return serverRoot.getResource(new ServerResourcePath(path));
- }
+ protected abstract ServerResource getServerResource(String path);
/* (non-Javadoc)
* @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
@@ -268,15 +262,36 @@
*/
public void removeAttribute(String name) {
// TODO - inform listeners
- attributes.remove(name);
+ Object removed = attributes.remove(name);
+ if(null != removed){
+ valueUnbound(new ServletContextAttributeEvent(this,name,removed));
+ }
}
+
/* (non-Javadoc)
* @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
*/
public void setAttribute(String name, Object object) {
// TODO - inform listeners
- attributes.put(name, object);
+ if (null == object) {
+ removeAttribute(name);
+ } else {
+ Object oldValue = attributes.put(name, object);
+ ServletContextAttributeEvent event = new
ServletContextAttributeEvent(this,name,object);
+ if(null != oldValue){
+ valueReplaced(event);
+ } else {
+ valueBound(event);
+ }
+ }
}
+ protected abstract void valueBound(ServletContextAttributeEvent event);
+
+ protected abstract void valueReplaced(ServletContextAttributeEvent event);
+
+ protected abstract void valueUnbound(
+ ServletContextAttributeEvent servletContextAttributeEvent);
+
}
Modified:
branches/jsf2.0/framework/jsf-test/src/test/java/org/richfaces/test/FacesServerTest.java
===================================================================
---
branches/jsf2.0/framework/jsf-test/src/test/java/org/richfaces/test/FacesServerTest.java 2008-11-10
17:06:53 UTC (rev 11070)
+++
branches/jsf2.0/framework/jsf-test/src/test/java/org/richfaces/test/FacesServerTest.java 2008-11-11
01:39:29 UTC (rev 11071)
@@ -66,7 +66,7 @@
facesServer.addInitParameter(ViewHandler.DEFAULT_SUFFIX_PARAM_NAME,
".xhtml");
facesServer.addInitParameter("com.sun.faces.validateXml", "true");
facesServer.addInitParameter("com.sun.faces.verifyObjects",
"true");
- facesServer.addContextListener(new ConfigureListener());
+ facesServer.addWebListener(new ConfigureListener());
facesServer.init();
}
@@ -101,8 +101,7 @@
*/
@Test
public void testGetConnection() throws Exception {
- WebClient webClient = new WebClient();
- webClient.setWebConnection(facesServer.getWebConnection());
+ WebClient webClient = new LocalWebClient(facesServer);
HtmlPage page = webClient.getPage("http://localhost/hello.jsf");
System.out.println(page.asXml());
}