diff test/org/tmatesoft/hg/test/TestAuxUtilities.java @ 312:f9f3e9b67ccc

Facilitate cancellation and progress reporting in changelog and manifest iterations
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 27 Sep 2011 05:29:12 +0200
parents b9592e21176a
children c1e3c18fd2f2
line wrap: on
line diff
--- a/test/org/tmatesoft/hg/test/TestAuxUtilities.java	Mon Sep 26 04:06:04 2011 +0200
+++ b/test/org/tmatesoft/hg/test/TestAuxUtilities.java	Tue Sep 27 05:29:12 2011 +0200
@@ -18,7 +18,14 @@
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.internal.ArrayHelper;
+import org.tmatesoft.hg.repo.HgChangelog;
+import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
+import org.tmatesoft.hg.repo.HgRepository;
+import org.tmatesoft.hg.util.Adaptable;
+import org.tmatesoft.hg.util.CancelSupport;
+import org.tmatesoft.hg.util.CancelledException;
 
 /**
  *
@@ -52,4 +59,76 @@
 		}
 		return rebuilt;
 	}
+
+	@Test
+	public void testCancelSupport() throws Exception {
+		HgRepository repository = Configuration.get().find("branches-1"); // any repo with more revisions
+		class CancelImpl implements CancelSupport {
+			private boolean shallStop = false;
+			public void stop() {
+				shallStop = true;
+			}
+			public void checkCancelled() throws CancelledException {
+				if (shallStop) {
+					throw new CancelledException();
+				}
+			}
+		}
+		class InspectorImplementsCancel implements HgChangelog.Inspector, CancelSupport {
+			public final int when2stop;
+			public int lastVisitet = 0;
+			private final CancelImpl cancelImpl = new CancelImpl(); 
+
+			public InspectorImplementsCancel(int limit) {
+				when2stop = limit;
+			}
+			
+			public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
+				lastVisitet = revisionNumber;
+				if (revisionNumber == when2stop) {
+					cancelImpl.stop();
+				}
+			}
+
+			public void checkCancelled() throws CancelledException {
+				cancelImpl.checkCancelled();
+			}
+		};
+		class InspectorImplementsAdaptable implements HgChangelog.Inspector, Adaptable {
+			public final int when2stop;
+			public int lastVisitet = 0;
+			private final CancelImpl cancelImpl = new CancelImpl();
+			
+			public InspectorImplementsAdaptable(int limit) {
+				when2stop = limit;
+			}
+			
+			public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) {
+				lastVisitet = revisionNumber;
+				if (revisionNumber == when2stop) {
+					cancelImpl.stop();
+				}
+			}
+			@SuppressWarnings("unchecked")
+			public <T> T getAdapter(Class<T> adapterClass) {
+				if (CancelSupport.class == adapterClass) {
+					return (T) cancelImpl;
+				}
+				return null;
+			}
+			
+		}
+		//
+		InspectorImplementsCancel insp1;
+		repository.getChangelog().all(insp1= new InspectorImplementsCancel(2));
+		Assert.assertEquals(insp1.when2stop, insp1.lastVisitet);
+		repository.getChangelog().all(insp1 = new InspectorImplementsCancel(12));
+		Assert.assertEquals(insp1.when2stop, insp1.lastVisitet);
+		//
+		InspectorImplementsAdaptable insp2;
+		repository.getChangelog().all(insp2= new InspectorImplementsAdaptable(3));
+		Assert.assertEquals(insp2.when2stop, insp2.lastVisitet);
+		repository.getChangelog().all(insp2 = new InspectorImplementsAdaptable(10));
+		Assert.assertEquals(insp2.when2stop, insp2.lastVisitet);
+	}
 }