Skip to content

Commit 4c1a0bc

Browse files
committed
1 parent 61a21cb commit 4c1a0bc

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

src/main/java/com/github/difflib/unifieddiff/UnifiedDiffWriter.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.github.difflib.unifieddiff;
1717

18+
import java.io.IOException;
1819
import java.io.Writer;
1920

2021
/**
@@ -23,7 +24,29 @@
2324
*/
2425
public class UnifiedDiffWriter {
2526

26-
public static void write(UnifiedDiff diff, Writer writer) {
27+
public static void write(UnifiedDiff diff, Writer writer) throws IOException {
28+
writer.write(diff.getHeader());
2729

30+
for (UnifiedDiffFile file : diff.getFiles()) {
31+
writeOrNothing(writer, file.getDiffCommand());
32+
if (file.getIndex() != null) {
33+
writer.write("index " + file.getIndex() + "\n");
34+
}
35+
if (file.getFromFile() != null) {
36+
writer.write("--- " + file.getFromFile() + "\n");
37+
}
38+
if (file.getToFile() != null) {
39+
writer.write("+++ " + file.getToFile() + "\n");
40+
}
41+
42+
}
43+
44+
writer.write(diff.getTail());
45+
}
46+
47+
private static void writeOrNothing(Writer writer, String str) throws IOException {
48+
if (str != null) {
49+
writer.append(str).append("\n");
50+
}
2851
}
2952
}

src/test/java/com/github/difflib/unifieddiff/UnifiedDiffReaderTest.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,8 @@
1919
import java.util.regex.Matcher;
2020
import java.util.regex.Pattern;
2121
import static org.assertj.core.api.Java6Assertions.assertThat;
22-
import org.junit.After;
23-
import org.junit.AfterClass;
2422
import static org.junit.Assert.assertEquals;
2523
import static org.junit.Assert.assertTrue;
26-
import org.junit.Before;
27-
import org.junit.BeforeClass;
2824
import org.junit.Test;
2925

3026
/**
@@ -33,25 +29,6 @@
3329
*/
3430
public class UnifiedDiffReaderTest {
3531

36-
public UnifiedDiffReaderTest() {
37-
}
38-
39-
@BeforeClass
40-
public static void setUpClass() {
41-
}
42-
43-
@AfterClass
44-
public static void tearDownClass() {
45-
}
46-
47-
@Before
48-
public void setUp() {
49-
}
50-
51-
@After
52-
public void tearDown() {
53-
}
54-
5532
@Test
5633
public void testSimpleParse() throws IOException {
5734
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(UnifiedDiffReaderTest.class.getResourceAsStream("jsqlparser_patch_1.diff"));

src/test/java/com/github/difflib/unifieddiff/UnifiedDiffWriterTest.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package com.github.difflib.unifieddiff;
1717

18-
import org.junit.After;
19-
import org.junit.AfterClass;
20-
import org.junit.Before;
21-
import org.junit.BeforeClass;
18+
import java.io.ByteArrayInputStream;
19+
import java.io.IOException;
20+
import java.io.StringWriter;
21+
import java.net.URI;
22+
import java.net.URISyntaxException;
23+
import java.nio.charset.Charset;
24+
import java.nio.file.Files;
25+
import java.nio.file.Paths;
2226
import org.junit.Test;
2327

2428
/**
@@ -30,25 +34,19 @@ public class UnifiedDiffWriterTest {
3034
public UnifiedDiffWriterTest() {
3135
}
3236

33-
@BeforeClass
34-
public static void setUpClass() {
35-
}
36-
37-
@AfterClass
38-
public static void tearDownClass() {
39-
}
40-
41-
@Before
42-
public void setUp() {
43-
}
44-
45-
@After
46-
public void tearDown() {
47-
}
48-
4937
@Test
50-
public void testWrite() {
38+
public void testWrite() throws URISyntaxException, IOException {
39+
String str = readFile(UnifiedDiffReaderTest.class.getResource("jsqlparser_patch_1.diff").toURI(), Charset.defaultCharset());
40+
UnifiedDiff diff = UnifiedDiffReader.parseUnifiedDiff(new ByteArrayInputStream(str.getBytes()));
5141

42+
StringWriter writer = new StringWriter();
43+
UnifiedDiffWriter.write(diff, writer);
44+
System.out.println(writer.toString());
5245
}
5346

47+
static String readFile(URI path, Charset encoding)
48+
throws IOException {
49+
byte[] encoded = Files.readAllBytes(Paths.get(path));
50+
return new String(encoded, encoding);
51+
}
5452
}

0 commit comments

Comments
 (0)