Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from xlate/schema_include_classpath
Allow loading included schemas on the class-path
- Loading branch information
Showing
5 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/main/java/io/xlate/edi/internal/schema/ClasspathURLStreamHandler.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,28 @@ | ||
package io.xlate.edi.internal.schema; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.net.URLStreamHandler; | ||
|
||
class ClasspathURLStreamHandler extends URLStreamHandler { | ||
/** The classloader to find resources from. */ | ||
private final ClassLoader classLoader; | ||
|
||
public ClasspathURLStreamHandler(ClassLoader classLoader) { | ||
this.classLoader = classLoader; | ||
} | ||
|
||
@Override | ||
protected URLConnection openConnection(URL u) throws IOException { | ||
final String resourcePath = u.getPath(); | ||
final URL resourceUrl = classLoader.getResource(resourcePath); | ||
|
||
if (resourceUrl == null) { | ||
throw new FileNotFoundException("Class-path resource not found: " + resourcePath); | ||
} | ||
|
||
return resourceUrl.openConnection(); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
src/test/java/io/xlate/edi/internal/schema/ClasspathURLStreamHandlerTest.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,40 @@ | ||
package io.xlate.edi.internal.schema; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ClasspathURLStreamHandlerTest { | ||
|
||
ClasspathURLStreamHandler cut; | ||
|
||
@BeforeEach | ||
void setup() { | ||
cut = new ClasspathURLStreamHandler(getClass().getClassLoader()); | ||
} | ||
|
||
@Test | ||
void testResourceUrlResolvesNull() throws MalformedURLException { | ||
URL target = new URL(null, "classpath:does/not/exist.txt", cut); | ||
@SuppressWarnings("unused") | ||
FileNotFoundException ex = assertThrows(FileNotFoundException.class, () -> { | ||
target.openConnection(); | ||
}); | ||
} | ||
|
||
@Test | ||
void testResourceUrlFound() throws IOException { | ||
URL target = new URL(null, "classpath:logging.properties", cut); | ||
URLConnection connection = target.openConnection(); | ||
assertNotNull(connection); | ||
} | ||
|
||
} |
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
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