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.

CLIRepositoryTestCase.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2012, IBM Corporation and others.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.lib;
  44. import static org.junit.Assert.assertEquals;
  45. import java.io.File;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.List;
  49. import org.eclipse.jgit.junit.JGitTestUtil;
  50. import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
  51. import org.eclipse.jgit.pgm.CLIGitCommand;
  52. import org.junit.Before;
  53. public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
  54. /** Test repository, initialized for this test case. */
  55. protected Repository db;
  56. /** Working directory of {@link #db}. */
  57. protected File trash;
  58. @Override
  59. @Before
  60. public void setUp() throws Exception {
  61. super.setUp();
  62. db = createWorkRepository();
  63. trash = db.getWorkTree();
  64. }
  65. protected String[] execute(String... cmds) throws Exception {
  66. List<String> result = new ArrayList<String>(cmds.length);
  67. for (String cmd : cmds)
  68. result.addAll(CLIGitCommand.execute(cmd, db));
  69. return result.toArray(new String[0]);
  70. }
  71. protected File writeTrashFile(final String name, final String data)
  72. throws IOException {
  73. return JGitTestUtil.writeTrashFile(db, name, data);
  74. }
  75. protected String read(final File file) throws IOException {
  76. return JGitTestUtil.read(file);
  77. }
  78. protected void deleteTrashFile(final String name) throws IOException {
  79. JGitTestUtil.deleteTrashFile(db, name);
  80. }
  81. /**
  82. * Execute the given commands and print the output to stdout. Use this
  83. * function instead of the normal {@link #execute(String...)} when preparing
  84. * a test case: the command is executed and then its output is printed on
  85. * stdout, thus making it easier to prepare the correct command and expected
  86. * output for the test case.
  87. *
  88. * @param cmds
  89. * The commands to execute
  90. * @return the result of the command, see {@link #execute(String...)}
  91. * @throws Exception
  92. */
  93. protected String[] executeAndPrint(String... cmds) throws Exception {
  94. String[] lines = execute(cmds);
  95. for (String line : lines) {
  96. System.out.println(line);
  97. }
  98. return lines;
  99. }
  100. /**
  101. * Execute the given commands and print test code comparing expected and
  102. * actual output. Use this function instead of the normal
  103. * {@link #execute(String...)} when preparing a test case: the command is
  104. * executed and test code is generated using the command output as a
  105. * template of what is expected. The code generated is printed on stdout and
  106. * can be pasted in the test case function.
  107. *
  108. * @param cmds
  109. * The commands to execute
  110. * @return the result of the command, see {@link #execute(String...)}
  111. * @throws Exception
  112. */
  113. protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
  114. String[] lines = execute(cmds);
  115. String cmdString = cmdString(cmds);
  116. if (lines.length == 0)
  117. System.out.println("\t\tassertTrue(execute(" + cmdString
  118. + ").length == 0);");
  119. else {
  120. System.out
  121. .println("\t\tassertArrayOfLinesEquals(new String[] { //");
  122. System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
  123. for (int i=1; i<lines.length; i++) {
  124. System.out.println("\", //");
  125. System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
  126. }
  127. System.out.println("\" //");
  128. System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
  129. }
  130. return lines;
  131. }
  132. protected String cmdString(String... cmds) {
  133. if (cmds.length == 0)
  134. return "";
  135. else if (cmds.length == 1)
  136. return "\"" + escapeJava(cmds[0]) + "\"";
  137. else {
  138. StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
  139. for (int i=1; i<cmds.length; i++) {
  140. sb.append(", ");
  141. sb.append(cmdString(cmds[i]));
  142. }
  143. return sb.toString();
  144. }
  145. }
  146. protected String escapeJava(String line) {
  147. // very crude implementation but ok for generating test code
  148. return line.replaceAll("\"", "\\\\\"") //
  149. .replaceAll("\\\\", "\\\\\\")
  150. .replaceAll("\t", "\\\\t");
  151. }
  152. protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
  153. assertEquals(toText(expected), toText(actual));
  154. }
  155. private static String toText(String[] lines) {
  156. StringBuilder b = new StringBuilder();
  157. for (String s : lines) {
  158. b.append(s);
  159. b.append('\n');
  160. }
  161. return b.toString();
  162. }
  163. }