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