diff --git a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java index e6f40f1da782..b8bce9b7ee82 100644 --- a/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java +++ b/plugins/git4idea/tests/git4idea/repo/GitRepositoryReaderTest.java @@ -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 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() { + @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 remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes(); + GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes); + GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch(); + Collection localBranches = branchesCollection.getLocalBranches(); + Collection 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 remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes(); - GitBranchesCollection branchesCollection = myRepositoryReader.readBranches(remotes); - GitLocalBranch currentBranch = myRepositoryReader.readCurrentBranch(); - Collection localBranches = branchesCollection.getLocalBranches(); - Collection remoteBranches = branchesCollection.getRemoteBranches(); - - assertEqualBranches(readCurrentBranch(resultDir), currentBranch); - assertBranches(localBranches, readBranches(resultDir, true)); - assertBranches(remoteBranches, readBranches(resultDir, false)); - } - }); - } - private static void assertBranches(Collection actualBranches, Collection expectedBranches) { VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker() { @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; - } }