1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.patch;
12
13 import static java.nio.charset.StandardCharsets.ISO_8859_1;
14 import static java.nio.charset.StandardCharsets.UTF_8;
15 import static org.junit.Assert.assertEquals;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.nio.charset.Charset;
23
24 import org.eclipse.jgit.junit.JGitTestUtil;
25 import org.junit.Test;
26
27 public class GetTextTest {
28 @Test
29 public void testGetText_BothISO88591() throws IOException {
30 final Charset cs = ISO_8859_1;
31 final Patch p = parseTestPatchFile();
32 assertTrue(p.getErrors().isEmpty());
33 assertEquals(1, p.getFiles().size());
34 final FileHeader fh = p.getFiles().get(0);
35 assertEquals(2, fh.getHunks().size());
36 assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
37 }
38
39 @Test
40 public void testGetText_NoBinary() throws IOException {
41 final Charset cs = ISO_8859_1;
42 final Patch p = parseTestPatchFile();
43 assertTrue(p.getErrors().isEmpty());
44 assertEquals(1, p.getFiles().size());
45 final FileHeader fh = p.getFiles().get(0);
46 assertEquals(0, fh.getHunks().size());
47 assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
48 }
49
50 @Test
51 public void testGetText_Convert() throws IOException {
52 final Charset csOld = ISO_8859_1;
53 final Charset csNew = UTF_8;
54 final Patch p = parseTestPatchFile();
55 assertTrue(p.getErrors().isEmpty());
56 assertEquals(1, p.getFiles().size());
57 final FileHeader fh = p.getFiles().get(0);
58 assertEquals(2, fh.getHunks().size());
59
60
61
62
63
64 String exp = readTestPatchFile(csOld);
65 exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
66
67 assertEquals(exp, fh.getScriptText(csOld, csNew));
68 }
69
70 @Test
71 public void testGetText_DiffCc() throws IOException {
72 final Charset csOld = ISO_8859_1;
73 final Charset csNew = UTF_8;
74 final Patch p = parseTestPatchFile();
75 assertTrue(p.getErrors().isEmpty());
76 assertEquals(1, p.getFiles().size());
77 final CombinedFileHeader fh = (CombinedFileHeader) p.getFiles().get(0);
78 assertEquals(1, fh.getHunks().size());
79
80
81
82
83
84 String exp = readTestPatchFile(csOld);
85 exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
86
87 assertEquals(exp, fh
88 .getScriptText(new Charset[] { csNew, csOld, csNew }));
89 }
90
91 private Patch parseTestPatchFile() throws IOException {
92 final String patchFile = JGitTestUtil.getName() + ".patch";
93 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
94 if (in == null) {
95 fail("No " + patchFile + " test vector");
96 return null;
97 }
98 final Patch p = new Patch();
99 p.parse(in);
100 return p;
101 }
102 }
103
104 private String readTestPatchFile(Charset cs) throws IOException {
105 final String patchFile = JGitTestUtil.getName() + ".patch";
106 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
107 if (in == null) {
108 fail("No " + patchFile + " test vector");
109 return null;
110 }
111 final InputStreamReader r = new InputStreamReader(in, cs);
112 char[] tmp = new char[2048];
113 final StringBuilder s = new StringBuilder();
114 int n;
115 while ((n = r.read(tmp)) > 0)
116 s.append(tmp, 0, n);
117 return s.toString();
118 }
119 }
120 }