Skip to content

Commit 1b449db

Browse files
authored
Propagate I/O failures from UnifiedDiffWriter (#234)
* Propagate I/O failures from UnifiedDiffWriter * Document no-op methods in failing writer test
1 parent 0643852 commit 1b449db

2 files changed

Lines changed: 54 additions & 15 deletions

File tree

‎java-diff-utils/src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,39 @@
1717

1818
import com.github.difflib.patch.AbstractDelta;
1919
import java.io.IOException;
20+
import java.io.UncheckedIOException;
2021
import java.io.Writer;
2122
import java.util.ArrayList;
2223
import java.util.List;
2324
import java.util.Objects;
2425
import java.util.function.Consumer;
2526
import java.util.function.Function;
26-
import java.util.logging.Level;
27-
import java.util.logging.Logger;
2827

2928
/**
3029
* @todo use an instance to store contextSize and originalLinesProvider.
3130
* @author Tobias Warneke (t.warneke@gmx.net)
3231
*/
3332
public class UnifiedDiffWriter {
3433

35-
private static final Logger LOG = Logger.getLogger(UnifiedDiffWriter.class.getName());
36-
3734
public static void write(
3835
UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Writer writer, int contextSize)
3936
throws IOException {
4037
Objects.requireNonNull(originalLinesProvider, "original lines provider needs to be specified");
41-
write(
42-
diff,
43-
originalLinesProvider,
44-
line -> {
45-
try {
46-
writer.append(line).append("\n");
47-
} catch (IOException ex) {
48-
LOG.log(Level.SEVERE, null, ex);
49-
}
50-
},
51-
contextSize);
38+
try {
39+
write(
40+
diff,
41+
originalLinesProvider,
42+
line -> {
43+
try {
44+
writer.append(line).append("\n");
45+
} catch (IOException ex) {
46+
throw new UncheckedIOException(ex);
47+
}
48+
},
49+
contextSize);
50+
} catch (UncheckedIOException ex) {
51+
throw ex.getCause();
52+
}
5253
}
5354

5455
public static void write(

‎java-diff-utils/src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
package com.github.difflib.unifieddiff;
1717

1818
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertSame;
20+
import static org.junit.jupiter.api.Assertions.assertThrows;
1921

2022
import com.github.difflib.DiffUtils;
2123
import com.github.difflib.patch.Patch;
2224
import java.io.ByteArrayInputStream;
2325
import java.io.IOException;
2426
import java.io.StringWriter;
27+
import java.io.Writer;
2528
import java.net.URI;
2629
import java.net.URISyntaxException;
2730
import java.nio.charset.Charset;
@@ -31,6 +34,8 @@
3134
import java.util.Collections;
3235
import java.util.List;
3336
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.params.ParameterizedTest;
38+
import org.junit.jupiter.params.provider.ValueSource;
3439

3540
/**
3641
*
@@ -81,6 +86,39 @@ public void testWriteWithNewFile() throws URISyntaxException, IOException {
8186
assertEquals("@@ -0,0 +1,2 @@", lines[2]);
8287
}
8388

89+
@ParameterizedTest
90+
@ValueSource(ints = {1, 2, 9, 16})
91+
public void testPropagateWriterFailures(int failOnWrite) {
92+
List<String> original = Collections.singletonList("old");
93+
Patch<String> patch = DiffUtils.diff(original, Collections.singletonList("new"));
94+
UnifiedDiff diff = UnifiedDiff.from("header", "tail", UnifiedDiffFile.from("original", "revised", patch));
95+
IOException failure = new IOException("write failed");
96+
int[] writes = {0};
97+
Writer writer = new Writer() {
98+
@Override
99+
public void write(char[] buffer, int offset, int length) throws IOException {
100+
if (++writes[0] == failOnWrite) {
101+
throw failure;
102+
}
103+
}
104+
105+
@Override
106+
public void flush() {
107+
// No buffered output in this test writer.
108+
}
109+
110+
@Override
111+
public void close() {
112+
// No resources to release.
113+
}
114+
};
115+
116+
assertSame(
117+
failure,
118+
assertThrows(IOException.class, () -> UnifiedDiffWriter.write(diff, name -> original, writer, 0)));
119+
assertEquals(failOnWrite, writes[0]);
120+
}
121+
84122
static String readFile(URI path, Charset encoding) throws IOException {
85123
byte[] encoded = Files.readAllBytes(Paths.get(path));
86124
return new String(encoded, encoding);

0 commit comments

Comments
 (0)