diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightTempDirTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightTempDirTestFixtureImpl.java index ae8d3cbeab0f..93dfcff1d0e6 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightTempDirTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/LightTempDirTestFixtureImpl.java @@ -1,24 +1,9 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.testFramework.fixtures.impl; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.util.Computable; -import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.*; import com.intellij.testFramework.LightPlatformTestCase; @@ -34,26 +19,27 @@ import java.util.List; /** * @author yole */ -@SuppressWarnings({"JUnitTestClassNamingConvention", "JUnitTestCaseWithNoTests"}) public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirTestFixture { private final VirtualFile mySourceRoot; - private final boolean myUsePlatformSourceRoot; public LightTempDirTestFixtureImpl() { - final VirtualFile fsRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///"); - Assert.assertNotNull(fsRoot); - try { - mySourceRoot = WriteAction.computeAndWait(() -> fsRoot.createChildDirectory(this, "root")); - } - catch (IOException e) { - throw new RuntimeException(e); - } - myUsePlatformSourceRoot = false; + this(false); } public LightTempDirTestFixtureImpl(boolean usePlatformSourceRoot) { - myUsePlatformSourceRoot = usePlatformSourceRoot; - mySourceRoot = null; + if (usePlatformSourceRoot) { + mySourceRoot = null; + } + else { + VirtualFile fsRoot = VirtualFileManager.getInstance().findFileByUrl("temp:///"); + Assert.assertNotNull(fsRoot); + try { + mySourceRoot = WriteAction.computeAndWait(() -> fsRoot.createChildDirectory(this, "root")); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } } @Override @@ -69,9 +55,9 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT } } - @Override @NotNull - public VirtualFile findOrCreateDir(@NotNull final String path) { + @Override + public VirtualFile findOrCreateDir(@NotNull String path) { return WriteAction.computeAndWait(() -> { try { return findOrCreateChildDir(getSourceRoot(), path); @@ -90,11 +76,11 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT @NotNull @Override - public VirtualFile copyAll(@NotNull final String dataDir, @NotNull final String targetDir, @NotNull final VirtualFileFilter filter) { + public VirtualFile copyAll(@NotNull String dataDir, @NotNull String targetDir, @NotNull VirtualFileFilter filter) { return ApplicationManager.getApplication().runWriteAction(new Computable() { @Override public VirtualFile compute() { - final VirtualFile from = LocalFileSystem.getInstance().refreshAndFindFileByPath(dataDir); + VirtualFile from = LocalFileSystem.getInstance().refreshAndFindFileByPath(dataDir); Assert.assertNotNull("Cannot find testdata directory " + dataDir, from); try { UsefulTestCase.refreshRecursively(from); @@ -116,8 +102,8 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT private VirtualFile findOrCreateChildDir(VirtualFile root, String relativePath) throws IOException { if (relativePath.length() == 0) return root; - String trimPath = StringUtil.trimStart(relativePath, "/"); - final List dirs = StringUtil.split(trimPath, "/"); + + List dirs = StringUtil.split(StringUtil.trimStart(relativePath, "/"), "/"); for (String dirName : dirs) { if (dirName.equals(".")) continue; @@ -132,18 +118,10 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT root = dir; } else { - try { - if (SystemInfo.isWindows && dirName.endsWith(":")) { - // naively ignore drive letters on Windows, todo @roman.shevchenko - dirName = dirName.substring(0, dirName.length() - 1); - } - root = root.createChildDirectory(this, dirName); - } - catch (IOException e) { - throw new RuntimeException(e); - } + root = root.createChildDirectory(this, dirName); } } + return root; } @@ -164,11 +142,11 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT return result; } - @Override @NotNull + @Override public VirtualFile createFile(@NotNull String targetPath) { - final String path = PathUtil.getParentPath(targetPath); - final String name = PathUtil.getFileName(targetPath); + String path = PathUtil.getParentPath(targetPath); + String name = PathUtil.getFileName(targetPath); try { return WriteAction.computeAndWait(() -> { VirtualFile targetDir = findOrCreateDir(path); @@ -180,41 +158,28 @@ public class LightTempDirTestFixtureImpl extends BaseFixture implements TempDirT } } - @Override @NotNull - public VirtualFile createFile(@NotNull String name, @NotNull final String text) throws IOException { - final VirtualFile file = createFile(name); + @Override + public VirtualFile createFile(@NotNull String name, @NotNull String text) throws IOException { + VirtualFile file = createFile(name); WriteAction.runAndWait(() -> VfsUtil.saveText(file, text)); return file; } public void deleteAll() { - ApplicationManager.getApplication().runWriteAction(new Runnable() { - @Override - public void run() { - final VirtualFile[] toDelete; - if (myUsePlatformSourceRoot) { - toDelete = getSourceRoot().getChildren(); - } - else { - toDelete = new VirtualFile[] {mySourceRoot}; - } - - for (VirtualFile file : toDelete) { - try { - file.delete(this); - } - catch (IOException ignored) { } + WriteAction.runAndWait(() -> { + VirtualFile[] toDelete = mySourceRoot != null ? new VirtualFile[]{mySourceRoot} : getSourceRoot().getChildren(); + for (VirtualFile file : toDelete) { + try { + file.delete(this); } + catch (IOException ignored) { } } }); } @NotNull private VirtualFile getSourceRoot() { - if (myUsePlatformSourceRoot) { - return LightPlatformTestCase.getSourceRoot(); - } - return mySourceRoot; + return mySourceRoot != null ? mySourceRoot : LightPlatformTestCase.getSourceRoot(); } } \ No newline at end of file diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/TempDirTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/TempDirTestFixtureImpl.java index 419fc298cb86..10c9ff68989c 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/TempDirTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/TempDirTestFixtureImpl.java @@ -1,23 +1,7 @@ -/* - * Copyright 2000-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.testFramework.fixtures.impl; -import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.WriteAction; -import com.intellij.openapi.util.Computable; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; @@ -46,31 +30,25 @@ public class TempDirTestFixtureImpl extends BaseFixture implements TempDirTestFi @NotNull @Override - public VirtualFile copyAll(@NotNull final String dataDir, @NotNull final String targetDir, @NotNull final VirtualFileFilter filter) { + public VirtualFile copyAll(@NotNull String dataDir, @NotNull String targetDir, @NotNull VirtualFileFilter filter) { createTempDirectory(); - return ApplicationManager.getApplication().runWriteAction(new Computable() { - @Override - public VirtualFile compute() { - try { - VirtualFile tempDir = - LocalFileSystem.getInstance().refreshAndFindFileByPath(myTempDir.getCanonicalPath().replace(File.separatorChar, '/')); - Assert.assertNotNull(tempDir); - if (targetDir.length() > 0) { - Assert.assertFalse("nested directories not implemented", targetDir.contains("/")); - VirtualFile child = tempDir.findChild(targetDir); - if (child == null) { - child = tempDir.createChildDirectory(this, targetDir); - } - tempDir = child; - } - final VirtualFile from = LocalFileSystem.getInstance().refreshAndFindFileByPath(dataDir); - Assert.assertNotNull(dataDir + " not found", from); - VfsUtil.copyDirectory(null, from, tempDir, filter); - return tempDir; - } - catch (IOException e) { - throw new RuntimeException(e); + return WriteAction.computeAndWait(() -> { + try { + VirtualFile tempDir = LocalFileSystem.getInstance().refreshAndFindFileByPath(myTempDir.getPath()); + Assert.assertNotNull(tempDir); + if (!targetDir.isEmpty()) { + Assert.assertFalse("nested directories not implemented", targetDir.contains("/")); + VirtualFile child = tempDir.findChild(targetDir); + if (child == null) child = tempDir.createChildDirectory(this, targetDir); + tempDir = child; } + VirtualFile from = LocalFileSystem.getInstance().refreshAndFindFileByPath(dataDir); + Assert.assertNotNull(dataDir + " not found", from); + VfsUtil.copyDirectory(null, from, tempDir, filter); + return tempDir; + } + catch (IOException e) { + throw new RuntimeException(e); } }); } @@ -83,9 +61,7 @@ public class TempDirTestFixtureImpl extends BaseFixture implements TempDirTestFi public File createTempFile(String fileName) throws IOException { String prefix = StringUtil.getPackageName(fileName); - if (prefix.length() < 3) { - prefix += "___"; - } + if (prefix.length() < 3) prefix += "___"; String suffix = "." + StringUtil.getShortName(fileName); File file = FileUtil.createTempFile(new File(getTempDirPath()), prefix, suffix, true); VfsRootAccess.allowRootAccess(getTestRootDisposable(), file.getPath()); @@ -93,43 +69,33 @@ public class TempDirTestFixtureImpl extends BaseFixture implements TempDirTestFi } @Override - public VirtualFile getFile(@NotNull final String path) { - try { - return WriteAction.computeAndWait(() -> { - final String fullPath = myTempDir.getCanonicalPath() + '/' + path; - VfsRootAccess.allowRootAccess(getTestRootDisposable(), fullPath); - final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(fullPath); - return file; - }); - } - catch (IOException e) { - throw new RuntimeException(e); - } + public VirtualFile getFile(@NotNull String path) { + String fullPath = myTempDir.getPath() + '/' + path; + VfsRootAccess.allowRootAccess(getTestRootDisposable(), fullPath); + return WriteAction.computeAndWait(() -> LocalFileSystem.getInstance().refreshAndFindFileByPath(fullPath)); } - @Override @NotNull - public VirtualFile createFile(@NotNull final String name) { - final File file = new File(createTempDirectory(), name); - return WriteAction.computeAndWait(() -> { - FileUtil.createIfDoesntExist(file); - VfsRootAccess.allowRootAccess(getTestRootDisposable(), file.getPath()); - return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file); - }); + @Override + public VirtualFile createFile(@NotNull String name) { + File file = new File(createTempDirectory(), name); + FileUtil.createIfDoesntExist(file); + VfsRootAccess.allowRootAccess(getTestRootDisposable(), file.getPath()); + return WriteAction.computeAndWait(() -> LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)); } - @Override @NotNull + @Override public VirtualFile findOrCreateDir(@NotNull String name) throws IOException { File file = new File(createTempDirectory(), name); VfsRootAccess.allowRootAccess(getTestRootDisposable(), file.getPath()); return VfsUtil.createDirectories(file.getPath()); } - @Override @NotNull - public VirtualFile createFile(@NotNull String name, @NotNull final String text) throws IOException { - final VirtualFile file = createFile(name); + @Override + public VirtualFile createFile(@NotNull String name, @NotNull String text) throws IOException { + VirtualFile file = createFile(name); WriteAction.runAndWait(() -> VfsUtil.saveText(file, text)); return file; } @@ -142,16 +108,16 @@ public class TempDirTestFixtureImpl extends BaseFixture implements TempDirTestFi @Override public void tearDown() throws Exception { - try { - if (myTempDir != null) { + if (myTempDir != null) { + try { WriteAction.runAndWait(() -> findOrCreateDir("").delete(this)); } - } - catch (Throwable e) { - addSuppressedException(e); - } - finally { - super.tearDown(); + catch (Throwable e) { + addSuppressedException(e); + } + finally { + super.tearDown(); + } } } @@ -160,17 +126,18 @@ public class TempDirTestFixtureImpl extends BaseFixture implements TempDirTestFi } @NotNull - protected File createTempDirectory() { - try { - if (myTempDir == null) { + private File createTempDirectory() { + if (myTempDir == null) { + try { File tempHome = getTempHome(); - myTempDir = tempHome == null ? FileUtil.createTempDirectory("unitTest", null, false) : - FileUtil.createTempDirectory(tempHome, "unitTest", null, false); + myTempDir = tempHome != null + ? FileUtil.createTempDirectory(tempHome, "unitTest", null, false) + : FileUtil.createTempDirectory("unitTest", null, false); + } + catch (IOException e) { + throw new RuntimeException("Cannot create temp dir", e); } - return myTempDir; - } - catch (IOException e) { - throw new RuntimeException("Cannot create temp dir", e); } + return myTempDir; } } \ No newline at end of file