NIHVIVO-701 Merge 5366 from branch
This commit is contained in:
parent
12b933fa32
commit
0459959ae7
5 changed files with 91 additions and 82 deletions
|
@ -4,8 +4,7 @@ package edu.cornell.mannlib.vitro.webapp;
|
|||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -37,32 +36,50 @@ public class ConfigurationPropertiesTest extends AbstractTestClass {
|
|||
private static final String NOT_THE_DESIRED_MAPPING = ConfigurationProperties.JNDI_BASE
|
||||
+ "/notTheDesiredMapping";
|
||||
|
||||
private static File tempDir;
|
||||
private static File testFile;
|
||||
private static File invalidFile;
|
||||
/**
|
||||
* The resource property files that we might configure.
|
||||
*/
|
||||
private static final String CONFIGURED_PROPERTY_FILE = "edu/cornell/mannlib/vitro/webapp/test_config.properties";
|
||||
private static final String INVALID_PROPERTY_FILE = "edu/cornell/mannlib/vitro/webapp/test_config_invalid.properties";
|
||||
private static final String DEFAULT_PROPERTY_FILE = "edu/cornell/mannlib/vitro/webapp/test_config_default.properties";
|
||||
|
||||
/**
|
||||
* The mappings that we might find from the property files.
|
||||
*/
|
||||
private static final String[][] MAP_VALUES_DEFAULT = new String[][] { {
|
||||
"whichfile", "test_config_default" } };
|
||||
private static final String[][] MAP_VALUES_CONFIGURED = new String[][] { {
|
||||
"whichfile", "test_config" } };
|
||||
|
||||
/**
|
||||
* A context to hold the JNDI mappings.
|
||||
*/
|
||||
private InitialContext initial;
|
||||
|
||||
/**
|
||||
* Create a good test file and a bad test file.
|
||||
*
|
||||
* (a class-path-based resource should already exist.)
|
||||
* The original default value for the configuration properties path.
|
||||
*/
|
||||
private static Object originalPath;
|
||||
|
||||
@BeforeClass
|
||||
public static void createTestFiles() throws IOException {
|
||||
tempDir = createTempDirectory(ConfigurationPropertiesTest.class
|
||||
.getSimpleName());
|
||||
testFile = createFile(tempDir, "testFile", "source = file\n");
|
||||
invalidFile = createFile(tempDir, "invalidFile",
|
||||
"source = bad Unicode constant \\uu1045");
|
||||
public static void alterTheDefaultPath() throws SecurityException,
|
||||
NoSuchFieldException, IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
Class<ConfigurationProperties> clazz = ConfigurationProperties.class;
|
||||
Field field = clazz.getDeclaredField("DEFAULT_CONFIG_PATH");
|
||||
field.setAccessible(true);
|
||||
originalPath = field.get(null);
|
||||
field.set(null, DEFAULT_PROPERTY_FILE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up.
|
||||
*/
|
||||
@AfterClass
|
||||
public static void removeTestFiles() {
|
||||
purgeDirectoryRecursively(tempDir);
|
||||
public static void restoreTheDefaultPath() throws SecurityException,
|
||||
NoSuchFieldException, IllegalArgumentException,
|
||||
IllegalAccessException {
|
||||
Class<ConfigurationProperties> clazz = ConfigurationProperties.class;
|
||||
Field field = clazz.getDeclaredField("DEFAULT_CONFIG_PATH");
|
||||
field.setAccessible(true);
|
||||
field.set(null, originalPath);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,51 +114,49 @@ public class ConfigurationPropertiesTest extends AbstractTestClass {
|
|||
// the tests
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
public void topLevelContextIsMissing() {
|
||||
setLoggerLevel(ConfigurationProperties.class, Level.ERROR);
|
||||
ConfigurationProperties.getMap();
|
||||
assertExpectedMap(MAP_VALUES_DEFAULT);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
public void noEnvironmentMapping() throws NamingException {
|
||||
setLoggerLevel(ConfigurationProperties.class, Level.ERROR);
|
||||
// We map something in the same JNDI environment,
|
||||
// but not the mapping we will be looking for.
|
||||
initial.bind(NOT_THE_DESIRED_MAPPING, "doesn't matter");
|
||||
ConfigurationProperties.getMap();
|
||||
assertExpectedMap(MAP_VALUES_DEFAULT);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void fileNotFound() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, "noSuchFileOrResource");
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, "noSuchResource");
|
||||
ConfigurationProperties.getMap();
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void invalidFileFormat() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, invalidFile.getPath());
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, INVALID_PROPERTY_FILE);
|
||||
ConfigurationProperties.getMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readFromResource() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING,
|
||||
"edu/cornell/mannlib/vitro/webapp/test_config.properties");
|
||||
assertExpectedMap(new String[][] { { "source", "resource" } });
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readFromFile() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, testFile.getPath());
|
||||
assertExpectedMap(new String[][] { { "source", "file" } });
|
||||
public void readFromConfiguredResource() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, CONFIGURED_PROPERTY_FILE);
|
||||
assertExpectedMap(MAP_VALUES_CONFIGURED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkOtherMethods() throws NamingException {
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, testFile.getPath());
|
||||
assertEquals("file", ConfigurationProperties.getProperty("source"));
|
||||
initial.bind(CONFIGURATION_PROPERTIES_MAPPING, CONFIGURED_PROPERTY_FILE);
|
||||
assertEquals("test_config",
|
||||
ConfigurationProperties.getProperty("whichfile"));
|
||||
assertEquals(null, ConfigurationProperties.getProperty("notThere"));
|
||||
assertEquals("default", ConfigurationProperties.getProperty("notThere",
|
||||
"default"));
|
||||
assertEquals("default",
|
||||
ConfigurationProperties.getProperty("notThere", "default"));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
@ -157,8 +172,8 @@ public class ConfigurationPropertiesTest extends AbstractTestClass {
|
|||
for (String[] pair : strings) {
|
||||
expected.put(pair[0], pair[1]);
|
||||
}
|
||||
assertEquals("properties map", expected, ConfigurationProperties
|
||||
.getMap());
|
||||
assertEquals("properties map", expected,
|
||||
ConfigurationProperties.getMap());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#
|
||||
# This is a data file for ConfigurationPropertiesTest.
|
||||
#
|
||||
source = resource
|
||||
whichfile = test_config
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
#
|
||||
# This is a data file for ConfigurationPropertiesTest.
|
||||
#
|
||||
whichfile = test_config_default
|
|
@ -0,0 +1,5 @@
|
|||
#
|
||||
# This is a data file for ConfigurationPropertiesTest.
|
||||
#
|
||||
whichfile = test_config_invalid
|
||||
source = bad Unicode constant \uu1045
|
Loading…
Add table
Add a link
Reference in a new issue