From 134b268ac5bc8ee69d48854e49c4d227b3fc44cb Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 07:43:09 -0500 Subject: [PATCH 01/14] Remove string input resource --- .../test/script/ScriptExecutorTest.java | 15 ++--- .../ioresource/StringInputResource.java | 56 ------------------ .../ioresource/StringInputResourceTest.java | 57 ------------------- 3 files changed, 4 insertions(+), 124 deletions(-) delete mode 100644 schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/StringInputResource.java delete mode 100644 schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/StringInputResourceTest.java diff --git a/schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptExecutorTest.java b/schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptExecutorTest.java index 580e12396f..193e0cddb3 100644 --- a/schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptExecutorTest.java +++ b/schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptExecutorTest.java @@ -28,21 +28,17 @@ package schemacrawler.test.script; -import static java.nio.charset.StandardCharsets.UTF_8; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; - +import java.io.StringReader; import java.io.StringWriter; import java.util.Collections; import java.util.HashMap; import java.util.Map; - import org.junit.jupiter.api.Test; - import schemacrawler.tools.command.script.GraalJSScriptExecutor; import schemacrawler.tools.command.script.ScriptEngineExecutor; import schemacrawler.tools.command.script.ScriptExecutor; -import us.fatehi.utility.ioresource.StringInputResource; public class ScriptExecutorTest { @@ -56,9 +52,8 @@ public void graal() throws Exception { scriptExecutor.initialize( context, - new StringInputResource( - "if (javaObj instanceof Java.type('java.lang.Object')) { print(\"Hello, World!\"); }") - .openNewInputReader(UTF_8), + new StringReader( + "if (javaObj instanceof Java.type('java.lang.Object')) { print(\"Hello, World!\"); }"), writer); assertThat(scriptExecutor.canGenerate(), is(true)); @@ -79,9 +74,7 @@ public void scriptEngine() throws Exception { final StringWriter writer = new StringWriter(); final ScriptExecutor scriptExecutor = new ScriptEngineExecutor("python"); scriptExecutor.initialize( - Collections.emptyMap(), - new StringInputResource("print(\"Hello, World!\")").openNewInputReader(UTF_8), - writer); + Collections.emptyMap(), new StringReader("print(\"Hello, World!\")"), writer); assertThat(scriptExecutor.canGenerate(), is(true)); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/StringInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/StringInputResource.java deleted file mode 100644 index 5d29c477b7..0000000000 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/StringInputResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* -======================================================================== -SchemaCrawler -http://www.schemacrawler.com -Copyright (c) 2000-2025, Sualeh Fatehi . -All rights reserved. ------------------------------------------------------------------------- - -SchemaCrawler is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -SchemaCrawler and the accompanying materials are made available under -the terms of the Eclipse Public License v1.0, GNU General Public License -v3 or GNU Lesser General Public License v3. - -You may elect to redistribute this code under any of these licenses. - -The Eclipse Public License is available at: -http://www.eclipse.org/legal/epl-v10.html - -The GNU General Public License v3 and the GNU Lesser General Public -License v3 are available at: -http://www.gnu.org/licenses/ - -======================================================================== -*/ - -package us.fatehi.utility.ioresource; - -import static java.util.Objects.requireNonNull; - -import java.io.Reader; -import java.io.StringReader; -import java.nio.charset.Charset; -import java.util.Objects; - -public class StringInputResource implements InputResource { - - private final String data; - - public StringInputResource(final String data) { - this.data = Objects.toString(data, ""); - } - - @Override - public Reader openNewInputReader(final Charset charset) { - requireNonNull(charset, "No input charset provided"); - return new StringReader(data); - } - - @Override - public String toString() { - return ""; - } -} diff --git a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/StringInputResourceTest.java b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/StringInputResourceTest.java deleted file mode 100644 index fe11b79ea3..0000000000 --- a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/StringInputResourceTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* -======================================================================== -SchemaCrawler -http://www.schemacrawler.com -Copyright (c) 2000-2025, Sualeh Fatehi . -All rights reserved. ------------------------------------------------------------------------- - -SchemaCrawler is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -SchemaCrawler and the accompanying materials are made available under -the terms of the Eclipse Public License v1.0, GNU General Public License -v3 or GNU Lesser General Public License v3. - -You may elect to redistribute this code under any of these licenses. - -The Eclipse Public License is available at: -http://www.eclipse.org/legal/epl-v10.html - -The GNU General Public License v3 and the GNU Lesser General Public -License v3 are available at: -http://www.gnu.org/licenses/ - -======================================================================== -*/ - -package us.fatehi.utility.test.ioresource; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.startsWith; -import static us.fatehi.utility.IOUtility.readFully; - -import java.io.IOException; - -import org.junit.jupiter.api.Test; - -import us.fatehi.utility.ioresource.StringInputResource; - -public class StringInputResourceTest { - - @Test - public void happyPath() throws IOException { - final StringInputResource resource = new StringInputResource("hello, world"); - assertThat("Description does not match", resource.getDescription(), is("")); - assertThat("toString() does not match", resource.toString(), is("")); - assertThat(readFully(resource.openNewInputReader(UTF_8)), startsWith("hello, world")); - } - - @Test - public void nullArgs() { - assertThat(readFully(new StringInputResource(null).openNewInputReader(UTF_8)), is("")); - } -} From 75b7bc217c958301dd8561ef786459776878586d Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 07:43:58 -0500 Subject: [PATCH 02/14] Remove reader input resource --- .../ioresource/ReaderInputResource.java | 59 --------------- .../ioresource/ReaderInputResourceTest.java | 75 ------------------- 2 files changed, 134 deletions(-) delete mode 100644 schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ReaderInputResource.java delete mode 100644 schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ReaderInputResourceTest.java diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ReaderInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ReaderInputResource.java deleted file mode 100644 index a929000fb7..0000000000 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ReaderInputResource.java +++ /dev/null @@ -1,59 +0,0 @@ -/* -======================================================================== -SchemaCrawler -http://www.schemacrawler.com -Copyright (c) 2000-2025, Sualeh Fatehi . -All rights reserved. ------------------------------------------------------------------------- - -SchemaCrawler is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -SchemaCrawler and the accompanying materials are made available under -the terms of the Eclipse Public License v1.0, GNU General Public License -v3 or GNU Lesser General Public License v3. - -You may elect to redistribute this code under any of these licenses. - -The Eclipse Public License is available at: -http://www.eclipse.org/legal/epl-v10.html - -The GNU General Public License v3 and the GNU Lesser General Public -License v3 are available at: -http://www.gnu.org/licenses/ - -======================================================================== -*/ - -package us.fatehi.utility.ioresource; - -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapReader; -import java.io.BufferedReader; -import java.io.Reader; -import java.nio.charset.Charset; -import java.util.logging.Level; -import java.util.logging.Logger; -import static java.util.Objects.requireNonNull; - -public class ReaderInputResource implements InputResource { - - private static final Logger LOGGER = Logger.getLogger(ReaderInputResource.class.getName()); - - private final Reader reader; - - public ReaderInputResource(final Reader reader) { - this.reader = requireNonNull(reader, "No reader provided"); - } - - @Override - public Reader openNewInputReader(final Charset charset) { - LOGGER.log(Level.FINE, "Input to provided reader"); - return wrapReader(getDescription(), new BufferedReader(reader), false); - } - - @Override - public String toString() { - return ""; - } -} diff --git a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ReaderInputResourceTest.java b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ReaderInputResourceTest.java deleted file mode 100644 index 2212f73cd7..0000000000 --- a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ReaderInputResourceTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* -======================================================================== -SchemaCrawler -http://www.schemacrawler.com -Copyright (c) 2000-2025, Sualeh Fatehi . -All rights reserved. ------------------------------------------------------------------------- - -SchemaCrawler is distributed in the hope that it will be useful, but -WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - -SchemaCrawler and the accompanying materials are made available under -the terms of the Eclipse Public License v1.0, GNU General Public License -v3 or GNU Lesser General Public License v3. - -You may elect to redistribute this code under any of these licenses. - -The Eclipse Public License is available at: -http://www.eclipse.org/legal/epl-v10.html - -The GNU General Public License v3 and the GNU Lesser General Public -License v3 are available at: -http://www.gnu.org/licenses/ - -======================================================================== -*/ - -package us.fatehi.utility.test.ioresource; - -import static java.nio.charset.StandardCharsets.UTF_8; -import static java.nio.file.Files.newBufferedReader; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static us.fatehi.utility.IOUtility.readFully; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - -import org.junit.jupiter.api.Test; - -import us.fatehi.utility.ioresource.ReaderInputResource; - -public class ReaderInputResourceTest { - - @Test - public void badArgs() { - assertThrows( - IOException.class, - () -> { - final Path noResource = Paths.get("no_resource"); - new ReaderInputResource(newBufferedReader(noResource)); - }); - } - - @Test - public void happyPath() throws IOException { - final Path fileResource = Files.createTempFile("sc", ".txt"); - Files.write(fileResource, "hello, world".getBytes(UTF_8)); - - final ReaderInputResource resource = new ReaderInputResource(newBufferedReader(fileResource)); - assertThat("Description does not match", resource.getDescription(), is("")); - assertThat("toString() does not match", resource.toString(), is("")); - assertThat(readFully(resource.openNewInputReader(UTF_8)), startsWith("hello, world")); - } - - @Test - public void nullArgs() { - assertThrows(NullPointerException.class, () -> new ReaderInputResource(null)); - } -} From a59c7bdd5545349abfcccf97578d7f7075e78a28 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 07:50:08 -0500 Subject: [PATCH 03/14] Remove reader wrap --- .../ioresource/ClasspathInputResource.java | 3 +-- .../utility/ioresource/FileInputResource.java | 3 +-- .../ioresource/InputResourceUtility.java | 24 ++----------------- 3 files changed, 4 insertions(+), 26 deletions(-) diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java index 9080ceef30..42b0c059ef 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java @@ -28,7 +28,6 @@ package us.fatehi.utility.ioresource; -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapReader; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; @@ -78,7 +77,7 @@ public Reader openNewInputReader(final Charset charset) { final Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); LOGGER.log(Level.FINE, "Opened input reader to classpath resource, " + classpathResource); - return wrapReader(getDescription(), reader, true); + return reader; } catch (final IOException e) { LOGGER.log(Level.FINE, "Could not read classpath resource", e); throw new NullPointerException( diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java index cb190cd2d4..5b636de297 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java @@ -31,7 +31,6 @@ import static java.nio.file.Files.exists; import static java.nio.file.Files.newBufferedReader; import static us.fatehi.utility.IOUtility.isFileReadable; -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapReader; import java.io.IOException; import java.io.Reader; import java.io.StringReader; @@ -72,7 +71,7 @@ public Reader openNewInputReader(final Charset charset) throws IOException { final Reader reader = newBufferedReader(inputFile, charset); LOGGER.log(Level.FINE, new StringFormat("Opened input reader to file <%s>", inputFile)); - return wrapReader(getDescription(), reader, true); + return reader; } @Override diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java index 0bbd548e05..0144f2fc62 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java @@ -28,18 +28,16 @@ package us.fatehi.utility.ioresource; -import static java.util.Objects.requireNonNull; -import static us.fatehi.utility.Utility.isBlank; -import java.io.FilterReader; import java.io.FilterWriter; import java.io.IOException; -import java.io.Reader; import java.io.Writer; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; +import static java.util.Objects.requireNonNull; +import static us.fatehi.utility.Utility.isBlank; import us.fatehi.utility.string.StringFormat; public class InputResourceUtility { @@ -82,24 +80,6 @@ public static Optional createInputResource(final String inputReso return Optional.ofNullable(inputResource); } - public static Reader wrapReader( - final String description, final Reader reader, final boolean shouldClose) { - requireNonNull(reader, "No reader provided"); - return new FilterReader(reader) { - @Override - public void close() throws IOException { - if (shouldClose) { - super.close(); - } - } - - @Override - public String toString() { - return description; - } - }; - } - public static Writer wrapWriter( final String description, final Writer writer, final boolean shouldClose) { requireNonNull(writer, "No writer provided"); From 5c32992c70351be3eee69c3d8afe460109928bd8 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 09:05:33 -0500 Subject: [PATCH 04/14] Remove redundant buffering --- .../src/main/java/schemacrawler/Version.java | 3 +-- .../schemacrawler/IdentifiersBuilder.java | 3 +-- .../java/schemacrawler/testdb/TestSchemaCreator.java | 5 ++--- .../utility/ioresource/ClasspathInputResource.java | 5 ++--- .../fatehi/utility/ioresource/FileInputResource.java | 12 +++--------- .../us/fatehi/utility/ioresource/InputResource.java | 4 ++-- .../integration/test/DatabaseScriptsTest.java | 2 +- 7 files changed, 12 insertions(+), 22 deletions(-) diff --git a/schemacrawler-api/src/main/java/schemacrawler/Version.java b/schemacrawler-api/src/main/java/schemacrawler/Version.java index f9527f51af..0e75b68083 100644 --- a/schemacrawler-api/src/main/java/schemacrawler/Version.java +++ b/schemacrawler-api/src/main/java/schemacrawler/Version.java @@ -50,8 +50,7 @@ public final class Version extends BaseProductVersion { static { try (final BufferedReader reader = - new BufferedReader( - new ClasspathInputResource("/help/SchemaCrawler.txt").openNewInputReader(UTF_8))) { + new ClasspathInputResource("/help/SchemaCrawler.txt").openNewInputReader(UTF_8)) { final List lines = reader.lines().collect(toList()); lines.add(""); diff --git a/schemacrawler-api/src/main/java/schemacrawler/schemacrawler/IdentifiersBuilder.java b/schemacrawler-api/src/main/java/schemacrawler/schemacrawler/IdentifiersBuilder.java index a5c045b23e..7dc993391e 100644 --- a/schemacrawler-api/src/main/java/schemacrawler/schemacrawler/IdentifiersBuilder.java +++ b/schemacrawler-api/src/main/java/schemacrawler/schemacrawler/IdentifiersBuilder.java @@ -31,8 +31,7 @@ public static IdentifiersBuilder builder() { private static Collection loadSql2003ReservedWords() { final Set reservedWords = new HashSet<>(); try (final BufferedReader reader = - new BufferedReader( - new ClasspathInputResource("/sql2003_reserved_words.txt").openNewInputReader(UTF_8))) { + new ClasspathInputResource("/sql2003_reserved_words.txt").openNewInputReader(UTF_8)) { String line; while ((line = reader.readLine()) != null) { reservedWords.add(line); diff --git a/schemacrawler-testdb/src/main/java/schemacrawler/testdb/TestSchemaCreator.java b/schemacrawler-testdb/src/main/java/schemacrawler/testdb/TestSchemaCreator.java index ee290baddb..a8ef3db31c 100644 --- a/schemacrawler-testdb/src/main/java/schemacrawler/testdb/TestSchemaCreator.java +++ b/schemacrawler-testdb/src/main/java/schemacrawler/testdb/TestSchemaCreator.java @@ -69,8 +69,7 @@ public static void executeScriptLine( } try (final BufferedReader scriptReader = - new BufferedReader( - new ClasspathInputResource(scriptResource).openNewInputReader(UTF_8)); ) { + new ClasspathInputResource(scriptResource).openNewInputReader(UTF_8)) { new SqlScript(scriptReader, delimiter, connection).run(); } catch (final IOException e) { throw new SQLRuntimeException(String.format("Could not read \"%s\"", scriptResource), e); @@ -89,7 +88,7 @@ public TestSchemaCreator(final Connection connection, final String scriptsResour @Override public void run() { try (final BufferedReader reader = - new BufferedReader(new ClasspathInputResource(scriptsResource).openNewInputReader(UTF_8))) { + new ClasspathInputResource(scriptsResource).openNewInputReader(UTF_8)) { reader.lines().forEach(line -> executeScriptLine(line, connection)); } catch (final IOException e) { throw new RuntimeException(e.getMessage(), e); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java index 42b0c059ef..f8859c8c66 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java @@ -32,7 +32,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.Reader; import java.net.URL; import java.nio.charset.Charset; import java.util.logging.Level; @@ -70,11 +69,11 @@ public String getDescription() { } @Override - public Reader openNewInputReader(final Charset charset) { + public BufferedReader openNewInputReader(final Charset charset) { requireNonNull(charset, "No input charset provided"); try { final InputStream inputStream = url.openStream(); - final Reader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); + final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); LOGGER.log(Level.FINE, "Opened input reader to classpath resource, " + classpathResource); return reader; diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java index 5b636de297..2f92d00fee 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java @@ -28,12 +28,10 @@ package us.fatehi.utility.ioresource; -import static java.nio.file.Files.exists; import static java.nio.file.Files.newBufferedReader; import static us.fatehi.utility.IOUtility.isFileReadable; +import java.io.BufferedReader; import java.io.IOException; -import java.io.Reader; -import java.io.StringReader; import java.nio.charset.Charset; import java.nio.file.Path; import java.util.logging.Level; @@ -61,14 +59,10 @@ public Path getInputFile() { } @Override - public Reader openNewInputReader(final Charset charset) throws IOException { + public BufferedReader openNewInputReader(final Charset charset) throws IOException { requireNonNull(charset, "No input charset provided"); - if (!exists(inputFile)) { - return new StringReader(""); - } - - final Reader reader = newBufferedReader(inputFile, charset); + final BufferedReader reader = newBufferedReader(inputFile, charset); LOGGER.log(Level.FINE, new StringFormat("Opened input reader to file <%s>", inputFile)); return reader; diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java index fb3d843748..60d9306929 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java @@ -28,8 +28,8 @@ package us.fatehi.utility.ioresource; +import java.io.BufferedReader; import java.io.IOException; -import java.io.Reader; import java.nio.charset.Charset; @FunctionalInterface @@ -39,5 +39,5 @@ default String getDescription() { return toString(); } - Reader openNewInputReader(Charset charset) throws IOException; + BufferedReader openNewInputReader(Charset charset) throws IOException; } diff --git a/schemacrawler/src/test/java/schemacrawler/integration/test/DatabaseScriptsTest.java b/schemacrawler/src/test/java/schemacrawler/integration/test/DatabaseScriptsTest.java index 0b50d91280..54f89c2110 100644 --- a/schemacrawler/src/test/java/schemacrawler/integration/test/DatabaseScriptsTest.java +++ b/schemacrawler/src/test/java/schemacrawler/integration/test/DatabaseScriptsTest.java @@ -139,7 +139,7 @@ public void booksDatabaseScripts() throws Exception { for (final String scriptName : scripts) { final Map scriptSectionsCounts = makeScriptSectionsCounts(); try (final BufferedReader reader = - new BufferedReader(new ClasspathInputResource(scriptName).openNewInputReader(UTF_8))) { + new ClasspathInputResource(scriptName).openNewInputReader(UTF_8)) { final List lines = reader.lines().filter(line -> !isBlank(line)).collect(Collectors.toList()); assertThat(lines, is(not(empty()))); From 5f4ededd231d59940a3ae82b4b87547b1a1f1cf2 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 09:33:01 -0500 Subject: [PATCH 05/14] Create base class for input resource --- .../utility/ioresource/BaseInputResource.java | 58 +++++++++++++++++++ .../ioresource/ClasspathInputResource.java | 47 ++++----------- .../utility/ioresource/FileInputResource.java | 27 ++------- .../ClasspathInputResourceTest.java | 8 --- 4 files changed, 74 insertions(+), 66 deletions(-) create mode 100644 schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java new file mode 100644 index 0000000000..46194cfaa1 --- /dev/null +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java @@ -0,0 +1,58 @@ +/* +======================================================================== +SchemaCrawler +http://www.schemacrawler.com +Copyright (c) 2000-2025, Sualeh Fatehi . +All rights reserved. +------------------------------------------------------------------------ + +SchemaCrawler is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +SchemaCrawler and the accompanying materials are made available under +the terms of the Eclipse Public License v1.0, GNU General Public License +v3 or GNU Lesser General Public License v3. + +You may elect to redistribute this code under any of these licenses. + +The Eclipse Public License is available at: +http://www.eclipse.org/legal/epl-v10.html + +The GNU General Public License v3 and the GNU Lesser General Public +License v3 are available at: +http://www.gnu.org/licenses/ + +======================================================================== +*/ + +package us.fatehi.utility.ioresource; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.logging.Level; +import java.util.logging.Logger; +import static java.util.Objects.requireNonNull; + +abstract class BaseInputResource implements InputResource { + + private static final Logger LOGGER = Logger.getLogger(BaseInputResource.class.getName()); + + protected abstract InputStream openNewInputStream() throws IOException; + + @Override + public final BufferedReader openNewInputReader(final Charset charset) throws IOException { + requireNonNull(charset, "No input charset provided"); + + InputStream inputStream = openNewInputStream(); + requireNonNull(charset, "No input stream provided"); + + final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); + LOGGER.log(Level.FINE, String.format("Opened resource <%s> for reading", getDescription())); + + return reader; + } +} diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java index f8859c8c66..c1bcc93d3c 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java @@ -28,65 +28,38 @@ package us.fatehi.utility.ioresource; -import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URL; -import java.nio.charset.Charset; -import java.util.logging.Level; -import java.util.logging.Logger; import static java.util.Objects.requireNonNull; /** * Converts a provided classpath resource into an input resource. NOTE: Always assumes that * resources are absolute. Leading slashes are not required, but ignored if provided. */ -public class ClasspathInputResource implements InputResource { +public class ClasspathInputResource extends BaseInputResource { - private static final Logger LOGGER = Logger.getLogger(ClasspathInputResource.class.getName()); - - private final String classpathResource; private URL url; public ClasspathInputResource(final String classpathResource) throws IOException { - this.classpathResource = requireNonNull(classpathResource, "No classpath resource provided"); - url = locateResource(); + requireNonNull(classpathResource, "No classpath resource provided"); + url = locateResource(classpathResource); if (url == null) { - final IOException e = new IOException("Cannot read classpath resource, " + classpathResource); - LOGGER.log(Level.FINE, e.getMessage(), e); + final IOException e = + new IOException(String.format("Cannot read classpath resource, <%s>", classpathResource)); throw e; } } - public String getClasspathResource() { - return classpathResource; - } - - @Override - public String getDescription() { - return url.toExternalForm(); - } - @Override - public BufferedReader openNewInputReader(final Charset charset) { - requireNonNull(charset, "No input charset provided"); - try { - final InputStream inputStream = url.openStream(); - final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); - LOGGER.log(Level.FINE, "Opened input reader to classpath resource, " + classpathResource); - - return reader; - } catch (final IOException e) { - LOGGER.log(Level.FINE, "Could not read classpath resource", e); - throw new NullPointerException( - String.format("Cannot open classpath resource <%s> for reading", classpathResource)); - } + protected InputStream openNewInputStream() throws IOException { + final InputStream inputStream = url.openStream(); + return inputStream; } @Override public String toString() { - return classpathResource; + return url.toExternalForm(); } /** @@ -95,7 +68,7 @@ public String toString() { * * @return URL for the located resource, or null if not found */ - private URL locateResource() { + private static URL locateResource(final String classpathResource) { final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final String resolvedClasspathResource; if (classpathResource.startsWith("/")) { diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java index 2f92d00fee..97fd2d8b20 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java @@ -28,43 +28,28 @@ package us.fatehi.utility.ioresource; -import static java.nio.file.Files.newBufferedReader; +import static java.nio.file.Files.newInputStream; import static us.fatehi.utility.IOUtility.isFileReadable; -import java.io.BufferedReader; import java.io.IOException; -import java.nio.charset.Charset; +import java.io.InputStream; import java.nio.file.Path; -import java.util.logging.Level; -import java.util.logging.Logger; import static java.util.Objects.requireNonNull; -import us.fatehi.utility.string.StringFormat; -public class FileInputResource implements InputResource { - - private static final Logger LOGGER = Logger.getLogger(FileInputResource.class.getName()); +public class FileInputResource extends BaseInputResource { private final Path inputFile; public FileInputResource(final Path filePath) throws IOException { inputFile = requireNonNull(filePath, "No file path provided").normalize().toAbsolutePath(); if (!isFileReadable(inputFile)) { - final IOException e = new IOException("Cannot read file, " + inputFile); - LOGGER.log(Level.FINE, e.getMessage(), e); + final IOException e = new IOException(String.format("Cannot read file, <%s>", inputFile)); throw e; } } - public Path getInputFile() { - return inputFile; - } - @Override - public BufferedReader openNewInputReader(final Charset charset) throws IOException { - requireNonNull(charset, "No input charset provided"); - - final BufferedReader reader = newBufferedReader(inputFile, charset); - LOGGER.log(Level.FINE, new StringFormat("Opened input reader to file <%s>", inputFile)); - + protected InputStream openNewInputStream() throws IOException { + final InputStream reader = newInputStream(inputFile); return reader; } diff --git a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ClasspathInputResourceTest.java b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ClasspathInputResourceTest.java index 2e18b8f6a4..2615677024 100644 --- a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ClasspathInputResourceTest.java +++ b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/ClasspathInputResourceTest.java @@ -31,15 +31,11 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertThrows; import static us.fatehi.utility.IOUtility.readFully; - import java.io.IOException; - import org.junit.jupiter.api.Test; - import us.fatehi.utility.ioresource.ClasspathInputResource; public class ClasspathInputResourceTest { @@ -53,10 +49,6 @@ public void badArgs() { public void happyPath() throws IOException { final String classpathResource = "/test-resource.txt"; final ClasspathInputResource resource = new ClasspathInputResource(classpathResource); - assertThat( - "Classpath resource does not match", - resource.getClasspathResource(), - is(classpathResource)); assertThat( "Description does not match", resource.getDescription(), endsWith(classpathResource)); assertThat("toString() does not match", resource.toString(), endsWith(classpathResource)); From 9f4f2b218949a4846c9664428713cecbfd036767 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 10:01:49 -0500 Subject: [PATCH 06/14] Centralize code --- .../test/utility/TestUtility.java | 63 +++++-------------- .../utility/ioresource/BaseInputResource.java | 28 ++++++++- .../utility/ioresource/InputResource.java | 3 +- .../ioresource/FileInputResourceTest.java | 9 ++- 4 files changed, 47 insertions(+), 56 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index ffaa8e143a..18d62a7747 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -34,9 +34,7 @@ import static java.nio.file.Files.deleteIfExists; import static java.nio.file.Files.exists; import static java.nio.file.Files.move; -import static java.nio.file.Files.newBufferedReader; import static java.nio.file.Files.newBufferedWriter; -import static java.nio.file.Files.newInputStream; import static java.nio.file.Files.newOutputStream; import static java.nio.file.Files.size; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; @@ -59,7 +57,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; @@ -70,12 +67,10 @@ import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.nio.channels.WritableByteChannel; -import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.nio.file.StandardOpenOption; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; @@ -89,7 +84,6 @@ import java.util.function.Predicate; import java.util.regex.Pattern; import java.util.stream.Stream; -import java.util.zip.ZipInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.commons.io.FileUtils; @@ -107,6 +101,8 @@ import schemacrawler.schemacrawler.SchemaRetrievalOptions; import schemacrawler.schemacrawler.SchemaRetrievalOptionsBuilder; import us.fatehi.utility.IOUtility; +import us.fatehi.utility.ioresource.ClasspathInputResource; +import us.fatehi.utility.ioresource.FileInputResource; import us.fatehi.utility.ioresource.InputResource; public final class TestUtility { @@ -146,14 +142,16 @@ public static List compareOutput( final List failures = new ArrayList<>(); final boolean contentEquals; - final Reader referenceReader = readerForResource(referenceFile, isCompressed); + final Reader referenceReader = + readerForInputResource(new ClasspathInputResource(referenceFile), isCompressed); if (referenceReader == null) { failures.add("Reference file not available, " + referenceFile); contentEquals = false; } else if ("png".equals(outputFormat)) { contentEquals = true; } else { - final Reader fileReader = readerForFile(testOutputTempFile, isCompressed); + final Reader fileReader = + readerForInputResource(new FileInputResource(testOutputTempFile), isCompressed); final Predicate linesFilter = new SvgElementFilter().and(new NeuteredLinesFilter()); final Function neuterMap = new NeuteredExpressionsFilter(); contentEquals = contentEquals(referenceReader, fileReader, failures, linesFilter, neuterMap); @@ -454,48 +452,16 @@ private static String lineMiscompare(final String expectedline, final String act return lineMiscompare; } - private static Reader openNewCompressedInputReader( - final InputStream inputStream, final Charset charset) throws IOException { - final ZipInputStream zipInputStream = new ZipInputStream(inputStream); - zipInputStream.getNextEntry(); - return new InputStreamReader(zipInputStream, charset); - } - - private static Reader readerForFile(final Path testOutputTempFile) throws IOException { - return readerForFile(testOutputTempFile, false); - } - - private static Reader readerForFile(final Path testOutputTempFile, final boolean isCompressed) - throws IOException { - - final BufferedReader bufferedReader; - if (isCompressed) { - final ZipInputStream inputStream = - new ZipInputStream(newInputStream(testOutputTempFile, StandardOpenOption.READ)); - inputStream.getNextEntry(); - - bufferedReader = new BufferedReader(new InputStreamReader(inputStream, UTF_8)); - } else { - bufferedReader = newBufferedReader(testOutputTempFile, UTF_8); - } - return bufferedReader; - } - - private static Reader readerForResource(final String resource, final boolean isCompressed) - throws IOException { - final InputStream inputStream = TestUtility.class.getResourceAsStream("/" + resource); - final Reader reader; - if (inputStream != null) { - final Charset charset = UTF_8; + private static Reader readerForInputResource( + final InputResource inputResource, final boolean isCompressed) { + try { if (isCompressed) { - reader = openNewCompressedInputReader(inputStream, charset); - } else { - reader = new InputStreamReader(inputStream, charset); + return inputResource.openNewCompressedInputReader(UTF_8); } - } else { - reader = null; + return inputResource.openNewInputReader(UTF_8); + } catch (final IOException e) { + return null; } - return reader; } private static void testAborted(final String message, final Exception e) { @@ -526,7 +492,8 @@ public void warning(final SAXParseException e) throws SAXException { failures.add(e.getMessage()); } }); - builder.parse(new InputSource(readerForFile(testOutputFile))); + final Reader reader = new FileInputResource(testOutputFile).openNewInputReader(UTF_8); + builder.parse(new InputSource(reader)); } private static Path writeToTempFile(final InputStream resourceStream) throws IOException { diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java index 46194cfaa1..7faba8f309 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java @@ -35,6 +35,7 @@ import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.zip.ZipInputStream; import static java.util.Objects.requireNonNull; abstract class BaseInputResource implements InputResource { @@ -45,14 +46,37 @@ abstract class BaseInputResource implements InputResource { @Override public final BufferedReader openNewInputReader(final Charset charset) throws IOException { + return openNewInputReader(charset, false); + } + + @Override + public final BufferedReader openNewCompressedInputReader(final Charset charset) + throws IOException { + return openNewInputReader(charset, true); + } + + private BufferedReader openNewInputReader(final Charset charset, final boolean isCompressed) + throws IOException { requireNonNull(charset, "No input charset provided"); - InputStream inputStream = openNewInputStream(); - requireNonNull(charset, "No input stream provided"); + final InputStream inputStream; + if (isCompressed) { + inputStream = openNewInputStream(); + requireNonNull(charset, "No input stream provided"); + } else { + inputStream = openNewCompressedInputStream(openNewInputStream()); + } final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); LOGGER.log(Level.FINE, String.format("Opened resource <%s> for reading", getDescription())); return reader; } + + private static InputStream openNewCompressedInputStream(final InputStream inputStream) + throws IOException { + final ZipInputStream zipInputStream = new ZipInputStream(inputStream); + zipInputStream.getNextEntry(); + return zipInputStream; + } } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java index 60d9306929..7d3ba98963 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java @@ -32,7 +32,6 @@ import java.io.IOException; import java.nio.charset.Charset; -@FunctionalInterface public interface InputResource { default String getDescription() { @@ -40,4 +39,6 @@ default String getDescription() { } BufferedReader openNewInputReader(Charset charset) throws IOException; + + BufferedReader openNewCompressedInputReader(Charset charset) throws IOException; } diff --git a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/FileInputResourceTest.java b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/FileInputResourceTest.java index b61adbcb6c..e3d163a52b 100644 --- a/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/FileInputResourceTest.java +++ b/schemacrawler-utility/src/test/java/us/fatehi/utility/test/ioresource/FileInputResourceTest.java @@ -29,19 +29,17 @@ package us.fatehi.utility.test.ioresource; import static java.nio.charset.StandardCharsets.UTF_8; +import static org.hamcrest.CoreMatchers.endsWith; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertThrows; import static us.fatehi.utility.IOUtility.readFully; - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; - import org.junit.jupiter.api.Test; - import us.fatehi.utility.ioresource.FileInputResource; public class FileInputResourceTest { @@ -57,9 +55,10 @@ public void happyPath() throws IOException { Files.write(fileResource, "hello, world".getBytes(UTF_8)); final FileInputResource resource = new FileInputResource(fileResource); - assertThat("File resource does not match", resource.getInputFile(), is(fileResource)); assertThat( - "Description does not match", resource.getDescription(), is(fileResource.toString())); + "Description does not match", + resource.getDescription(), + endsWith(fileResource.getFileName().toString())); assertThat("toString() does not match", resource.toString(), is(fileResource.toString())); assertThat(readFully(resource.openNewInputReader(UTF_8)), startsWith("hello, world")); } From cf7c0aac494c39d41533200f4b700a25420253bb Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 10:51:05 -0500 Subject: [PATCH 07/14] Use GZIP for compression --- .../integration/test/LoadSnapshotTest.java | 10 ++++------ .../test/serialize/CatalogJavaSerializationTest.java | 7 +++---- .../fatehi/utility/ioresource/BaseInputResource.java | 7 +++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java index e29f27bd0f..a84c95d706 100644 --- a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java +++ b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java @@ -28,7 +28,9 @@ package schemacrawler.integration.test; +import static java.nio.file.Files.newInputStream; import static java.nio.file.Files.size; +import static java.nio.file.StandardOpenOption.READ; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.greaterThan; @@ -38,15 +40,11 @@ import static schemacrawler.test.utility.DatabaseTestUtility.schemaRetrievalOptionsDefault; import static schemacrawler.test.utility.TestUtility.failTestSetup; import static schemacrawler.tools.utility.SchemaCrawlerUtility.getCatalog; - -import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import schemacrawler.schema.Catalog; import schemacrawler.schema.Schema; import schemacrawler.schemacrawler.SchemaCrawlerOptions; @@ -64,8 +62,8 @@ public class LoadSnapshotTest { @Test public void loadSnapshot() throws Exception { - final FileInputStream inputFileStream = new FileInputStream(serializedCatalogFile.toFile()); - final JavaSerializedCatalog serializedCatalog = new JavaSerializedCatalog(inputFileStream); + final JavaSerializedCatalog serializedCatalog = + new JavaSerializedCatalog(newInputStream(serializedCatalogFile, READ)); final Catalog catalog = serializedCatalog.getCatalog(); final Schema schema = catalog.lookupSchema("PUBLIC.BOOKS").orElse(null); diff --git a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java index 952297354e..c7f9ad917a 100644 --- a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java +++ b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java @@ -28,6 +28,8 @@ package schemacrawler.test.serialize; +import static java.nio.file.Files.newInputStream; +import static java.nio.file.StandardOpenOption.READ; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; @@ -38,12 +40,9 @@ import static schemacrawler.test.utility.DatabaseTestUtility.schemaRetrievalOptionsDefault; import static schemacrawler.test.utility.TestUtility.fileHeaderOf; import static schemacrawler.tools.utility.SchemaCrawlerUtility.getCatalog; - import java.nio.file.Files; import java.nio.file.Path; - import org.junit.jupiter.api.Test; - import schemacrawler.schema.Catalog; import schemacrawler.schema.Schema; import schemacrawler.schemacrawler.SchemaCrawlerOptions; @@ -79,7 +78,7 @@ public void catalogSerializationWithJava(final DatabaseConnectionSource dataSour assertThat(fileHeaderOf(testOutputFile), is("ACED")); final JavaSerializedCatalog javaSerializedCatalogForLoad = - new JavaSerializedCatalog(Files.newInputStream(testOutputFile)); + new JavaSerializedCatalog(newInputStream(testOutputFile, READ)); final Catalog catalogDeserialized = javaSerializedCatalogForLoad.getCatalog(); final Schema schemaDeserialized = catalogDeserialized.lookupSchema("PUBLIC.BOOKS").orElse(null); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java index 7faba8f309..3066777eb0 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java @@ -35,7 +35,7 @@ import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.zip.ZipInputStream; +import java.util.zip.GZIPInputStream; import static java.util.Objects.requireNonNull; abstract class BaseInputResource implements InputResource { @@ -60,7 +60,7 @@ private BufferedReader openNewInputReader(final Charset charset, final boolean i requireNonNull(charset, "No input charset provided"); final InputStream inputStream; - if (isCompressed) { + if (!isCompressed) { inputStream = openNewInputStream(); requireNonNull(charset, "No input stream provided"); } else { @@ -75,8 +75,7 @@ private BufferedReader openNewInputReader(final Charset charset, final boolean i private static InputStream openNewCompressedInputStream(final InputStream inputStream) throws IOException { - final ZipInputStream zipInputStream = new ZipInputStream(inputStream); - zipInputStream.getNextEntry(); + final GZIPInputStream zipInputStream = new GZIPInputStream(inputStream); return zipInputStream; } } From b7e3e941442514f0a90ff6db282383d2d1228ed7 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 11:06:21 -0500 Subject: [PATCH 08/14] Remove check for compressed files --- .../test/utility/FileHasContent.java | 2 +- .../test/utility/TestUtility.java | 24 ++++--------------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/FileHasContent.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/FileHasContent.java index 25a423ec7c..d0be6630f3 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/FileHasContent.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/FileHasContent.java @@ -156,7 +156,7 @@ public boolean matches(final Object actualValue) { } else { // Check file contents final String outputFormatValue = getNonNullOutputFormatValue(); - failures = compareOutput(referenceFile, file, outputFormatValue, false); + failures = compareOutput(referenceFile, file, outputFormatValue); return failures != null && failures.isEmpty(); } } catch (final Exception e) { diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index 18d62a7747..2d07d0d0bc 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -112,23 +112,10 @@ public static void clean(final String dirname) throws Exception { buildDirectory().resolve("unit_tests_results_output").resolve(dirname).toFile()); } - public static List compareCompressedOutput( - final String referenceFile, final Path testOutputTempFile, final String outputFormat) - throws Exception { - return compareOutput(referenceFile, testOutputTempFile, outputFormat, true); - } - - public static List compareOutput( - final String referenceFile, final Path testOutputTempFile, final String outputFormat) - throws Exception { - return compareOutput(referenceFile, testOutputTempFile, outputFormat, false); - } - public static List compareOutput( final String referenceFile, final Path testOutputTempFile, - final String outputFormat, - final boolean isCompressed) + final String outputFormat) throws Exception { requireNonNull(referenceFile, "Reference file is not defined"); @@ -143,7 +130,7 @@ public static List compareOutput( final boolean contentEquals; final Reader referenceReader = - readerForInputResource(new ClasspathInputResource(referenceFile), isCompressed); + readerForInputResource(new ClasspathInputResource(referenceFile)); if (referenceReader == null) { failures.add("Reference file not available, " + referenceFile); contentEquals = false; @@ -151,7 +138,7 @@ public static List compareOutput( contentEquals = true; } else { final Reader fileReader = - readerForInputResource(new FileInputResource(testOutputTempFile), isCompressed); + readerForInputResource(new FileInputResource(testOutputTempFile)); final Predicate linesFilter = new SvgElementFilter().and(new NeuteredLinesFilter()); final Function neuterMap = new NeuteredExpressionsFilter(); contentEquals = contentEquals(referenceReader, fileReader, failures, linesFilter, neuterMap); @@ -453,11 +440,8 @@ private static String lineMiscompare(final String expectedline, final String act } private static Reader readerForInputResource( - final InputResource inputResource, final boolean isCompressed) { + final InputResource inputResource) { try { - if (isCompressed) { - return inputResource.openNewCompressedInputReader(UTF_8); - } return inputResource.openNewInputReader(UTF_8); } catch (final IOException e) { return null; From 78543982a64b420b496a752c9fdf0a0c3d940cc9 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 11:13:20 -0500 Subject: [PATCH 09/14] Remove code for reading compressed files --- .../test/utility/TestUtility.java | 10 +++---- .../utility/ioresource/BaseInputResource.java | 26 +------------------ .../utility/ioresource/InputResource.java | 3 +-- 3 files changed, 5 insertions(+), 34 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index 2d07d0d0bc..e5563c7d46 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -113,9 +113,7 @@ public static void clean(final String dirname) throws Exception { } public static List compareOutput( - final String referenceFile, - final Path testOutputTempFile, - final String outputFormat) + final String referenceFile, final Path testOutputTempFile, final String outputFormat) throws Exception { requireNonNull(referenceFile, "Reference file is not defined"); @@ -137,8 +135,7 @@ public static List compareOutput( } else if ("png".equals(outputFormat)) { contentEquals = true; } else { - final Reader fileReader = - readerForInputResource(new FileInputResource(testOutputTempFile)); + final Reader fileReader = readerForInputResource(new FileInputResource(testOutputTempFile)); final Predicate linesFilter = new SvgElementFilter().and(new NeuteredLinesFilter()); final Function neuterMap = new NeuteredExpressionsFilter(); contentEquals = contentEquals(referenceReader, fileReader, failures, linesFilter, neuterMap); @@ -439,8 +436,7 @@ private static String lineMiscompare(final String expectedline, final String act return lineMiscompare; } - private static Reader readerForInputResource( - final InputResource inputResource) { + private static Reader readerForInputResource(final InputResource inputResource) { try { return inputResource.openNewInputReader(UTF_8); } catch (final IOException e) { diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java index 3066777eb0..d044395acc 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java @@ -35,7 +35,6 @@ import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.zip.GZIPInputStream; import static java.util.Objects.requireNonNull; abstract class BaseInputResource implements InputResource { @@ -46,36 +45,13 @@ abstract class BaseInputResource implements InputResource { @Override public final BufferedReader openNewInputReader(final Charset charset) throws IOException { - return openNewInputReader(charset, false); - } - - @Override - public final BufferedReader openNewCompressedInputReader(final Charset charset) - throws IOException { - return openNewInputReader(charset, true); - } - - private BufferedReader openNewInputReader(final Charset charset, final boolean isCompressed) - throws IOException { requireNonNull(charset, "No input charset provided"); - final InputStream inputStream; - if (!isCompressed) { - inputStream = openNewInputStream(); - requireNonNull(charset, "No input stream provided"); - } else { - inputStream = openNewCompressedInputStream(openNewInputStream()); - } + final InputStream inputStream = openNewInputStream(); final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); LOGGER.log(Level.FINE, String.format("Opened resource <%s> for reading", getDescription())); return reader; } - - private static InputStream openNewCompressedInputStream(final InputStream inputStream) - throws IOException { - final GZIPInputStream zipInputStream = new GZIPInputStream(inputStream); - return zipInputStream; - } } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java index 7d3ba98963..60d9306929 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.nio.charset.Charset; +@FunctionalInterface public interface InputResource { default String getDescription() { @@ -39,6 +40,4 @@ default String getDescription() { } BufferedReader openNewInputReader(Charset charset) throws IOException; - - BufferedReader openNewCompressedInputReader(Charset charset) throws IOException; } From d9cc1a1cf28a6ff5ffcd1a60fe7c2042e48631f9 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 11:32:28 -0500 Subject: [PATCH 10/14] Find efficient way to read a file into a string --- .../test/utility/TestUtility.java | 5 +++++ .../schemacrawler/tools/lint/LintUtility.java | 21 +++---------------- .../SchemaCrawlerExecutableChainTest.java | 8 ++----- .../test/SchemaCrawlerExecutableTest.java | 10 +++------ .../java/us/fatehi/utility/IOUtility.java | 11 ---------- 5 files changed, 13 insertions(+), 42 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index e5563c7d46..c3eb6d1e86 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -304,6 +304,11 @@ public static SchemaRetrievalOptions newSchemaRetrievalOptions() throws IOExcept .toOptions(); } + public static String readFileFully(final Path filePath) throws IOException { + final byte[] bytes = Files.readAllBytes(filePath); + return new String(bytes, UTF_8); + } + public static Path savePropertiesToTempFile(final Properties properties) throws IOException { requireNonNull(properties, "No properties provided"); final Path propertiesFile = Files.createTempFile("schemacrawler", ".properties"); diff --git a/schemacrawler-lint/src/main/java/schemacrawler/tools/lint/LintUtility.java b/schemacrawler-lint/src/main/java/schemacrawler/tools/lint/LintUtility.java index a86e954657..46f9f2c6d2 100644 --- a/schemacrawler-lint/src/main/java/schemacrawler/tools/lint/LintUtility.java +++ b/schemacrawler-lint/src/main/java/schemacrawler/tools/lint/LintUtility.java @@ -28,16 +28,10 @@ package schemacrawler.tools.lint; -import static us.fatehi.utility.IOUtility.readFully; -import java.io.IOException; -import java.nio.charset.StandardCharsets; +import static us.fatehi.utility.IOUtility.readResourceFully; import java.util.List; -import java.util.logging.Level; import java.util.logging.Logger; import us.fatehi.utility.UtilityMarker; -import us.fatehi.utility.ioresource.ClasspathInputResource; -import us.fatehi.utility.ioresource.InputResource; -import us.fatehi.utility.string.StringFormat; @UtilityMarker public final class LintUtility { @@ -65,16 +59,7 @@ private LintUtility() {} */ public static String readDescription(final String linterId) { final String descriptionResource = String.format("/help/%s.txt", linterId); - try { - final InputResource inputResource = new ClasspathInputResource(descriptionResource); - final String descriptionText = - readFully(inputResource.openNewInputReader(StandardCharsets.UTF_8)); - return descriptionText; - } catch (final IOException e) { - LOGGER.log( - Level.FINE, - new StringFormat("Could not find description resource for linter {0}, at {1}", linterId)); - return ""; - } + final String descriptionText = readResourceFully(descriptionResource); + return descriptionText; } } diff --git a/schemacrawler-scripting/src/test/java/schemacrawler/test/script/SchemaCrawlerExecutableChainTest.java b/schemacrawler-scripting/src/test/java/schemacrawler/test/script/SchemaCrawlerExecutableChainTest.java index 6a5f07173c..54253d7605 100644 --- a/schemacrawler-scripting/src/test/java/schemacrawler/test/script/SchemaCrawlerExecutableChainTest.java +++ b/schemacrawler-scripting/src/test/java/schemacrawler/test/script/SchemaCrawlerExecutableChainTest.java @@ -38,16 +38,12 @@ import static schemacrawler.test.utility.ScriptTestUtility.commandLineScriptExecution; import static schemacrawler.test.utility.TestUtility.compareOutput; import static schemacrawler.test.utility.TestUtility.deleteIfPossible; +import static schemacrawler.test.utility.TestUtility.readFileFully; import static schemacrawler.test.utility.TestUtility.validateDiagram; -import static us.fatehi.utility.IOUtility.readFully; - -import java.io.FileReader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; - import org.junit.jupiter.api.Test; - import schemacrawler.schemacrawler.LimitOptionsBuilder; import schemacrawler.schemacrawler.SchemaCrawlerOptions; import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder; @@ -114,7 +110,7 @@ public void executableChain(final DatabaseConnectionSource dataSource) throws Ex executable.execute(); assertThat( - readFully(new FileReader(testOutputFile.toFile())).replaceAll("\\R", ""), + readFileFully(testOutputFile).replaceAll("\\R", ""), is("Created files \"schema.txt\" and \"schema.png\"")); final Path schemaFile = Paths.get("schema.txt"); diff --git a/schemacrawler-tools/src/test/java/schemacrawler/test/SchemaCrawlerExecutableTest.java b/schemacrawler-tools/src/test/java/schemacrawler/test/SchemaCrawlerExecutableTest.java index 3a265eb18f..40fa0ce9ba 100644 --- a/schemacrawler-tools/src/test/java/schemacrawler/test/SchemaCrawlerExecutableTest.java +++ b/schemacrawler-tools/src/test/java/schemacrawler/test/SchemaCrawlerExecutableTest.java @@ -39,14 +39,10 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static schemacrawler.test.utility.DatabaseTestUtility.schemaRetrievalOptionsDefault; +import static schemacrawler.test.utility.TestUtility.readFileFully; import static us.fatehi.utility.IOUtility.createTempFilePath; -import static us.fatehi.utility.IOUtility.readFully; - -import java.io.FileReader; import java.nio.file.Path; - import org.junit.jupiter.api.Test; - import schemacrawler.schema.Catalog; import schemacrawler.schemacrawler.SchemaCrawlerOptions; import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder; @@ -89,7 +85,7 @@ public void executable(final DatabaseConnectionSource dataSource) throws Excepti + lineSeparator() + "TestOptions [testCommandParameter=]" + lineSeparator(), - equalTo(readFully(new FileReader(testOutputFile.toFile())))); + equalTo(readFileFully(testOutputFile))); assertThat(executable.toString(), is("test-command")); assertThat(executable.getOutputOptions(), is(outputOptions)); @@ -202,7 +198,7 @@ public void executable_with_settings(final DatabaseConnectionSource dataSource) + lineSeparator() + "TestOptions [testCommandParameter=]" + lineSeparator(), - equalTo(readFully(new FileReader(testOutputFile.toFile())))); + equalTo(readFileFully(testOutputFile))); assertThat(executable.toString(), is("test-command")); assertThat(executable.getCatalog(), is(sameInstance(mockCatalog))); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/IOUtility.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/IOUtility.java index c554d8a6ac..1e887777b6 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/IOUtility.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/IOUtility.java @@ -40,8 +40,6 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; @@ -159,15 +157,6 @@ && isDirectory(parentPath) && isWritable(parentPath); } - public static String readFully(final InputStream stream) { - if (stream == null) { - LOGGER.log(Level.FINE, "Cannot read null stream"); - return ""; - } - final Reader reader = new InputStreamReader(stream, UTF_8); - return readFully(reader); - } - /** * Reads the stream fully, and returns a byte array of data. * From 0af0cf20e8d7dac7516fa48b33e0d6bcedc8d664 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 12:08:01 -0500 Subject: [PATCH 11/14] Simplify code --- .../test/utility/TestUtility.java | 56 ++----------------- .../utility/ioresource/BaseInputResource.java | 2 - .../ioresource/ClasspathInputResource.java | 2 +- .../utility/ioresource/FileInputResource.java | 2 +- .../utility/ioresource/InputResource.java | 4 +- 5 files changed, 10 insertions(+), 56 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index c3eb6d1e86..786fea23b5 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -35,7 +35,6 @@ import static java.nio.file.Files.exists; import static java.nio.file.Files.move; import static java.nio.file.Files.newBufferedWriter; -import static java.nio.file.Files.newOutputStream; import static java.nio.file.Files.size; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static java.nio.file.StandardOpenOption.CREATE; @@ -56,17 +55,12 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.io.PrintStream; import java.io.Reader; import java.io.Writer; import java.math.BigInteger; import java.nio.ByteBuffer; -import java.nio.channels.Channels; import java.nio.channels.FileChannel; -import java.nio.channels.ReadableByteChannel; -import java.nio.channels.WritableByteChannel; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -94,7 +88,6 @@ import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import static java.util.Objects.requireNonNull; -import static us.fatehi.utility.Utility.isBlank; import schemacrawler.schemacrawler.InformationSchemaKey; import schemacrawler.schemacrawler.InformationSchemaViews; import schemacrawler.schemacrawler.InformationSchemaViewsBuilder; @@ -171,14 +164,12 @@ public static List compareOutput( } public static Path copyResourceToTempFile(final String resource) throws IOException { - if (isBlank(resource)) { - throw new IOException("Cannot read empty resource"); - } + final InputResource inputResource = new ClasspathInputResource(resource); + final Path tempFile = + IOUtility.createTempFilePath("resource", "data").normalize().toAbsolutePath(); + Files.copy(inputResource.openNewInputStream(), tempFile); - try (final InputStream resourceStream = TestUtility.class.getResourceAsStream(resource)) { - requireNonNull(resourceStream, "Resource not found, " + resource); - return writeToTempFile(resourceStream); - } + return tempFile; } public static ResultSet createMockResultSet(final String[] columnNames, final Object[][] data) @@ -220,11 +211,6 @@ public static void deleteIfPossible(final Path testOutputTargetFilePath) { } } - public static V failTestSetup(final String message) { - testAborted(message, null); - return null; - } - public static V failTestSetup(final String message, final Exception e) { testAborted(message, e); return null; @@ -411,26 +397,6 @@ private static StackTraceElement currentMethodStackTraceElement() { return null; } - private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) - throws IOException { - final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); - while (src.read(buffer) != -1) { - // prepare the buffer to be drained - buffer.flip(); - // write to the channel, may block - dest.write(buffer); - // If partial transfer, shift remainder down - // If buffer is empty, same as doing clear() - buffer.compact(); - } - // EOF will leave buffer in fill state - buffer.flip(); - // make sure the buffer is fully drained. - while (buffer.hasRemaining()) { - dest.write(buffer); - } - } - private static String lineMiscompare(final String expectedline, final String actualLine) { final StringBuilder buffer = new StringBuilder(); buffer.append(">> expected followed by actual:").append("\n"); @@ -481,18 +447,6 @@ public void warning(final SAXParseException e) throws SAXException { builder.parse(new InputSource(reader)); } - private static Path writeToTempFile(final InputStream resourceStream) throws IOException { - final Path tempFile = - IOUtility.createTempFilePath("resource", "data").normalize().toAbsolutePath(); - - try (final OutputStream tempFileStream = - newOutputStream(tempFile, WRITE, TRUNCATE_EXISTING, CREATE)) { - fastChannelCopy(Channels.newChannel(resourceStream), Channels.newChannel(tempFileStream)); - } - - return tempFile; - } - private TestUtility() { // Prevent instantiation } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java index d044395acc..369e3042a7 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/BaseInputResource.java @@ -41,8 +41,6 @@ abstract class BaseInputResource implements InputResource { private static final Logger LOGGER = Logger.getLogger(BaseInputResource.class.getName()); - protected abstract InputStream openNewInputStream() throws IOException; - @Override public final BufferedReader openNewInputReader(final Charset charset) throws IOException { requireNonNull(charset, "No input charset provided"); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java index c1bcc93d3c..3b29babc6c 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ClasspathInputResource.java @@ -52,7 +52,7 @@ public ClasspathInputResource(final String classpathResource) throws IOException } @Override - protected InputStream openNewInputStream() throws IOException { + public InputStream openNewInputStream() throws IOException { final InputStream inputStream = url.openStream(); return inputStream; } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java index 97fd2d8b20..32823ae5cc 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileInputResource.java @@ -48,7 +48,7 @@ public FileInputResource(final Path filePath) throws IOException { } @Override - protected InputStream openNewInputStream() throws IOException { + public InputStream openNewInputStream() throws IOException { final InputStream reader = newInputStream(inputFile); return reader; } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java index 60d9306929..a8a9508ab1 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResource.java @@ -30,14 +30,16 @@ import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.Charset; -@FunctionalInterface public interface InputResource { default String getDescription() { return toString(); } + InputStream openNewInputStream() throws IOException; + BufferedReader openNewInputReader(Charset charset) throws IOException; } From 32ccdcccc62eeac6f4b068d63e1df33985fa93e3 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 12:19:51 -0500 Subject: [PATCH 12/14] Simplify code --- .../schemacrawler/test/utility/DatabaseTestUtility.java | 8 ++------ .../test/java/schemacrawler/test/utility/TestUtility.java | 8 +++----- .../integration/test/SchemaCrawlerOptionsConfigTest.java | 4 +--- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/DatabaseTestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/DatabaseTestUtility.java index 0396ce0484..0c94098591 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/DatabaseTestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/DatabaseTestUtility.java @@ -33,7 +33,6 @@ import java.sql.Connection; import java.util.Map; import java.util.Properties; - import schemacrawler.crawl.SchemaCrawler; import schemacrawler.schema.Catalog; import schemacrawler.schemacrawler.LoadOptionsBuilder; @@ -45,7 +44,6 @@ import us.fatehi.utility.PropertiesUtility; import us.fatehi.utility.datasource.DatabaseConnectionSource; import us.fatehi.utility.datasource.DatabaseConnectionSourceUtility; -import us.fatehi.utility.ioresource.ClasspathInputResource; public final class DatabaseTestUtility { @@ -75,15 +73,13 @@ public static Catalog getCatalog( public static Map loadHsqldbConfig() throws IOException { final Properties properties = - TestUtility.loadProperties( - new ClasspathInputResource("/hsqldb.INFORMATION_SCHEMA.config.properties")); + TestUtility.loadPropertiesFromClasspath("/hsqldb.INFORMATION_SCHEMA.config.properties"); return PropertiesUtility.propertiesMap(properties); } public static Path tempHsqldbConfig() throws IOException { final Properties properties = - TestUtility.loadProperties( - new ClasspathInputResource("/hsqldb.INFORMATION_SCHEMA.config.properties")); + TestUtility.loadPropertiesFromClasspath("/hsqldb.INFORMATION_SCHEMA.config.properties"); return TestUtility.savePropertiesToTempFile(properties); } diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index 786fea23b5..0790e2c596 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -253,16 +253,14 @@ public static String javaVersion() { * * @param inputResource Properties resource. * @return Properties + * @throws IOException */ - public static Properties loadProperties(final InputResource inputResource) { - requireNonNull(inputResource, "No input resource provided"); - + public static Properties loadPropertiesFromClasspath(final String resource) throws IOException { + final InputResource inputResource = new ClasspathInputResource(resource); try (final Reader reader = inputResource.openNewInputReader(UTF_8); ) { final Properties properties = new Properties(); properties.load(reader); return properties; - } catch (final IOException e) { - return new Properties(); } } diff --git a/schemacrawler-commandline/src/test/java/schemacrawler/test/schemacrawler/integration/test/SchemaCrawlerOptionsConfigTest.java b/schemacrawler-commandline/src/test/java/schemacrawler/test/schemacrawler/integration/test/SchemaCrawlerOptionsConfigTest.java index 77d01b5cae..3b96e0d87c 100644 --- a/schemacrawler-commandline/src/test/java/schemacrawler/test/schemacrawler/integration/test/SchemaCrawlerOptionsConfigTest.java +++ b/schemacrawler-commandline/src/test/java/schemacrawler/test/schemacrawler/integration/test/SchemaCrawlerOptionsConfigTest.java @@ -52,7 +52,6 @@ import schemacrawler.tools.commandline.utility.SchemaCrawlerOptionsConfig; import schemacrawler.tools.options.Config; import us.fatehi.utility.PropertiesUtility; -import us.fatehi.utility.ioresource.ClasspathInputResource; public class SchemaCrawlerOptionsConfigTest { @@ -121,8 +120,7 @@ public void nullBuilders() { private Map loadConfig(final String configResource) { try { - final Properties properties = - TestUtility.loadProperties(new ClasspathInputResource(configResource)); + final Properties properties = TestUtility.loadPropertiesFromClasspath(configResource); return PropertiesUtility.propertiesMap(properties); } catch (final IOException e) { fail("Could not load " + configResource, e); From 673e33c2d7503761a93362ed26cb3e46ffda4054 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 12:45:54 -0500 Subject: [PATCH 13/14] Take a closer look at writing output --- .../integration/test/LoadSnapshotTest.java | 8 ++- .../integration/test/OfflineSnapshotTest.java | 6 ++- .../serialize/SerializationCommand.java | 8 +-- .../CatalogJavaSerializationTest.java | 6 ++- .../CatalogJsonSerializationTest.java | 11 ++-- .../ioresource/ConsoleOutputResource.java | 5 +- .../ioresource/FileOutputResource.java | 7 +-- .../ioresource/InputResourceUtility.java | 24 --------- .../ioresource/NonCloseableWriter.java | 51 +++++++++++++++++++ .../ioresource/WriterOutputResource.java | 8 +-- 10 files changed, 85 insertions(+), 49 deletions(-) create mode 100644 schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/NonCloseableWriter.java diff --git a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java index a84c95d706..de1688517c 100644 --- a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java +++ b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/LoadSnapshotTest.java @@ -30,7 +30,10 @@ import static java.nio.file.Files.newInputStream; import static java.nio.file.Files.size; +import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.READ; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.greaterThan; @@ -40,8 +43,8 @@ import static schemacrawler.test.utility.DatabaseTestUtility.schemaRetrievalOptionsDefault; import static schemacrawler.test.utility.TestUtility.failTestSetup; import static schemacrawler.tools.utility.SchemaCrawlerUtility.getCatalog; -import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -92,7 +95,8 @@ public void serializeCatalog(final DatabaseConnectionSource dataSource) { serializedCatalogFile = IOUtility.createTempFilePath("schemacrawler", "ser"); final JavaSerializedCatalog serializedCatalog = new JavaSerializedCatalog(catalog); - serializedCatalog.save(new FileOutputStream(serializedCatalogFile.toFile())); + serializedCatalog.save( + Files.newOutputStream(serializedCatalogFile, WRITE, CREATE, TRUNCATE_EXISTING)); assertThat("Database was not serialized", size(serializedCatalogFile), greaterThan(0L)); } catch (final IOException e) { failTestSetup("Could not serialize catalog", e); diff --git a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/OfflineSnapshotTest.java b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/OfflineSnapshotTest.java index a5b6f7d8b2..4acc7054ca 100644 --- a/schemacrawler-offline/src/test/java/schemacrawler/integration/test/OfflineSnapshotTest.java +++ b/schemacrawler-offline/src/test/java/schemacrawler/integration/test/OfflineSnapshotTest.java @@ -29,6 +29,9 @@ package schemacrawler.integration.test; import static java.nio.file.Files.size; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.greaterThan; @@ -219,7 +222,8 @@ public void serializeCatalog(final DatabaseConnectionSource dataSource) { serializedCatalogFile = IOUtility.createTempFilePath("schemacrawler", "ser"); final JavaSerializedCatalog serializedCatalog = new JavaSerializedCatalog(catalog); final OutputStream outputStream = - new GZIPOutputStream(Files.newOutputStream(serializedCatalogFile)); + new GZIPOutputStream( + Files.newOutputStream(serializedCatalogFile, WRITE, CREATE, TRUNCATE_EXISTING)); serializedCatalog.save(outputStream); assertThat("Database was not serialized", size(serializedCatalogFile), greaterThan(0L)); } catch (final IOException e) { diff --git a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/serialize/SerializationCommand.java b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/serialize/SerializationCommand.java index 870c11f8c3..1d237cccf1 100644 --- a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/serialize/SerializationCommand.java +++ b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/serialize/SerializationCommand.java @@ -29,13 +29,14 @@ package schemacrawler.tools.command.serialize; import static java.nio.file.Files.newOutputStream; - +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import java.io.IOException; import java.io.OutputStream; import java.io.Writer; import java.nio.file.Path; import java.util.zip.GZIPOutputStream; - import schemacrawler.schema.Catalog; import schemacrawler.schemacrawler.exceptions.IORuntimeException; import schemacrawler.schemacrawler.exceptions.InternalRuntimeException; @@ -87,7 +88,8 @@ public void execute() { outputOptions = OutputOptionsBuilder.builder(outputOptions).withOutputFile(outputFile).toOptions(); - try (final OutputStream out = new GZIPOutputStream(newOutputStream(outputFile))) { + try (final OutputStream out = + new GZIPOutputStream(newOutputStream(outputFile, WRITE, CREATE, TRUNCATE_EXISTING))) { catalogSerializer.save(out); } catch (final IOException e) { throw new IORuntimeException("Could not save catalog", e); diff --git a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java index c7f9ad917a..dd5dc7f8d4 100644 --- a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java +++ b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJavaSerializationTest.java @@ -29,7 +29,10 @@ package schemacrawler.test.serialize; import static java.nio.file.Files.newInputStream; +import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.READ; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; @@ -73,7 +76,8 @@ public void catalogSerializationWithJava(final DatabaseConnectionSource dataSour final Path testOutputFile = IOUtility.createTempFilePath("sc_java_serialization", "ser"); final JavaSerializedCatalog javaSerializedCatalogForSave = new JavaSerializedCatalog(catalog); - javaSerializedCatalogForSave.save(Files.newOutputStream(testOutputFile)); + javaSerializedCatalogForSave.save( + Files.newOutputStream(testOutputFile, WRITE, CREATE, TRUNCATE_EXISTING)); assertThat("Catalog was not serialized", Files.size(testOutputFile), greaterThan(0L)); assertThat(fileHeaderOf(testOutputFile), is("ACED")); diff --git a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJsonSerializationTest.java b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJsonSerializationTest.java index 1d1a2c0333..9f8c854edf 100644 --- a/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJsonSerializationTest.java +++ b/schemacrawler-scripting/src/test/java/schemacrawler/test/serialize/CatalogJsonSerializationTest.java @@ -28,6 +28,9 @@ package schemacrawler.test.serialize; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.greaterThan; @@ -41,23 +44,18 @@ import static schemacrawler.test.utility.FileHasContent.outputOf; import static schemacrawler.test.utility.TestUtility.fileHeaderOf; import static schemacrawler.tools.utility.SchemaCrawlerUtility.getCatalog; - -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.MissingNode; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; - import schemacrawler.schema.Catalog; import schemacrawler.schemacrawler.SchemaCrawlerOptions; import schemacrawler.test.utility.DatabaseTestUtility; @@ -96,7 +94,8 @@ public void catalogSerializationWithJson( getCatalog(dataSource, schemaRetrievalOptionsDefault, schemaCrawlerOptions, new Config()); final Path testOutputFile = IOUtility.createTempFilePath("sc_serialized_catalog", "json"); - try (final OutputStream out = new FileOutputStream(testOutputFile.toFile())) { + try (final OutputStream out = + Files.newOutputStream(testOutputFile, WRITE, CREATE, TRUNCATE_EXISTING)) { new JsonSerializedCatalog(catalog).save(out); } assertThat("Catalog was not serialized", Files.size(testOutputFile), greaterThan(0L)); diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ConsoleOutputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ConsoleOutputResource.java index 4fb83339b1..50e59d7940 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ConsoleOutputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/ConsoleOutputResource.java @@ -28,8 +28,6 @@ package us.fatehi.utility.ioresource; -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapWriter; - import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; @@ -47,7 +45,8 @@ public Writer openNewOutputWriter(final Charset charset, final boolean appendOut throws IOException { final Writer writer = new BufferedWriter(new OutputStreamWriter(System.out, charset)); LOGGER.log(Level.FINE, "Opened output writer to console"); - return wrapWriter(getDescription(), writer, false); + // Console should not be closed + return new NonCloseableWriter(writer); } @Override diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileOutputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileOutputResource.java index d791715d98..06e56de6a2 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileOutputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/FileOutputResource.java @@ -33,9 +33,6 @@ import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; import static java.nio.file.StandardOpenOption.WRITE; -import static java.util.Objects.requireNonNull; -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapWriter; - import java.io.IOException; import java.io.Writer; import java.nio.charset.Charset; @@ -43,7 +40,7 @@ import java.nio.file.Path; import java.util.logging.Level; import java.util.logging.Logger; - +import static java.util.Objects.requireNonNull; import us.fatehi.utility.string.StringFormat; public final class FileOutputResource implements OutputResource { @@ -72,7 +69,7 @@ public Writer openNewOutputWriter(final Charset charset, final boolean appendOut } final Writer writer = newBufferedWriter(outputFile, charset, openOptions); LOGGER.log(Level.FINE, new StringFormat("Opened output writer to file <%s>", outputFile)); - return wrapWriter(getDescription(), writer, true); + return writer; } @Override diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java index 0144f2fc62..63ef163de5 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/InputResourceUtility.java @@ -28,15 +28,11 @@ package us.fatehi.utility.ioresource; -import java.io.FilterWriter; -import java.io.IOException; -import java.io.Writer; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Optional; import java.util.logging.Level; import java.util.logging.Logger; -import static java.util.Objects.requireNonNull; import static us.fatehi.utility.Utility.isBlank; import us.fatehi.utility.string.StringFormat; @@ -80,26 +76,6 @@ public static Optional createInputResource(final String inputReso return Optional.ofNullable(inputResource); } - public static Writer wrapWriter( - final String description, final Writer writer, final boolean shouldClose) { - requireNonNull(writer, "No writer provided"); - return new FilterWriter(writer) { - @Override - public void close() throws IOException { - if (shouldClose) { - super.close(); - } else { - super.flush(); - } - } - - @Override - public String toString() { - return description; - } - }; - } - private InputResourceUtility() { // Prevent instantiation } diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/NonCloseableWriter.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/NonCloseableWriter.java new file mode 100644 index 0000000000..cae86280a5 --- /dev/null +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/NonCloseableWriter.java @@ -0,0 +1,51 @@ +/* +======================================================================== +SchemaCrawler +http://www.schemacrawler.com +Copyright (c) 2000-2025, Sualeh Fatehi . +All rights reserved. +------------------------------------------------------------------------ + +SchemaCrawler is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +SchemaCrawler and the accompanying materials are made available under +the terms of the Eclipse Public License v1.0, GNU General Public License +v3 or GNU Lesser General Public License v3. + +You may elect to redistribute this code under any of these licenses. + +The Eclipse Public License is available at: +http://www.eclipse.org/legal/epl-v10.html + +The GNU General Public License v3 and the GNU Lesser General Public +License v3 are available at: +http://www.gnu.org/licenses/ + +======================================================================== +*/ + +package us.fatehi.utility.ioresource; + +import java.io.FilterWriter; +import java.io.IOException; +import java.io.Writer; + +final class NonCloseableWriter extends FilterWriter { + + NonCloseableWriter(final Writer out) { + super(out); + } + + /** Flush but do not close. */ + @Override + public void close() throws IOException { + super.flush(); + } + + @Override + public String toString() { + return out.toString(); + } +} diff --git a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/WriterOutputResource.java b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/WriterOutputResource.java index 14dd623b4a..730f7d8fb1 100644 --- a/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/WriterOutputResource.java +++ b/schemacrawler-utility/src/main/java/us/fatehi/utility/ioresource/WriterOutputResource.java @@ -28,13 +28,11 @@ package us.fatehi.utility.ioresource; -import static java.util.Objects.requireNonNull; -import static us.fatehi.utility.ioresource.InputResourceUtility.wrapWriter; - import java.io.Writer; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; +import static java.util.Objects.requireNonNull; public final class WriterOutputResource implements OutputResource { @@ -49,7 +47,9 @@ public WriterOutputResource(final Writer writer) { @Override public Writer openNewOutputWriter(final Charset charset, final boolean appendOutput) { LOGGER.log(Level.FINE, "Output to provided writer"); - return wrapWriter(getDescription(), writer, false); + // Since the original write was provided to us, + // we should not allow it to be closed + return new NonCloseableWriter(writer); } @Override From e9ca2e3adf7dfc30c584d087eef36708fa735642 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Sun, 10 Nov 2024 12:56:45 -0500 Subject: [PATCH 14/14] Close readers --- .../test/utility/TestUtility.java | 5 ++-- .../tools/command/script/ScriptCommand.java | 23 ++++++++++--------- .../command/template/MustacheRenderer.java | 10 +++----- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java index 0790e2c596..150e694948 100644 --- a/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java +++ b/schemacrawler-api/src/test/java/schemacrawler/test/utility/TestUtility.java @@ -441,8 +441,9 @@ public void warning(final SAXParseException e) throws SAXException { failures.add(e.getMessage()); } }); - final Reader reader = new FileInputResource(testOutputFile).openNewInputReader(UTF_8); - builder.parse(new InputSource(reader)); + try (final Reader reader = new FileInputResource(testOutputFile).openNewInputReader(UTF_8); ) { + builder.parse(new InputSource(reader)); + } } private TestUtility() { diff --git a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/script/ScriptCommand.java b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/script/ScriptCommand.java index 0faf7c37bf..6866b3009f 100644 --- a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/script/ScriptCommand.java +++ b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/script/ScriptCommand.java @@ -103,20 +103,21 @@ public void execute() { try { final Charset inputCharset = outputOptions.getInputCharset(); final InputResource inputResource = commandOptions.getInputResource().get(); - final Reader reader = inputResource.openNewInputReader(inputCharset); - final Writer writer = outputOptions.openNewOutputWriter(); + try (final Reader reader = inputResource.openNewInputReader(inputCharset); + final Writer writer = outputOptions.openNewOutputWriter(); ) { - LOGGER.log(Level.CONFIG, new StringFormat("Evaluating script, %s", inputResource)); + LOGGER.log(Level.CONFIG, new StringFormat("Evaluating script, %s", inputResource)); - // Set up the context - final Map context = new HashMap<>(); - context.put("title", outputOptions.getTitle()); - context.put("catalog", catalog); - context.put("connection", connection); - context.put("chain", new CommandChain(this)); + // Set up the context + final Map context = new HashMap<>(); + context.put("title", outputOptions.getTitle()); + context.put("catalog", catalog); + context.put("connection", connection); + context.put("chain", new CommandChain(this)); - scriptExecutor.initialize(context, reader, writer); - scriptExecutor.run(); + scriptExecutor.initialize(context, reader, writer); + scriptExecutor.run(); + } } catch (final Exception e) { throw new InternalRuntimeException("Could not execute script", e); } diff --git a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/template/MustacheRenderer.java b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/template/MustacheRenderer.java index 00b4858523..454321be73 100644 --- a/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/template/MustacheRenderer.java +++ b/schemacrawler-scripting/src/main/java/schemacrawler/tools/command/template/MustacheRenderer.java @@ -29,15 +29,13 @@ package schemacrawler.tools.command.template; import static us.fatehi.utility.ioresource.InputResourceUtility.createInputResource; - +import java.io.Reader; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.Map; - import com.github.mustachejava.DefaultMustacheFactory; import com.github.mustachejava.Mustache; import com.github.mustachejava.MustacheFactory; - import schemacrawler.schemacrawler.exceptions.ConfigurationException; import schemacrawler.schemacrawler.exceptions.ExecutionRuntimeException; import schemacrawler.tools.options.OutputOptions; @@ -58,11 +56,9 @@ public void execute() { new ConfigurationException( String.format("Mustache template not found <%s>", templateLocation))); - try { + try (final Reader reader = inputResource.openNewInputReader(StandardCharsets.UTF_8)) { final MustacheFactory mustacheFactory = new DefaultMustacheFactory(); - final Mustache mustache = - mustacheFactory.compile( - inputResource.openNewInputReader(StandardCharsets.UTF_8), templateLocation); + final Mustache mustache = mustacheFactory.compile(reader, templateLocation); try (final Writer writer = outputOptions.openNewOutputWriter()) { // Evaluate the template