You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JGitTestUtil.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Imran M Yousuf <imyousuf@smartitengineering.com>
  4. * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk> and others
  5. *
  6. * This program and the accompanying materials are made available under the
  7. * terms of the Eclipse Distribution License v. 1.0 which is available at
  8. * https://www.eclipse.org/org/documents/edl-v10.php.
  9. *
  10. * SPDX-License-Identifier: BSD-3-Clause
  11. */
  12. package org.eclipse.jgit.junit;
  13. import static java.nio.charset.StandardCharsets.UTF_8;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.io.OutputStreamWriter;
  20. import java.io.Writer;
  21. import java.lang.reflect.Method;
  22. import java.net.URISyntaxException;
  23. import java.net.URL;
  24. import java.nio.file.Path;
  25. import org.eclipse.jgit.lib.Repository;
  26. import org.eclipse.jgit.util.FileUtils;
  27. import org.eclipse.jgit.util.IO;
  28. import org.eclipse.jgit.util.RawParseUtils;
  29. import org.junit.Assert;
  30. import org.junit.Test;
  31. /**
  32. * Abstract test util class
  33. */
  34. public abstract class JGitTestUtil {
  35. /** Constant <code>CLASSPATH_TO_RESOURCES="org/eclipse/jgit/test/resources/"</code> */
  36. public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/";
  37. private JGitTestUtil() {
  38. throw new UnsupportedOperationException();
  39. }
  40. /**
  41. * Get name of current test by inspecting stack trace
  42. *
  43. * @return the name
  44. */
  45. public static String getName() {
  46. GatherStackTrace stack;
  47. try {
  48. throw new GatherStackTrace();
  49. } catch (GatherStackTrace wanted) {
  50. stack = wanted;
  51. }
  52. try {
  53. for (StackTraceElement stackTrace : stack.getStackTrace()) {
  54. String className = stackTrace.getClassName();
  55. String methodName = stackTrace.getMethodName();
  56. Method method;
  57. try {
  58. method = Class.forName(className) //
  59. .getMethod(methodName, (Class[]) null);
  60. } catch (NoSuchMethodException e) {
  61. // could be private, i.e. not a test method
  62. // could have arguments, not handled
  63. continue;
  64. }
  65. Test annotation = method.getAnnotation(Test.class);
  66. if (annotation != null)
  67. return methodName;
  68. }
  69. } catch (ClassNotFoundException shouldNeverOccur) {
  70. // Fall through and crash.
  71. }
  72. throw new AssertionError("Cannot determine name of current test");
  73. }
  74. @SuppressWarnings("serial")
  75. private static class GatherStackTrace extends Exception {
  76. // Thrown above to collect the stack frame.
  77. }
  78. /**
  79. * Assert byte arrays are equal
  80. *
  81. * @param exp
  82. * expected value
  83. * @param act
  84. * actual value
  85. */
  86. public static void assertEquals(byte[] exp, byte[] act) {
  87. Assert.assertEquals(s(exp), s(act));
  88. }
  89. private static String s(byte[] raw) {
  90. return RawParseUtils.decode(raw);
  91. }
  92. /**
  93. * Get test resource file.
  94. *
  95. * @param fileName
  96. * @return the test resource file
  97. */
  98. public static File getTestResourceFile(String fileName) {
  99. if (fileName == null || fileName.length() <= 0) {
  100. return null;
  101. }
  102. final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName);
  103. if (url == null) {
  104. // If URL is null then try to load it as it was being
  105. // loaded previously
  106. return new File("tst", fileName);
  107. }
  108. if ("jar".equals(url.getProtocol())) {
  109. try {
  110. File tmp = File.createTempFile("tmp_", "_" + fileName);
  111. copyTestResource(fileName, tmp);
  112. return tmp;
  113. } catch (IOException err) {
  114. throw new RuntimeException("Cannot create temporary file", err);
  115. }
  116. }
  117. try {
  118. return new File(url.toURI());
  119. } catch (IllegalArgumentException e) {
  120. throw new IllegalArgumentException(e.getMessage() + " " + url);
  121. } catch (URISyntaxException e) {
  122. return new File(url.getPath());
  123. }
  124. }
  125. /**
  126. * Copy test resource.
  127. *
  128. * @param name
  129. * @param dest
  130. * @throws IOException
  131. */
  132. public static void copyTestResource(String name, File dest)
  133. throws IOException {
  134. URL url = cl().getResource(CLASSPATH_TO_RESOURCES + name);
  135. if (url == null)
  136. throw new FileNotFoundException(name);
  137. try (InputStream in = url.openStream();
  138. FileOutputStream out = new FileOutputStream(dest)) {
  139. byte[] buf = new byte[4096];
  140. for (int n; (n = in.read(buf)) > 0;)
  141. out.write(buf, 0, n);
  142. }
  143. }
  144. private static ClassLoader cl() {
  145. return JGitTestUtil.class.getClassLoader();
  146. }
  147. /**
  148. * Write a trash file.
  149. *
  150. * @param db
  151. * @param name
  152. * @param data
  153. * @return the trash file
  154. * @throws IOException
  155. */
  156. public static File writeTrashFile(final Repository db,
  157. final String name, final String data) throws IOException {
  158. File path = new File(db.getWorkTree(), name);
  159. write(path, data);
  160. return path;
  161. }
  162. /**
  163. * Write a trash file.
  164. *
  165. * @param db
  166. * @param subdir
  167. * @param name
  168. * @param data
  169. * @return the trash file
  170. * @throws IOException
  171. */
  172. public static File writeTrashFile(final Repository db,
  173. final String subdir,
  174. final String name, final String data) throws IOException {
  175. File path = new File(db.getWorkTree() + "/" + subdir, name);
  176. write(path, data);
  177. return path;
  178. }
  179. /**
  180. * Write a string as a UTF-8 file.
  181. *
  182. * @param f
  183. * file to write the string to. Caller is responsible for making
  184. * sure it is in the trash directory or will otherwise be cleaned
  185. * up at the end of the test. If the parent directory does not
  186. * exist, the missing parent directories are automatically
  187. * created.
  188. * @param body
  189. * content to write to the file.
  190. * @throws IOException
  191. * the file could not be written.
  192. */
  193. public static void write(File f, String body)
  194. throws IOException {
  195. FileUtils.mkdirs(f.getParentFile(), true);
  196. try (Writer w = new OutputStreamWriter(new FileOutputStream(f),
  197. UTF_8)) {
  198. w.write(body);
  199. }
  200. }
  201. /**
  202. * Fully read a UTF-8 file and return as a string.
  203. *
  204. * @param file
  205. * file to read the content of.
  206. * @return UTF-8 decoded content of the file, empty string if the file
  207. * exists but has no content.
  208. * @throws IOException
  209. * the file does not exist, or could not be read.
  210. */
  211. public static String read(File file) throws IOException {
  212. final byte[] body = IO.readFully(file);
  213. return new String(body, 0, body.length, UTF_8);
  214. }
  215. /**
  216. * Read a file's content
  217. *
  218. * @param db
  219. * @param name
  220. * @return the content of the file
  221. * @throws IOException
  222. */
  223. public static String read(Repository db, String name)
  224. throws IOException {
  225. File file = new File(db.getWorkTree(), name);
  226. return read(file);
  227. }
  228. /**
  229. * Check if file exists
  230. *
  231. * @param db
  232. * @param name
  233. * name of the file
  234. * @return {@code true} if the file exists
  235. */
  236. public static boolean check(Repository db, String name) {
  237. File file = new File(db.getWorkTree(), name);
  238. return file.exists();
  239. }
  240. /**
  241. * Delete a trash file.
  242. *
  243. * @param db
  244. * @param name
  245. * @throws IOException
  246. */
  247. public static void deleteTrashFile(final Repository db,
  248. final String name) throws IOException {
  249. File path = new File(db.getWorkTree(), name);
  250. FileUtils.delete(path);
  251. }
  252. /**
  253. * Write a symbolic link
  254. *
  255. * @param db
  256. * the repository
  257. * @param link
  258. * the path of the symbolic link to create
  259. * @param target
  260. * the target of the symbolic link
  261. * @return the path to the symbolic link
  262. * @throws Exception
  263. * @since 4.2
  264. */
  265. public static Path writeLink(Repository db, String link,
  266. String target) throws Exception {
  267. return FileUtils.createSymLink(new File(db.getWorkTree(), link),
  268. target);
  269. }
  270. /**
  271. * Concatenate byte arrays.
  272. *
  273. * @param b
  274. * byte arrays to combine together.
  275. * @return a single byte array that contains all bytes copied from input
  276. * byte arrays.
  277. * @since 4.9
  278. */
  279. public static byte[] concat(byte[]... b) {
  280. int n = 0;
  281. for (byte[] a : b) {
  282. n += a.length;
  283. }
  284. byte[] data = new byte[n];
  285. n = 0;
  286. for (byte[] a : b) {
  287. System.arraycopy(a, 0, data, n, a.length);
  288. n += a.length;
  289. }
  290. return data;
  291. }
  292. }