update copyrights
[fedora-idea.git] / platform / lvcs-impl / testSrc / com / intellij / historyIntegrTests / ExternalChangesAndRefreshingTest.java
blobd6e1ee66fdbda3a26687a4a6ef448f1924dabd23
1 /*
2 * Copyright 2000-2009 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.
17 package com.intellij.historyIntegrTests;
20 import static com.intellij.history.core.LocalVcsTestCase.list;
21 import com.intellij.history.core.Paths;
22 import com.intellij.history.core.revisions.Revision;
23 import com.intellij.history.utils.RunnableAdapter;
24 import com.intellij.openapi.command.CommandProcessor;
25 import com.intellij.openapi.roots.ModifiableRootModel;
26 import com.intellij.openapi.roots.ModuleRootManager;
27 import com.intellij.openapi.util.io.FileUtil;
28 import com.intellij.openapi.vfs.*;
30 import java.io.File;
31 import java.io.IOException;
32 import java.util.concurrent.Semaphore;
34 public class ExternalChangesAndRefreshingTest extends IntegrationTestCase {
35 public void testRefreshingSynchronously() throws Exception {
36 doTestRefreshing(false);
39 public void testRefreshingAsynchronously() throws Exception {
40 doTestRefreshing(true);
43 @Override
44 protected void runBareRunnable(Runnable r) throws Throwable {
45 if (getName().equals("testRefreshingAsynchronously")) {
46 // this methods waits for another thread to finish, that leds
47 // to deadlock in swing-thread. Therefore we have to run this test
48 // outside of swing-thread
49 r.run();
51 else {
52 super.runBareRunnable(r);
56 private void doTestRefreshing(boolean async) throws Exception {
57 String path1 = createFileExternally("f1.txt");
58 String path2 = createFileExternally("f2.txt");
60 assertFalse(hasVcsEntry(path1));
61 assertFalse(hasVcsEntry(path2));
63 refreshVFS(async);
65 assertTrue(hasVcsEntry(path1));
66 assertTrue(hasVcsEntry(path2));
68 assertEquals(2, getVcsRevisionsFor(root).size());
71 public void testChangeSetName() throws Exception {
72 createFileExternally("f.txt");
73 refreshVFS();
74 Revision r = getVcsRevisionsFor(root).get(0);
75 assertEquals("External change", r.getCauseChangeName());
78 public void testRefreshDuringCommand() {
79 // shouldn't throw
80 CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
81 public void run() {
82 refreshVFS();
84 }, "", null);
87 public void testRefreshingSpecifiedFiles() throws Exception {
88 String f1 = createFileExternally("f1.txt");
89 String f2 = createFileExternally("f2.txt");
91 LocalFileSystem.getInstance().refreshIoFiles(list(new File(f1), new File(f2)));
93 assertTrue(hasVcsEntry(f1));
94 assertTrue(hasVcsEntry(f2));
97 public void testCommandDuringRefresh() throws Exception {
98 createFileExternally("f.txt");
100 VirtualFileListener l = new VirtualFileAdapter() {
101 @Override
102 public void fileCreated(VirtualFileEvent e) {
103 executeSomeCommand();
107 // shouldn't throw
108 addFileListenerDuring(l, new Runnable() {
109 public void run() {
110 refreshVFS();
115 private void executeSomeCommand() {
116 CommandProcessor.getInstance().executeCommand(myProject, new Runnable() {
117 public void run() {
119 }, "", null);
122 public void testContentOfFileChangedDuringRefresh() throws Exception {
123 final VirtualFile f = root.createChildData(null, "file.txt");
124 f.setBinaryContent("before".getBytes());
126 performAllPendingJobs();
128 ContentChangesListener l = new ContentChangesListener(f);
129 addFileListenerDuring(l, new RunnableAdapter() {
130 @Override
131 public void doRun() throws IOException {
132 changeFileExternally(f.getPath(), "after");
133 refreshVFS();
137 // todo unrelable test because content recorded before LvcsFileListener does its job
138 assertEquals("before", l.getContentBefore());
139 assertEquals("after", l.getContentAfter());
142 private void performAllPendingJobs() {
143 refreshVFS();
146 public void testFileCreationDuringRefresh() throws Exception {
147 final String path = createFileExternally("f.txt");
148 changeFileExternally(path, "content");
150 final String[] content = new String[1];
151 VirtualFileListener l = new VirtualFileAdapter() {
152 @Override
153 public void fileCreated(VirtualFileEvent e) {
154 try {
155 if (!e.getFile().getPath().equals(path)) return;
156 content[0] = new String(e.getFile().contentsToByteArray());
158 catch (IOException ex) {
159 throw new RuntimeException(ex);
164 addFileListenerDuring(l, new Runnable() {
165 public void run() {
166 refreshVFS();
169 assertEquals("content", content[0]);
172 public void ignoreTestCreationOfExcludedDirectoryDuringRefresh() throws Exception {
173 // todo does not work due to FileListener order. FileIndex gets event later than Lvcs.
175 VirtualFile dir = root.createChildDirectory(null, "EXCLUDED");
176 String p = dir.getPath();
178 assertTrue(hasVcsEntry(p));
180 ModifiableRootModel m = ModuleRootManager.getInstance(myModule).getModifiableModel();
181 m.getContentEntries()[0].addExcludeFolder(dir);
182 m.commit();
184 assertFalse(hasVcsEntry(p));
186 dir.delete(null);
188 createDirectoryExternally("EXCLUDED");
189 refreshVFS();
191 assertFalse(hasVcsEntry(p));
194 public void testDeletionOfFilteredDirectoryExternallyDoesNotThrowExceptionDuringRefresh() throws Exception {
195 VirtualFile f = root.createChildDirectory(null, FILTERED_DIR_NAME);
196 String path = Paths.appended(root.getPath(), FILTERED_DIR_NAME);
198 assertFalse(hasVcsEntry(path));
200 new File(path).delete();
201 refreshVFS();
203 assertFalse(hasVcsEntry(path));
206 public void testCreationOfExcludedDirWithFilesDuringRefreshShouldNotThrowException() throws Exception {
207 // there was a problem with the DirectoryIndex - the files that were created during the refresh
208 // were not correctly excluded, thereby causing the LocalHistory to fail during addition of
209 // files under the excluded dir.
211 File targetDir = createTargetDir();
213 FileUtil.copyDir(targetDir, new File(root.getPath(), "target"));
214 VirtualFileManager.getInstance().refresh(false);
216 VirtualFile classes = root.findFileByRelativePath("target/classes");
217 addExcludedDir(classes);
218 classes.getParent().delete(null);
220 FileUtil.copyDir(targetDir, new File(root.getPath(), "target"));
221 VirtualFileManager.getInstance().refresh(false); // shouldn't throw
224 private File createTargetDir() throws IOException {
225 File result = createTempDirectory();
226 File classes = new File(result, "classes");
227 classes.mkdir();
228 new File(classes, "bak.txt").createNewFile();
229 return result;
232 private void refreshVFS() {
233 refreshVFS(false);
236 private void refreshVFS(boolean async) {
237 try {
238 final Semaphore s = new Semaphore(1);
239 s.acquire();
240 VirtualFileManager.getInstance().refresh(async, new Runnable() {
241 public void run() {
242 s.release();
245 s.acquire();
247 catch (InterruptedException e) {
248 throw new RuntimeException(e);