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
|
@ -144,6 +144,12 @@
|
|||
</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
<listener-class>
|
||||
edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup
|
||||
</listener-class>
|
||||
</listener>
|
||||
|
||||
<!--
|
||||
<listener>
|
||||
<listener-class>
|
||||
|
|
|
@ -6,24 +6,10 @@ import java.io.FileNotFoundException;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* The interface for the File Storage system.
|
||||
*/
|
||||
public interface FileStorage {
|
||||
/**
|
||||
* The default implementation will use this key to ask
|
||||
* {@link ConfigurationProperties} for the file storage base directory.
|
||||
*/
|
||||
String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory";
|
||||
|
||||
/**
|
||||
* The default implementation will use this key to ask
|
||||
* {@link ConfigurationProperties} for the default URI namespace.
|
||||
*/
|
||||
String PROPERTY_DEFAULT_NAMESPACE = "Vitro.defaultNamespace";
|
||||
|
||||
/**
|
||||
* The name of the root directory, within the base directory.
|
||||
*/
|
||||
|
|
|
@ -1,56 +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 java.io.IOException;
|
||||
|
||||
/**
|
||||
* Create an instance of {@link FileStorage} -- either the default
|
||||
* implementation, or one specified by a system property.
|
||||
*/
|
||||
public class FileStorageFactory {
|
||||
/**
|
||||
* If this system property is set, it will be taken as the name of the
|
||||
* implementing class.
|
||||
*/
|
||||
public static final String PROPERTY_IMPLEMETATION_CLASSNAME = FileStorage.class
|
||||
.getName();
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Get an instance of {@link FileStorage}. By default, this will be an
|
||||
* instance of {@link FileStorageImpl}.
|
||||
* </p>
|
||||
* <p>
|
||||
* If the System Property named by
|
||||
* {#SYSTEM_PROPERTY_IMPLEMETATION_CLASSNAME} is set, it must contain the
|
||||
* name of the implementation class, which must be a sub-class of
|
||||
* {@link FileStorage}, and must have a public, no-argument constructor.
|
||||
* </p>
|
||||
*/
|
||||
public static FileStorage getFileStorage() throws IOException {
|
||||
String className = System.getProperty(PROPERTY_IMPLEMETATION_CLASSNAME);
|
||||
if (className == null) {
|
||||
return new FileStorageImpl();
|
||||
}
|
||||
|
||||
try {
|
||||
Class<?> clazz = Class.forName(className);
|
||||
Object instance = clazz.newInstance();
|
||||
return FileStorage.class.cast(instance);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't create a FileStorage instance", e);
|
||||
} catch (ClassCastException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't create a FileStorage instance", e);
|
||||
} catch (InstantiationException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't create a FileStorage instance", e);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException(
|
||||
"Can't create a FileStorage instance", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -16,7 +16,6 @@ import java.io.OutputStream;
|
|||
import java.io.PrintWriter;
|
||||
import java.io.Reader;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
@ -24,8 +23,6 @@ import java.util.Properties;
|
|||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* The default implementation of {@link FileStorage}.
|
||||
*/
|
||||
|
@ -40,21 +37,6 @@ public class FileStorageImpl implements FileStorage {
|
|||
// Constructors and helper methods.
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Use the configuration properties to create an instance.
|
||||
*
|
||||
* @throws IllegalArgumentException
|
||||
* if the configuration property for the base directory is
|
||||
* missing, or if it doesn't point to an existing, writeable
|
||||
* directory.
|
||||
* @throws IllegalArgumentException
|
||||
* if the configuration property for the default namespace is
|
||||
* missing, or if it isn't in the expected form.
|
||||
*/
|
||||
FileStorageImpl() throws IOException {
|
||||
this(figureBaseDir(), figureFileNamespace());
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the arguments to create an instance. If the base directory is empty,
|
||||
* initialize it. Otherwise, check that it was initialized to the same
|
||||
|
@ -123,57 +105,6 @@ public class FileStorageImpl implements FileStorage {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration property for the file storage base directory, and
|
||||
* check that it points to an existing, writeable directory.
|
||||
*
|
||||
* For use by the constructor in implementations of {@link FileStorage}.
|
||||
*/
|
||||
static File figureBaseDir() {
|
||||
String baseDirPath = ConfigurationProperties
|
||||
.getProperty(PROPERTY_FILE_STORAGE_BASE_DIR);
|
||||
if (baseDirPath == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Configuration properties must contain a value for '"
|
||||
+ PROPERTY_FILE_STORAGE_BASE_DIR + "'");
|
||||
}
|
||||
return new File(baseDirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration property for the default namespace, and derive the
|
||||
* file namespace from it. The default namespace is assumed to be in this
|
||||
* form: <code>http://vivo.mydomain.edu/individual/</code>
|
||||
*
|
||||
* For use by the constructor in implementations of {@link FileStorage}.
|
||||
*
|
||||
* @returns the file namespace is assumed to be in this form:
|
||||
* <code>http://vivo.mydomain.edu/file/</code>
|
||||
*/
|
||||
static Collection<String> figureFileNamespace() {
|
||||
String defaultNamespace = ConfigurationProperties
|
||||
.getProperty(PROPERTY_DEFAULT_NAMESPACE);
|
||||
if (defaultNamespace == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Configuration properties must contain a value for '"
|
||||
+ PROPERTY_DEFAULT_NAMESPACE + "'");
|
||||
}
|
||||
|
||||
String defaultSuffix = "/individual/";
|
||||
String fileSuffix = "/file/";
|
||||
|
||||
if (!defaultNamespace.endsWith(defaultSuffix)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Default namespace does not match the expected form: '"
|
||||
+ defaultNamespace + "'");
|
||||
}
|
||||
|
||||
int hostLength = defaultNamespace.length() - defaultSuffix.length();
|
||||
String fileNamespace = defaultNamespace.substring(0, hostLength)
|
||||
+ fileSuffix;
|
||||
return Collections.singleton(fileNamespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign arbitrary prefixes to these namespaces.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,116 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* Initializes the file storage system, and stores a reference in the servlet
|
||||
* context.
|
||||
*/
|
||||
public class FileStorageSetup implements ServletContextListener {
|
||||
/**
|
||||
* The implementation of the {@link FileStorage} system will be stored in
|
||||
* the {@link ServletContext} as an attribute with this name.
|
||||
*/
|
||||
public static final String ATTRIBUTE_NAME = FileStorage.class.getName();
|
||||
|
||||
/**
|
||||
* The default implementation will use this key to ask
|
||||
* {@link ConfigurationProperties} for the file storage base directory.
|
||||
*/
|
||||
static final String PROPERTY_FILE_STORAGE_BASE_DIR = "upload.directory";
|
||||
|
||||
/**
|
||||
* The default implementation will use this key to ask
|
||||
* {@link ConfigurationProperties} for the default URI namespace.
|
||||
*/
|
||||
static final String PROPERTY_DEFAULT_NAMESPACE = "Vitro.defaultNamespace";
|
||||
|
||||
/**
|
||||
* Create an implementation of {@link FileStorage} and store it in the
|
||||
* {@link ServletContext}, as an attribute named according to
|
||||
* {@link #ATTRIBUTE_NAME}.
|
||||
*/
|
||||
@Override
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
FileStorage fs;
|
||||
try {
|
||||
File baseDirectory = figureBaseDir();
|
||||
Collection<String> fileNamespace = figureFileNamespace();
|
||||
fs = new FileStorageImpl(baseDirectory, fileNamespace);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(
|
||||
"Failed to initialize the file system.", e);
|
||||
}
|
||||
|
||||
ServletContext sc = sce.getServletContext();
|
||||
sc.setAttribute(ATTRIBUTE_NAME, fs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration property for the file storage base directory, and
|
||||
* check that it points to an existing, writeable directory.
|
||||
*
|
||||
* For use by the constructor in implementations of {@link FileStorage}.
|
||||
*/
|
||||
private File figureBaseDir() {
|
||||
String baseDirPath = ConfigurationProperties
|
||||
.getProperty(PROPERTY_FILE_STORAGE_BASE_DIR);
|
||||
if (baseDirPath == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Configuration properties must contain a value for '"
|
||||
+ PROPERTY_FILE_STORAGE_BASE_DIR + "'");
|
||||
}
|
||||
return new File(baseDirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configuration property for the default namespace, and derive the
|
||||
* file namespace from it. The default namespace is assumed to be in this
|
||||
* form: <code>http://vivo.mydomain.edu/individual/</code>
|
||||
*
|
||||
* For use by the constructor in implementations of {@link FileStorage}.
|
||||
*
|
||||
* @returns the file namespace is assumed to be in this form:
|
||||
* <code>http://vivo.mydomain.edu/file/</code>
|
||||
*/
|
||||
private Collection<String> figureFileNamespace() {
|
||||
String defaultNamespace = ConfigurationProperties
|
||||
.getProperty(PROPERTY_DEFAULT_NAMESPACE);
|
||||
if (defaultNamespace == null) {
|
||||
throw new IllegalArgumentException(
|
||||
"Configuration properties must contain a value for '"
|
||||
+ PROPERTY_DEFAULT_NAMESPACE + "'");
|
||||
}
|
||||
|
||||
String defaultSuffix = "/individual/";
|
||||
String fileSuffix = "/file/";
|
||||
|
||||
if (!defaultNamespace.endsWith(defaultSuffix)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Default namespace does not match the expected form: '"
|
||||
+ defaultNamespace + "'");
|
||||
}
|
||||
|
||||
int hostLength = defaultNamespace.length() - defaultSuffix.length();
|
||||
String fileNamespace = defaultNamespace.substring(0, hostLength)
|
||||
+ fileSuffix;
|
||||
return Collections.singleton(fileNamespace);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void contextDestroyed(ServletContextEvent sce) {
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
}
|
|
@ -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