forked from kamranzafar/JCL
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing resource loading when resources are in a directory instead of …
…inside a jar file Includes some simple tests, and a simple test jar file (with one property file in it) to ensure that the normal case still works.
- Loading branch information
Matt Hoffman
committed
Nov 11, 2015
1 parent
a29489d
commit 1afd4f9
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
JCL2/core/src/test/java/org/xeustechnologies/jcl/ClasspathResourcesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.xeustechnologies.jcl; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.Properties; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Test handling resources inside and outside jars | ||
*/ | ||
public class ClasspathResourcesTest { | ||
|
||
@Test | ||
public void testLoadResource() throws Exception { | ||
final String name = "test"; | ||
ClasspathResources jarResources = getClasspathResources(name); | ||
|
||
URL resourceURL = jarResources.getResourceURL("test.properties"); | ||
Properties props = getProperties(resourceURL); | ||
assertEquals("testval", props.getProperty("testkey")); | ||
} | ||
|
||
@Test | ||
public void testLoadResourcesFromJar() throws Exception { | ||
final String name = "test.jar"; | ||
ClasspathResources jarResources = getClasspathResources(name); | ||
|
||
URL resourceURL = jarResources.getResourceURL("test.properties"); | ||
Properties props = getProperties(resourceURL); | ||
assertEquals("testval in jar", props.getProperty("testkey")); | ||
|
||
resourceURL = jarResources.getResourceURL("test/subdir.properties"); | ||
props = getProperties(resourceURL); | ||
assertEquals("testval in jar in subdirectory", props.getProperty("testkey")); | ||
} | ||
|
||
private ClasspathResources getClasspathResources(String name) { | ||
final URL testJar = ClassLoader.getSystemClassLoader().getResource(name); | ||
assertNotNull("Could not find file or directory named '" + name + "'. It should be in the test resources directory", testJar); | ||
ClasspathResources jarResources = new ClasspathResources(); | ||
jarResources.loadResource(testJar); | ||
return jarResources; | ||
} | ||
|
||
private Properties getProperties(URL resourceURL) throws IOException { | ||
assertNotNull(resourceURL); | ||
Properties props = new Properties(); | ||
props.load(resourceURL.openStream()); | ||
return props; | ||
} | ||
} |
Binary file not shown.