Skip to content

Commit

Permalink
closes InputStream on parsing
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
ivangsa committed Nov 15, 2023
1 parent 06699f3 commit 5e13ff2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/main/java/io/zenwave360/jsonrefparser/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,27 @@ public static void withResourceClassLoader(ClassLoader resourceClassLoader) {

public static ExtendedJsonContext parse(URI uri) throws IOException {
if("classpath".contentEquals(uri.getScheme())) {
return parse(resourceClassLoader.getResourceAsStream(uri.getPath().replaceFirst("^/", "")), uri);
try(var inputStream = resourceClassLoader.getResourceAsStream(uri.getPath().replaceFirst("^/", ""))) {
return parse(inputStream, uri);
}
}
// TODO: it does not support yet parsing http/https files directly
return parse(new FileInputStream(new File(uri)), uri);
try(var inputStream = new FileInputStream(new File(uri))) {
return parse(inputStream, uri);
}
}

public static ExtendedJsonContext parse(File file) throws IOException {
return parse(new FileInputStream(file), file);
try(var inputStream = new FileInputStream(file)) {
return parse(inputStream, file);
}
}

public static ExtendedJsonContext parse(String content) {
try {
return parse(new ByteArrayInputStream(content.getBytes()), "string");
try(var inputStream = new ByteArrayInputStream(content.getBytes())) {
return parse(inputStream, "string");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/io/zenwave360/jsonrefparser/RefParserTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.zenwave360.jsonrefparser;


import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Map;

/**
* Test that files are not locked after parsing.
*/
public class RefParserTests {


private static final Logger log = LoggerFactory.getLogger(RefParserTests.class.getName());

@Test
public void testParserWithUri() throws Exception {
Files.copy(Path.of("src/test/resources/indexed-array.yml"), Path.of("target/indexed-array.yml"), StandardCopyOption.REPLACE_EXISTING);
Map<String, Object> parsed = new $RefParser(Path.of("target/indexed-array.yml").toUri()).parse().getRefs().schema();
Files.delete(Path.of("target/indexed-array.yml"));
}

@Test
public void testParserWithFile() throws Exception {
Files.copy(Path.of("src/test/resources/indexed-array.yml"), Path.of("target/indexed-array.yml"), StandardCopyOption.REPLACE_EXISTING);
Map<String, Object> parsed = new $RefParser(new File("target/indexed-array.yml")).parse().getRefs().schema();
Files.delete(Path.of("target/indexed-array.yml"));
}

}

0 comments on commit 5e13ff2

Please sign in to comment.