Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

JGitTestUtil.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 java.io.File;
  47. import java.io.FileNotFoundException;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import java.io.OutputStreamWriter;
  52. import java.io.Writer;
  53. import java.lang.reflect.Method;
  54. import java.net.URISyntaxException;
  55. import java.net.URL;
  56. import java.nio.file.Path;
  57. import org.eclipse.jgit.lib.Repository;
  58. import org.eclipse.jgit.util.FileUtils;
  59. import org.eclipse.jgit.util.IO;
  60. import org.eclipse.jgit.util.RawParseUtils;
  61. import org.junit.Assert;
  62. import org.junit.Test;
  63. public abstract class JGitTestUtil {
  64. public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/";
  65. private JGitTestUtil() {
  66. throw new UnsupportedOperationException();
  67. }
  68. public static String getName() {
  69. GatherStackTrace stack;
  70. try {
  71. throw new GatherStackTrace();
  72. } catch (GatherStackTrace wanted) {
  73. stack = wanted;
  74. }
  75. try {
  76. for (StackTraceElement stackTrace : stack.getStackTrace()) {
  77. String className = stackTrace.getClassName();
  78. String methodName = stackTrace.getMethodName();
  79. Method method;
  80. try {
  81. method = Class.forName(className) //
  82. .getMethod(methodName, (Class[]) null);
  83. } catch (NoSuchMethodException e) {
  84. // could be private, i.e. not a test method
  85. // could have arguments, not handled
  86. continue;
  87. }
  88. Test annotation = method.getAnnotation(Test.class);
  89. if (annotation != null)
  90. return methodName;
  91. }
  92. } catch (ClassNotFoundException shouldNeverOccur) {
  93. // Fall through and crash.
  94. }
  95. throw new AssertionError("Cannot determine name of current test");
  96. }
  97. @SuppressWarnings("serial")
  98. private static class GatherStackTrace extends Exception {
  99. // Thrown above to collect the stack frame.
  100. }
  101. public static void assertEquals(byte[] exp, byte[] act) {
  102. Assert.assertEquals(s(exp), s(act));
  103. }
  104. private static String s(byte[] raw) {
  105. return RawParseUtils.decode(raw);
  106. }
  107. public static File getTestResourceFile(final String fileName) {
  108. if (fileName == null || fileName.length() <= 0) {
  109. return null;
  110. }
  111. final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName);
  112. if (url == null) {
  113. // If URL is null then try to load it as it was being
  114. // loaded previously
  115. return new File("tst", fileName);
  116. }
  117. if ("jar".equals(url.getProtocol())) {
  118. try {
  119. File tmp = File.createTempFile("tmp_", "_" + fileName);
  120. copyTestResource(fileName, tmp);
  121. return tmp;
  122. } catch (IOException err) {
  123. throw new RuntimeException("Cannot create temporary file", err);
  124. }
  125. }
  126. try {
  127. return new File(url.toURI());
  128. } catch (IllegalArgumentException e) {
  129. throw new IllegalArgumentException(e.getMessage() + " " + url);
  130. } catch (URISyntaxException e) {
  131. return new File(url.getPath());
  132. }
  133. }
  134. public static void copyTestResource(String name, File dest)
  135. throws IOException {
  136. URL url = cl().getResource(CLASSPATH_TO_RESOURCES + name);
  137. if (url == null)
  138. throw new FileNotFoundException(name);
  139. InputStream in = url.openStream();
  140. try {
  141. FileOutputStream out = new FileOutputStream(dest);
  142. try {
  143. byte[] buf = new byte[4096];
  144. for (int n; (n = in.read(buf)) > 0;)
  145. out.write(buf, 0, n);
  146. } finally {
  147. out.close();
  148. }
  149. } finally {
  150. in.close();
  151. }
  152. }
  153. private static ClassLoader cl() {
  154. return JGitTestUtil.class.getClassLoader();
  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. public static File writeTrashFile(final Repository db,
  163. final String subdir,
  164. final String name, final String data) throws IOException {
  165. File path = new File(db.getWorkTree() + "/" + subdir, name);
  166. write(path, data);
  167. return path;
  168. }
  169. /**
  170. * Write a string as a UTF-8 file.
  171. *
  172. * @param f
  173. * file to write the string to. Caller is responsible for making
  174. * sure it is in the trash directory or will otherwise be cleaned
  175. * up at the end of the test. If the parent directory does not
  176. * exist, the missing parent directories are automatically
  177. * created.
  178. * @param body
  179. * content to write to the file.
  180. * @throws IOException
  181. * the file could not be written.
  182. */
  183. public static void write(final File f, final String body)
  184. throws IOException {
  185. FileUtils.mkdirs(f.getParentFile(), true);
  186. Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
  187. try {
  188. w.write(body);
  189. } finally {
  190. w.close();
  191. }
  192. }
  193. /**
  194. * Fully read a UTF-8 file and return as a string.
  195. *
  196. * @param file
  197. * file to read the content of.
  198. * @return UTF-8 decoded content of the file, empty string if the file
  199. * exists but has no content.
  200. * @throws IOException
  201. * the file does not exist, or could not be read.
  202. */
  203. public static String read(final File file) throws IOException {
  204. final byte[] body = IO.readFully(file);
  205. return new String(body, 0, body.length, "UTF-8");
  206. }
  207. public static String read(final Repository db, final String name)
  208. throws IOException {
  209. File file = new File(db.getWorkTree(), name);
  210. return read(file);
  211. }
  212. public static boolean check(final Repository db, final String name) {
  213. File file = new File(db.getWorkTree(), name);
  214. return file.exists();
  215. }
  216. public static void deleteTrashFile(final Repository db,
  217. final String name) throws IOException {
  218. File path = new File(db.getWorkTree(), name);
  219. FileUtils.delete(path);
  220. }
  221. /**
  222. * @param db
  223. * the repository
  224. * @param link
  225. * the path of the symbolic link to create
  226. * @param target
  227. * the target of the symbolic link
  228. * @return the path to the symbolic link
  229. * @throws Exception
  230. * @since 4.2
  231. */
  232. public static Path writeLink(Repository db, String link,
  233. String target) throws Exception {
  234. return FileUtils.createSymLink(new File(db.getWorkTree(), link),
  235. target);
  236. }
  237. }