changeset 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 54562de502f7
files .hgignore TODO build.xml test/org/tmatesoft/hg/test/ErrorCollectorExt.java test/org/tmatesoft/hg/test/TestHistory.java test/org/tmatesoft/hg/test/TestManifest.java test/org/tmatesoft/hg/test/TestStatus.java
diffstat 7 files changed, 81 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Fri Jan 28 03:50:52 2011 +0100
+++ b/.hgignore	Fri Jan 28 04:57:46 2011 +0100
@@ -4,3 +4,4 @@
 hg4j.jar
 hg4j-tests.jar
 hg4j-console.jar
+TEST-*.xml
--- a/TODO	Fri Jan 28 03:50:52 2011 +0100
+++ b/TODO	Fri Jan 28 04:57:46 2011 +0100
@@ -24,6 +24,7 @@
   + glob
   + pattern
 
+* Tests with JUnit 
 
 Proposed:
 - LogCommand.revision(int... rev)+ to walk selected revisions only (list->sort(array) on execute, binary search)
--- a/build.xml	Fri Jan 28 03:50:52 2011 +0100
+++ b/build.xml	Fri Jan 28 04:57:46 2011 +0100
@@ -40,8 +40,10 @@
     </target>
 	
 	<target name="tests" depends="build-tests">
-		<junit printsummary="on">
+		<junit>
 			<classpath path="hg4j.jar;hg4j-tests.jar;${junit.jar}"/>
+			<formatter type="xml"/>
+			<formatter type="plain" usefile="no"/>
 			<test name="org.tmatesoft.hg.test.TestHistory"/>
 			<test name="org.tmatesoft.hg.test.TestManifest"/>
 			<test name="org.tmatesoft.hg.test.TestStatus"/>
--- a/test/org/tmatesoft/hg/test/ErrorCollectorExt.java	Fri Jan 28 03:50:52 2011 +0100
+++ b/test/org/tmatesoft/hg/test/ErrorCollectorExt.java	Fri Jan 28 04:57:46 2011 +0100
@@ -16,6 +16,11 @@
  */
 package org.tmatesoft.hg.test;
 
+import static org.junit.Assert.assertThat;
+
+import java.util.concurrent.Callable;
+
+import org.hamcrest.Matcher;
 import org.junit.rules.ErrorCollector;
 
 /**
@@ -28,4 +33,13 @@
 	public void verify() throws Throwable {
 		super.verify();
 	}
+
+	public <T> void checkThat(final String reason, final T value, final Matcher<T> matcher) {
+		checkSucceeds(new Callable<Object>() {
+			public Object call() throws Exception {
+				assertThat(reason, value, matcher);
+				return value;
+			}
+		});
+	}
 }
\ No newline at end of file
--- a/test/org/tmatesoft/hg/test/TestHistory.java	Fri Jan 28 03:50:52 2011 +0100
+++ b/test/org/tmatesoft/hg/test/TestHistory.java	Fri Jan 28 04:57:46 2011 +0100
@@ -16,10 +16,17 @@
  */
 package org.tmatesoft.hg.test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.is;
+
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 
+import org.hamcrest.CoreMatchers;
+import org.junit.Rule;
 import org.junit.Test;
 import org.tmatesoft.hg.core.Cset;
 import org.tmatesoft.hg.core.LogCommand;
@@ -39,14 +46,18 @@
  */
 public class TestHistory {
 
+	@Rule
+	public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
+
 	private final HgRepository repo;
 	private ExecHelper eh;
 	private LogOutputParser changelogParser;
 	
-	public static void main(String[] args) throws Exception {
+	public static void main(String[] args) throws Throwable {
 		TestHistory th = new TestHistory();
 		th.testCompleteLog();
 		th.testFollowHistory();
+		th.errorCollector.verify();
 		th.testPerformance();
 	}
 	
@@ -85,9 +96,20 @@
 				};
 				H h = new H();
 				new LogCommand(repo).file(f, true).execute(h);
-				System.out.print("hg log - FOLLOW FILE HISTORY");
-				System.out.println("\tcopyReported:" + h.copyReported + ", and was " + (h.fromMatched ? "CORRECT" : "WRONG"));
-				report("hg log - FOLLOW FILE HISTORY", h.getChanges(), false);
+				String what = "hg log - FOLLOW FILE HISTORY";
+				errorCollector.checkThat(what + "#copyReported ", h.copyReported, is(true));
+				errorCollector.checkThat(what + "#copyFromMatched", h.fromMatched, is(true));
+				//
+				// cmdline always gives in changesets in order from newest (bigger rev number) to oldest.
+				// LogCommand does other way round, from oldest to newest, follewed by revisions of copy source, if any
+				// (apparently older than oldest of the copy target). Hence need to sort Java results according to rev numbers
+				final LinkedList<Cset> sorted = new LinkedList<Cset>(h.getChanges());
+				Collections.sort(sorted, new Comparator<Cset>() {
+					public int compare(Cset cs1, Cset cs2) {
+						return cs1.getRevision() < cs2.getRevision() ? 1 : -1;
+					}
+				});
+				report(what, sorted, false);
 			}
 		} catch (IllegalArgumentException ex) {
 			System.out.println("Can't test file history with follow because need to query specific file with history");
@@ -100,7 +122,6 @@
 			Collections.reverse(consoleResult);
 		}
 		Iterator<LogOutputParser.Record> consoleResultItr = consoleResult.iterator();
-		boolean hasErrors = false;
 		for (Cset cs : r) {
 			LogOutputParser.Record cr = consoleResultItr.next();
 			int x = cs.getRevision() == cr.changesetIndex ? 0x1 : 0;
@@ -108,17 +129,10 @@
 			x |= cs.getNodeid().toString().equals(cr.changesetNodeid) ? 0x4 : 0;
 			x |= cs.getUser().equals(cr.user) ? 0x8 : 0;
 			x |= cs.getComment().equals(cr.description) ? 0x10 : 0;
-			if (x != 0x1f) {
-				System.err.printf("Error in %d (%d):0%o\n", cs.getRevision(), cr.changesetIndex, x);
-				hasErrors = true;
-			}
+			errorCollector.checkThat(String.format(what + ". Error in %d:%d. ", cs.getRevision(), cr.changesetIndex), x, equalTo(0x1f));
 			consoleResultItr.remove();
 		}
-		if (consoleResultItr.hasNext()) {
-			System.out.println("Insufficient results from Java");
-			hasErrors = true;
-		}
-		System.out.println(what + (hasErrors ? " FAIL" : " OK"));
+		errorCollector.checkThat(what + ". Insufficient results from Java ", consoleResultItr.hasNext(), equalTo(false));
 	}
 
 	public void testPerformance() throws Exception {
--- a/test/org/tmatesoft/hg/test/TestManifest.java	Fri Jan 28 03:50:52 2011 +0100
+++ b/test/org/tmatesoft/hg/test/TestManifest.java	Fri Jan 28 04:57:46 2011 +0100
@@ -16,20 +16,24 @@
  */
 package org.tmatesoft.hg.test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.tmatesoft.hg.repo.HgRepository.TIP;
 
+import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.Map;
 
 import org.junit.Assume;
+import org.junit.Rule;
 import org.junit.Test;
 import org.tmatesoft.hg.core.LogCommand.FileRevision;
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.core.Path;
 import org.tmatesoft.hg.core.RepositoryTreeWalker;
+import org.tmatesoft.hg.repo.HgLookup;
 import org.tmatesoft.hg.repo.HgRepository;
-import org.tmatesoft.hg.repo.HgLookup;
 
 
 /**
@@ -39,6 +43,9 @@
  */
 public class TestManifest {
 
+	@Rule
+	public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
+
 	private final HgRepository repo;
 	private ManifestOutputParser manifestParser;
 	private ExecHelper eh;
@@ -54,11 +61,12 @@
 		public void begin(Nodeid manifestRevision) {}
 	};
 
-	public static void main(String[] args) throws Exception {
+	public static void main(String[] args) throws Throwable {
 		TestManifest tm = new TestManifest();
 		tm.testTip();
 		tm.testFirstRevision();
 		tm.testRevisionInTheMiddle();
+		tm.errorCollector.verify();
 	}
 	
 	public TestManifest() throws Exception {
@@ -100,26 +108,13 @@
 
 	private void report(String what) throws Exception {
 		final Map<Path, Nodeid> cmdLineResult = new LinkedHashMap<Path, Nodeid>(manifestParser.getResult());
-		boolean error = false;
 		for (FileRevision fr : revisions) {
 			Nodeid nid = cmdLineResult.remove(fr.getPath());
-			if (nid == null) {
-				System.out.println("Extra " + fr.getPath() + " in Java result");
-				error = true;
-			} else {
-				if (!nid.equals(fr.getRevision())) {
-					System.out.println("Non-matching nodeid:" + nid);
-					error = true;
-				}
+			errorCollector.checkThat("Extra " + fr.getPath() + " in Java result", nid, notNullValue());
+			if (nid != null) {
+				errorCollector.checkThat("Non-matching nodeid:" + nid, nid, equalTo(fr.getRevision()));
 			}
 		}
-		if (!cmdLineResult.isEmpty()) {
-			System.out.println("Non-matched entries from command line:");
-			error = true;
-			for (Path p : cmdLineResult.keySet()) {
-				System.out.println(p);
-			}
-		}
-		System.out.println(what + (error ? " ERROR" : " OK"));
+		errorCollector.checkThat("Non-matched entries from command line:", cmdLineResult, equalTo(Collections.<Path,Nodeid>emptyMap()));
 	}
 }
--- a/test/org/tmatesoft/hg/test/TestStatus.java	Fri Jan 28 03:50:52 2011 +0100
+++ b/test/org/tmatesoft/hg/test/TestStatus.java	Fri Jan 28 04:57:46 2011 +0100
@@ -16,14 +16,17 @@
  */
 package org.tmatesoft.hg.test;
 
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.tmatesoft.hg.repo.HgRepository.TIP;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 
 import org.junit.Assume;
+import org.junit.Rule;
 import org.junit.Test;
 import org.tmatesoft.hg.core.Path;
 import org.tmatesoft.hg.core.StatusCommand;
@@ -40,15 +43,19 @@
  */
 public class TestStatus {
 
+	@Rule
+	public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
+
 	private final HgRepository repo;
 	private StatusOutputParser statusParser;
 	private ExecHelper eh;
 
-	public static void main(String[] args) throws Exception {
+	public static void main(String[] args) throws Throwable {
 		TestStatus test = new TestStatus();
 		test.testLowLevel();
 		test.testStatusCommand();
 		test.testPerformance();
+		test.errorCollector.verify();
 	}
 	
 	public TestStatus() throws Exception {
@@ -144,15 +151,14 @@
 	}
 	
 	
-	private static void report(String what, HgStatusCollector.Record r, StatusOutputParser statusParser) {
-		System.out.println(">>>" + what);
-		reportNotEqual("MODIFIED", r.getModified(), statusParser.getModified());
-		reportNotEqual("ADDED", r.getAdded(), statusParser.getAdded());
-		reportNotEqual("REMOVED", r.getRemoved(), statusParser.getRemoved());
-		reportNotEqual("CLEAN", r.getClean(), statusParser.getClean());
-		reportNotEqual("IGNORED", r.getIgnored(), statusParser.getIgnored());
-		reportNotEqual("MISSING", r.getMissing(), statusParser.getMissing());
-		reportNotEqual("UNKNOWN", r.getUnknown(), statusParser.getUnknown());
+	private void report(String what, HgStatusCollector.Record r, StatusOutputParser statusParser) {
+		reportNotEqual(what + "#MODIFIED", r.getModified(), statusParser.getModified());
+		reportNotEqual(what + "#ADDED", r.getAdded(), statusParser.getAdded());
+		reportNotEqual(what + "#REMOVED", r.getRemoved(), statusParser.getRemoved());
+		reportNotEqual(what + "#CLEAN", r.getClean(), statusParser.getClean());
+		reportNotEqual(what + "#IGNORED", r.getIgnored(), statusParser.getIgnored());
+		reportNotEqual(what + "#MISSING", r.getMissing(), statusParser.getMissing());
+		reportNotEqual(what + "#UNKNOWN", r.getUnknown(), statusParser.getUnknown());
 		List<Path> copiedKeyDiff = difference(r.getCopied().keySet(), statusParser.getCopied().keySet());
 		HashMap<Path, String> copyDiff = new HashMap<Path,String>();
 		if (copiedKeyDiff.isEmpty()) {
@@ -168,30 +174,13 @@
 				}
 			}
 		}
-		System.out.println("COPIED" + (copiedKeyDiff.isEmpty() && copyDiff.isEmpty() ? " are the same" : " are NOT the same:"));
-		for (Path s : copiedKeyDiff) {
-			System.out.println("\tNon-matching key:" + s);
-		}
-		for (Path s : copyDiff.keySet()) {
-			System.out.println(s + " : " + copyDiff.get(s));
-		}
-		// TODO compare equals
-		System.out.println("<<<\n");
+		errorCollector.checkThat(what + "#Non-matching 'copied' keys: ", copiedKeyDiff, equalTo(Collections.<Path>emptyList()));
+		errorCollector.checkThat(what + "#COPIED", copyDiff, equalTo(Collections.<Path,String>emptyMap()));
 	}
 	
-	private static <T> void reportNotEqual(String what, Collection<T> l1, Collection<T> l2) {
+	private <T> void reportNotEqual(String what, Collection<T> l1, Collection<T> l2) {
 		List<T> diff = difference(l1, l2);
-		System.out.print(what);
-		if (!diff.isEmpty()) {
-			System.out.print(" are NOT the same: ");
-			for (T t : diff) {
-				System.out.print(t);
-				System.out.print(", ");
-			}
-			System.out.println();
-		} else {
-			System.out.println(" are the same");
-		}
+		errorCollector.checkThat(what, diff, equalTo(Collections.<T>emptyList()));
 	}
 
 	private static <T> List<T> difference(Collection<T> l1, Collection<T> l2) {