|
16 | 16 | package com.github.difflib.unifieddiff; |
17 | 17 |
|
18 | 18 | 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; |
19 | 21 |
|
20 | 22 | import com.github.difflib.DiffUtils; |
21 | 23 | import com.github.difflib.patch.Patch; |
22 | 24 | import java.io.ByteArrayInputStream; |
23 | 25 | import java.io.IOException; |
24 | 26 | import java.io.StringWriter; |
| 27 | +import java.io.Writer; |
25 | 28 | import java.net.URI; |
26 | 29 | import java.net.URISyntaxException; |
27 | 30 | import java.nio.charset.Charset; |
|
31 | 34 | import java.util.Collections; |
32 | 35 | import java.util.List; |
33 | 36 | import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
34 | 39 |
|
35 | 40 | /** |
36 | 41 | * |
@@ -81,6 +86,39 @@ public void testWriteWithNewFile() throws URISyntaxException, IOException { |
81 | 86 | assertEquals("@@ -0,0 +1,2 @@", lines[2]); |
82 | 87 | } |
83 | 88 |
|
| 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 | + |
84 | 122 | static String readFile(URI path, Charset encoding) throws IOException { |
85 | 123 | byte[] encoded = Files.readAllBytes(Paths.get(path)); |
86 | 124 | return new String(encoded, encoding); |
|
0 commit comments