NIHVIVO-1229 Create the RevisionInfoBean, with setup listener and unit tests.
This commit is contained in:
parent
bc86f50c5b
commit
b438f7d36d
6 changed files with 860 additions and 11 deletions
|
@ -0,0 +1,140 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.config;
|
||||
|
||||
import static edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean.DUMMY_BEAN;
|
||||
import static edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean.LevelRevisionInfo.DUMMY_LEVEL;
|
||||
import static junit.framework.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import stubs.javax.servlet.ServletContextStub;
|
||||
import stubs.javax.servlet.http.HttpSessionStub;
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean.LevelRevisionInfo;
|
||||
|
||||
/**
|
||||
* Tests for RevisionInfoBean
|
||||
*/
|
||||
public class RevisionInfoBeanTest extends AbstractTestClass {
|
||||
private static final Date SAMPLE_DATE = new Date();
|
||||
|
||||
private static final LevelRevisionInfo LEVEL_1_INFO = new LevelRevisionInfo(
|
||||
"level1name", "level1release", "level1revision");
|
||||
private static final LevelRevisionInfo LEVEL_2_INFO = new LevelRevisionInfo(
|
||||
"level2name", "level2release", "level2revision");
|
||||
private static final LevelRevisionInfo LEVEL_3_INFO = new LevelRevisionInfo(
|
||||
"level3name", "level3release", "level3revision");
|
||||
|
||||
private static final RevisionInfoBean BEAN_NO_LEVEL = buildBean(SAMPLE_DATE);
|
||||
private static final RevisionInfoBean BEAN_1_LEVEL = buildBean(SAMPLE_DATE,
|
||||
LEVEL_1_INFO);
|
||||
private static final RevisionInfoBean BEAN_MULTI_LEVEL = buildBean(
|
||||
SAMPLE_DATE, LEVEL_1_INFO, LEVEL_2_INFO, LEVEL_3_INFO);
|
||||
|
||||
private static RevisionInfoBean buildBean(Date date,
|
||||
LevelRevisionInfo... levels) {
|
||||
return new RevisionInfoBean(date, Arrays.asList(levels));
|
||||
}
|
||||
|
||||
private ServletContextStub context;
|
||||
private HttpSessionStub session;
|
||||
|
||||
@Before
|
||||
public void setupContext() {
|
||||
context = new ServletContextStub();
|
||||
session = new HttpSessionStub();
|
||||
session.setServletContext(context);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void suppressInfoMessages() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.WARN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanNormal() {
|
||||
RevisionInfoBean.setBean(context, BEAN_1_LEVEL);
|
||||
assertEquals("stored bean", BEAN_1_LEVEL,
|
||||
RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setBeanNull() {
|
||||
RevisionInfoBean.setBean(context, null);
|
||||
assertEquals("dummy bean", DUMMY_BEAN,
|
||||
RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanNoSession() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.ERROR);
|
||||
|
||||
assertEquals("noBean", DUMMY_BEAN, RevisionInfoBean.getBean(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanNoAttribute() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.ERROR);
|
||||
|
||||
assertEquals("noAttribute", DUMMY_BEAN,
|
||||
RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBeanAttributeIsWrongClass() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.OFF);
|
||||
|
||||
context.setAttribute(RevisionInfoBean.ATTRIBUTE_NAME, "A string!");
|
||||
assertEquals("noAttribute", DUMMY_BEAN,
|
||||
RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeBean() {
|
||||
RevisionInfoBean.setBean(context, BEAN_1_LEVEL);
|
||||
assertEquals("stored bean", BEAN_1_LEVEL,
|
||||
RevisionInfoBean.getBean(session));
|
||||
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.ERROR);
|
||||
|
||||
RevisionInfoBean.removeBean(context);
|
||||
assertEquals("dummy bean", DUMMY_BEAN,
|
||||
RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReleaseLabelOneLevel() {
|
||||
RevisionInfoBean.setBean(context, BEAN_1_LEVEL);
|
||||
assertEquals("1 level release", LEVEL_1_INFO.getRelease(),
|
||||
RevisionInfoBean.getBean(session).getReleaseLabel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReleaseLabelManyLevels() {
|
||||
RevisionInfoBean.setBean(context, BEAN_MULTI_LEVEL);
|
||||
assertEquals("many level release", LEVEL_3_INFO.getRelease(),
|
||||
RevisionInfoBean.getBean(session).getReleaseLabel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReleaseLabelNoLevels() {
|
||||
RevisionInfoBean.setBean(context, BEAN_NO_LEVEL);
|
||||
assertEquals("0 level release", DUMMY_LEVEL.getRelease(),
|
||||
RevisionInfoBean.getBean(session).getReleaseLabel());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReleaseLabelNoBean() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.ERROR);
|
||||
|
||||
assertEquals("no bean release", DUMMY_LEVEL.getRelease(),
|
||||
RevisionInfoBean.getBean(session).getReleaseLabel());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,183 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.config;
|
||||
|
||||
import static edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean.DUMMY_BEAN;
|
||||
import static edu.cornell.mannlib.vitro.webapp.config.RevisionInfoSetup.DATE_FORMAT;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import stubs.javax.servlet.ServletContextStub;
|
||||
import stubs.javax.servlet.http.HttpSessionStub;
|
||||
|
||||
import com.ibm.icu.text.SimpleDateFormat;
|
||||
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.config.RevisionInfoBean.LevelRevisionInfo;
|
||||
|
||||
/**
|
||||
* Test for RevisionInfoSetup
|
||||
*/
|
||||
public class RevisionInfoSetupTest extends AbstractTestClass {
|
||||
private ServletContextStub context;
|
||||
private HttpSessionStub session;
|
||||
private ServletContextListener listener;
|
||||
private ServletContextEvent event;
|
||||
|
||||
@Before
|
||||
public void setupContext() {
|
||||
context = new ServletContextStub();
|
||||
|
||||
session = new HttpSessionStub();
|
||||
session.setServletContext(context);
|
||||
|
||||
event = new ServletContextEvent(context);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createContextListener() {
|
||||
listener = new RevisionInfoSetup();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void suppressInfoMessages() {
|
||||
setLoggerLevel(RevisionInfoBean.class, Level.WARN);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noResourceFile() {
|
||||
setLoggerLevel(RevisionInfoSetup.class, Level.OFF);
|
||||
testThisExpectedFailure("no resource", null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceFileIsEmpty() {
|
||||
setLoggerLevel(RevisionInfoSetup.class, Level.OFF);
|
||||
testThisExpectedFailure("empty resource", "");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceFileHasNoSignificantLines() {
|
||||
setLoggerLevel(RevisionInfoSetup.class, Level.OFF);
|
||||
testThisExpectedFailure("no siginificant lines", " \n # \n\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceFileHasInvalidDateLine() {
|
||||
setLoggerLevel(RevisionInfoSetup.class, Level.OFF);
|
||||
testThisExpectedFailure("invalid date line", "BOGUS DATE LINE\n"
|
||||
+ "name ~ release ~ revision");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void resourceFileHasInvalidLevelLine() {
|
||||
setLoggerLevel(RevisionInfoSetup.class, Level.OFF);
|
||||
testThisExpectedFailure("invalid level line", "2010-10-13 23:55:00\n"
|
||||
+ "name ~ release ~revision");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleSingleLevel() {
|
||||
testThisResourceFile(
|
||||
"simple single level",
|
||||
"2010-10-13 23:55:00\n" + "name ~ release ~ revision",
|
||||
bean(date("2010-10-13 23:55:00"),
|
||||
level("name", "release", "revision")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreWhiteSpaceAroundDate() {
|
||||
testThisResourceFile(
|
||||
"white space around date",
|
||||
" 1999-01-01 00:00:00 \n" + "name ~ release ~ revision",
|
||||
bean(date("1999-01-01 00:00:00"),
|
||||
level("name", "release", "revision")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreWhiteSpaceInLevelInfo() {
|
||||
testThisResourceFile(
|
||||
"white space in level info",
|
||||
"2010-10-13 23:55:00\n"
|
||||
+ " name ~ release ~ revision ",
|
||||
bean(date("2010-10-13 23:55:00"),
|
||||
level("name", "release", "revision")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ignoreBlankLinesAndComments() {
|
||||
testThisResourceFile(
|
||||
"ignore empty lines",
|
||||
"2010-10-13 23:55:00\n" + "\n" + " \n" + " # \n"
|
||||
+ "name ~ release ~ revision",
|
||||
bean(date("2010-10-13 23:55:00"),
|
||||
level("name", "release", "revision")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseMultipleLevels() {
|
||||
testThisResourceFile(
|
||||
"multiple levels",
|
||||
"2010-10-13 23:55:00\n" + "name ~ release ~ revision\n"
|
||||
+ "name2 ~ release2 ~ revision2\n",
|
||||
bean(date("2010-10-13 23:55:00"),
|
||||
level("name", "release", "revision"),
|
||||
level("name2", "release2", "revision2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseNoLevels() {
|
||||
testThisResourceFile("no levels", "2010-10-13 23:55:00\n",
|
||||
bean(date("2010-10-13 23:55:00")));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// helper methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private RevisionInfoBean bean(Date date, LevelRevisionInfo... levels) {
|
||||
return new RevisionInfoBean(date, Arrays.asList(levels));
|
||||
}
|
||||
|
||||
private LevelRevisionInfo level(String name, String release, String revision) {
|
||||
return new LevelRevisionInfo(name, release, revision);
|
||||
}
|
||||
|
||||
private Date date(String string) {
|
||||
try {
|
||||
return new SimpleDateFormat(DATE_FORMAT).parse(string);
|
||||
} catch (ParseException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't parse this date string: '" + string + "'");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test these file contents, compare to this expected bean.
|
||||
*/
|
||||
private void testThisResourceFile(String message, String fileContents,
|
||||
RevisionInfoBean expected) {
|
||||
context.setMockResource(RevisionInfoSetup.RESOURCE_PATH, fileContents);
|
||||
|
||||
listener.contextInitialized(event);
|
||||
assertEquals(message, expected, RevisionInfoBean.getBean(session));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test these file contents, expect the dummy bean as a result.
|
||||
*/
|
||||
private void testThisExpectedFailure(String message, String fileContents) {
|
||||
testThisResourceFile(message, fileContents, DUMMY_BEAN);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
package stubs.javax.servlet;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
@ -19,7 +20,6 @@ import javax.servlet.ServletException;
|
|||
/**
|
||||
* A simple stand-in for the {@link ServletContext}, for use in unit tests.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class ServletContextStub implements ServletContext {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -27,6 +27,18 @@ public class ServletContextStub implements ServletContext {
|
|||
// ----------------------------------------------------------------------
|
||||
|
||||
private final Map<String, Object> attributes = new HashMap<String, Object>();
|
||||
private final Map<String, String> mockResources = new HashMap<String, String>();
|
||||
|
||||
public void setMockResource(String path, String contents) {
|
||||
if (path == null) {
|
||||
throw new NullPointerException("path may not be null.");
|
||||
}
|
||||
if (contents == null) {
|
||||
mockResources.remove(path);
|
||||
} else {
|
||||
mockResources.put(path, contents);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub methods
|
||||
|
@ -56,6 +68,15 @@ public class ServletContextStub implements ServletContext {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResourceAsStream(String path) {
|
||||
if (mockResources.containsKey(path)) {
|
||||
return new ByteArrayInputStream(mockResources.get(path).getBytes());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Un-implemented methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -79,7 +100,7 @@ public class ServletContextStub implements ServletContext {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getInitParameterNames() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getInitParameterNames() not implemented.");
|
||||
|
@ -128,13 +149,7 @@ public class ServletContextStub implements ServletContext {
|
|||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResourceAsStream(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getResourceAsStream() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Set getResourcePaths(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getResourcePaths() not implemented.");
|
||||
|
@ -159,14 +174,14 @@ public class ServletContextStub implements ServletContext {
|
|||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getServletNames() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServletNames() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getServlets() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServlets() not implemented.");
|
||||
|
|
132
webapp/test/stubs/javax/servlet/http/HttpSessionStub.java
Normal file
132
webapp/test/stubs/javax/servlet/http/HttpSessionStub.java
Normal file
|
@ -0,0 +1,132 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package stubs.javax.servlet.http;
|
||||
|
||||
import java.util.Enumeration;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* A simple stand-in for the HttpSession, for use in unit tests.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class HttpSessionStub implements HttpSession {
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub infrastructure
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private ServletContext context;
|
||||
|
||||
public void setServletContext(ServletContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public ServletContext getServletContext() {
|
||||
return this.context;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Un-implemented methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getAttribute() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Enumeration getAttributeNames() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getAttributeNames() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getCreationTime() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getCreationTime() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
throw new RuntimeException("HttpSessionStub.getId() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLastAccessedTime() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getLastAccessedTime() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxInactiveInterval() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getMaxInactiveInterval() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public javax.servlet.http.HttpSessionContext getSessionContext() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getSessionContext() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValue(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getValue() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getValueNames() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.getValueNames() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invalidate() {
|
||||
throw new RuntimeException(
|
||||
"HttpSessionStub.invalidate() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNew() {
|
||||
throw new RuntimeException("HttpSessionStub.isNew() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putValue(String arg0, Object arg1) {
|
||||
throw new RuntimeException(
|
||||
"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.");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue