tikhomirov@202: /* tikhomirov@536: * Copyright (c) 2011-2013 TMate Software Ltd tikhomirov@202: * tikhomirov@202: * This program is free software; you can redistribute it and/or modify tikhomirov@202: * it under the terms of the GNU General Public License as published by tikhomirov@202: * the Free Software Foundation; version 2 of the License. tikhomirov@202: * tikhomirov@202: * This program is distributed in the hope that it will be useful, tikhomirov@202: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@202: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@202: * GNU General Public License for more details. tikhomirov@202: * tikhomirov@202: * For information on how to redistribute this software under tikhomirov@202: * the terms of a license other than GNU General Public License tikhomirov@202: * contact TMate Software at support@hg4j.com tikhomirov@202: */ tikhomirov@202: package org.tmatesoft.hg.test; tikhomirov@202: tikhomirov@202: import static org.hamcrest.CoreMatchers.equalTo; tikhomirov@202: tikhomirov@202: import java.io.File; tikhomirov@202: import java.util.ArrayList; tikhomirov@202: import java.util.HashSet; tikhomirov@202: import java.util.List; tikhomirov@202: tikhomirov@202: import org.junit.Assert; tikhomirov@202: import org.junit.Rule; tikhomirov@202: import org.junit.Test; tikhomirov@202: import org.tmatesoft.hg.core.HgChangeset; tikhomirov@202: import org.tmatesoft.hg.core.HgIncomingCommand; tikhomirov@202: import org.tmatesoft.hg.core.HgLogCommand; tikhomirov@202: import org.tmatesoft.hg.core.Nodeid; tikhomirov@202: import org.tmatesoft.hg.repo.HgLookup; tikhomirov@202: import org.tmatesoft.hg.repo.HgRemoteRepository; tikhomirov@202: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@202: tikhomirov@202: /** tikhomirov@202: * tikhomirov@202: * @author Artem Tikhomirov tikhomirov@202: * @author TMate Software Ltd. tikhomirov@202: */ tikhomirov@202: public class TestIncoming { tikhomirov@202: tikhomirov@202: @Rule tikhomirov@202: public ErrorCollectorExt errorCollector = new ErrorCollectorExt(); tikhomirov@202: tikhomirov@202: public static void main(String[] args) throws Throwable { tikhomirov@202: Configuration.get().remoteServers("http://localhost:8000/"); tikhomirov@202: TestIncoming t = new TestIncoming(); tikhomirov@202: t.testSimple(); tikhomirov@202: t.errorCollector.verify(); tikhomirov@202: } tikhomirov@202: tikhomirov@202: public TestIncoming() { tikhomirov@202: // Configuration.get().remoteServers("http://localhost:8000/"); tikhomirov@202: } tikhomirov@202: tikhomirov@202: @Test tikhomirov@202: public void testSimple() throws Exception { tikhomirov@202: int x = 0; tikhomirov@202: HgLookup lookup = new HgLookup(); tikhomirov@202: for (HgRemoteRepository hgRemote : Configuration.get().allRemote()) { tikhomirov@536: File dest = RepoUtils.initEmptyTempRepo("test-incoming-" + x++); tikhomirov@202: HgRepository localRepo = lookup.detect(dest); tikhomirov@202: // Idea: tikhomirov@202: // hg in, hg4j in, compare tikhomirov@202: // hg pull total/2 tikhomirov@202: // hg in, hg4j in, compare tikhomirov@202: List incoming = runAndCompareIncoming(localRepo, hgRemote); tikhomirov@204: Assert.assertTrue("Need remote repository of reasonable size to test incoming command for partially filled case", incoming.size() >= 5); tikhomirov@202: // tikhomirov@202: Nodeid median = incoming.get(incoming.size() / 2); tikhomirov@202: System.out.println("About to pull up to revision " + median.shortNotation()); tikhomirov@202: new ExecHelper(new OutputParser.Stub(), dest).run("hg", "pull", "-r", median.toString(), hgRemote.getLocation()); tikhomirov@202: // tikhomirov@202: // shall re-read repository to pull up new changes tikhomirov@202: localRepo = lookup.detect(dest); tikhomirov@202: runAndCompareIncoming(localRepo, hgRemote); tikhomirov@202: } tikhomirov@202: } tikhomirov@202: tikhomirov@202: private List runAndCompareIncoming(HgRepository localRepo, HgRemoteRepository hgRemote) throws Exception { tikhomirov@202: // need new command instance as subsequence exec[Lite|Full] on the same command would yield same result, tikhomirov@202: // regardless of the pull in between. tikhomirov@202: HgIncomingCommand cmd = new HgIncomingCommand(localRepo); tikhomirov@202: cmd.against(hgRemote); tikhomirov@202: HgLogCommand.CollectHandler collector = new HgLogCommand.CollectHandler(); tikhomirov@202: LogOutputParser outParser = new LogOutputParser(true); tikhomirov@282: ExecHelper eh = new ExecHelper(outParser, localRepo.getWorkingDir()); tikhomirov@202: cmd.executeFull(collector); tikhomirov@202: eh.run("hg", "incoming", "--debug", hgRemote.getLocation()); tikhomirov@215: List liteResult = cmd.executeLite(); tikhomirov@203: report(collector, outParser, liteResult, errorCollector); tikhomirov@202: return liteResult; tikhomirov@202: } tikhomirov@202: tikhomirov@203: static void report(HgLogCommand.CollectHandler collector, LogOutputParser outParser, List liteResult, ErrorCollectorExt errorCollector) { tikhomirov@203: TestHistory.report("hg vs execFull", collector.getChanges(), outParser.getResult(), false, errorCollector); tikhomirov@202: // tikhomirov@202: ArrayList expected = new ArrayList(outParser.getResult().size()); tikhomirov@202: for (LogOutputParser.Record r : outParser.getResult()) { tikhomirov@202: Nodeid nid = Nodeid.fromAscii(r.changesetNodeid); tikhomirov@202: expected.add(nid); tikhomirov@202: } tikhomirov@202: checkNodeids("hg vs execLite:", liteResult, expected, errorCollector); tikhomirov@202: // tikhomirov@202: expected = new ArrayList(outParser.getResult().size()); tikhomirov@202: for (HgChangeset cs : collector.getChanges()) { tikhomirov@202: expected.add(cs.getNodeid()); tikhomirov@202: } tikhomirov@202: checkNodeids("execFull vs execLite:", liteResult, expected, errorCollector); tikhomirov@202: } tikhomirov@202: tikhomirov@202: static void checkNodeids(String what, List liteResult, List expected, ErrorCollectorExt errorCollector) { tikhomirov@202: HashSet set = new HashSet(liteResult); tikhomirov@202: for (Nodeid nid : expected) { tikhomirov@202: boolean removed = set.remove(nid); tikhomirov@653: errorCollector.checkThat(what + " Missing " + nid.shortNotation() + " in execLite result", removed, equalTo(true)); tikhomirov@202: } tikhomirov@653: errorCollector.checkThat(what + " Superfluous cset reported by execLite", set.isEmpty(), equalTo(true)); tikhomirov@202: } tikhomirov@202: }