[git] migrate GitRepositoryReaderTest to use JUnit Parametrized

This commit is contained in:
Kirill Likhodedov
2014-04-11 16:50:16 +04:00
parent 90365a7d07
commit 49eaa919bf
@@ -4,8 +4,10 @@ import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsTestUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.io.ZipUtil;
import com.intellij.util.ui.UIUtil;
import com.intellij.vcs.log.impl.HashImpl;
import git4idea.GitBranch;
import git4idea.GitLocalBranch;
@@ -13,6 +15,11 @@ import git4idea.branch.GitBranchesCollection;
import git4idea.test.GitPlatformTest;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.FileFilter;
@@ -20,22 +27,61 @@ import java.io.IOException;
import java.util.Collection;
import java.util.List;
@RunWith(Parameterized.class)
public class GitRepositoryReaderTest extends GitPlatformTest {
private File myDataDir;
private File myTempDir;
@NotNull private final File myTestCaseDir;
private File myTempDir;
private GitRepositoryReader myRepositoryReader;
private File myGitDir;
@Override
protected void setUp() throws Exception {
super.setUp();
myTempDir = new File(myProjectRoot.getPath(), "test");
@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> data() {
File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
myDataDir = new File(new File(pluginRoot, "testData"), "repo");
File dataDir = new File(new File(pluginRoot, "testData"), "repo");
File[] testCases = dataDir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
return ContainerUtil.map(testCases, new Function<File, Object[]>() {
@Override
public Object[] fun(File file) {
return new Object[] { file.getName(), file };
}
});
}
@SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors", "UnusedParameters"})
public GitRepositoryReaderTest(@NotNull String name, @NotNull File testDir) {
myTestCaseDir = testDir;
}
@Override
@Before
public void setUp() throws Exception {
super.setUp();
myTempDir = new File(myProjectRoot.getPath(), "test");
prepareTest(myTestCaseDir);
}
@After
@Override
public void tearDown() throws Exception {
FileUtil.delete(myTempDir);
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
try {
GitRepositoryReaderTest.super.tearDown();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
private void prepareTest(File testDir) throws IOException {
@@ -53,28 +99,6 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
myRepositoryReader = new GitRepositoryReader(myGitDir);
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
private void doTest(@NotNull ResultConsumer test) throws Exception {
File[] files = myDataDir.listFiles(new FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
for (File dir : files) {
prepareTest(dir);
test.consume(dir);
cleanupTest();
}
}
private void cleanupTest() {
FileUtil.delete(myTempDir);
}
@NotNull
private static String readHead(@NotNull File dir) throws IOException {
@@ -93,22 +117,27 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
return new GitLocalBranch(branchAndHash.get(1), HashImpl.build(branchAndHash.get(0)));
}
@Test
public void testHEAD() throws Exception {
doTest(new ResultConsumer() {
@Override
public void consume(@NotNull File resultDir) throws Exception {
assertEquals("HEAD is incorrect", readHead(resultDir), myRepositoryReader.readCurrentRevision());
}
});
assertEquals("HEAD is incorrect", readHead(myTempDir), myRepositoryReader.readCurrentRevision());
}
@Test
public void testCurrentBranch() throws Exception {
doTest(new ResultConsumer() {
@Override
public void consume(@NotNull File resultDir) throws Exception {
assertEqualBranches(readCurrentBranch(resultDir), myRepositoryReader.readCurrentBranch());
}
});
assertEqualBranches(readCurrentBranch(myTempDir), myRepositoryReader.readCurrentBranch());
}
@Test
public void testBranches() throws Exception {
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch();
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
assertEqualBranches(readCurrentBranch(myTempDir), currentBranch);
assertBranches(localBranches, readBranches(myTempDir, true));
assertBranches(remoteBranches, readBranches(myTempDir, false));
}
private static void assertEqualBranches(@NotNull GitLocalBranch expected, @NotNull GitLocalBranch actual) {
@@ -116,23 +145,6 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
assertEquals(expected.getHash(), actual.getHash());
}
public void testBranches() throws Exception {
doTest(new ResultConsumer() {
@Override
public void consume(@NotNull File resultDir) throws Exception {
Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes);
GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch();
Collection<? extends GitBranch> localBranches = branchesCollection.getLocalBranches();
Collection<? extends GitBranch> remoteBranches = branchesCollection.getRemoteBranches();
assertEqualBranches(readCurrentBranch(resultDir), currentBranch);
assertBranches(localBranches, readBranches(resultDir, true));
assertBranches(remoteBranches, readBranches(resultDir, false));
}
});
}
private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<? extends GitBranch> expectedBranches) {
VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, GitBranch>() {
@Override
@@ -156,7 +168,4 @@ public class GitRepositoryReaderTest extends GitPlatformTest {
return actual.getName().equals(expected.getName()) && actual.getHash().equals(expected.getHash());
}
private abstract static class ResultConsumer {
public abstract void consume(@NotNull File resultDir) throws Exception;
}
}