NIHVIVO-510 Create the FileStorageSetup initializer, instead of the FileStorageFactory
This commit is contained in:
parent
bcf06634ee
commit
5b9bf808c8
9 changed files with 459 additions and 328 deletions
|
@ -1,187 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
|
||||
|
||||
import static edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageFactory.PROPERTY_IMPLEMETATION_CLASSNAME;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import stubs.javax.naming.InitialContextStub;
|
||||
import stubs.javax.naming.spi.InitialContextFactoryStub;
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* This just checks the interaction between the configuration properties, the
|
||||
* system properties, and the implementation of {@link FileStorage}.
|
||||
*/
|
||||
public class FileStorageFactoryTest extends AbstractTestClass {
|
||||
private static final String configProperties = "#mock config properties file\n";
|
||||
private static File tempDir;
|
||||
private static File propsFile;
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
tempDir = createTempDirectory("FileStorageFactoryTest");
|
||||
|
||||
propsFile = createFile(tempDir, "config.properties", configProperties);
|
||||
|
||||
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
|
||||
InitialContextFactoryStub.class.getName());
|
||||
InitialContextStub.reset();
|
||||
new InitialContext().bind("java:comp/env/path.configuration", propsFile
|
||||
.getPath());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeConfigurationProperties() throws NamingException,
|
||||
SecurityException, NoSuchMethodException, IllegalArgumentException,
|
||||
IllegalAccessException, InvocationTargetException {
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
purgeDirectoryRecursively(tempDir);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createDefaultImplementation() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(),
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
FileStorage fs = FileStorageFactory.getFileStorage();
|
||||
assertEquals("implementation class", FileStorageImpl.class, fs
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createAlternateImplementation() throws IOException {
|
||||
System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME,
|
||||
FileStorageStub.class.getName());
|
||||
FileStorage fs = FileStorageFactory.getFileStorage();
|
||||
assertEquals("implementation class", FileStorageStub.class, fs
|
||||
.getClass());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void baseDirectoryDoesntExist() throws IOException {
|
||||
setConfigurationProperties("/bogus/Directory",
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void defaultNamespaceIsBogus() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(), "namespace");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void noSuchClass() throws IOException {
|
||||
System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME, "bogus.Classname");
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void doesntImplement() throws IOException {
|
||||
System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME,
|
||||
NotFileStorage.class.getName());
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void noZeroArgsConstructor() throws IOException {
|
||||
System.setProperty(PROPERTY_IMPLEMETATION_CLASSNAME,
|
||||
NoConstructor.class.getName());
|
||||
FileStorageFactory.getFileStorage();
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Helper methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private void setConfigurationProperties(String baseDir,
|
||||
String defaultNamespace) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put(FileStorage.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir);
|
||||
map.put(FileStorage.PROPERTY_DEFAULT_NAMESPACE, defaultNamespace);
|
||||
|
||||
try {
|
||||
Field f = ConfigurationProperties.class.getDeclaredField("theMap");
|
||||
f.setAccessible(true);
|
||||
f.set(null, map);
|
||||
} catch (Exception e) {
|
||||
fail("Exception while setting config properties: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Helper classes
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/** An alternative implementation of FileStorage. */
|
||||
public static class FileStorageStub implements FileStorage {
|
||||
public void createFile(String id, String filename, InputStream bytes)
|
||||
throws FileAlreadyExistsException, IOException {
|
||||
}
|
||||
|
||||
public boolean deleteFile(String id) throws IOException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getFilename(String id) throws IOException {
|
||||
return "filename";
|
||||
}
|
||||
|
||||
public InputStream getInputStream(String id, String filename)
|
||||
throws FileNotFoundException, IOException {
|
||||
return new ByteArrayInputStream(new byte[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** This class has no zero-argument constructor. */
|
||||
public static class NoConstructor implements FileStorage {
|
||||
@SuppressWarnings("unused")
|
||||
public NoConstructor(String string) {
|
||||
}
|
||||
|
||||
public void createFile(String id, String filename, InputStream bytes)
|
||||
throws FileAlreadyExistsException, IOException {
|
||||
}
|
||||
|
||||
public boolean deleteFile(String id) throws IOException {
|
||||
return true;
|
||||
}
|
||||
|
||||
public String getFilename(String id) throws IOException {
|
||||
return "filename";
|
||||
}
|
||||
|
||||
public InputStream getInputStream(String id, String filename)
|
||||
throws FileNotFoundException, IOException {
|
||||
return new ByteArrayInputStream(new byte[0]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** This class does not implement the FileStorage interface. */
|
||||
public static class NotFileStorage {
|
||||
}
|
||||
}
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
|
@ -19,7 +20,6 @@ import java.util.HashSet;
|
|||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
@ -50,6 +50,10 @@ public class FileStorageImplTest extends AbstractTestClass {
|
|||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// tests
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void baseDirDoesntExist() throws IOException {
|
||||
File baseDir = new File(tempDir, "doesntExist");
|
||||
|
|
|
@ -0,0 +1,142 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import stubs.javax.naming.InitialContextStub;
|
||||
import stubs.javax.naming.spi.InitialContextFactoryStub;
|
||||
import stubs.javax.servlet.ServletContextStub;
|
||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Test the methods of {@link FileStorageSetup}
|
||||
*/
|
||||
public class FileStorageSetupTest extends AbstractTestClass {
|
||||
// ----------------------------------------------------------------------
|
||||
// framework
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private static final String configProperties = "#mock config properties file\n";
|
||||
private static File tempDir;
|
||||
|
||||
private FileStorageSetup fss;
|
||||
private ServletContextEvent sce;
|
||||
private ServletContext sc;
|
||||
|
||||
/**
|
||||
* Use a mock {@link InitialContext} to create an empty
|
||||
* {@link ConfigurationProperties} object. Each test can use
|
||||
* {@link #setConfigurationProperties(String, String)} to populate it as
|
||||
* they choose.
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void createConfigurationProperties() throws Exception {
|
||||
tempDir = createTempDirectory("FileStorageFactoryTest");
|
||||
|
||||
File propsFile = createFile(tempDir, "config.properties",
|
||||
configProperties);
|
||||
|
||||
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
|
||||
InitialContextFactoryStub.class.getName());
|
||||
InitialContextStub.reset();
|
||||
new InitialContext().bind("java:comp/env/path.configuration", propsFile
|
||||
.getPath());
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createFileStorageSetup() {
|
||||
fss = new FileStorageSetup();
|
||||
sc = new ServletContextStub();
|
||||
sce = new ServletContextEvent(sc);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void cleanup() {
|
||||
purgeDirectoryRecursively(tempDir);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// tests
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void baseDirectoryNotSpecified() {
|
||||
setConfigurationProperties(null, "http://vivo.myDomain.edu/individual/");
|
||||
fss.contextInitialized(sce);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void baseDirectoryDoesntExist() throws IOException {
|
||||
setConfigurationProperties("/bogus/Directory",
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
fss.contextInitialized(sce);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void defaultNamespaceNotSpecified() {
|
||||
setConfigurationProperties(tempDir.getPath(), null);
|
||||
fss.contextInitialized(sce);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void defaultNamespaceIsBogus() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(), "namespace");
|
||||
fss.contextInitialized(sce);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void success() throws IOException {
|
||||
setConfigurationProperties(tempDir.getPath(),
|
||||
"http://vivo.myDomain.edu/individual/");
|
||||
fss.contextInitialized(sce);
|
||||
|
||||
Object o = sc.getAttribute(FileStorageSetup.ATTRIBUTE_NAME);
|
||||
FileStorage fs = (FileStorage) o;
|
||||
|
||||
assertEquals("implementation class", FileStorageImpl.class, fs
|
||||
.getClass());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Helper methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private void setConfigurationProperties(String baseDir,
|
||||
String defaultNamespace) {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
if (baseDir != null) {
|
||||
map.put(FileStorageSetup.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir);
|
||||
}
|
||||
if (defaultNamespace != null) {
|
||||
map.put(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE,
|
||||
defaultNamespace);
|
||||
}
|
||||
|
||||
try {
|
||||
Field f = ConfigurationProperties.class.getDeclaredField("theMap");
|
||||
f.setAccessible(true);
|
||||
f.set(null, map);
|
||||
} catch (Exception e) {
|
||||
fail("Exception while setting config properties: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
189
webapp/test/stubs/javax/servlet/ServletContextStub.java
Normal file
189
webapp/test/stubs/javax/servlet/ServletContextStub.java
Normal file
|
@ -0,0 +1,189 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package stubs.javax.servlet;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.Servlet;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
|
||||
/**
|
||||
* A simple stand-in for the {@link ServletContext}, for use in unit tests.
|
||||
*/
|
||||
public class ServletContextStub implements ServletContext {
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub infrastructure
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private final Map<String, Object> attributes = new HashMap<String, Object>();
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Stub methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public Object getAttribute(String name) {
|
||||
return attributes.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enumeration<String> getAttributeNames() {
|
||||
return Collections.enumeration(attributes.keySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttribute(String name) {
|
||||
attributes.remove(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttribute(String name, Object object) {
|
||||
if (object == null) {
|
||||
removeAttribute(name);
|
||||
} else {
|
||||
attributes.put(name, object);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Un-implemented methods
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public ServletContext getContext(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getContext() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContextPath() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getContextPath() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInitParameter(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getInitParameter() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Enumeration getInitParameterNames() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getInitParameterNames() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMajorVersion() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getMajorVersion() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimeType(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getMimeType() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinorVersion() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getMinorVersion() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestDispatcher getNamedDispatcher(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getNamedDispatcher() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRealPath(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getRealPath() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public RequestDispatcher getRequestDispatcher(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getRequestDispatcher() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getResource(String arg0) throws MalformedURLException {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getResource() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getResourceAsStream(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getResourceAsStream() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set getResourcePaths(String arg0) {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getResourcePaths() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerInfo() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServerInfo() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Servlet getServlet(String arg0) throws ServletException {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServlet() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServletContextName() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServletContextName() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Enumeration getServletNames() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServletNames() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Enumeration getServlets() {
|
||||
throw new RuntimeException(
|
||||
"ServletContextStub.getServlets() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(String arg0) {
|
||||
throw new RuntimeException("ServletContextStub.log() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Exception arg0, String arg1) {
|
||||
throw new RuntimeException("ServletContextStub.log() not implemented.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(String arg0, Throwable arg1) {
|
||||
throw new RuntimeException("ServletContextStub.log() not implemented.");
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue