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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.FileOutputStream;
  48. import java.io.IOException;
  49. import java.io.OutputStreamWriter;
  50. import java.io.Writer;
  51. import java.lang.reflect.Method;
  52. import java.net.URISyntaxException;
  53. import java.net.URL;
  54. import org.eclipse.jgit.storage.file.FileRepository;
  55. import org.eclipse.jgit.util.FileUtils;
  56. import org.eclipse.jgit.util.RawParseUtils;
  57. import org.junit.Assert;
  58. import org.junit.Test;
  59. public abstract class JGitTestUtil {
  60. public static final String CLASSPATH_TO_RESOURCES = "org/eclipse/jgit/test/resources/";
  61. private JGitTestUtil() {
  62. throw new UnsupportedOperationException();
  63. }
  64. public static String getName() {
  65. GatherStackTrace stack;
  66. try {
  67. throw new GatherStackTrace();
  68. } catch (GatherStackTrace wanted) {
  69. stack = wanted;
  70. }
  71. try {
  72. for (StackTraceElement stackTrace : stack.getStackTrace()) {
  73. String className = stackTrace.getClassName();
  74. String methodName = stackTrace.getMethodName();
  75. Method method;
  76. try {
  77. method = Class.forName(className) //
  78. .getMethod(methodName, (Class[]) null);
  79. } catch (NoSuchMethodException e) {
  80. // could be private, i.e. not a test method
  81. // could have arguments, not handled
  82. continue;
  83. }
  84. Test annotation = method.getAnnotation(Test.class);
  85. if (annotation != null)
  86. return methodName;
  87. }
  88. } catch (ClassNotFoundException shouldNeverOccur) {
  89. // Fall through and crash.
  90. }
  91. throw new AssertionError("Cannot determine name of current test");
  92. }
  93. @SuppressWarnings("serial")
  94. private static class GatherStackTrace extends Exception {
  95. // Thrown above to collect the stack frame.
  96. }
  97. public static void assertEquals(byte[] exp, byte[] act) {
  98. Assert.assertEquals(s(exp), s(act));
  99. }
  100. private static String s(byte[] raw) {
  101. return RawParseUtils.decode(raw);
  102. }
  103. public static File getTestResourceFile(final String fileName) {
  104. if (fileName == null || fileName.length() <= 0) {
  105. return null;
  106. }
  107. final URL url = cl().getResource(CLASSPATH_TO_RESOURCES + fileName);
  108. if (url == null) {
  109. // If URL is null then try to load it as it was being
  110. // loaded previously
  111. return new File("tst", fileName);
  112. }
  113. try {
  114. return new File(url.toURI());
  115. } catch(URISyntaxException e) {
  116. return new File(url.getPath());
  117. }
  118. }
  119. private static ClassLoader cl() {
  120. return JGitTestUtil.class.getClassLoader();
  121. }
  122. public static File writeTrashFile(final FileRepository db,
  123. final String name, final String data) throws IOException {
  124. File path = new File(db.getWorkTree(), name);
  125. write(path, data);
  126. return path;
  127. }
  128. public static File writeTrashFile(final FileRepository db,
  129. final String subdir,
  130. final String name, final String data) throws IOException {
  131. File path = new File(db.getWorkTree() + "/" + subdir, name);
  132. write(path, data);
  133. return path;
  134. }
  135. /**
  136. * Write a string as a UTF-8 file.
  137. *
  138. * @param f
  139. * file to write the string to. Caller is responsible for making
  140. * sure it is in the trash directory or will otherwise be cleaned
  141. * up at the end of the test. If the parent directory does not
  142. * exist, the missing parent directories are automatically
  143. * created.
  144. * @param body
  145. * content to write to the file.
  146. * @throws IOException
  147. * the file could not be written.
  148. */
  149. public static void write(final File f, final String body)
  150. throws IOException {
  151. FileUtils.mkdirs(f.getParentFile(), true);
  152. Writer w = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
  153. try {
  154. w.write(body);
  155. } finally {
  156. w.close();
  157. }
  158. }
  159. public static void deleteTrashFile(final FileRepository db,
  160. final String name) throws IOException {
  161. File path = new File(db.getWorkTree(), name);
  162. FileUtils.delete(path);
  163. }
  164. }