2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.openapi.vcs;
18 import com.intellij.openapi.util.Pair;
19 import com.intellij.openapi.vcs.changes.Change;
20 import com.intellij.openapi.vcs.changes.ChangeListManager;
21 import com.intellij.openapi.vcs.changes.ChangeListManagerImpl;
22 import com.intellij.openapi.vcs.changes.ContentRevision;
23 import com.intellij.openapi.vcs.changes.committed.MockAbstractVcs;
24 import com.intellij.openapi.vcs.changes.ui.ChangesComparator;
25 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
26 import com.intellij.openapi.vcs.impl.LocalChangesUnderRoots;
27 import com.intellij.openapi.vcs.impl.projectlevelman.AllVcses;
28 import com.intellij.openapi.vcs.impl.projectlevelman.AllVcsesI;
29 import com.intellij.openapi.vfs.VirtualFile;
30 import com.intellij.testFramework.PlatformTestCase;
31 import com.intellij.testFramework.VfsTestUtil;
32 import com.intellij.testFramework.vcs.MockChangeListManager;
33 import com.intellij.testFramework.vcs.MockContentRevision;
34 import com.intellij.vcsUtil.VcsUtil;
36 import java.lang.reflect.Field;
40 * @author Kirill Likhodedov
42 public class LocalChangesUnderRootsTest extends PlatformTestCase {
44 private LocalChangesUnderRoots myLocalChangesUnderRoots;
45 private MockChangeListManager myChangeListManager;
46 private VirtualFile myBaseDir;
49 protected void setUp() throws Exception {
52 myChangeListManager = new MockChangeListManager();
53 myBaseDir = myProject.getBaseDir();
54 myLocalChangesUnderRoots = new LocalChangesUnderRoots(ChangeListManager.getInstance(myProject),
55 ProjectLevelVcsManager.getInstance(myProject));
57 substituteChangeListManager();
61 protected void tearDown() throws Exception {
62 ((ChangeListManagerImpl) ChangeListManager.getInstance(myProject)).stopEveryThingIfInTestMode();
66 // This is not good, but declaring MockChangeListManager might break other tests
67 private void substituteChangeListManager() throws NoSuchFieldException, IllegalAccessException {
68 Field myChangeManager = LocalChangesUnderRoots.class.getDeclaredField("myChangeManager");
69 myChangeManager.setAccessible(true);
70 myChangeManager.set(myLocalChangesUnderRoots, myChangeListManager);
73 public void testChangesInTwoGitRoots() {
74 AllVcsesI myVcses = AllVcses.getInstance(myProject);
75 myVcses.registerManually(new MockAbstractVcs(myProject, "Mock"));
77 List<VirtualFile> roots = createRootStructure(
78 Pair.create(myBaseDir.getPath(), "Mock"),
79 Pair.create("community", "Mock")
82 Change changeBeforeCommunity = createChangeForPath("a.txt");
83 Change changeAfterCommunity = createChangeForPath("readme.txt");
84 Change changeInCommunity = createChangeForPath("community/com.txt");
85 myChangeListManager.addChanges(changeBeforeCommunity, changeAfterCommunity, changeInCommunity);
87 Map<VirtualFile, Collection<Change>> expected = new HashMap<VirtualFile, Collection<Change>>();
88 expected.put(roots.get(0), Arrays.asList(changeBeforeCommunity, changeAfterCommunity));
89 expected.put(roots.get(1), Collections.singletonList(changeInCommunity));
91 Map<VirtualFile, Collection<Change>> changesUnderRoots = myLocalChangesUnderRoots.getChangesUnderRoots(roots);
92 assertEqualMaps(expected, changesUnderRoots);
95 private static void assertEqualMaps(Map<VirtualFile, Collection<Change>> expected, Map<VirtualFile, Collection<Change>> actual) {
96 assertEquals("Maps size is different. " + expectedActualMessage(expected, actual), expected.size(), actual.size());
97 for (Map.Entry<VirtualFile, Collection<Change>> expectedEntry : expected.entrySet()) {
98 VirtualFile root = expectedEntry.getKey();
99 if (!actual.containsKey(root)) {
100 fail("Didn't find root [" + root + "]. " + expectedActualMessage(expected, actual));
102 List<Change> expectedChanges = new ArrayList<Change>(expectedEntry.getValue());
103 List<Change> actualChanges = new ArrayList<Change>(actual.get(root));
104 Collections.sort(expectedChanges, ChangesComparator.getInstance(false));
105 Collections.sort(actualChanges, ChangesComparator.getInstance(false));
106 assertEquals("Changes not equal for root [" + root + "]. " + expectedActualMessage(expected, actual), expectedChanges, actualChanges);
110 private static String expectedActualMessage(Object expected, Object actual) {
111 return "\nExpected:\n " + expected + "\nActual:\n" + actual;
114 private List<VirtualFile> createRootStructure(Pair<String, String>... pathAndVcs) {
115 List<VirtualFile> roots = new ArrayList<VirtualFile>();
116 List<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>();
117 for (Pair<String, String> pathAndVc : pathAndVcs) {
118 String path = pathAndVc.first;
119 String vcs = pathAndVc.second;
122 if (path.equals(myBaseDir.getPath())) {
125 vf = VfsTestUtil.createDir(myBaseDir, path);
128 mappings.add(new VcsDirectoryMapping(vf.getPath(), vcs));
131 ProjectLevelVcsManager.getInstance(myProject).setDirectoryMappings(mappings);
135 private Change createChangeForPath(String path) {
136 VirtualFile file = VfsTestUtil.createFile(myBaseDir, path);
137 FilePath filePath = VcsUtil.getFilePath(file);
138 ContentRevision beforeRevision = new MockContentRevision(filePath, new VcsRevisionNumber.Int(1));
139 ContentRevision afterRevision = new MockContentRevision(filePath, new VcsRevisionNumber.Int(2));
140 return new Change(beforeRevision, afterRevision);