NIHVIVO-1207 enhance stub classes for unit tests.

This commit is contained in:
jeb228 2010-11-04 14:50:38 +00:00
parent 78df1b181f
commit 631870cad2
4 changed files with 447 additions and 105 deletions

View file

@ -3,20 +3,36 @@
package stubs.javax.servlet.http;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A simple stand-in for the HttpSession, for use in unit tests.
*/
@SuppressWarnings("deprecation")
public class HttpSessionStub implements HttpSession {
private static final Log log = LogFactory.getLog(HttpSessionStub.class);
// ----------------------------------------------------------------------
// Stub infrastructure
// ----------------------------------------------------------------------
private String id = "arbitraryId";
private ServletContext context;
private final Map<String, Object> attributes = new HashMap<String, Object>();
@SuppressWarnings("unused")
private int maxInactiveInterval;
public void setId(String id) {
this.id = id;
}
public void setServletContext(ServletContext context) {
this.context = context;
@ -26,21 +42,52 @@ public class HttpSessionStub implements HttpSession {
// Stub methods
// ----------------------------------------------------------------------
@Override
public String getId() {
return this.id;
}
@Override
public ServletContext getServletContext() {
return this.context;
}
@Override
public void setAttribute(String name, Object value) {
if (name == null) {
throw new NullPointerException("name may not be null.");
}
if (value == null) {
removeAttribute(name);
}
attributes.put(name, value);
log.debug("setAttribute: " + name + "=" + value);
}
@Override
public void removeAttribute(String name) {
attributes.remove(name);
log.debug("removeAttribute: " + name);
}
@Override
public Object getAttribute(String name) {
return attributes.get(name);
}
/**
* So far, we don't do anything with this, or even confirm that it has been
* set. We just don't throw an exception if someone wants to set it.
*/
@Override
public void setMaxInactiveInterval(int seconds) {
this.maxInactiveInterval = seconds;
}
// ----------------------------------------------------------------------
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public Object getAttribute(String arg0) {
throw new RuntimeException(
"HttpSessionStub.getAttribute() not implemented.");
}
@Override
@SuppressWarnings("rawtypes")
public Enumeration getAttributeNames() {
@ -54,11 +101,6 @@ public class HttpSessionStub implements HttpSession {
"HttpSessionStub.getCreationTime() not implemented.");
}
@Override
public String getId() {
throw new RuntimeException("HttpSessionStub.getId() not implemented.");
}
@Override
public long getLastAccessedTime() {
throw new RuntimeException(
@ -106,27 +148,9 @@ public class HttpSessionStub implements HttpSession {
"HttpSessionStub.putValue() not implemented.");
}
@Override
public void removeAttribute(String arg0) {
throw new RuntimeException(
"HttpSessionStub.removeAttribute() not implemented.");
}
public void removeValue(String arg0) {
throw new RuntimeException(
"HttpSessionStub.removeValue() not implemented.");
}
@Override
public void setAttribute(String arg0, Object arg1) {
throw new RuntimeException(
"HttpSessionStub.setAttribute() not implemented.");
}
@Override
public void setMaxInactiveInterval(int arg0) {
throw new RuntimeException(
"HttpSessionStub.setMaxInactiveInterval() not implemented.");
}
}