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 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.junit;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import java.io.File;
  48. import java.io.FileNotFoundException;
  49. import java.io.FileOutputStream;
  50. import java.io.IOException;
  51. import java.io.InputStream;
  52. import java.io.OutputStreamWriter;
  53. import java.io.Writer;
  54. import java.lang.reflect.Method;
  55. import java.net.URISyntaxException;
  56. import java.net.URL;
  57. import java.nio.file.Path;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.util.FileUtils;
  60. import org.eclipse.jgit.util.IO;
  61. import org.eclipse.jgit.util.RawParseUtils;
  62. import org.junit.Assert;
  63. import org.junit.Test;
  64. /**
  65. * Abstract test util class
  66. */
  67. public abstract class JGitTestUtil {
  68. /** Constant <code>CLASSPATH_TO_RESOURCES="org/eclipse/jgit/test/resources/"</code> */
  69. public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/";
  70. private JGitTestUtil() {
  71. throw new UnsupportedOperationException();
  72. }
  73. /**
  74. * Get name of current test by inspecting stack trace
  75. *
  76. * @return the name
  77. */
  78. public static String getName() {
  79. GatherStackTrace stack;
  80. try {
  81. throw new GatherStackTrace();
  82. } catch (GatherStackTrace wanted) {
  83. stack = wanted;
  84. }
  85. try {
  86. for (StackTraceElement stackTrace : stack.getStackTrace()) {
  87. String className = stackTrace.getClassName();
  88. String methodName = stackTrace.getMethodName();
  89. Method method;
  90. try {
  91. method = Class.forName(className) //
  92. .getMethod(methodName, (Class[]) null);
  93. } catch (NoSuchMethodException e) {
  94. // could be private, i.e. not a test method
  95. // could have arguments, not handled
  96. continue;
  97. }
  98. Test annotation = method.getAnnotation(Test.class);
  99. if (annotation != null)
  100. return methodName;
  101. }
  102. } catch (ClassNotFoundException shouldNeverOccur) {
  103. // Fall through and crash.
  104. }
  105. throw new AssertionError("Cannot determine name of current test");
  106. }
  107. @SuppressWarnings("serial")
  108. private static class GatherStackTrace extends Exception {
  109. // Thrown above to collect the stack frame.
  110. }
  111. /**
  112. * Assert byte arrays are equal
  113. *
  114. * @param exp
  115. * expected value
  116. * @param act
  117. * actual value
  118. */
  119. public static void assertEquals(byte[] exp, byte[] act) {
  120. Assert.assertEquals(s(exp), s(act));
  121. }
  122. private static String s(byte[] raw) {
  123. return RawParseUtils.decode(raw);
  124. }
  125. /**
  126. * Get test resource file.
  127. *
  128. * @param fileName
  129. * @return the test resource file
  130. */
  131. public static File getTestResourceFile(String fileName) {
  132. if (fileName == null || fileName.length() <= 0) {
  133. return null;
  134. }
  135. final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName);
  136. if (url == null) {
  137. // If URL is null then try to load it as it was being
  138. // loaded previously
  139. return new File("tst", fileName);
  140. }
  141. if ("jar".equals(url.getProtocol())) {
  142. try {
  143. File tmp = File.createTempFile("tmp_", "_" + fileName);
  144. copyTestResource(fileName, tmp);
  145. return tmp;
  146. } catch (IOException err) {
  147. throw new RuntimeException("Cannot create temporary file", err);
  148. }
  149. }
  150. try {
  151. return new File(url.toURI());
  152. } catch (IllegalArgumentException e) {
  153. throw new IllegalArgumentException(e.getMessage() + " " + url);
  154. } catch (URISyntaxException e) {
  155. return new File(url.getPath());
  156. }
  157. }
  158. /**
  159. * Copy test resource.
  160. *
  161. * @param name
  162. * @param dest
  163. * @throws IOException
  164. */
  165. public static void copyTestResource(String name, File dest)
  166. throws IOException {
  167. URL url = cl().getResource(CLASSPATH_TO_RESOURCES + name);
  168. if (url == null)
  169. throw new FileNotFoundException(name);
  170. try (InputStream in = url.openStream();
  171. FileOutputStream out = new FileOutputStream(dest)) {
  172. byte[] buf = new byte[4096];
  173. for (int n; (n = in.read(buf)) > 0;)
  174. out.write(buf, 0, n);
  175. }
  176. }
  177. private static ClassLoader cl() {
  178. return JGitTestUtil.class.getClassLoader();
  179. }
  180. /**
  181. * Write a trash file.
  182. *
  183. * @param db
  184. * @param name
  185. * @param data
  186. * @return the trash file
  187. * @throws IOException
  188. */
  189. public static File writeTrashFile(final Repository db,
  190. final String name, final String data) throws IOException {
  191. File path = new File(db.getWorkTree(), name);
  192. write(path, data);
  193. return path;
  194. }
  195. /**
  196. * Write a trash file.
  197. *
  198. * @param db
  199. * @param subdir
  200. * @param name
  201. * @param data
  202. * @return the trash file
  203. * @throws IOException
  204. */
  205. public static File writeTrashFile(final Repository db,
  206. final String subdir,
  207. final String name, final String data) throws IOException {
  208. File path = new File(db.getWorkTree() + "/" + subdir, name);
  209. write(path, data);
  210. return path;
  211. }
  212. /**
  213. * Write a string as a UTF-8 file.
  214. *
  215. * @param f
  216. * file to write the string to. Caller is responsible for making
  217. * sure it is in the trash directory or will otherwise be cleaned
  218. * up at the end of the test. If the parent directory does not
  219. * exist, the missing parent directories are automatically
  220. * created.
  221. * @param body
  222. * content to write to the file.
  223. * @throws IOException
  224. * the file could not be written.
  225. */
  226. public static void write(File f, String body)
  227. throws IOException {
  228. FileUtils.mkdirs(f.getParentFile(), true);
  229. try (Writer w = new OutputStreamWriter(new FileOutputStream(f),
  230. UTF_8)) {
  231. w.write(body);
  232. }
  233. }
  234. /**
  235. * Fully read a UTF-8 file and return as a string.
  236. *
  237. * @param file
  238. * file to read the content of.
  239. * @return UTF-8 decoded content of the file, empty string if the file
  240. * exists but has no content.
  241. * @throws IOException
  242. * the file does not exist, or could not be read.
  243. */
  244. public static String read(File file) throws IOException {
  245. final byte[] body = IO.readFully(file);
  246. return new String(body, 0, body.length, UTF_8);
  247. }
  248. /**
  249. * Read a file's content
  250. *
  251. * @param db
  252. * @param name
  253. * @return the content of the file
  254. * @throws IOException
  255. */
  256. public static String read(Repository db, String name)
  257. throws IOException {
  258. File file = new File(db.getWorkTree(), name);
  259. return read(file);
  260. }
  261. /**
  262. * Check if file exists
  263. *
  264. * @param db
  265. * @param name
  266. * name of the file
  267. * @return {@code true} if the file exists
  268. */
  269. public static boolean check(Repository db, String name) {
  270. File file = new File(db.getWorkTree(), name);
  271. return file.exists();
  272. }
  273. /**
  274. * Delete a trash file.
  275. *
  276. * @param db
  277. * @param name
  278. * @throws IOException
  279. */
  280. public static void deleteTrashFile(final Repository db,
  281. final String name) throws IOException {
  282. File path = new File(db.getWorkTree(), name);
  283. FileUtils.delete(path);
  284. }
  285. /**
  286. * Write a symbolic link
  287. *
  288. * @param db
  289. * the repository
  290. * @param link
  291. * the path of the symbolic link to create
  292. * @param target
  293. * the target of the symbolic link
  294. * @return the path to the symbolic link
  295. * @throws Exception
  296. * @since 4.2
  297. */
  298. public static Path writeLink(Repository db, String link,
  299. String target) throws Exception {
  300. return FileUtils.createSymLink(new File(db.getWorkTree(), link),
  301. target);
  302. }
  303. /**
  304. * Concatenate byte arrays.
  305. *
  306. * @param b
  307. * byte arrays to combine together.
  308. * @return a single byte array that contains all bytes copied from input
  309. * byte arrays.
  310. * @since 4.9
  311. */
  312. public static byte[] concat(byte[]... b) {
  313. int n = 0;
  314. for (byte[] a : b) {
  315. n += a.length;
  316. }
  317. byte[] data = new byte[n];
  318. n = 0;
  319. for (byte[] a : b) {
  320. System.arraycopy(a, 0, data, n, a.length);
  321. n += a.length;
  322. }
  323. return data;
  324. }
  325. }