aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrançois Rey2012-09-13 18:11:18 (EDT)
committerChris Aniszczyk2013-03-27 09:26:13 (EDT)
commit741ecf56b7eab161326806d566ef22c911496789 (patch)
treee63c0f225ad81972f096463f5a4b46244a7050c5
parentedd47d10b9e96bde1958b19d04feef3cc8bf00fc (diff)
downloadjgit-741ecf56b7eab161326806d566ef22c911496789.zip
jgit-741ecf56b7eab161326806d566ef22c911496789.tar.gz
jgit-741ecf56b7eab161326806d566ef22c911496789.tar.bz2
New functions to facilitate the writing of CLI test casesrefs/changes/63/5963/6
Writing CLI test cases is tedious because of all the formatting and escaping subtleties needed when comparing actual output with what's expected. While creating a test case the two new functions are to be used instead of the existing execute() in order to prepare the correct command and expected output and to generate the corresponding test code that can be pasted into the test case function. Change-Id: Ia66dc449d3f6fb861c300fef8b56fba83a56c94c Signed-off-by: Chris Aniszczyk <zx@twitter.com>
-rw-r--r--org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java90
-rw-r--r--org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java14
2 files changed, 90 insertions, 14 deletions
diff --git a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
index 400927e..755174b 100644
--- a/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
+++ b/org.eclipse.jgit.pgm.test/src/org/eclipse/jgit/lib/CLIRepositoryTestCase.java
@@ -42,6 +42,8 @@
*/
package org.eclipse.jgit.lib;
+import static org.junit.Assert.assertEquals;
+
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -83,4 +85,92 @@ public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
protected void deleteTrashFile(final String name) throws IOException {
JGitTestUtil.deleteTrashFile(db, name);
}
+
+ /**
+ * Execute the given commands and print the output to stdout. Use this
+ * function instead of the normal {@link #execute(String...)} when preparing
+ * a test case: the command is executed and then its output is printed on
+ * stdout, thus making it easier to prepare the correct command and expected
+ * output for the test case.
+ *
+ * @param cmds
+ * The commands to execute
+ * @return the result of the command, see {@link #execute(String...)}
+ * @throws Exception
+ */
+ protected String[] executeAndPrint(String... cmds) throws Exception {
+ String[] lines = execute(cmds);
+ for (String line : lines) {
+ System.out.println(line);
+ }
+ return lines;
+ }
+
+ /**
+ * Execute the given commands and print test code comparing expected and
+ * actual output. Use this function instead of the normal
+ * {@link #execute(String...)} when preparing a test case: the command is
+ * executed and test code is generated using the command output as a
+ * template of what is expected. The code generated is printed on stdout and
+ * can be pasted in the test case function.
+ *
+ * @param cmds
+ * The commands to execute
+ * @return the result of the command, see {@link #execute(String...)}
+ * @throws Exception
+ */
+ protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
+ String[] lines = execute(cmds);
+ String cmdString = cmdString(cmds);
+ if (lines.length == 0)
+ System.out.println("\t\tassertTrue(execute(" + cmdString
+ + ").length == 0);");
+ else {
+ System.out
+ .println("\t\tassertArrayOfLinesEquals(new String[] { //");
+ System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
+ for (int i=1; i<lines.length; i++) {
+ System.out.println("\", //");
+ System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
+ }
+ System.out.println("\" //");
+ System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
+ }
+ return lines;
+ }
+
+ protected String cmdString(String... cmds) {
+ if (cmds.length == 0)
+ return "";
+ else if (cmds.length == 1)
+ return "\"" + escapeJava(cmds[0]) + "\"";
+ else {
+ StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
+ for (int i=1; i<cmds.length; i++) {
+ sb.append(", ");
+ sb.append(cmdString(cmds[i]));
+ }
+ return sb.toString();
+ }
+ }
+
+ protected String escapeJava(String line) {
+ // very crude implementation but ok for generating test code
+ return line.replaceAll("\"", "\\\\\"") //
+ .replaceAll("\\\\", "\\\\\\")
+ .replaceAll("\t", "\\\\t");
+ }
+
+ protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
+ assertEquals(toText(expected), toText(actual));
+ }
+
+ private String toText(String[] lines) {
+ StringBuilder b = new StringBuilder();
+ for (String s : lines) {
+ b.append(s);
+ b.append('\n');
+ }
+ return b.toString();
+ }
}
diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java
index ed2c5d0..13f8c31 100644
--- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java
+++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/StatusTest.java
@@ -42,7 +42,6 @@
*/
package org.eclipse.jgit.pgm;
-import static org.junit.Assert.assertEquals;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
@@ -214,17 +213,4 @@ public class StatusTest extends CLIRepositoryTestCase {
"" //
}, execute("git status")); //
}
-
- private void assertArrayOfLinesEquals(String[] expected, String[] actual) {
- assertEquals(toText(expected), toText(actual));
- }
-
- private String toText(String[] lines) {
- StringBuilder b = new StringBuilder();
- for (String s : lines) {
- b.append(s);
- b.append('\n');
- }
- return b.toString();
- }
}