changeset 474:09f2d38ecf26

Tests for phases support
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 15:36:21 +0200
parents 5c09a9f2e073
children 0e34b8f3946a
files build.xml cmdline/org/tmatesoft/hg/console/Main.java src/org/tmatesoft/hg/internal/PhasesHelper.java src/org/tmatesoft/hg/repo/HgPhase.java test-data/test-repos.jar test/org/tmatesoft/hg/test/TestPhases.java
diffstat 6 files changed, 128 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/build.xml	Wed Jul 11 22:45:29 2012 +0200
+++ b/build.xml	Thu Jul 12 15:36:21 2012 +0200
@@ -96,6 +96,7 @@
 			<test name="org.tmatesoft.hg.test.TestClone" />
 			<test name="org.tmatesoft.hg.test.TestIncoming" />
 			<test name="org.tmatesoft.hg.test.TestOutgoing" />
+			<test name="org.tmatesoft.hg.test.TestPhases" />
 		</junit>
 	</target>
 
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Wed Jul 11 22:45:29 2012 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jul 12 15:36:21 2012 +0200
@@ -157,38 +157,6 @@
 		Assert.assertTrue(mqManager.getActiveQueueName().length() > 0);
 	}
 	
-	
-	// -R ${system_property:user.home}/hg/test-phases/
-	// TODO as junit test
-	private void dumpPhases() throws Exception {
-		HgPhase[] result1 = new HgPhase[hgRepo.getChangelog().getRevisionCount()];
-		HgPhase[] result2 = new HgPhase[hgRepo.getChangelog().getRevisionCount()];
-		final long start1 = System.nanoTime();
-		HgParentChildMap<HgChangelog> pw = new HgParentChildMap<HgChangelog>(hgRepo.getChangelog());
-		pw.init();
-		final long start1bis = System.nanoTime();
-		PhasesHelper ph = new PhasesHelper(hgRepo, pw);
-		for (int i = 0, l = hgRepo.getChangelog().getLastRevision(); i <= l; i++) {
-			result1[i] = ph.getPhase(i, null);
-		}
-		final long start2 = System.nanoTime();
-		ph = new PhasesHelper(hgRepo);
-		for (int i = 0, l = hgRepo.getChangelog().getLastRevision(); i <= l; i++) {
-			result2[i] = ph.getPhase(i, null);
-		}
-		final long end = System.nanoTime();
-		System.out.printf("With ParentWalker(simulates log command for whole repo): %d ms (pw init: %,d ns)\n", (start2 - start1)/1000, start1bis - start1);
-		printPhases(result1);
-		System.out.printf("Without ParentWalker (simulates log command for single file): %d ms\n", (end - start2)/1000);
-		printPhases(result2);
-	}
-	
-	private static void printPhases(HgPhase[] phase) {
-		for (int i = 0; i < phase.length; i++) {
-			System.out.printf("rev:%3d, phase:%s\n", i, phase[i]);
-		}
-	}
-
 	// hg4j repo
 	public void checkWalkFileRevisions() throws Exception {
 		//  hg --debug manifest --rev 150 | grep cmdline/org/tmatesoft/hg/console/Main.java
--- a/src/org/tmatesoft/hg/internal/PhasesHelper.java	Wed Jul 11 22:45:29 2012 +0200
+++ b/src/org/tmatesoft/hg/internal/PhasesHelper.java	Thu Jul 12 15:36:21 2012 +0200
@@ -62,6 +62,10 @@
 		repo = hgRepo;
 		parentHelper = pw;
 	}
+	
+	public HgRepository getRepo() {
+		return repo;
+	}
 
 	public boolean isCapableOfPhases() throws HgInvalidControlFileException {
 		if (null == repoSupporsPhases) {
--- a/src/org/tmatesoft/hg/repo/HgPhase.java	Wed Jul 11 22:45:29 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/HgPhase.java	Thu Jul 12 15:36:21 2012 +0200
@@ -26,7 +26,6 @@
 	
 	Public("public"), Draft("draft"), Secret("secret"), Undefined("");
 
-	@SuppressWarnings("unused")
 	private final String hgString;
 
 	private HgPhase(String stringRepresentation) {
@@ -45,4 +44,17 @@
 		}
 		throw new IllegalArgumentException(String.format("Bad phase index: %d", value));
 	}
+	
+	public static HgPhase parse(String value) {
+		if (Public.hgString.equals(value)) {
+			return Public;
+		}
+		if (Draft.hgString.equals(value)) {
+			return Draft;
+		}
+		if (Secret.hgString.equals(value)) {
+			return Secret;
+		}
+		throw new IllegalArgumentException(String.format("Bad phase name: %d", value));
+	}
 }
Binary file test-data/test-repos.jar has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/org/tmatesoft/hg/test/TestPhases.java	Thu Jul 12 15:36:21 2012 +0200
@@ -0,0 +1,110 @@
+/*
+ * 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 java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.tmatesoft.hg.internal.PhasesHelper;
+import org.tmatesoft.hg.repo.HgChangelog;
+import org.tmatesoft.hg.repo.HgLookup;
+import org.tmatesoft.hg.repo.HgParentChildMap;
+import org.tmatesoft.hg.repo.HgPhase;
+import org.tmatesoft.hg.repo.HgRepository;
+
+/**
+ * {hg4j.tests.repos}/test-phases/
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class TestPhases {
+	
+	@Rule
+	public ErrorCollectorExt errorCollector = new ErrorCollectorExt();
+
+	@Test
+	public void testHelperNoParentChildMap() throws Exception {
+		HgRepository repo = Configuration.get().find("test-phases");
+		HgPhase[] expected = readPhases(repo);
+		final long start = System.nanoTime();
+		PhasesHelper ph = new PhasesHelper(repo, null);
+		initAndCheck(ph, expected);
+		final long end = System.nanoTime();
+		System.out.printf("Without ParentWalker (simulates log command for single file): %d ms\n", (end - start)/1000);
+	}
+	
+	@Test
+	public void testHelperWithParentChildMap() throws Exception {
+		HgRepository repo = Configuration.get().find("test-phases");
+		HgPhase[] expected = readPhases(repo);
+		final long start1 = System.nanoTime();
+		HgParentChildMap<HgChangelog> pw = new HgParentChildMap<HgChangelog>(repo.getChangelog());
+		pw.init();
+		final long start2 = System.nanoTime();
+		PhasesHelper ph = new PhasesHelper(repo, pw);
+		initAndCheck(ph, expected);
+		final long end = System.nanoTime();
+		System.out.printf("With ParentWalker(simulates log command for whole repo): %d ms (pw init: %,d ns)\n", (end - start1)/1000, start2 - start1);
+	}
+
+	private HgPhase[] initAndCheck(PhasesHelper ph, HgPhase[] expected) {
+		HgChangelog clog = ph.getRepo().getChangelog();
+		HgPhase[] result = new HgPhase[clog.getRevisionCount()];
+		for (int i = 0, l = clog.getLastRevision(); i <= l; i++) {
+			result[i] = ph.getPhase(i, null);
+		}
+		assertEquals(expected.length, result.length);
+		for (int i = 0; i < result.length; i++) {
+			errorCollector.assertTrue(result[i] == expected[i]);
+		}
+		return result;
+	}
+	
+	private static HgPhase[] readPhases(HgRepository repo) throws Exception {
+		HgPhase[] result = new HgPhase[repo.getChangelog().getRevisionCount()];
+		OutputParser.Stub output = new OutputParser.Stub();
+		ExecHelper eh = new ExecHelper(output, repo.getWorkingDir());
+		eh.run("hg", "phase", "-r", "0:-1");
+		Matcher m = Pattern.compile("(\\d+): (\\w+)$", Pattern.MULTILINE).matcher(output.result());
+		int i = 0;
+		while (m.find()) {
+			int x = Integer.parseInt(m.group(1));
+			assert x == i;
+			HgPhase v = HgPhase.parse(m.group(2));
+			result[x] = v;
+			i++;
+		}
+		return result;
+	}
+
+	public static void main(String[] args) throws Exception {
+		HgRepository repo = new HgLookup().detect(System.getProperty("user.home") + "/hg/test-phases/");
+		HgPhase[] v = readPhases(repo);
+		printPhases(v);
+	}
+
+	private static void printPhases(HgPhase[] phase) {
+		for (int i = 0; i < phase.length; i++) {
+			System.out.printf("rev:%3d, phase:%s\n", i, phase[i]);
+		}
+	}
+
+}