1 package com.intellij.openapi.vcs;
3 import com.intellij.openapi.util.Pair;
4 import com.intellij.openapi.vcs.changes.Change;
5 import com.intellij.openapi.vcs.changes.ChangeListManager;
6 import com.intellij.openapi.vcs.changes.ContentRevision;
7 import com.intellij.openapi.vcs.changes.committed.MockAbstractVcs;
8 import com.intellij.testFramework.vcs.MockContentRevision;
9 import com.intellij.openapi.vcs.changes.ui.ChangesComparator;
10 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
11 import com.intellij.openapi.vcs.impl.LocalChangesUnderRoots;
12 import com.intellij.openapi.vcs.impl.projectlevelman.AllVcses;
13 import com.intellij.openapi.vcs.impl.projectlevelman.AllVcsesI;
14 import com.intellij.openapi.vfs.VirtualFile;
15 import com.intellij.testFramework.PlatformTestCase;
16 import com.intellij.testFramework.VfsTestUtil;
17 import com.intellij.testFramework.vcs.MockChangeListManager;
18 import com.intellij.vcsUtil.VcsUtil;
19 import org.junit.Before;
20 import org.junit.Test;
22 import java.lang.reflect.Field;
26 * @author Kirill Likhodedov
28 public class LocalChangesUnderRootsTest extends PlatformTestCase {
30 private LocalChangesUnderRoots myLocalChangesUnderRoots;
31 private MockChangeListManager myChangeListManager;
32 private VirtualFile myBaseDir;
35 protected void setUp() throws Exception {
38 myChangeListManager = new MockChangeListManager();
39 myBaseDir = myProject.getBaseDir();
40 myLocalChangesUnderRoots = new LocalChangesUnderRoots(ChangeListManager.getInstance(myProject),
41 ProjectLevelVcsManager.getInstance(myProject));
43 substituteChangeListManager();
46 // This is not good, but declaring MockChangeListManager might break other tests
47 private void substituteChangeListManager() throws NoSuchFieldException, IllegalAccessException {
48 Field myChangeManager = LocalChangesUnderRoots.class.getDeclaredField("myChangeManager");
49 myChangeManager.setAccessible(true);
50 myChangeManager.set(myLocalChangesUnderRoots, myChangeListManager);
54 public void testChangesInTwoGitRoots() {
55 AllVcsesI myVcses = AllVcses.getInstance(myProject);
56 myVcses.registerManually(new MockAbstractVcs(myProject, "Mock"));
58 List<VirtualFile> roots = createRootStructure(
59 Pair.create(myBaseDir.getPath(), "Mock"),
60 Pair.create("community", "Mock")
63 Change changeBeforeCommunity = createChangeForPath("a.txt");
64 Change changeAfterCommunity = createChangeForPath("readme.txt");
65 Change changeInCommunity = createChangeForPath("community/com.txt");
66 myChangeListManager.addChanges(changeBeforeCommunity, changeAfterCommunity, changeInCommunity);
68 Map<VirtualFile, Collection<Change>> expected = new HashMap<VirtualFile, Collection<Change>>();
69 expected.put(roots.get(0), Arrays.asList(changeBeforeCommunity, changeAfterCommunity));
70 expected.put(roots.get(1), Arrays.asList(changeInCommunity));
72 Map<VirtualFile, Collection<Change>> changesUnderRoots = myLocalChangesUnderRoots.getChangesUnderRoots(roots);
73 assertEqualMaps(expected, changesUnderRoots);
76 private static void assertEqualMaps(Map<VirtualFile, Collection<Change>> expected, Map<VirtualFile, Collection<Change>> actual) {
77 assertEquals("Maps size is different. " + expectedActualMessage(expected, actual), expected.size(), actual.size());
78 for (Map.Entry<VirtualFile, Collection<Change>> expectedEntry : expected.entrySet()) {
79 VirtualFile root = expectedEntry.getKey();
80 if (!actual.containsKey(root)) {
81 fail("Didn't find root [" + root + "]. " + expectedActualMessage(expected, actual));
83 List<Change> expectedChanges = new ArrayList<Change>(expectedEntry.getValue());
84 List<Change> actualChanges = new ArrayList<Change>(actual.get(root));
85 Collections.sort(expectedChanges, ChangesComparator.getInstance(false));
86 Collections.sort(actualChanges, ChangesComparator.getInstance(false));
87 assertEquals("Changes not equal for root [" + root + "]. " + expectedActualMessage(expected, actual), expectedChanges, actualChanges);
91 private static String expectedActualMessage(Object expected, Object actual) {
92 return "\nExpected:\n " + expected + "\nActual:\n" + actual;
95 private List<VirtualFile> createRootStructure(Pair<String, String>... pathAndVcs) {
96 List<VirtualFile> roots = new ArrayList<VirtualFile>();
97 List<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>();
98 for (Pair<String, String> pathAndVc : pathAndVcs) {
99 String path = pathAndVc.first;
100 String vcs = pathAndVc.second;
103 if (path.equals(myBaseDir.getPath())) {
106 vf = VfsTestUtil.createDir(myBaseDir, path);
109 mappings.add(new VcsDirectoryMapping(vf.getPath(), vcs));
112 ProjectLevelVcsManager.getInstance(myProject).setDirectoryMappings(mappings);
116 private Change createChangeForPath(String path) {
117 VirtualFile file = VfsTestUtil.createFile(myBaseDir, path);
118 FilePath filePath = VcsUtil.getFilePath(file);
119 ContentRevision beforeRevision = new MockContentRevision(filePath, new VcsRevisionNumber.Int(1));
120 ContentRevision afterRevision = new MockContentRevision(filePath, new VcsRevisionNumber.Int(2));
121 return new Change(beforeRevision, afterRevision);