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.3KB

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