NIHVIVO-1261 Convert to use the new ConfigurationProperties class.
This commit is contained in:
parent
c2021e7d93
commit
5fa2864fbe
2 changed files with 41 additions and 106 deletions
|
@ -3,62 +3,41 @@
|
||||||
package edu.cornell.mannlib.vitro.webapp.filestorage;
|
package edu.cornell.mannlib.vitro.webapp.filestorage;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.naming.InitialContext;
|
|
||||||
|
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
import org.junit.AfterClass;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import stubs.javax.naming.InitialContextStub;
|
import stubs.edu.cornell.mannlib.vitro.webapp.config.ConfigurationPropertiesStub;
|
||||||
import stubs.javax.naming.spi.InitialContextFactoryStub;
|
import stubs.javax.servlet.ServletContextStub;
|
||||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
|
import edu.cornell.mannlib.vitro.webapp.filestorage.backend.FileStorageSetup;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class FileServingHelperTest extends AbstractTestClass {
|
public class FileServingHelperTest extends AbstractTestClass {
|
||||||
private static final String DEFAULT_NAMESPACE = "http://some.crazy.domain/individual/";
|
private static final String DEFAULT_NAMESPACE = "http://some.crazy.domain/individual/";
|
||||||
private static final String CONFIG_PROPERTIES = "#mock config properties file\n";
|
|
||||||
private static File tempDir;
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
// framework
|
// framework
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
|
private ServletContextStub ctx;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use a mock {@link InitialContext} to create an empty
|
* Set the desired default namespace into the ConfigurationProperties.
|
||||||
* {@link ConfigurationProperties} object. Each test can use
|
|
||||||
* {@link #setConfigurationProperties(String, String)} to populate it as
|
|
||||||
* they choose.
|
|
||||||
*/
|
*/
|
||||||
@BeforeClass
|
@Before
|
||||||
public static void createConfigurationProperties() throws Exception {
|
public void createConfigurationProperties() throws Exception {
|
||||||
tempDir = createTempDirectory("FileServingHelperTest");
|
setLoggerLevel(ConfigurationProperties.class, Level.WARN);
|
||||||
|
|
||||||
File propsFile = createFile(tempDir, "config.properties",
|
ctx = new ServletContextStub();
|
||||||
CONFIG_PROPERTIES);
|
|
||||||
|
|
||||||
System.setProperty(InitialContext.INITIAL_CONTEXT_FACTORY,
|
ConfigurationPropertiesStub props = new ConfigurationPropertiesStub();
|
||||||
InitialContextFactoryStub.class.getName());
|
props.setProperty(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE,
|
||||||
InitialContextStub.reset();
|
DEFAULT_NAMESPACE);
|
||||||
new InitialContext().bind("java:comp/env/path.configuration", propsFile
|
props.setBean(ctx);
|
||||||
.getPath());
|
|
||||||
|
|
||||||
setConfigurationProperties(DEFAULT_NAMESPACE);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void cleanup() {
|
|
||||||
purgeDirectoryRecursively(tempDir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
@ -79,8 +58,8 @@ public class FileServingHelperTest extends AbstractTestClass {
|
||||||
@Test
|
@Test
|
||||||
public void notInDefaultNamespace() {
|
public void notInDefaultNamespace() {
|
||||||
setLoggerLevel(FileServingHelper.class, Level.ERROR);
|
setLoggerLevel(FileServingHelper.class, Level.ERROR);
|
||||||
assertCorrectUrl("notInTheNamespace",
|
assertCorrectUrl("notInTheNamespace", "somefilename.ext",
|
||||||
"somefilename.ext", "notInTheNamespace");
|
"notInTheNamespace");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -99,21 +78,9 @@ public class FileServingHelperTest extends AbstractTestClass {
|
||||||
// Helper methods
|
// Helper methods
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
private static void setConfigurationProperties(String defaultNamespace) {
|
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertCorrectUrl(String uri, String filename, String expected) {
|
private void assertCorrectUrl(String uri, String filename, String expected) {
|
||||||
String actual = FileServingHelper.getBytestreamAliasUrl(uri, filename, null);
|
String actual = FileServingHelper.getBytestreamAliasUrl(uri, filename,
|
||||||
|
ctx);
|
||||||
assertEquals("url", expected, actual);
|
assertEquals("url", expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,30 +4,22 @@ package edu.cornell.mannlib.vitro.webapp.filestorage.backend;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
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.ServletContext;
|
||||||
import javax.servlet.ServletContextEvent;
|
import javax.servlet.ServletContextEvent;
|
||||||
|
|
||||||
import org.apache.log4j.Level;
|
import org.apache.log4j.Level;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.AfterClass;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.BeforeClass;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import stubs.javax.naming.InitialContextStub;
|
import stubs.edu.cornell.mannlib.vitro.webapp.config.ConfigurationPropertiesStub;
|
||||||
import stubs.javax.naming.spi.InitialContextFactoryStub;
|
|
||||||
import stubs.javax.servlet.ServletContextStub;
|
import stubs.javax.servlet.ServletContextStub;
|
||||||
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
|
||||||
import edu.cornell.mannlib.vitro.webapp.ConfigurationProperties;
|
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the methods of {@link FileStorageSetup}
|
* Test the methods of {@link FileStorageSetup}
|
||||||
|
@ -37,7 +29,6 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
// framework
|
// framework
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
private static final String configProperties = "#mock config properties file\n";
|
|
||||||
private static File tempDir;
|
private static File tempDir;
|
||||||
private static File fsBaseDir;
|
private static File fsBaseDir;
|
||||||
|
|
||||||
|
@ -45,46 +36,26 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
private ServletContextEvent sce;
|
private ServletContextEvent sce;
|
||||||
private ServletContext sc;
|
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
|
@Before
|
||||||
public void createFileStorageSetup() {
|
public void createFileStorageSetup() {
|
||||||
fss = new FileStorageSetup();
|
fss = new FileStorageSetup();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void createContext() {
|
||||||
sc = new ServletContextStub();
|
sc = new ServletContextStub();
|
||||||
sce = new ServletContextEvent(sc);
|
sce = new ServletContextEvent(sc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void createBaseDirectory() {
|
public void createBaseDirectory() throws IOException {
|
||||||
|
tempDir = createTempDirectory("FileStorageFactoryTest");
|
||||||
fsBaseDir = new File(tempDir, "fsBaseDirectory");
|
fsBaseDir = new File(tempDir, "fsBaseDirectory");
|
||||||
fsBaseDir.mkdir();
|
fsBaseDir.mkdir();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void cleanupBaseDirectory() {
|
public void cleanupBaseDirectory() {
|
||||||
purgeDirectoryRecursively(fsBaseDir);
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterClass
|
|
||||||
public static void cleanup() {
|
|
||||||
purgeDirectoryRecursively(tempDir);
|
purgeDirectoryRecursively(tempDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +73,7 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void baseDirectoryDoesntExist() throws IOException {
|
public void baseDirectoryDoesntExist() {
|
||||||
setLoggerLevel(FileStorageSetup.class, Level.OFF);
|
setLoggerLevel(FileStorageSetup.class, Level.OFF);
|
||||||
setConfigurationProperties("/bogus/Directory",
|
setConfigurationProperties("/bogus/Directory",
|
||||||
"http://vivo.myDomain.edu/individual/");
|
"http://vivo.myDomain.edu/individual/");
|
||||||
|
@ -122,7 +93,7 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
|
|
||||||
// This no longer throws an exception - it should be a success.
|
// This no longer throws an exception - it should be a success.
|
||||||
@Test
|
@Test
|
||||||
public void defaultNamespaceIsBogus() throws IOException {
|
public void defaultNamespaceIsBogus() {
|
||||||
setLoggerLevel(FileStorageSetup.class, Level.ERROR);
|
setLoggerLevel(FileStorageSetup.class, Level.ERROR);
|
||||||
setConfigurationProperties(fsBaseDir.getPath(), "namespace");
|
setConfigurationProperties(fsBaseDir.getPath(), "namespace");
|
||||||
fss.contextInitialized(sce);
|
fss.contextInitialized(sce);
|
||||||
|
@ -135,7 +106,7 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void success() throws IOException {
|
public void success() {
|
||||||
setConfigurationProperties(fsBaseDir.getPath(),
|
setConfigurationProperties(fsBaseDir.getPath(),
|
||||||
"http://vivo.myDomain.edu/individual/");
|
"http://vivo.myDomain.edu/individual/");
|
||||||
fss.contextInitialized(sce);
|
fss.contextInitialized(sce);
|
||||||
|
@ -153,22 +124,19 @@ public class FileStorageSetupTest extends AbstractTestClass {
|
||||||
|
|
||||||
private void setConfigurationProperties(String baseDir,
|
private void setConfigurationProperties(String baseDir,
|
||||||
String defaultNamespace) {
|
String defaultNamespace) {
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
ConfigurationPropertiesStub props = new ConfigurationPropertiesStub();
|
||||||
|
|
||||||
if (baseDir != null) {
|
if (baseDir != null) {
|
||||||
map.put(FileStorageSetup.PROPERTY_FILE_STORAGE_BASE_DIR, baseDir);
|
props.setProperty(FileStorageSetup.PROPERTY_FILE_STORAGE_BASE_DIR,
|
||||||
|
baseDir);
|
||||||
}
|
}
|
||||||
if (defaultNamespace != null) {
|
if (defaultNamespace != null) {
|
||||||
map.put(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE,
|
props.setProperty(FileStorageSetup.PROPERTY_DEFAULT_NAMESPACE,
|
||||||
defaultNamespace);
|
defaultNamespace);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
setLoggerLevel(ConfigurationProperties.class, Level.WARN);
|
||||||
Field f = ConfigurationProperties.class.getDeclaredField("theMap");
|
props.setBean(sc);
|
||||||
f.setAccessible(true);
|
|
||||||
f.set(null, map);
|
|
||||||
} catch (Exception e) {
|
|
||||||
fail("Exception while setting config properties: " + e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue