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
.*;
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);
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
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
));
65 assertTrue(hasVcsEntry(path1
));
66 assertTrue(hasVcsEntry(path2
));
68 assertEquals(2, getVcsRevisionsFor(root
).size());
71 public void testChangeSetName() throws Exception
{
72 createFileExternally("f.txt");
74 Revision r
= getVcsRevisionsFor(root
).get(0);
75 assertEquals("External change", r
.getCauseChangeName());
78 public void testRefreshDuringCommand() {
80 CommandProcessor
.getInstance().executeCommand(myProject
, new Runnable() {
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() {
102 public void fileCreated(VirtualFileEvent e
) {
103 executeSomeCommand();
108 addFileListenerDuring(l
, new Runnable() {
115 private void executeSomeCommand() {
116 CommandProcessor
.getInstance().executeCommand(myProject
, new Runnable() {
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() {
131 public void doRun() throws IOException
{
132 changeFileExternally(f
.getPath(), "after");
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() {
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() {
153 public void fileCreated(VirtualFileEvent e
) {
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() {
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
);
184 assertFalse(hasVcsEntry(p
));
188 createDirectoryExternally("EXCLUDED");
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();
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");
228 new File(classes
, "bak.txt").createNewFile();
232 private void refreshVFS() {
236 private void refreshVFS(boolean async
) {
238 final Semaphore s
= new Semaphore(1);
240 VirtualFileManager
.getInstance().refresh(async
, new Runnable() {
247 catch (InterruptedException e
) {
248 throw new RuntimeException(e
);