Skip to content

Commit

Permalink
Fixing resource loading when resources are in a directory instead of …
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,15 @@ private void loadResourceContent(String resource, String pack) {
logger.finest( "Loading resource: " + entryName );

JclJarEntry entry = new JclJarEntry();
entry.setBaseUrl(resource);
File parentFile = resourceFile.getAbsoluteFile().getParentFile();
if (parentFile == null) {
// I don't believe this is actually possible with an absolute path. With no parent, we must be at the root of the filesystem.
entry.setBaseUrl("file:/");
} else {
entry.setBaseUrl(parentFile.toURI().toString());
}
entry.setResourceBytes(content);

jarEntryContents.put( entryName, entry );
}
} catch (IOException e) {
Expand Down
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 added JCL2/core/src/test/resources/test.jar
Binary file not shown.

0 comments on commit 1afd4f9

Please sign in to comment.