Skip to content

Instantly share code, notes, and snippets.

@jca02266
Created December 4, 2016 05:25
Show Gist options
  • Save jca02266/7bd5ec5348ad1506c32d4ff90610cd47 to your computer and use it in GitHub Desktop.
Save jca02266/7bd5ec5348ad1506c32d4ff90610cd47 to your computer and use it in GitHub Desktop.
package sample;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;
public class ResourceUtils {
private static String path(String path) {
return path.replace('\\', '/'); // convert to UNIX path
}
public static List<String> findResources(String pattern) {
return findResources(Pattern.compile(pattern));
}
public static List<String> findResources(Pattern pattern) {
List<String> list = new ArrayList<String>();
Enumeration<URL> urls = null;
try {
urls = ResourceUtils.class.getClassLoader().getResources("");
} catch (IOException e) {
return list;
}
while (urls.hasMoreElements()) {
findResourcesRecursive(list, urls.nextElement().getPath(), pattern);
}
return list;
}
private static void findResourcesRecursive(List<String> list, String path, Pattern pattern) {
File file = new File(path);
if (file.isDirectory()) {
for (File file2: file.listFiles()) {
findResourcesRecursive(list, file2.getPath(), pattern);
}
}
else {
if (pattern.matcher(file.getName()).find()) {
list.add(ResourceUtils.path(path));
}
}
}
public static String getResourcePath(String path) {
URL url = ResourceUtils.class.getClass().getResource(path);
return ResourceUtils.path(url.getPath());
}
}
package sample;
import org.junit.Test;
import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.*;
public class ResourceUtilsTest {
@Test
public void testGetResources() throws Exception {
Enumeration<URL> urls;
urls = this.getClass().getClassLoader().getResources("");
assertThat(urls.hasMoreElements(), is(true));
assertThat(urls.nextElement().toString(), is("file:/C:/workspace/poi/build/classes/test/"));
assertThat(urls.nextElement().toString(), is("file:/C:/workspace/poi/build/classes/main/"));
assertThat(urls.nextElement().toString(), is("file:/C:/workspace/poi/build/resources/test/"));
assertThat(urls.nextElement().toString(), is("file:/C:/workspace/poi/build/resources/main/"));
assertThat(urls.hasMoreElements(), is(false));
urls = this.getClass().getClassLoader().getResources("foobar");
assertThat(urls.hasMoreElements(), is(false));
urls = this.getClass().getClassLoader().getResources("sample.xlsx");
assertThat(urls.hasMoreElements(), is(true));
}
@Test
public void testClassGetResources() throws Exception {
URL url;
url = this.getClass().getResource("/");
assertThat(url.toString(), is("file:/C:/workspace/poi/build/classes/test/"));
url = this.getClass().getResource("sample.xlsx");
assertThat(url, is(nullValue()));
url = this.getClass().getResource("/sample.xlsx");
assertThat(url.toString(), is("file:/C:/workspace/poi/build/resources/test/sample.xlsx"));
}
@Test
public void testClassLoaderGetResources() throws Exception {
URL url;
url = this.getClass().getClassLoader().getResource("/");
assertThat(url, is(nullValue()));
url = this.getClass().getClassLoader().getResource("sample.xlsx");
assertThat(url.toString(), is("file:/C:/workspace/poi/build/resources/test/sample.xlsx"));
url = this.getClass().getResource("/sample.xlsx");
assertThat(url.toString(), is("file:/C:/workspace/poi/build/resources/test/sample.xlsx"));
}
@Test
public void testFindResources() throws Exception {
List<String> list = ResourceUtils.findResources("sample.*\\.xlsx");
assertThat(list.size(), is(3));
assertThat(list.get(0), is("C:/workspace/poi/build/resources/test/sample.xlsx"));
assertThat(list.get(1), is("C:/workspace/poi/build/resources/test/sampleRow.xlsx"));
assertThat(list.get(2), is("C:/workspace/poi/build/resources/main/sample2.xlsx"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment