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 org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertTrue;
48 import static org.junit.Assert.fail;
49
50 import java.io.IOException;
51 import java.io.InputStream;
52 import java.io.InputStreamReader;
53 import java.nio.charset.Charset;
54
55 import org.eclipse.jgit.junit.JGitTestUtil;
56 import org.junit.Test;
57
58 public class GetTextTest {
59 @Test
60 public void testGetText_BothISO88591() throws IOException {
61 final Charset cs = Charset.forName("ISO-8859-1");
62 final Patch p = parseTestPatchFile();
63 assertTrue(p.getErrors().isEmpty());
64 assertEquals(1, p.getFiles().size());
65 final FileHeader fh = p.getFiles().get(0);
66 assertEquals(2, fh.getHunks().size());
67 assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
68 }
69
70 @Test
71 public void testGetText_NoBinary() throws IOException {
72 final Charset cs = Charset.forName("ISO-8859-1");
73 final Patch p = parseTestPatchFile();
74 assertTrue(p.getErrors().isEmpty());
75 assertEquals(1, p.getFiles().size());
76 final FileHeader fh = p.getFiles().get(0);
77 assertEquals(0, fh.getHunks().size());
78 assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
79 }
80
81 @Test
82 public void testGetText_Convert() throws IOException {
83 final Charset csOld = Charset.forName("ISO-8859-1");
84 final Charset csNew = Charset.forName("UTF-8");
85 final Patch p = parseTestPatchFile();
86 assertTrue(p.getErrors().isEmpty());
87 assertEquals(1, p.getFiles().size());
88 final FileHeader fh = p.getFiles().get(0);
89 assertEquals(2, fh.getHunks().size());
90
91
92
93
94
95 String exp = readTestPatchFile(csOld);
96 exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
97
98 assertEquals(exp, fh.getScriptText(csOld, csNew));
99 }
100
101 @Test
102 public void testGetText_DiffCc() throws IOException {
103 final Charset csOld = Charset.forName("ISO-8859-1");
104 final Charset csNew = Charset.forName("UTF-8");
105 final Patch p = parseTestPatchFile();
106 assertTrue(p.getErrors().isEmpty());
107 assertEquals(1, p.getFiles().size());
108 final CombinedFileHeader fh = (CombinedFileHeader) p.getFiles().get(0);
109 assertEquals(1, fh.getHunks().size());
110
111
112
113
114
115 String exp = readTestPatchFile(csOld);
116 exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
117
118 assertEquals(exp, fh
119 .getScriptText(new Charset[] { csNew, csOld, csNew }));
120 }
121
122 private Patch parseTestPatchFile() throws IOException {
123 final String patchFile = JGitTestUtil.getName() + ".patch";
124 final InputStream in = getClass().getResourceAsStream(patchFile);
125 if (in == null) {
126 fail("No " + patchFile + " test vector");
127 return null;
128 }
129 try {
130 final Patch p = new Patch();
131 p.parse(in);
132 return p;
133 } finally {
134 in.close();
135 }
136 }
137
138 private String readTestPatchFile(final Charset cs) throws IOException {
139 final String patchFile = JGitTestUtil.getName() + ".patch";
140 final InputStream in = getClass().getResourceAsStream(patchFile);
141 if (in == null) {
142 fail("No " + patchFile + " test vector");
143 return null;
144 }
145 try {
146 final InputStreamReader r = new InputStreamReader(in, cs);
147 char[] tmp = new char[2048];
148 final StringBuilder s = new StringBuilder();
149 int n;
150 while ((n = r.read(tmp)) > 0)
151 s.append(tmp, 0, n);
152 return s.toString();
153 } finally {
154 in.close();
155 }
156 }
157 }