View Javadoc
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  
45  import static org.junit.Assert.assertEquals;
46  
47  import java.io.File;
48  import java.io.IOException;
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  import org.eclipse.jgit.junit.JGitTestUtil;
53  import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
54  import org.eclipse.jgit.pgm.CLIGitCommand;
55  import org.junit.Before;
56  
57  public class CLIRepositoryTestCase extends LocalDiskRepositoryTestCase {
58  	/** Test repository, initialized for this test case. */
59  	protected Repository db;
60  
61  	/** Working directory of {@link #db}. */
62  	protected File trash;
63  
64  	@Override
65  	@Before
66  	public void setUp() throws Exception {
67  		super.setUp();
68  		db = createWorkRepository();
69  		trash = db.getWorkTree();
70  	}
71  
72  	protected String[] execute(String... cmds) throws Exception {
73  		List<String> result = new ArrayList<String>(cmds.length);
74  		for (String cmd : cmds)
75  			result.addAll(CLIGitCommand.execute(cmd, db));
76  		return result.toArray(new String[0]);
77  	}
78  
79  	protected File writeTrashFile(final String name, final String data)
80  			throws IOException {
81  		return JGitTestUtil.writeTrashFile(db, name, data);
82  	}
83  
84  	protected String read(final File file) throws IOException {
85  		return JGitTestUtil.read(file);
86  	}
87  
88  	protected void deleteTrashFile(final String name) throws IOException {
89  		JGitTestUtil.deleteTrashFile(db, name);
90  	}
91  
92  	/**
93  	 * Execute the given commands and print the output to stdout. Use this
94  	 * function instead of the normal {@link #execute(String...)} when preparing
95  	 * a test case: the command is executed and then its output is printed on
96  	 * stdout, thus making it easier to prepare the correct command and expected
97  	 * output for the test case.
98  	 *
99  	 * @param cmds
100 	 *            The commands to execute
101 	 * @return the result of the command, see {@link #execute(String...)}
102 	 * @throws Exception
103 	 */
104 	protected String[] executeAndPrint(String... cmds) throws Exception {
105 		String[] lines = execute(cmds);
106 		for (String line : lines) {
107 			System.out.println(line);
108 		}
109 		return lines;
110 	}
111 
112 	/**
113 	 * Execute the given commands and print test code comparing expected and
114 	 * actual output. Use this function instead of the normal
115 	 * {@link #execute(String...)} when preparing a test case: the command is
116 	 * executed and test code is generated using the command output as a
117 	 * template of what is expected. The code generated is printed on stdout and
118 	 * can be pasted in the test case function.
119 	 *
120 	 * @param cmds
121 	 *            The commands to execute
122 	 * @return the result of the command, see {@link #execute(String...)}
123 	 * @throws Exception
124 	 */
125 	protected String[] executeAndPrintTestCode(String... cmds) throws Exception {
126 		String[] lines = execute(cmds);
127 		String cmdString = cmdString(cmds);
128 		if (lines.length == 0)
129 			System.out.println("\t\tassertTrue(execute(" + cmdString
130 					+ ").length == 0);");
131 		else {
132 			System.out
133 					.println("\t\tassertArrayOfLinesEquals(new String[] { //");
134 			System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[0]));
135 			for (int i=1; i<lines.length; i++) {
136 				System.out.println("\", //");
137 				System.out.print("\t\t\t\t\t\t\"" + escapeJava(lines[i]));
138 			}
139 			System.out.println("\" //");
140 			System.out.println("\t\t\t\t}, execute(" + cmdString + ")); //");
141 		}
142 		return lines;
143 	}
144 
145 	protected String cmdString(String... cmds) {
146 		if (cmds.length == 0)
147 			return "";
148 		else if (cmds.length == 1)
149 			return "\"" + escapeJava(cmds[0]) + "\"";
150 		else {
151 			StringBuilder sb = new StringBuilder(cmdString(cmds[0]));
152 			for (int i=1; i<cmds.length; i++) {
153 				sb.append(", ");
154 				sb.append(cmdString(cmds[i]));
155 			}
156 			return sb.toString();
157 		}
158 	}
159 
160 	protected String escapeJava(String line) {
161 		// very crude implementation but ok for generating test code
162 		return line.replaceAll("\"", "\\\\\"") //
163 				.replaceAll("\\\\", "\\\\\\")
164 				.replaceAll("\t", "\\\\t");
165 	}
166 
167 	protected void assertArrayOfLinesEquals(String[] expected, String[] actual) {
168 		assertEquals(toText(expected), toText(actual));
169 	}
170 
171 	private static String toText(String[] lines) {
172 		StringBuilder b = new StringBuilder();
173 		for (String s : lines) {
174 			b.append(s);
175 			b.append('\n');
176 		}
177 		return b.toString();
178 	}
179 }