diff --git a/core/src/main/java/org/jclouds/JcloudsVersion.java b/core/src/main/java/org/jclouds/JcloudsVersion.java index f3019a231b..81422b9620 100644 --- a/core/src/main/java/org/jclouds/JcloudsVersion.java +++ b/core/src/main/java/org/jclouds/JcloudsVersion.java @@ -60,13 +60,18 @@ public class JcloudsVersion { @VisibleForTesting 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(); 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) { throw new IllegalStateException(format("Unable to load version resource file '%s'", VERSION_RESOURCE_FILE), exception); } diff --git a/core/src/test/java/org/jclouds/JcloudsVersionTest.java b/core/src/test/java/org/jclouds/JcloudsVersionTest.java index 99acf27984..18b8d5d622 100644 --- a/core/src/test/java/org/jclouds/JcloudsVersionTest.java +++ b/core/src/test/java/org/jclouds/JcloudsVersionTest.java @@ -19,7 +19,10 @@ package org.jclouds; 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.util.List; @@ -35,18 +38,10 @@ import com.google.common.collect.Iterables; @Test(singleThreaded = true) public class JcloudsVersionTest { - @Test + @Test(expectedExceptions = { NullPointerException.class }) public void testFailsIfResourceFileMissing() { - ClassLoader original = Thread.currentThread().getContextClassLoader(); - Thread.currentThread().setContextClassLoader( - new ResourceHidingClassLoader(original, VERSION_RESOURCE_FILE)); - try { - new JcloudsVersion(); - fail("Expected NullPointerException"); - } catch (NullPointerException expected) { - } finally { - Thread.currentThread().setContextClassLoader(original); - } + new JcloudsVersion(new ResourceHidingClassLoader(JcloudsVersion.class.getClassLoader(), + VERSION_RESOURCE_FILE)); } @Test(expectedExceptions = { IllegalArgumentException.class })