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 java.io.File; kitaev@213: import java.io.IOException; kitaev@213: import java.util.Arrays; kitaev@213: import java.util.LinkedList; kitaev@213: import java.util.List; kitaev@213: kitaev@213: import org.hamcrest.CoreMatchers; kitaev@213: import org.junit.Rule; kitaev@213: import org.junit.Test; kitaev@213: import org.tmatesoft.hg.core.HgCloneCommand; kitaev@213: import org.tmatesoft.hg.repo.HgRemoteRepository; kitaev@213: kitaev@213: /** kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class TestClone { 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: TestClone t = new TestClone(); kitaev@213: t.testSimpleClone(); kitaev@213: t.errorCollector.verify(); kitaev@213: } kitaev@213: kitaev@213: public TestClone() { kitaev@213: } kitaev@213: kitaev@213: @Test kitaev@213: public void testSimpleClone() throws Exception { kitaev@213: int x = 0; kitaev@213: final File tempDir = Configuration.get().getTempDir(); kitaev@213: for (HgRemoteRepository hgRemote : Configuration.get().allRemote()) { kitaev@213: HgCloneCommand cmd = new HgCloneCommand(); kitaev@213: cmd.source(hgRemote); kitaev@213: File dest = new File(tempDir, "test-clone-" + x++); kitaev@213: if (dest.exists()) { kitaev@213: rmdir(dest); kitaev@213: } kitaev@213: cmd.destination(dest); kitaev@213: cmd.execute(); kitaev@213: verify(hgRemote, dest); kitaev@213: } kitaev@213: } kitaev@213: kitaev@213: private void verify(HgRemoteRepository hgRemote, File dest) throws Exception { kitaev@213: ExecHelper eh = new ExecHelper(new OutputParser.Stub(), dest); kitaev@213: eh.run("hg", "verify"); kitaev@213: errorCollector.checkThat("Verify", eh.getExitValue(), CoreMatchers.equalTo(0)); kitaev@213: eh.run("hg", "out", hgRemote.getLocation()); kitaev@213: errorCollector.checkThat("Outgoing", eh.getExitValue(), CoreMatchers.equalTo(1)); kitaev@213: eh.run("hg", "in", hgRemote.getLocation()); kitaev@213: errorCollector.checkThat("Incoming", eh.getExitValue(), CoreMatchers.equalTo(1)); kitaev@213: } kitaev@213: kitaev@213: static void rmdir(File dest) throws IOException { kitaev@213: LinkedList queue = new LinkedList(); kitaev@213: queue.addAll(Arrays.asList(dest.listFiles())); kitaev@213: while (!queue.isEmpty()) { kitaev@213: File next = queue.removeFirst(); kitaev@213: if (next.isDirectory()) { kitaev@213: List files = Arrays.asList(next.listFiles()); kitaev@213: if (!files.isEmpty()) { kitaev@213: queue.addAll(files); kitaev@213: queue.add(next); kitaev@213: } kitaev@213: // fall through kitaev@213: } kitaev@213: next.delete(); kitaev@213: } kitaev@213: dest.delete(); kitaev@213: } kitaev@213: }