Skip to content

Commit

Permalink
Don't insert a line break when nothing is processed (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed Sep 30, 2024
1 parent 07e7f0c commit d3a47ee
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}.
*
Expand Down Expand Up @@ -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<String> getWriter() {
return writer;
Expand Down

0 comments on commit d3a47ee

Please sign in to comment.