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