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
45
46 package org.eclipse.jgit.junit;
47
48 import static java.nio.charset.StandardCharsets.UTF_8;
49
50 import java.io.File;
51 import java.io.FileNotFoundException;
52 import java.io.FileOutputStream;
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.io.OutputStreamWriter;
56 import java.io.Writer;
57 import java.lang.reflect.Method;
58 import java.net.URISyntaxException;
59 import java.net.URL;
60 import java.nio.file.Path;
61
62 import org.eclipse.jgit.lib.Repository;
63 import org.eclipse.jgit.util.FileUtils;
64 import org.eclipse.jgit.util.IO;
65 import org.eclipse.jgit.util.RawParseUtils;
66 import org.junit.Assert;
67 import org.junit.Test;
68
69
70
71
72 public abstract class JGitTestUtil {
73
74 public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/";
75
76 private JGitTestUtil() {
77 throw new UnsupportedOperationException();
78 }
79
80
81
82
83
84
85 public static String getName() {
86 GatherStackTrace stack;
87 try {
88 throw new GatherStackTrace();
89 } catch (GatherStackTrace wanted) {
90 stack = wanted;
91 }
92
93 try {
94 for (StackTraceElement stackTrace : stack.getStackTrace()) {
95 String className = stackTrace.getClassName();
96 String methodName = stackTrace.getMethodName();
97 Method method;
98 try {
99 method = Class.forName(className)
100 .getMethod(methodName, (Class[]) null);
101 } catch (NoSuchMethodException e) {
102
103
104 continue;
105 }
106
107 Test annotation = method.getAnnotation(Test.class);
108 if (annotation != null)
109 return methodName;
110 }
111 } catch (ClassNotFoundException shouldNeverOccur) {
112
113 }
114
115 throw new AssertionError("Cannot determine name of current test");
116 }
117
118 @SuppressWarnings("serial")
119 private static class GatherStackTrace extends Exception {
120
121 }
122
123
124
125
126
127
128
129
130
131 public static void assertEquals(byte[] exp, byte[] act) {
132 Assert.assertEquals(s(exp), s(act));
133 }
134
135 private static String s(byte[] raw) {
136 return RawParseUtils.decode(raw);
137 }
138
139
140
141
142
143
144
145 public static File getTestResourceFile(String fileName) {
146 if (fileName == null || fileName.length() <= 0) {
147 return null;
148 }
149 final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName);
150 if (url == null) {
151
152
153 return new File("tst", fileName);
154 }
155 if ("jar".equals(url.getProtocol())) {
156 try {
157 File tmp = File.createTempFile("tmp_", "_" + fileName);
158 copyTestResource(fileName, tmp);
159 return tmp;
160 } catch (IOException err) {
161 throw new RuntimeException("Cannot create temporary file", err);
162 }
163 }
164 try {
165 return new File(url.toURI());
166 } catch (IllegalArgumentException e) {
167 throw new IllegalArgumentException(e.getMessage() + " " + url);
168 } catch (URISyntaxException e) {
169 return new File(url.getPath());
170 }
171 }
172
173
174
175
176
177
178
179
180 public static void copyTestResource(String name, File dest)
181 throws IOException {
182 URL url = cl().getResource(CLASSPATH_TO_RESOURCES + name);
183 if (url == null)
184 throw new FileNotFoundException(name);
185 try (InputStream in = url.openStream();
186 FileOutputStream out = new FileOutputStream(dest)) {
187 byte[] buf = new byte[4096];
188 for (int n; (n = in.read(buf)) > 0;)
189 out.write(buf, 0, n);
190 }
191 }
192
193 private static ClassLoader cl() {
194 return JGitTestUtil.class.getClassLoader();
195 }
196
197
198
199
200
201
202
203
204
205
206 public static File writeTrashFile(final Repository db,
207 final String name, final String data) throws IOException {
208 File path = new File(db.getWorkTree(), name);
209 write(path, data);
210 return path;
211 }
212
213
214
215
216
217
218
219
220
221
222
223 public static File writeTrashFile(final Repository db,
224 final String subdir,
225 final String name, final String data) throws IOException {
226 File path = new File(db.getWorkTree() + "/" + subdir, name);
227 write(path, data);
228 return path;
229 }
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245 public static void write(File f, String body)
246 throws IOException {
247 FileUtils.mkdirs(f.getParentFile(), true);
248 try (Writer w = new OutputStreamWriter(new FileOutputStream(f),
249 UTF_8)) {
250 w.write(body);
251 }
252 }
253
254
255
256
257
258
259
260
261
262
263
264 public static String read(File file) throws IOException {
265 final byte[] body = IO.readFully(file);
266 return new String(body, 0, body.length, UTF_8);
267 }
268
269
270
271
272
273
274
275
276
277 public static String read(Repository db, String name)
278 throws IOException {
279 File file = new File(db.getWorkTree(), name);
280 return read(file);
281 }
282
283
284
285
286
287
288
289
290
291 public static boolean check(Repository db, String name) {
292 File file = new File(db.getWorkTree(), name);
293 return file.exists();
294 }
295
296
297
298
299
300
301
302
303 public static void deleteTrashFile(final Repository db,
304 final String name) throws IOException {
305 File path = new File(db.getWorkTree(), name);
306 FileUtils.delete(path);
307 }
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322 public static Path writeLink(Repository db, String link,
323 String target) throws Exception {
324 return FileUtils.createSymLink(new File(db.getWorkTree(), link),
325 target);
326 }
327
328
329
330
331
332
333
334
335
336
337 public static byte[] concat(byte[]... b) {
338 int n = 0;
339 for (byte[] a : b) {
340 n += a.length;
341 }
342
343 byte[] data = new byte[n];
344 n = 0;
345 for (byte[] a : b) {
346 System.arraycopy(a, 0, data, n, a.length);
347 n += a.length;
348 }
349 return data;
350 }
351 }