NIHVIVO-701 Merge 5366 from branch

This commit is contained in:
jeb228 2010-07-21 19:40:40 +00:00
parent 12b933fa32
commit 0459959ae7
5 changed files with 91 additions and 82 deletions

View file

@ -2,9 +2,6 @@
package edu.cornell.mannlib.vitro.webapp;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
@ -44,6 +41,12 @@ public class ConfigurationProperties {
private static final Log log = LogFactory
.getLog(ConfigurationProperties.class);
/**
* If we don't find the path to the config properties from a JNDI mapping,
* use this. Not final, so we can jigger it for unit tests.
*/
private static String DEFAULT_CONFIG_PATH = "deploy.properties";
/**
* The JNDI naming context where Tomcat stores environment attributes.
*/
@ -152,75 +155,57 @@ public class ConfigurationProperties {
}
/**
* Find the path to the Configuration properties file.
* Find the path to the Configuration properties file. If we can't find it
* by the JNDI mapping, use the default path.
*
* @throws IllegalStateException
* If we can't find the path.
*/
private static String getConfigurationFilePath() {
String message = "";
try {
message = "JNDI Lookup on \"" + JNDI_BASE
+ "\" failed. Is the context file missing?";
Context envCtx = (Context) new InitialContext().lookup(JNDI_BASE);
if (envCtx == null) {
log.error(message);
throw new IllegalStateException(message);
log.warn("JNDI Lookup on \"" + JNDI_BASE
+ "\" failed. Is the context file missing?");
return DEFAULT_CONFIG_PATH;
}
// Get the name of the configuration properties file.
message = "Could not find a JNDI Environment naming for '"
+ PATH_CONFIGURATION
+ "'. Is the context file set up correctly?";
String configPath = (String) envCtx.lookup(PATH_CONFIGURATION);
if (configPath == null) {
log.error(message);
throw new IllegalStateException(message);
log.warn("Could not find a JNDI Environment naming for '"
+ PATH_CONFIGURATION
+ "'. Is the context file set up correctly?");
return DEFAULT_CONFIG_PATH;
}
return configPath;
} catch (NamingException e) {
throw new IllegalStateException(message, e);
log.warn("JNDI lookup failed. "
+ "Using default path for config properties.", e);
return DEFAULT_CONFIG_PATH;
}
}
/**
* Find the Configuration properties file.
*
* First try to interpret the path as a file path (like
* /usr/local/config.props).
*
* If that doesn't work, try it as a resource path (relative to
* WEB-INF/classes).
* Interpret the path as a resource path (relative to WEB-INF/classes).
*
* @throws IllegalArgumentException
* If the path fails to locate a file or a resource.
*/
public static InputStream getConfigurationInputStream(String configPath) {
InputStream inStream = null;
private static InputStream getConfigurationInputStream(String configPath) {
InputStream inStream = ConfigurationProperties.class.getClassLoader()
.getResourceAsStream(configPath);
// Try to find this as a file.
File file = new File(configPath);
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
inStream = null;
if (inStream != null) {
return inStream;
}
// If no file, try to find it as a resource.
if (inStream == null) {
inStream = ConfigurationProperties.class.getClassLoader()
.getResourceAsStream(configPath);
}
// If neither file nor resource, give up.
if (inStream == null) {
throw new IllegalArgumentException(
"Failed to find a configuration properties file at '"
+ file.getAbsolutePath() + "', or a resource at '"
+ configPath + "'");
}
return inStream;
throw new IllegalArgumentException(
"Failed to find a configuration properties resource at '"
+ configPath + "'");
}
}

View file

@ -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());
}
}

View file

@ -1,4 +1,4 @@
#
# This is a data file for ConfigurationPropertiesTest.
#
source = resource
whichfile = test_config

View file

@ -0,0 +1,4 @@
#
# This is a data file for ConfigurationPropertiesTest.
#
whichfile = test_config_default

View file

@ -0,0 +1,5 @@
#
# This is a data file for ConfigurationPropertiesTest.
#
whichfile = test_config_invalid
source = bad Unicode constant \uu1045