2 * Copyright 2000-2016 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.testFramework;
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.application.WriteAction;
20 import com.intellij.openapi.util.Key;
21 import com.intellij.openapi.util.SystemInfo;
22 import com.intellij.openapi.util.io.FileUtil;
23 import com.intellij.openapi.util.text.StringUtil;
24 import com.intellij.openapi.vfs.LocalFileSystem;
25 import com.intellij.openapi.vfs.VfsUtil;
26 import com.intellij.openapi.vfs.VirtualFile;
27 import com.intellij.openapi.vfs.VirtualFileManager;
28 import com.intellij.openapi.vfs.newvfs.BulkFileListener;
29 import com.intellij.openapi.vfs.newvfs.events.*;
30 import com.intellij.util.PathUtil;
31 import com.intellij.util.messages.MessageBusConnection;
32 import org.jetbrains.annotations.NotNull;
33 import org.junit.Assert;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.stream.Collectors;
44 public class VfsTestUtil {
45 public static final Key<String> TEST_DATA_FILE_PATH = Key.create("TEST_DATA_FILE_PATH");
47 private VfsTestUtil() { }
49 public static VirtualFile createFile(@NotNull VirtualFile root, @NotNull String relativePath) {
50 return createFile(root, relativePath, "");
53 public static VirtualFile createFile(@NotNull VirtualFile root, @NotNull String relativePath, @NotNull String text) {
54 return createFileOrDir(root, relativePath, text, false);
57 public static VirtualFile createDir(@NotNull VirtualFile root, @NotNull String relativePath) {
58 return createFileOrDir(root, relativePath, "", true);
61 private static VirtualFile createFileOrDir(VirtualFile root, String relativePath, String text, boolean dir) {
63 return WriteAction.compute(() -> {
64 VirtualFile parent = root;
65 for (String name : StringUtil.tokenize(PathUtil.getParentPath(relativePath), "/")) {
66 VirtualFile child = parent.findChild(name);
67 if (child == null || !child.isValid()) {
68 child = parent.createChildDirectory(VfsTestUtil.class, name);
73 parent.getChildren();//need this to ensure that fileCreated event is fired
75 String name = PathUtil.getFileName(relativePath);
78 file = parent.createChildDirectory(VfsTestUtil.class, name);
81 file = parent.findChild(name);
83 file = parent.createChildData(VfsTestUtil.class, name);
85 VfsUtil.saveText(file, text);
90 catch (IOException e) {
91 throw new RuntimeException(e);
95 public static void deleteFile(@NotNull VirtualFile file) {
96 UtilKt.deleteFile(file);
99 public static void clearContent(@NotNull final VirtualFile file) {
100 ApplicationManager.getApplication().runWriteAction(() -> {
102 VfsUtil.saveText(file, "");
104 catch (IOException e) {
105 throw new RuntimeException(e);
110 public static void overwriteTestData(String filePath, String actual) {
112 FileUtil.writeToFile(new File(filePath), actual);
114 catch (IOException e) {
115 throw new AssertionError(e);
120 public static VirtualFile findFileByCaseSensitivePath(@NotNull String absolutePath) {
121 String vfsPath = FileUtil.toSystemIndependentName(absolutePath);
122 VirtualFile vFile = LocalFileSystem.getInstance().findFileByPath(vfsPath);
123 Assert.assertNotNull("file " + absolutePath + " not found", vFile);
124 String realVfsPath = vFile.getPath();
125 if (!SystemInfo.isFileSystemCaseSensitive && !vfsPath.equals(realVfsPath) &&
126 vfsPath.equalsIgnoreCase(realVfsPath)) {
127 Assert.fail("Please correct case-sensitivity of path to prevent test failure on case-sensitive file systems:\n" +
128 " path " + vfsPath + "\n" +
129 "real path " + realVfsPath);
134 public static void assertFilePathEndsWithCaseSensitivePath(@NotNull VirtualFile file, @NotNull String suffixPath) {
135 String vfsSuffixPath = FileUtil.toSystemIndependentName(suffixPath);
136 String vfsPath = file.getPath();
137 if (!SystemInfo.isFileSystemCaseSensitive && !vfsPath.endsWith(vfsSuffixPath) &&
138 StringUtil.endsWithIgnoreCase(vfsPath, vfsSuffixPath)) {
139 String realSuffixPath = vfsPath.substring(vfsPath.length() - vfsSuffixPath.length());
140 Assert.fail("Please correct case-sensitivity of path to prevent test failure on case-sensitive file systems:\n" +
141 " path " + suffixPath + "\n" +
142 "real path " + realSuffixPath);
147 public static List<VFileEvent> getEvents(@NotNull Runnable action) {
148 List<VFileEvent> allEvents = new ArrayList<>();
150 MessageBusConnection connection = ApplicationManager.getApplication().getMessageBus().connect();
151 connection.subscribe(VirtualFileManager.VFS_CHANGES, new BulkFileListener.Adapter() {
153 public void after(@NotNull List<? extends VFileEvent> events) {
154 allEvents.addAll(events);
161 connection.disconnect();
168 public static List<String> print(@NotNull List<? extends VFileEvent> events) {
169 return events.stream().map(VfsTestUtil::print).collect(Collectors.toList());
172 private static String print(VFileEvent e) {
174 if (e instanceof VFileCreateEvent) type = 'C';
175 else if (e instanceof VFileDeleteEvent) type = 'D';
176 else if (e instanceof VFileContentChangeEvent) type = 'U';
177 else if (e instanceof VFilePropertyChangeEvent) type = 'P';
178 return type + " : " + e.getPath();