Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;

/**
* Static helper methods for loading {@code Stream}-backed resources.
Expand Down Expand Up @@ -120,7 +121,6 @@ public static boolean resourceExists(String resourcePath) {
* @throws IOException if there is a problem acquiring the resource at the specified path.
*/
public static InputStream getInputStreamForPath(String resourcePath) throws IOException {

URL url = getURLForPath(resourcePath);
if (url == null) {
throw new IOException("Resource [" + resourcePath + "] could not be found.");
Expand All @@ -135,7 +135,7 @@ public static InputStream getInputStreamForPath(String resourcePath) throws IOEx
* ({@link #CLASSPATH_PREFIX CLASSPATH_PREFIX},
* {@link #URL_PREFIX URL_PREFIX}, or {@link #FILE_PREFIX FILE_PREFIX}). If the path is not prefixed by one
* of these schemes, the path is assumed to be a file-based path that can be loaded with a
* call to {@link URI#create(String)}.
* call to {@link Paths#get(String, String...)}.
*
* @param resourcePath the String path representing the resource to obtain.
* @return the URL for the specified resource.
Expand All @@ -148,8 +148,10 @@ public static URL getURLForPath(String resourcePath) throws IOException {
url = ClassUtils.getResource(stripPrefix(resourcePath));
} else if (resourcePath.startsWith(URL_PREFIX)) {
url = URI.create(stripPrefix(resourcePath)).toURL();
} else if (resourcePath.startsWith(FILE_PREFIX)) {
url = Paths.get(stripPrefix(resourcePath)).toUri().toURL();
} else {
Comment thread
lprimak marked this conversation as resolved.
url = URI.create(resourcePath).toURL();
url = Paths.get(resourcePath).toUri().toURL();
}
Comment thread
lprimak marked this conversation as resolved.

if (url == null) {
Expand Down
18 changes: 16 additions & 2 deletions lang/src/test/java/org/apache/shiro/lang/util/ClassUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import static org.apache.shiro.lang.io.ResourceUtils.getInputStreamForPath;
import static org.assertj.core.api.Assertions.assertThat;

class ClassUtilsTest {

@Test
void testGetPrimitiveClasses() throws UnknownClassException {

assertThat(ClassUtils.forName("boolean")).isEqualTo(boolean.class);
assertThat(ClassUtils.forName("byte")).isEqualTo(byte.class);
assertThat(ClassUtils.forName("char")).isEqualTo(char.class);
Expand Down Expand Up @@ -84,4 +85,17 @@ void testGetClass() {
assertThat(ClassUtils.forName(ClassUtilsTest.class.getName())).isEqualTo(ClassUtilsTest.class);
assertThat(ClassUtils.forName(ClassUtilsTest[].class.getName())).isEqualTo(ClassUtilsTest[].class);
}

@Test
void inputStreamFileLoading() throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW If a test can throw a checked exception, I usually just go for declaring throwing of the Exception base class. That way I won't get extra clutter if methods needs to throw more checked exception.

try (InputStream is = getInputStreamForPath("classpath:org/apache/shiro/lang/util/ClassUtilsTest.class")) {
assertThat(is.readAllBytes()).isNotEmpty();
}
try (InputStream is = getInputStreamForPath("target/test-classes/test-data/file.json")) {
assertThat(is.readAllBytes()).isNotEmpty();
}
try (InputStream is = getInputStreamForPath("file:target/test-classes/test-data/file.json")) {
assertThat(is.readAllBytes()).isNotEmpty();
}
Comment thread
lprimak marked this conversation as resolved.
}
}
4 changes: 4 additions & 0 deletions lang/src/test/resources/test-data/file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 1,
"name": "example"
}
Loading