comparison test/org/tmatesoft/hg/test/TestStatus.java @ 103:0b2dcca7de9f

ErrorCollector in tests to grab multiple errors
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 28 Jan 2011 04:57:46 +0100
parents a3a2e5deb320
children dd4d2d0e42cd
comparison
equal deleted inserted replaced
102:a3a2e5deb320 103:0b2dcca7de9f
14 * the terms of a license other than GNU General Public License 14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com 15 * contact TMate Software at support@hg4j.com
16 */ 16 */
17 package org.tmatesoft.hg.test; 17 package org.tmatesoft.hg.test;
18 18
19 import static org.hamcrest.CoreMatchers.equalTo;
19 import static org.tmatesoft.hg.repo.HgRepository.TIP; 20 import static org.tmatesoft.hg.repo.HgRepository.TIP;
20 21
21 import java.util.Collection; 22 import java.util.Collection;
23 import java.util.Collections;
22 import java.util.HashMap; 24 import java.util.HashMap;
23 import java.util.LinkedList; 25 import java.util.LinkedList;
24 import java.util.List; 26 import java.util.List;
25 27
26 import org.junit.Assume; 28 import org.junit.Assume;
29 import org.junit.Rule;
27 import org.junit.Test; 30 import org.junit.Test;
28 import org.tmatesoft.hg.core.Path; 31 import org.tmatesoft.hg.core.Path;
29 import org.tmatesoft.hg.core.StatusCommand; 32 import org.tmatesoft.hg.core.StatusCommand;
30 import org.tmatesoft.hg.repo.HgLookup; 33 import org.tmatesoft.hg.repo.HgLookup;
31 import org.tmatesoft.hg.repo.HgRepository; 34 import org.tmatesoft.hg.repo.HgRepository;
38 * @author Artem Tikhomirov 41 * @author Artem Tikhomirov
39 * @author TMate Software Ltd. 42 * @author TMate Software Ltd.
40 */ 43 */
41 public class TestStatus { 44 public class TestStatus {
42 45
46 @Rule
47 public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
48
43 private final HgRepository repo; 49 private final HgRepository repo;
44 private StatusOutputParser statusParser; 50 private StatusOutputParser statusParser;
45 private ExecHelper eh; 51 private ExecHelper eh;
46 52
47 public static void main(String[] args) throws Exception { 53 public static void main(String[] args) throws Throwable {
48 TestStatus test = new TestStatus(); 54 TestStatus test = new TestStatus();
49 test.testLowLevel(); 55 test.testLowLevel();
50 test.testStatusCommand(); 56 test.testStatusCommand();
51 test.testPerformance(); 57 test.testPerformance();
58 test.errorCollector.verify();
52 } 59 }
53 60
54 public TestStatus() throws Exception { 61 public TestStatus() throws Exception {
55 this(new HgLookup().detectFromWorkingDir()); 62 this(new HgLookup().detectFromWorkingDir());
56 } 63 }
142 final long end = System.currentTimeMillis(); 149 final long end = System.currentTimeMillis();
143 System.out.printf("'hg status -A --rev 3:80', %d runs: Native client total %d (%d per run), Java client %d (%d)\n", runs, start2-start1, (start2-start1)/runs, end-start2, (end-start2)/runs); 150 System.out.printf("'hg status -A --rev 3:80', %d runs: Native client total %d (%d per run), Java client %d (%d)\n", runs, start2-start1, (start2-start1)/runs, end-start2, (end-start2)/runs);
144 } 151 }
145 152
146 153
147 private static void report(String what, HgStatusCollector.Record r, StatusOutputParser statusParser) { 154 private void report(String what, HgStatusCollector.Record r, StatusOutputParser statusParser) {
148 System.out.println(">>>" + what); 155 reportNotEqual(what + "#MODIFIED", r.getModified(), statusParser.getModified());
149 reportNotEqual("MODIFIED", r.getModified(), statusParser.getModified()); 156 reportNotEqual(what + "#ADDED", r.getAdded(), statusParser.getAdded());
150 reportNotEqual("ADDED", r.getAdded(), statusParser.getAdded()); 157 reportNotEqual(what + "#REMOVED", r.getRemoved(), statusParser.getRemoved());
151 reportNotEqual("REMOVED", r.getRemoved(), statusParser.getRemoved()); 158 reportNotEqual(what + "#CLEAN", r.getClean(), statusParser.getClean());
152 reportNotEqual("CLEAN", r.getClean(), statusParser.getClean()); 159 reportNotEqual(what + "#IGNORED", r.getIgnored(), statusParser.getIgnored());
153 reportNotEqual("IGNORED", r.getIgnored(), statusParser.getIgnored()); 160 reportNotEqual(what + "#MISSING", r.getMissing(), statusParser.getMissing());
154 reportNotEqual("MISSING", r.getMissing(), statusParser.getMissing()); 161 reportNotEqual(what + "#UNKNOWN", r.getUnknown(), statusParser.getUnknown());
155 reportNotEqual("UNKNOWN", r.getUnknown(), statusParser.getUnknown());
156 List<Path> copiedKeyDiff = difference(r.getCopied().keySet(), statusParser.getCopied().keySet()); 162 List<Path> copiedKeyDiff = difference(r.getCopied().keySet(), statusParser.getCopied().keySet());
157 HashMap<Path, String> copyDiff = new HashMap<Path,String>(); 163 HashMap<Path, String> copyDiff = new HashMap<Path,String>();
158 if (copiedKeyDiff.isEmpty()) { 164 if (copiedKeyDiff.isEmpty()) {
159 for (Path jk : r.getCopied().keySet()) { 165 for (Path jk : r.getCopied().keySet()) {
160 Path jv = r.getCopied().get(jk); 166 Path jv = r.getCopied().get(jk);
166 } else { 172 } else {
167 copyDiff.put(jk, "ERRONEOUSLY REPORTED IN JAVA"); 173 copyDiff.put(jk, "ERRONEOUSLY REPORTED IN JAVA");
168 } 174 }
169 } 175 }
170 } 176 }
171 System.out.println("COPIED" + (copiedKeyDiff.isEmpty() && copyDiff.isEmpty() ? " are the same" : " are NOT the same:")); 177 errorCollector.checkThat(what + "#Non-matching 'copied' keys: ", copiedKeyDiff, equalTo(Collections.<Path>emptyList()));
172 for (Path s : copiedKeyDiff) { 178 errorCollector.checkThat(what + "#COPIED", copyDiff, equalTo(Collections.<Path,String>emptyMap()));
173 System.out.println("\tNon-matching key:" + s);
174 }
175 for (Path s : copyDiff.keySet()) {
176 System.out.println(s + " : " + copyDiff.get(s));
177 }
178 // TODO compare equals
179 System.out.println("<<<\n");
180 } 179 }
181 180
182 private static <T> void reportNotEqual(String what, Collection<T> l1, Collection<T> l2) { 181 private <T> void reportNotEqual(String what, Collection<T> l1, Collection<T> l2) {
183 List<T> diff = difference(l1, l2); 182 List<T> diff = difference(l1, l2);
184 System.out.print(what); 183 errorCollector.checkThat(what, diff, equalTo(Collections.<T>emptyList()));
185 if (!diff.isEmpty()) {
186 System.out.print(" are NOT the same: ");
187 for (T t : diff) {
188 System.out.print(t);
189 System.out.print(", ");
190 }
191 System.out.println();
192 } else {
193 System.out.println(" are the same");
194 }
195 } 184 }
196 185
197 private static <T> List<T> difference(Collection<T> l1, Collection<T> l2) { 186 private static <T> List<T> difference(Collection<T> l1, Collection<T> l2) {
198 LinkedList<T> result = new LinkedList<T>(l2); 187 LinkedList<T> result = new LinkedList<T>(l2);
199 for (T t : l1) { 188 for (T t : l1) {