Fixing test regression from c473da3 by allowing a different resource classloader to be supplied in testing

This commit is contained in:
Andrew Phillips 2012-01-16 22:34:33 -05:00
parent d9b04c3cc4
commit a55081bcc6
2 changed files with 15 additions and 15 deletions

View File

@ -59,13 +59,18 @@ public class JcloudsVersion {
@VisibleForTesting @VisibleForTesting
JcloudsVersion() { JcloudsVersion() {
this(readVersionPropertyFromClasspath()); this(JcloudsVersion.class.getClassLoader());
} }
private static String readVersionPropertyFromClasspath() { @VisibleForTesting
JcloudsVersion(ClassLoader resourceLoader) {
this(readVersionPropertyFromClasspath(resourceLoader));
}
private static String readVersionPropertyFromClasspath(ClassLoader resourceLoader) {
Properties versionProperties = new Properties(); Properties versionProperties = new Properties();
try { try {
versionProperties.load(checkNotNull(JcloudsVersion.class.getClassLoader().getResourceAsStream(VERSION_RESOURCE_FILE), VERSION_RESOURCE_FILE)); versionProperties.load(checkNotNull(resourceLoader.getResourceAsStream(VERSION_RESOURCE_FILE), VERSION_RESOURCE_FILE));
} catch (IOException exception) { } catch (IOException exception) {
throw new IllegalStateException(format("Unable to load version resource file '%s'", VERSION_RESOURCE_FILE), exception); throw new IllegalStateException(format("Unable to load version resource file '%s'", VERSION_RESOURCE_FILE), exception);
} }

View File

@ -19,7 +19,10 @@
package org.jclouds; package org.jclouds;
import static org.jclouds.JcloudsVersion.VERSION_RESOURCE_FILE; import static org.jclouds.JcloudsVersion.VERSION_RESOURCE_FILE;
import static org.testng.Assert.*; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -35,18 +38,10 @@ import com.google.common.collect.Iterables;
@Test(singleThreaded = true) @Test(singleThreaded = true)
public class JcloudsVersionTest { public class JcloudsVersionTest {
@Test @Test(expectedExceptions = { NullPointerException.class })
public void testFailsIfResourceFileMissing() { public void testFailsIfResourceFileMissing() {
ClassLoader original = Thread.currentThread().getContextClassLoader(); new JcloudsVersion(new ResourceHidingClassLoader(JcloudsVersion.class.getClassLoader(),
Thread.currentThread().setContextClassLoader( VERSION_RESOURCE_FILE));
new ResourceHidingClassLoader(original, VERSION_RESOURCE_FILE));
try {
new JcloudsVersion();
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
} finally {
Thread.currentThread().setContextClassLoader(original);
}
} }
@Test(expectedExceptions = { IllegalArgumentException.class }) @Test(expectedExceptions = { IllegalArgumentException.class })