Mercurial > hg4j
changeset 536:2813a26b8999
Tests: refactor various utility methods to a single location
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 05 Feb 2013 16:36:58 +0100 |
parents | d9c07e1432c4 |
children | 5a455624be4f |
files | test/org/tmatesoft/hg/test/RepoUtils.java test/org/tmatesoft/hg/test/TestAddRemove.java test/org/tmatesoft/hg/test/TestByteChannel.java test/org/tmatesoft/hg/test/TestCheckout.java test/org/tmatesoft/hg/test/TestDirstate.java test/org/tmatesoft/hg/test/TestIncoming.java test/org/tmatesoft/hg/test/TestOutgoing.java test/org/tmatesoft/hg/test/TestRevert.java |
diffstat | 8 files changed, 93 insertions(+), 61 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/org/tmatesoft/hg/test/RepoUtils.java Tue Feb 05 16:36:58 2013 +0100 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2013 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.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.tmatesoft.hg.internal.RequiresFile.*; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +import org.tmatesoft.hg.internal.RepoInitializer; +import org.tmatesoft.hg.repo.HgRepository; + +/** + * + * @author Artem Tikhomirov + * @author TMate Software Ltd. + */ +public class RepoUtils { + + static File initEmptyTempRepo(String dirName) throws IOException { + File dest = createEmptyDir(dirName); + RepoInitializer ri = new RepoInitializer(); + ri.setRequires(STORE | FNCACHE | DOTENCODE); + ri.initEmptyRepository(new File(dest, ".hg")); + return dest; + } + + static File createEmptyDir(String dirName) throws IOException { + File dest = new File(Configuration.get().getTempDir(), dirName); + if (dest.exists()) { + TestClone.rmdir(dest); + } + dest.mkdirs(); + return dest; + } + + static File cloneRepoToTempLocation(String configRepoName, String name, boolean noupdate) throws Exception, InterruptedException { + return cloneRepoToTempLocation(Configuration.get().find(configRepoName), name, noupdate); + } + + static File cloneRepoToTempLocation(HgRepository repo, String name, boolean noupdate) throws IOException, InterruptedException { + File testRepoLoc = createEmptyDir(name); + ExecHelper eh = new ExecHelper(new OutputParser.Stub(), testRepoLoc.getParentFile()); + ArrayList<String> cmd = new ArrayList<String>(); + cmd.add("hg"); cmd.add("clone"); + if (noupdate) { + cmd.add("--noupdate"); + } + cmd.add(repo.getWorkingDir().toString()); + cmd.add(testRepoLoc.getName()); + eh.run(cmd.toArray(new String[cmd.size()])); + assertEquals("[sanity]", 0, eh.getExitValue()); + return testRepoLoc; + } + + static void modifyFileAppend(File f) throws IOException { + assertTrue(f.isFile()); + FileOutputStream fos = new FileOutputStream(f, true); + fos.write("XXX".getBytes()); + fos.close(); + } + +}
--- a/test/org/tmatesoft/hg/test/TestAddRemove.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestAddRemove.java Tue Feb 05 16:36:58 2013 +0100 @@ -47,7 +47,7 @@ @Test public void testScheduleAddition() throws Exception { - File testRepoLoc = TestRevert.cloneRepoToTempLocation("log-1", "test-addremove-1", false); + File testRepoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-addremove-1", false); repo = new HgLookup().detect(testRepoLoc); StatusOutputParser statusParser = new StatusOutputParser(); @@ -72,7 +72,7 @@ @Test public void testScheduleRemoval() throws Exception { - File testRepoLoc = TestRevert.cloneRepoToTempLocation("log-1", "test-addremove-2", false); + File testRepoLoc = RepoUtils.cloneRepoToTempLocation("log-1", "test-addremove-2", false); repo = new HgLookup().detect(testRepoLoc); StatusOutputParser statusParser = new StatusOutputParser();
--- a/test/org/tmatesoft/hg/test/TestByteChannel.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestByteChannel.java Tue Feb 05 16:36:58 2013 +0100 @@ -96,7 +96,7 @@ @Test public void testWorkingCopyFileAccess() throws Exception { - final File repoDir = TestIncoming.initEmptyTempRepo("testWorkingCopyFileAccess"); + final File repoDir = RepoUtils.initEmptyTempRepo("testWorkingCopyFileAccess"); final Map<String, ?> props = Collections.singletonMap(Internals.CFG_PROPERTY_REVLOG_STREAM_CACHE, false); repo = new HgLookup(new BasicSessionContext(props, null)).detect(repoDir); File f1 = new File(repoDir, "file1");
--- a/test/org/tmatesoft/hg/test/TestCheckout.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestCheckout.java Tue Feb 05 16:36:58 2013 +0100 @@ -17,6 +17,7 @@ package org.tmatesoft.hg.test; import static org.junit.Assert.assertEquals; +import static org.tmatesoft.hg.test.RepoUtils.cloneRepoToTempLocation; import java.io.File; import java.io.FileFilter; @@ -47,7 +48,7 @@ @Test public void testCleanCheckoutInEmptyDir() throws Exception { - File testRepoLoc = TestRevert.cloneRepoToTempLocation(Configuration.get().find("log-1"), "test-checkoutClean", true); + File testRepoLoc = cloneRepoToTempLocation("log-1", "test-checkoutClean", true); repo = new HgLookup().detect(testRepoLoc); // nothing but .hg dir assertEquals("[sanity]", 0, testRepoLoc.listFiles(new FilesOnlyFilter()).length); @@ -83,7 +84,7 @@ @Test public void testBranchCheckout() throws Exception { - File testRepoLoc = TestRevert.cloneRepoToTempLocation(Configuration.get().find("log-branches"), "test-checkoutBranch", true); + File testRepoLoc = cloneRepoToTempLocation("log-branches", "test-checkoutBranch", true); repo = new HgLookup().detect(testRepoLoc); new HgCheckoutCommand(repo).changeset(3 /*branch test*/).execute();
--- a/test/org/tmatesoft/hg/test/TestDirstate.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestDirstate.java Tue Feb 05 16:36:58 2013 +0100 @@ -55,7 +55,7 @@ @Test public void testParentsEmptyRepo() throws Exception { // check contract return values for empty/nonexistent dirstate - repo = new HgLookup().detect(TestIncoming.initEmptyTempRepo("testParentsEmptyRepo")); + repo = new HgLookup().detect(RepoUtils.initEmptyTempRepo("testParentsEmptyRepo")); final Pair<Nodeid, Nodeid> wcParents = repo.getWorkingCopyParents(); Assert.assertTrue(wcParents.first().isNull()); Assert.assertTrue(wcParents.second().isNull());
--- a/test/org/tmatesoft/hg/test/TestIncoming.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestIncoming.java Tue Feb 05 16:36:58 2013 +0100 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2012 TMate Software Ltd + * Copyright (c) 2011-2013 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 @@ -17,10 +17,8 @@ package org.tmatesoft.hg.test; import static org.hamcrest.CoreMatchers.equalTo; -import static org.tmatesoft.hg.internal.RequiresFile.*; import java.io.File; -import java.io.IOException; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -32,7 +30,6 @@ import org.tmatesoft.hg.core.HgIncomingCommand; import org.tmatesoft.hg.core.HgLogCommand; import org.tmatesoft.hg.core.Nodeid; -import org.tmatesoft.hg.internal.RepoInitializer; import org.tmatesoft.hg.repo.HgLookup; import org.tmatesoft.hg.repo.HgRemoteRepository; import org.tmatesoft.hg.repo.HgRepository; @@ -63,7 +60,7 @@ int x = 0; HgLookup lookup = new HgLookup(); for (HgRemoteRepository hgRemote : Configuration.get().allRemote()) { - File dest = initEmptyTempRepo("test-incoming-" + x++); + File dest = RepoUtils.initEmptyTempRepo("test-incoming-" + x++); HgRepository localRepo = lookup.detect(dest); // Idea: // hg in, hg4j in, compare @@ -122,21 +119,4 @@ } errorCollector.checkThat(what + " Superfluous cset reported by HgIncomingCommand.execLite", set.isEmpty(), equalTo(true)); } - - static File createEmptyDir(String dirName) throws IOException { - File dest = new File(Configuration.get().getTempDir(), dirName); - if (dest.exists()) { - TestClone.rmdir(dest); - } - dest.mkdirs(); - return dest; - } - - static File initEmptyTempRepo(String dirName) throws IOException { - File dest = createEmptyDir(dirName); - RepoInitializer ri = new RepoInitializer(); - ri.setRequires(STORE | FNCACHE | DOTENCODE); - ri.initEmptyRepository(new File(dest, ".hg")); - return dest; - } }
--- a/test/org/tmatesoft/hg/test/TestOutgoing.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestOutgoing.java Tue Feb 05 16:36:58 2013 +0100 @@ -55,7 +55,7 @@ int x = 0; HgLookup lookup = new HgLookup(); for (HgRemoteRepository hgRemote : Configuration.get().allRemote()) { - File dest = TestIncoming.createEmptyDir("test-outgoing-" + x++); + File dest = RepoUtils.createEmptyDir("test-outgoing-" + x++); ExecHelper eh0 = new ExecHelper(new OutputParser.Stub(false), null); eh0.run("hg", "clone", hgRemote.getLocation(), dest.toString()); eh0.cwd(dest);
--- a/test/org/tmatesoft/hg/test/TestRevert.java Tue Feb 05 15:54:37 2013 +0100 +++ b/test/org/tmatesoft/hg/test/TestRevert.java Tue Feb 05 16:36:58 2013 +0100 @@ -17,12 +17,8 @@ package org.tmatesoft.hg.test; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.ArrayList; import org.junit.Rule; import org.junit.Test; @@ -52,11 +48,11 @@ @Test public void testCommand() throws Exception { // get a copy of a repository - File testRepoLoc = cloneRepoToTempLocation(Configuration.get().find("log-1"), "test-revert", false); + File testRepoLoc = RepoUtils.cloneRepoToTempLocation(Configuration.get().find("log-1"), "test-revert", false); repo = new HgLookup().detect(testRepoLoc); Path targetFile = Path.create("b"); - modifyFileAppend(new File(testRepoLoc, targetFile.toString())); + RepoUtils.modifyFileAppend(new File(testRepoLoc, targetFile.toString())); StatusOutputParser statusParser = new StatusOutputParser(); eh = new ExecHelper(statusParser, testRepoLoc); @@ -75,30 +71,4 @@ errorCollector.assertEquals(1, statusParser.getUnknown().size()); errorCollector.assertEquals(targetFile.toString() + ".orig", statusParser.getUnknown().get(0).toString()); } - - private static void modifyFileAppend(File f) throws IOException { - assertTrue(f.isFile()); - FileOutputStream fos = new FileOutputStream(f, true); - fos.write("XXX".getBytes()); - fos.close(); - } - - static File cloneRepoToTempLocation(String configRepoName, String name, boolean noupdate) throws Exception, InterruptedException { - return cloneRepoToTempLocation(Configuration.get().find(configRepoName), name, noupdate); - } - - static File cloneRepoToTempLocation(HgRepository repo, String name, boolean noupdate) throws IOException, InterruptedException { - File testRepoLoc = TestIncoming.createEmptyDir(name); - ExecHelper eh = new ExecHelper(new OutputParser.Stub(), testRepoLoc.getParentFile()); - ArrayList<String> cmd = new ArrayList<String>(); - cmd.add("hg"); cmd.add("clone"); - if (noupdate) { - cmd.add("--noupdate"); - } - cmd.add(repo.getWorkingDir().toString()); - cmd.add(testRepoLoc.getName()); - eh.run(cmd.toArray(new String[cmd.size()])); - assertEquals("[sanity]", 0, eh.getExitValue()); - return testRepoLoc; - } }