← 输入框|github.com|刷新
Skip to content
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 @@ -17,38 +17,39 @@

import com.github.difflib.patch.AbstractDelta;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;

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

private static final Logger LOG = Logger.getLogger(UnifiedDiffWriter.class.getName());

public static void write(
UnifiedDiff diff, Function<String, List<String>> originalLinesProvider, Writer writer, int contextSize)
throws IOException {
Objects.requireNonNull(originalLinesProvider, "original lines provider needs to be specified");
write(
diff,
originalLinesProvider,
line -> {
try {
writer.append(line).append("\n");
} catch (IOException ex) {
LOG.log(Level.SEVERE, null, ex);
}
},
contextSize);
try {
write(
diff,
originalLinesProvider,
line -> {
try {
writer.append(line).append("\n");
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
},
contextSize);
} catch (UncheckedIOException ex) {
throw ex.getCause();
}
}

public static void write(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package com.github.difflib.unifieddiff;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.github.difflib.DiffUtils;
import com.github.difflib.patch.Patch;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
Expand All @@ -31,6 +34,8 @@
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
*
Expand Down Expand Up @@ -81,6 +86,39 @@ public void testWriteWithNewFile() throws URISyntaxException, IOException {
assertEquals("@@ -0,0 +1,2 @@", lines[2]);
}

@ParameterizedTest
@ValueSource(ints = {1, 2, 9, 16})
public void testPropagateWriterFailures(int failOnWrite) {
List<String> original = Collections.singletonList("old");
Patch<String> patch = DiffUtils.diff(original, Collections.singletonList("new"));
UnifiedDiff diff = UnifiedDiff.from("header", "tail", UnifiedDiffFile.from("original", "revised", patch));
IOException failure = new IOException("write failed");
int[] writes = {0};
Writer writer = new Writer() {
@Override
public void write(char[] buffer, int offset, int length) throws IOException {
if (++writes[0] == failOnWrite) {
throw failure;
}
}

@Override
public void flush() {
// No buffered output in this test writer.
}

@Override
public void close() {
// No resources to release.
}
};

assertSame(
failure,
assertThrows(IOException.class, () -> UnifiedDiffWriter.write(diff, name -> original, writer, 0)));
assertEquals(failOnWrite, writes[0]);
}

static String readFile(URI path, Charset encoding) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
Expand Down
Loading