Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix "batch-reset creates empty records" #560

Merged
merged 3 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public void literal(final String name, final String value) {

@Override
protected void onResetStream() {
pipe.resetStream();
encoder.onResetStream();
}

@Override
Expand Down Expand Up @@ -372,7 +372,9 @@ protected void onResetStream() {

@Override
protected void onCloseStream() {
writeFooter();
if (!atStreamStart) {
writeFooter();
}
sendAndClearData();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class MarcXmlEncoderTest {
private static final String RECORD_ID = "92005291";

private static StringBuilder resultCollector;
private static int resultCollectorsResetStreamCount;
private static MarcXmlEncoder encoder;

@Before
Expand All @@ -62,6 +63,11 @@ public void setUp() {
public void process(final String obj) {
resultCollector.append(obj);
}
@Override
public void resetStream() {
++resultCollectorsResetStreamCount;
}

});
resultCollector = new StringBuilder();
}
Expand Down Expand Up @@ -389,4 +395,26 @@ public void shouldNotEncodeNestedTypeLiteralAsAttribute() {
assertEquals(expected, actual);
}

@Test
public void issue543_shouldNotWriteFooterWhenRecordIsEmpty() {
encoder.closeStream();
String actual = resultCollector.toString();
assertTrue(actual.isEmpty());
}

@Test
public void issue543_shouldOnlyResetStreamOnce() {
resultCollectorsResetStreamCount = 0;
encoder.resetStream();
assertEquals(resultCollectorsResetStreamCount, 1);
}

@Test
public void issue543_shouldOnlyResetStreamOnceUsingWrapper() {
resultCollectorsResetStreamCount = 0;
encoder.setEnsureCorrectMarc21Xml(true);
encoder.resetStream();
assertEquals(resultCollectorsResetStreamCount, 1);
}

}
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;
dr0i marked this conversation as resolved.
Show resolved Hide resolved
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
Loading