changeset 479:59b7c817bc4d

Methods from Main graduated as tests
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 20:02:49 +0200
parents e74580e24feb
children f3fab7a20841
files build.xml cmdline/org/tmatesoft/hg/console/Main.java test/org/tmatesoft/hg/test/OutputParser.java test/org/tmatesoft/hg/test/TestCatCommand.java test/org/tmatesoft/hg/test/TestFileFlags.java test/org/tmatesoft/hg/test/TestManifest.java
diffstat 6 files changed, 143 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/build.xml	Thu Jul 12 19:11:12 2012 +0200
+++ b/build.xml	Thu Jul 12 20:02:49 2012 +0200
@@ -99,6 +99,7 @@
 			<test name="org.tmatesoft.hg.test.TestPhases" />
 			<test name="org.tmatesoft.hg.test.TestMqExtension" />
 			<test name="org.tmatesoft.hg.test.TestFileFlags" />
+			<test name="org.tmatesoft.hg.test.TestCatCommand" />
 		</junit>
 	</target>
 
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jul 12 19:11:12 2012 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jul 12 20:02:49 2012 +0200
@@ -16,6 +16,7 @@
  */
 package org.tmatesoft.hg.console;
 
+import static org.junit.Assert.*;
 import static org.tmatesoft.hg.repo.HgRepository.TIP;
 import static org.tmatesoft.hg.repo.HgRepository.WORKING_COPY;
 import static org.tmatesoft.hg.util.LogFacility.Severity.*;
@@ -28,8 +29,8 @@
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.regex.Pattern;
 
-import org.junit.Assert;
 import org.tmatesoft.hg.core.HgManifestHandler;
 import org.tmatesoft.hg.core.HgCallbackTargetException;
 import org.tmatesoft.hg.core.HgCatCommand;
@@ -44,6 +45,7 @@
 import org.tmatesoft.hg.internal.BasicSessionContext;
 import org.tmatesoft.hg.internal.ByteArrayChannel;
 import org.tmatesoft.hg.internal.DigestHelper;
+import org.tmatesoft.hg.internal.IntMap;
 import org.tmatesoft.hg.internal.PathGlobMatcher;
 import org.tmatesoft.hg.internal.PhasesHelper;
 import org.tmatesoft.hg.internal.RelativePathRewrite;
@@ -73,6 +75,8 @@
 import org.tmatesoft.hg.repo.ext.MqManager.PatchRecord;
 import org.tmatesoft.hg.repo.HgWorkingCopyStatusCollector;
 import org.tmatesoft.hg.repo.HgRevisionMap;
+import org.tmatesoft.hg.test.ExecHelper;
+import org.tmatesoft.hg.test.OutputParser;
 import org.tmatesoft.hg.util.FileWalker;
 import org.tmatesoft.hg.util.LogFacility;
 import org.tmatesoft.hg.util.Pair;
@@ -105,7 +109,6 @@
 
 	public static void main(String[] args) throws Exception {
 		Main m = new Main(args);
-//		m.checkWalkFileRevisions();
 //		m.buildFileLog();
 //		m.testConsoleLog();
 //		m.testTreeTraversal();
@@ -114,7 +117,6 @@
 //		m.testReadWorkingCopy();
 //		m.testParents();
 //		m.testEffectiveFileLog();
-//		m.testCatAtCsetRevision();
 //		m.testMergeState();
 //		m.testFileStatus();
 //		m.dumpBranches();
@@ -128,12 +130,6 @@
 //		m.bunchOfTests();
 	}
 
-	// hg4j repo
-	public void checkWalkFileRevisions() throws Exception {
-		//  hg --debug manifest --rev 150 | grep cmdline/org/tmatesoft/hg/console/Main.java
-		hgRepo.getManifest().walkFileRevisions(Path.create("cmdline/org/tmatesoft/hg/console/Main.java"), new ManifestDump(), 100, 150, 200, 210, 300);
-	}
-
 	private void buildFileLog() throws Exception {
 		final long start = System.nanoTime();
 		HgLogCommand cmd = new HgLogCommand(hgRepo);
@@ -351,30 +347,6 @@
 		}
 	}
 	
-	// TODO as test in TestCat
-	private void testCatAtCsetRevision() throws Exception {
-		HgCatCommand cmd = new HgCatCommand(hgRepo);
-		final Path file = Path.create("src/org/tmatesoft/hg/internal/RevlogStream.java");
-		cmd.file(file);
-		final Nodeid cset = Nodeid.fromAscii("08db726a0fb7914ac9d27ba26dc8bbf6385a0554");
-		cmd.changeset(cset);
-		final ByteArrayChannel sink = new ByteArrayChannel();
-		cmd.execute(sink);
-		System.out.println(sink.toArray().length);
-		HgChangesetFileSneaker i = new HgChangesetFileSneaker(hgRepo);
-		boolean result = i.changeset(cset).checkExists(file);
-		Assert.assertFalse(result);
-		Assert.assertFalse(i.exists());
-		result = i.followRenames(true).checkExists(file);
-		Assert.assertTrue(result);
-		Assert.assertTrue(i.exists());
-		HgCatCommand cmd2 = new HgCatCommand(hgRepo).revision(i.getFileRevision());
-		final ByteArrayChannel sink2 = new ByteArrayChannel();
-		cmd2.execute(sink2);
-		System.out.println(sink2.toArray().length);
-		Assert.assertEquals(sink.toArray().length, sink2.toArray().length);
-	}
-	
 	private void testMergeState() throws Exception {
 		final HgMergeState mergeState = hgRepo.getMergeState();
 		mergeState.refresh();
--- a/test/org/tmatesoft/hg/test/OutputParser.java	Thu Jul 12 19:11:12 2012 +0200
+++ b/test/org/tmatesoft/hg/test/OutputParser.java	Thu Jul 12 20:02:49 2012 +0200
@@ -52,10 +52,10 @@
 		}
 
 		public Iterable<String> lines() {
-			return lines("(.+)$");
+			return lines(Pattern.compile("(.+)$", Pattern.MULTILINE), 1);
 		}
-		public Iterable<String> lines(String pattern) {
-			final Matcher m = Pattern.compile(pattern, Pattern.MULTILINE).matcher(result);
+		public Iterable<String> lines(Pattern pattern, final int group) {
+			final Matcher m = pattern.matcher(result);
 			class S implements Iterable<String>, Iterator<String> {
 				public Iterator<String> iterator() {
 					return this;
@@ -71,7 +71,7 @@
 
 				public String next() {
 					if (next) {
-						String rv = m.group(1);
+						String rv = m.group(group);
 						next = m.find();
 						return rv;
 					}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/org/tmatesoft/hg/test/TestCatCommand.java	Thu Jul 12 20:02:49 2012 +0200
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2012 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.test;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.tmatesoft.hg.core.HgCatCommand;
+import org.tmatesoft.hg.core.HgChangesetFileSneaker;
+import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.internal.ByteArrayChannel;
+import org.tmatesoft.hg.repo.HgLookup;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.util.Path;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class TestCatCommand {
+	
+	private final HgRepository repo;
+	
+	public TestCatCommand() throws Exception {
+		repo = new HgLookup().detectFromWorkingDir();
+	}
+	
+	@Test
+	public void testCatAtCsetRevision() throws Exception {
+		HgCatCommand cmd = new HgCatCommand(repo);
+		final Path file = Path.create("src/org/tmatesoft/hg/internal/RevlogStream.java");
+		cmd.file(file);
+		final Nodeid cset = Nodeid.fromAscii("08db726a0fb7914ac9d27ba26dc8bbf6385a0554");
+		cmd.changeset(cset);
+		final ByteArrayChannel sink = new ByteArrayChannel();
+		cmd.execute(sink);
+		final int result1 = sink.toArray().length;
+		HgChangesetFileSneaker i = new HgChangesetFileSneaker(repo);
+		boolean result = i.changeset(cset).checkExists(file);
+		assertFalse(result);
+		assertFalse(i.exists());
+		result = i.followRenames(true).checkExists(file);
+		assertTrue(result);
+		assertTrue(i.exists());
+		HgCatCommand cmd2 = new HgCatCommand(repo).revision(i.getFileRevision());
+		final ByteArrayChannel sink2 = new ByteArrayChannel();
+		cmd2.execute(sink2);
+		final int result2 = sink2.toArray().length;
+		assertEquals(result1, result2);
+	}
+
+}
--- a/test/org/tmatesoft/hg/test/TestFileFlags.java	Thu Jul 12 19:11:12 2012 +0200
+++ b/test/org/tmatesoft/hg/test/TestFileFlags.java	Thu Jul 12 20:02:49 2012 +0200
@@ -77,7 +77,7 @@
 			assertTrue(fw.supportsExecFlag());
 			assertTrue(fw.supportsLinkFlag());
 		}
-		ExecHelper eh = new ExecHelper(new OutputParser.Stub(true), repo.getWorkingDir());
+		ExecHelper eh = new ExecHelper(new OutputParser.Stub(false), repo.getWorkingDir());
 		eh.run("hg", "checkout", "-C");
 
 		boolean exec, link, file;
--- a/test/org/tmatesoft/hg/test/TestManifest.java	Thu Jul 12 19:11:12 2012 +0200
+++ b/test/org/tmatesoft/hg/test/TestManifest.java	Thu Jul 12 20:02:49 2012 +0200
@@ -18,13 +18,14 @@
 
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.notNullValue;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 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 java.util.regex.Pattern;
 
 import org.junit.Rule;
 import org.junit.Test;
@@ -32,8 +33,11 @@
 import org.tmatesoft.hg.core.HgFileRevision;
 import org.tmatesoft.hg.core.HgManifestCommand;
 import org.tmatesoft.hg.core.Nodeid;
+import org.tmatesoft.hg.internal.IntMap;
 import org.tmatesoft.hg.repo.HgLookup;
+import org.tmatesoft.hg.repo.HgManifest;
 import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.repo.HgManifest.Flags;
 import org.tmatesoft.hg.util.Path;
 
 
@@ -67,6 +71,7 @@
 		tm.testTip();
 		tm.testFirstRevision();
 		tm.testRevisionInTheMiddle();
+		tm.testWalkFileRevisions();
 		tm.errorCollector.verify();
 	}
 	
@@ -98,6 +103,65 @@
 		}
 		testRevision(rev);
 	}
+	
+	@Test
+	public void testWalkFileRevisions() throws Exception {
+		//  hg --debug manifest --rev 150 | grep cmdline/org/tmatesoft/hg/console/Main.java
+		String fname = "cmdline/org/tmatesoft/hg/console/Main.java";
+		Pattern ptrn = Pattern.compile("(\\w+)\\s+\\d{3}\\s+\\Q" + fname + "\\E$", Pattern.MULTILINE);
+		OutputParser.Stub output = new OutputParser.Stub();
+		ExecHelper eh = new ExecHelper(output, repo.getWorkingDir());
+		int[] revisions = new int[] { 100, 150, 200, 210, 300 };
+		final IntMap<Nodeid> expected = new IntMap<Nodeid>(10);
+		for (int r : revisions) {
+			eh.run("hg", "--debug", "manifest", "--rev", Integer.toString(r));
+			for (String l : output.lines(ptrn, 1)) {
+				assertFalse(expected.containsKey(r));
+				expected.put(r, Nodeid.fromAscii(l));
+			}
+			if (!expected.containsKey(r)) {
+				expected.put(r, Nodeid.NULL);
+			}
+		}
+		assertEquals(revisions.length, expected.size());
+		final Path path = Path.create(fname);
+		repo.getManifest().walkFileRevisions(path, new HgManifest.Inspector() {
+
+			private Nodeid expectedNodeid;
+			private int clogRev;
+
+			public boolean begin(int mainfestRevision, Nodeid nid, int changelogRevision) {
+				clogRev = changelogRevision;
+				assertTrue(expected.containsKey(changelogRevision));
+				expectedNodeid = expected.get(changelogRevision);
+				// we walk revisions of specific file, hence can't get into manifest without
+				// that file, expectedNodeid should have been reported by `hg manifest`:
+				assertTrue(!expectedNodeid.isNull());
+				return true;
+			}
+
+			public boolean next(Nodeid nid, Path fname, Flags flags) {
+				assertEquals(path, fname); // only selected path shall pass
+				assertEquals(expectedNodeid, nid);
+				expected.remove(clogRev);
+				return true;
+			}
+			
+			public boolean end(int manifestRevision) {
+				return true;
+			}
+			
+		}, revisions);
+		for (int r : revisions) {
+			if (expected.containsKey(r)) {
+				// Each  manifest entry reported by `hg manifest` shall be 
+				// reported (and removed) by walkFileRevisions(), too. Only null nodeids
+				// (revisions where the file was missing) are left 
+				assertTrue(expected.get(r).isNull());
+			}
+		}
+	}
+
 
 	private void testRevision(int rev) throws Exception {
 		manifestParser.reset();