changeset 475:0e34b8f3946a

Tests for MqManager
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 12 Jul 2012 16:57:40 +0200
parents 09f2d38ecf26
children c02b5710d9ac
files cmdline/org/tmatesoft/hg/console/Main.java src/org/tmatesoft/hg/repo/ext/MqManager.java test-data/test-repos.jar test/org/tmatesoft/hg/test/OutputParser.java test/org/tmatesoft/hg/test/TestMqExtension.java
diffstat 5 files changed, 141 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jul 12 15:36:21 2012 +0200
+++ b/cmdline/org/tmatesoft/hg/console/Main.java	Thu Jul 12 16:57:40 2012 +0200
@@ -108,8 +108,6 @@
 //		m.checkWalkFileRevisions();
 //		m.checkSubProgress();
 //		m.checkFileFlags();
-//		m.testMqManager();
-//		m.dumpPhases();
 //		m.buildFileLog();
 //		m.testConsoleLog();
 //		m.testTreeTraversal();
@@ -131,32 +129,7 @@
 //		m.dumpCompleteManifestHigh();
 //		m.bunchOfTests();
 	}
-	
-	
-	// TODO as junit tests in 'default'
-	// -R ${system_property:user.home}/hg/test-mq
-	private void testMqManager() throws Exception {
-		MqManager mqManager = new MqManager(hgRepo);
-		mqManager.refresh();
-		int i = 1;
-		System.out.println("Complete patch queue:");
-		for (PatchRecord pr : mqManager.getAllKnownPatches()) {
-			System.out.printf("#%-3d %s from %s\n", i++, pr.getName(), pr.getPatchLocation());
-		}
-		i = 1;
-		System.out.println("Patches from the queue already applied to the repo:");
-		for (PatchRecord pr : mqManager.getAppliedPatches()) {
-			System.out.printf("#%-3d %s, known as cset:%s\n", i++, pr.getName(), pr.getRevision().shortNotation());
-		}
-		boolean allAppliedAreKnown = mqManager.getAllKnownPatches().containsAll(mqManager.getAppliedPatches());
-		System.out.printf("[sanity] allAppliedAreKnown:%b, not yet applied:%d\n", allAppliedAreKnown, mqManager.getQueueSize());
-		Assert.assertTrue(allAppliedAreKnown);
 
-		System.out.printf("Queues: %s, active:%s\n", mqManager.getQueueNames(), mqManager.getActiveQueueName());
-		Assert.assertTrue(mqManager.getQueueNames().size() > 1);
-		Assert.assertTrue(mqManager.getActiveQueueName().length() > 0);
-	}
-	
 	// hg4j repo
 	public void checkWalkFileRevisions() throws Exception {
 		//  hg --debug manifest --rev 150 | grep cmdline/org/tmatesoft/hg/console/Main.java
@@ -202,6 +175,8 @@
 	private void checkFileFlags() throws Exception {
 		// ~/hg/test-flags repo
 		// TODO transform to a test once I keep test-flags in test-repos.jar
+		// JAR can't keep symlinks. Perhaps, a solution would be to keep repo without WC and
+		// perform an `hg up` before use
 		HgDataFile link = hgRepo.getFileNode("file-link");
 		HgDataFile exec = hgRepo.getFileNode("file-exec");
 		HgDataFile file = hgRepo.getFileNode("regular-file");
--- a/src/org/tmatesoft/hg/repo/ext/MqManager.java	Thu Jul 12 15:36:21 2012 +0200
+++ b/src/org/tmatesoft/hg/repo/ext/MqManager.java	Thu Jul 12 16:57:40 2012 +0200
@@ -198,7 +198,7 @@
 		return Collections.unmodifiableList(queueNames);
 	}
 	
-	public class PatchRecord {
+	public final class PatchRecord {
 		private final Nodeid nodeid;
 		private final String name;
 		private final Path location;
@@ -238,6 +238,13 @@
 		public Path getPatchLocation() {
 			return location;
 		}
+		
+		@Override
+		public String toString() {
+			String fmt = "mq.PatchRecord[name:%s; %spath:%s]";
+			String ni = nodeid != null ? String.format("applied as: %s; ", nodeid.shortNotation()) : "";
+			return String.format(fmt, name, ni, location);
+		}
 	}
 
 	// TODO refine API and extract into separate classes
Binary file test-data/test-repos.jar has changed
--- a/test/org/tmatesoft/hg/test/OutputParser.java	Thu Jul 12 15:36:21 2012 +0200
+++ b/test/org/tmatesoft/hg/test/OutputParser.java	Thu Jul 12 16:57:40 2012 +0200
@@ -16,6 +16,11 @@
  */
 package org.tmatesoft.hg.test;
 
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  *
  * @author Artem Tikhomirov
@@ -45,5 +50,39 @@
 		public CharSequence result() {
 			return result;
 		}
+
+		public Iterable<String> lines() {
+			return lines("(.+)$");
+		}
+		public Iterable<String> lines(String pattern) {
+			final Matcher m = Pattern.compile(pattern, Pattern.MULTILINE).matcher(result);
+			class S implements Iterable<String>, Iterator<String> {
+				public Iterator<String> iterator() {
+					return this;
+				}
+				private boolean next;
+				{
+					next = m.find();
+				}
+
+				public boolean hasNext() {
+					return next;
+				}
+
+				public String next() {
+					if (next) {
+						String rv = m.group();
+						next = m.find();
+						return rv;
+					}
+					throw new NoSuchElementException();
+				}
+
+				public void remove() {
+					throw new UnsupportedOperationException();
+				}
+			};
+			return new S();
+		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/org/tmatesoft/hg/test/TestMqExtension.java	Thu Jul 12 16:57:40 2012 +0200
@@ -0,0 +1,92 @@
+/*
+ * 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.Iterator;
+import java.util.LinkedList;
+
+import org.junit.Test;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.repo.ext.MqManager;
+import org.tmatesoft.hg.repo.ext.MqManager.PatchRecord;
+
+/**
+ * {junit-test-repos}/test-mq/
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class TestMqExtension {
+
+	@Test
+	public void testMqManager() throws Exception {
+		HgRepository repo = Configuration.get().find("test-mq");
+		MqManager mqManager = new MqManager(repo);
+		mqManager.refresh();
+		OutputParser.Stub output = new OutputParser.Stub();
+		ExecHelper eh = new ExecHelper(output, repo.getWorkingDir());
+		// `hg qseries`
+		eh.run("hg", "qseries");
+		LinkedList<PatchRecord> allKnownPatches = new LinkedList<PatchRecord>(mqManager.getAllKnownPatches());
+		assertTrue("[sanity]", allKnownPatches.size() > 0);
+		for (CharSequence l : output.lines()) {
+			for (Iterator<PatchRecord> it = allKnownPatches.listIterator(); it.hasNext(); ) {
+				if (it.next().getName().equals(l)) {
+					it.remove();
+				}
+			}
+		}
+		assertTrue("Known patches shall match those from `hg qseries`", allKnownPatches.isEmpty());
+		//
+		// `hg qapplied`, patches from the queue already applied to the repo
+		eh.run("hg", "qapplied");
+		LinkedList<PatchRecord> appliedPatches = new LinkedList<PatchRecord>(mqManager.getAppliedPatches());
+		assertTrue("[sanity]", appliedPatches.size() > 0);
+		for (CharSequence l : output.lines()) {
+			for (Iterator<PatchRecord> it = appliedPatches.listIterator(); it.hasNext(); ) {
+				if (it.next().getName().equals(l)) {
+					it.remove();
+				}
+			}
+		}
+		assertTrue("Each patch reported as applied shall match thos from `hg qapplied`", appliedPatches.isEmpty());
+		
+		assertTrue("[sanity] ",mqManager.getQueueSize() > 0);
+		boolean allAppliedAreKnown = mqManager.getAllKnownPatches().containsAll(mqManager.getAppliedPatches());
+		assertTrue(allAppliedAreKnown); // ensure instances are the same, ==
+
+		// `hg qqueue`
+		assertTrue("[sanity]",mqManager.getQueueNames().size() > 1);
+		assertTrue(mqManager.getActiveQueueName().length() > 0);
+		eh.run("hg", "qqueue");
+		boolean activeQueueFound = false;
+		LinkedList<String> queueNames = new LinkedList<String>(mqManager.getQueueNames());
+		for (String l : output.lines()) {
+			if (l.endsWith("(active)")) {
+				l = l.substring(0, l.length() - 8).trim();
+				assertEquals(l, mqManager.getActiveQueueName());
+				assertFalse("only single active queue", activeQueueFound);
+				activeQueueFound = true;
+			}
+			assertTrue(queueNames.remove(l));
+		}
+		assertTrue(activeQueueFound);
+		assertTrue(queueNames.isEmpty()); // every queue name we found matches `hg qqueue` output 
+	}
+}