From d3a47eeb70166d1ffe66ffd1cf7f3926dbc89c4d Mon Sep 17 00:00:00 2001 From: Pascal Christoph Date: Fri, 27 Sep 2024 15:56:40 +0200 Subject: [PATCH] Don't insert a line break when nothing is processed (#543) --- .../org/metafacture/io/ObjectFileWriter.java | 23 +++++++++++-------- .../metafacture/io/ObjectFileWriterTest.java | 20 +++++++++++----- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/metafacture-io/src/main/java/org/metafacture/io/ObjectFileWriter.java b/metafacture-io/src/main/java/org/metafacture/io/ObjectFileWriter.java index f7462982..36d45426 100644 --- a/metafacture-io/src/main/java/org/metafacture/io/ObjectFileWriter.java +++ b/metafacture-io/src/main/java/org/metafacture/io/ObjectFileWriter.java @@ -90,18 +90,21 @@ public void setCompression(final String compression) { @Override public void process(final T obj) { assert !closed; - try { - if (firstObject) { - getWriter().write(getHeader()); - firstObject = false; + final String objStr = obj.toString(); + if (!objStr.isEmpty()) { + try { + if (firstObject) { + getWriter().write(getHeader()); + firstObject = false; + } + else { + getWriter().write(getSeparator()); + } + getWriter().write(objStr); } - else { - getWriter().write(getSeparator()); + catch (final IOException e) { + throw new MetafactureException(e); } - getWriter().write(obj.toString()); - } - catch (final IOException e) { - throw new MetafactureException(e); } } diff --git a/metafacture-io/src/test/java/org/metafacture/io/ObjectFileWriterTest.java b/metafacture-io/src/test/java/org/metafacture/io/ObjectFileWriterTest.java index 64d877fb..77d9ad61 100644 --- a/metafacture-io/src/test/java/org/metafacture/io/ObjectFileWriterTest.java +++ b/metafacture-io/src/test/java/org/metafacture/io/ObjectFileWriterTest.java @@ -20,6 +20,12 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeFalse; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.metafacture.commons.ResourceUtil; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -28,12 +34,6 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.metafacture.commons.ResourceUtil; - /** * Tests for class {@link ObjectFileWriter}. * @@ -105,6 +105,14 @@ public void shouldIncrementCountOnResetBeforeStartingNewFile() throws IOExceptio assertTrue(new File(tempFolder.getRoot(), "test-1").exists()); } + @Test + public void issue543_shouldResultEmptyWhenNothingIsProcessed() throws IOException { + writer.process(""); + writer.closeStream(); + + assertOutput(""); + } + @Override protected ConfigurableObjectWriter getWriter() { return writer;