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