cleanup
[idea/community.git] / plugins / git4idea / tests / git4idea / repo / GitRepositoryReaderTest.java
1 package git4idea.repo;
2
3 import com.intellij.openapi.application.PluginPathManager;
4 import com.intellij.openapi.util.io.FileUtil;
5 import com.intellij.openapi.util.io.FileUtilRt;
6 import com.intellij.openapi.util.text.StringUtil;
7 import com.intellij.openapi.vcs.VcsTestUtil;
8 import com.intellij.util.Function;
9 import com.intellij.util.containers.ContainerUtil;
10 import com.intellij.util.io.ZipUtil;
11 import com.intellij.util.ui.UIUtil;
12 import com.intellij.vcs.log.Hash;
13 import com.intellij.vcs.log.impl.HashImpl;
14 import git4idea.GitBranch;
15 import git4idea.GitLocalBranch;
16 import git4idea.test.GitPlatformTest;
17 import junit.framework.TestCase;
18 import org.jetbrains.annotations.NotNull;
19 import org.junit.After;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.junit.runners.Parameterized;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.util.Collection;
28 import java.util.List;
29
30 @RunWith(Parameterized.class)
31 public class GitRepositoryReaderTest extends GitPlatformTest {
32
33   @NotNull private final File myTestCaseDir;
34
35   private File myTempDir;
36   private GitRepositoryReader myRepositoryReader;
37   private File myGitDir;
38
39   @Parameterized.Parameters(name = "{0}")
40   public static Collection<Object[]> data() {
41     File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
42     File dataDir = new File(new File(pluginRoot, "testData"), "repo");
43     File[] testCases = dataDir.listFiles(FileUtilRt.ALL_DIRECTORIES);
44     return ContainerUtil.map(testCases, new Function<File, Object[]>() {
45       @Override
46       public Object[] fun(File file) {
47         return new Object[] { file.getName(), file };
48       }
49     });
50   }
51
52   @SuppressWarnings({"UnusedParameters"})
53   public GitRepositoryReaderTest(@NotNull String name, @NotNull File testDir) {
54     myTestCaseDir = testDir;
55   }
56
57   @Override
58   @Before
59   public void setUp() throws Exception {
60     super.setUp();
61     myTempDir = new File(myProjectRoot.getPath(), "test");
62     prepareTest(myTestCaseDir);
63   }
64
65   @After
66   @Override
67   public void tearDown() throws Exception {
68     FileUtil.delete(myTempDir);
69     UIUtil.invokeAndWaitIfNeeded(new Runnable() {
70       @Override
71       public void run() {
72         try {
73           GitRepositoryReaderTest.super.tearDown();
74         }
75         catch (Exception e) {
76           throw new RuntimeException(e);
77         }
78       }
79     });
80   }
81
82   private void prepareTest(File testDir) throws IOException {
83     assertTrue("Temp directory was not created", myTempDir.mkdir());
84     FileUtil.copyDir(testDir, myTempDir);
85     myGitDir = new File(myTempDir, ".git");
86     File dotGit = new File(myTempDir, "dot_git");
87     if (!dotGit.exists()) {
88       File dotGitZip = new File(myTempDir, "dot_git.zip");
89       assertTrue("Neither dot_git nor dot_git.zip were found", dotGitZip.exists());
90       ZipUtil.extract(dotGitZip, myTempDir, null);
91     }
92     FileUtil.rename(dotGit, myGitDir);
93     TestCase.assertTrue(myGitDir.exists());
94     myRepositoryReader = new GitRepositoryReader(myGitDir);
95   }
96
97
98   @NotNull
99   private static String readHead(@NotNull File dir) throws IOException {
100     return FileUtil.loadFile(new File(dir, "head.txt")).trim();
101   }
102
103   @NotNull
104   private static Branch readCurrentBranch(@NotNull File resultDir) throws IOException {
105     String branch = FileUtil.loadFile(new File(resultDir, "current-branch.txt")).trim();
106     return readBranchFromLine(branch);
107   }
108
109   @NotNull
110   private static Branch readBranchFromLine(@NotNull String branch) {
111     List<String> branchAndHash = StringUtil.split(branch, " ");
112     return new Branch(branchAndHash.get(1), HashImpl.build(branchAndHash.get(0)));
113   }
114
115   @Test
116   public void testBranches() throws Exception {
117     Collection<GitRemote> remotes = GitConfig.read(myPlatformFacade, new File(myGitDir, "config")).parseRemotes();
118     GitBranchState state = myRepositoryReader.readState(remotes);
119
120     assertEquals("HEAD revision is incorrect", readHead(myTempDir), state.getCurrentRevision());
121     assertEqualBranches(readCurrentBranch(myTempDir), state.getCurrentBranch());
122     assertBranches(state.getLocalBranches(), readBranches(myTempDir, true));
123     assertBranches(state.getRemoteBranches(), readBranches(myTempDir, false));
124   }
125
126   private static void assertEqualBranches(@NotNull Branch expected, @NotNull GitLocalBranch actual) {
127     assertEquals(expected.name, actual.getName());
128     assertEquals("Incorrect hash of branch " + actual.getName(), expected.hash, actual.getHash());
129   }
130
131   private static void assertBranches(Collection<? extends GitBranch> actualBranches, Collection<Branch> expectedBranches) {
132     VcsTestUtil.assertEqualCollections(actualBranches, expectedBranches, new VcsTestUtil.EqualityChecker<GitBranch, Branch>() {
133       @Override
134       public boolean areEqual(GitBranch actual, Branch expected) {
135         return branchesAreEqual(actual, expected);
136       }
137     });
138   }
139
140   @NotNull
141   private static Collection<Branch> readBranches(@NotNull File resultDir, boolean local) throws IOException {
142     String content = FileUtil.loadFile(new File(resultDir, local ? "local-branches.txt" : "remote-branches.txt"));
143     Collection<Branch> branches = ContainerUtil.newArrayList();
144     for (String line : StringUtil.splitByLines(content)) {
145       branches.add(readBranchFromLine(line));
146     }
147     return branches;
148   }
149
150   private static boolean branchesAreEqual(GitBranch actual, Branch expected) {
151     return actual.getFullName().equals(expected.name) && actual.getHash().equals(expected.hash);
152   }
153
154   private static class Branch {
155     final String name;
156     final Hash hash;
157
158     private Branch(String name, Hash hash) {
159       this.name = name;
160       this.hash = hash;
161     }
162
163     @Override
164     public String toString() {
165       return name;
166     }
167   }
168
169 }