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; 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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Collections; import java.util.Collections;
@ -44,6 +41,12 @@ public class ConfigurationProperties {
private static final Log log = LogFactory private static final Log log = LogFactory
.getLog(ConfigurationProperties.class); .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. * 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 * @throws IllegalStateException
* If we can't find the path. * If we can't find the path.
*/ */
private static String getConfigurationFilePath() { private static String getConfigurationFilePath() {
String message = "";
try { try {
message = "JNDI Lookup on \"" + JNDI_BASE
+ "\" failed. Is the context file missing?";
Context envCtx = (Context) new InitialContext().lookup(JNDI_BASE); Context envCtx = (Context) new InitialContext().lookup(JNDI_BASE);
if (envCtx == null) { if (envCtx == null) {
log.error(message); log.warn("JNDI Lookup on \"" + JNDI_BASE
throw new IllegalStateException(message); + "\" failed. Is the context file missing?");
return DEFAULT_CONFIG_PATH;
} }
// Get the name of the configuration properties file. // 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); String configPath = (String) envCtx.lookup(PATH_CONFIGURATION);
if (configPath == null) { if (configPath == null) {
log.error(message); log.warn("Could not find a JNDI Environment naming for '"
throw new IllegalStateException(message); + PATH_CONFIGURATION
+ "'. Is the context file set up correctly?");
return DEFAULT_CONFIG_PATH;
} }
return configPath; return configPath;
} catch (NamingException e) { } 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. * Find the Configuration properties file.
* *
* First try to interpret the path as a file path (like * Interpret the path as a resource path (relative to WEB-INF/classes).
* /usr/local/config.props).
*
* If that doesn't work, try it as a resource path (relative to
* WEB-INF/classes).
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* If the path fails to locate a file or a resource. * If the path fails to locate a file or a resource.
*/ */
public static InputStream getConfigurationInputStream(String configPath) { private static InputStream getConfigurationInputStream(String configPath) {
InputStream inStream = null; InputStream inStream = ConfigurationProperties.class.getClassLoader()
.getResourceAsStream(configPath);
// Try to find this as a file. if (inStream != null) {
File file = new File(configPath); return inStream;
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
inStream = null;
} }
// If no file, try to find it as a resource. throw new IllegalArgumentException(
if (inStream == null) { "Failed to find a configuration properties resource at '"
inStream = ConfigurationProperties.class.getClassLoader() + configPath + "'");
.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;
} }
} }

View file

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

View file

@ -1,4 +1,4 @@
# #
# This is a data file for ConfigurationPropertiesTest. # 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