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