From f15d638fb978a240e69060cf01ef92cb46c08dce Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 6 Nov 2018 17:43:16 +0100 Subject: [PATCH] Cleanup (warnings; formatting) --- .../testFramework/PlatformTestUtil.java | 126 +++++++----------- 1 file changed, 50 insertions(+), 76 deletions(-) diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java index b61dee4ca120..ce61a84494f1 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestUtil.java @@ -68,7 +68,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.concurrency.AsyncPromise; import org.jetbrains.concurrency.Promise; -import org.junit.Assert; import javax.swing.*; import javax.swing.tree.TreeModel; @@ -91,9 +90,11 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Function; import java.util.function.Predicate; import java.util.jar.JarFile; +import java.util.stream.Collectors; +import java.util.stream.Stream; import static com.intellij.openapi.application.ApplicationManager.getApplication; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; /** * @author yole @@ -136,8 +137,11 @@ public class PlatformTestUtil { registerExtension(Extensions.getRootArea(), name, t, parentDisposable); } - public static void registerExtension(@NotNull ExtensionsArea area, @NotNull ExtensionPointName name, @NotNull final T t, @NotNull Disposable parentDisposable) { - final ExtensionPoint extensionPoint = area.getExtensionPoint(name.getName()); + public static void registerExtension(@NotNull ExtensionsArea area, + @NotNull ExtensionPointName name, + @NotNull T t, + @NotNull Disposable parentDisposable) { + ExtensionPoint extensionPoint = area.getExtensionPoint(name.getName()); extensionPoint.registerExtension(t); Disposer.register(parentDisposable, () -> extensionPoint.unregisterExtension(t)); } @@ -373,11 +377,12 @@ public class PlatformTestUtil { AtomicBoolean laterInvoked = new AtomicBoolean(); app.invokeLater(() -> laterInvoked.set(true)); UIUtil.dispatchAllInvocationEvents(); - Assert.assertTrue(laterInvoked.get()); + assertTrue(laterInvoked.get()); TimeoutUtil.sleep(sleptAlready ? 10 : delay); sleptAlready = true; if (getMillisSince(start) > MAX_WAIT_TIME) { + String queue = ((AppScheduledExecutorService)AppExecutorUtil.getAppScheduledExecutorService()).dumpQueue(); throw new AssertionError("Couldn't await alarm" + "; alarm passed=" + alarmInvoked1.get() + "; modality1=" + initialModality + @@ -388,7 +393,7 @@ public class PlatformTestUtil { "; app.disposed=" + app.isDisposed() + "; alarm.disposed=" + alarm.isDisposed() + "; alarm.requests=" + alarm.getActiveRequestCount() + - "\n delayQueue=" + StringUtil.trimLog(((AppScheduledExecutorService)AppExecutorUtil.getAppScheduledExecutorService()).dumpQueue(), 1000) + + "\n delayQueue=" + StringUtil.trimLog(queue, 1000) + "\n invocatorQueue=" + LaterInvocator.getLaterInvocatorQueue() ); } @@ -516,12 +521,12 @@ public class PlatformTestUtil { public static void invokeNamedAction(final String actionId) { final AnAction action = ActionManager.getInstance().getAction(actionId); - Assert.assertNotNull(action); + assertNotNull(action); final Presentation presentation = new Presentation(); @SuppressWarnings("deprecation") final DataContext context = DataManager.getInstance().getDataContext(); final AnActionEvent event = AnActionEvent.createFromAnAction(action, null, "", context); action.beforeActionPerformedUpdate(event); - Assert.assertTrue(presentation.isEnabled()); + assertTrue(presentation.isEnabled()); action.actionPerformed(event); } @@ -620,6 +625,7 @@ public class PlatformTestUtil { return finish - start; } + @SuppressWarnings("CallToSystemGC") public static void assertTiming(String message, long expected, int attempts, @NotNull Runnable actionToMeasure) { while (true) { attempts--; @@ -641,8 +647,8 @@ public class PlatformTestUtil { } } - private static HashMap buildNameToFileMap(VirtualFile[] files, @Nullable VirtualFileFilter filter) { - HashMap map = new HashMap<>(); + private static Map buildNameToFileMap(VirtualFile[] files, @Nullable VirtualFileFilter filter) { + Map map = new HashMap<>(); for (VirtualFile file : files) { if (filter != null && !filter.accept(file)) continue; map.put(file.getName(), file); @@ -659,20 +665,13 @@ public class PlatformTestUtil { FileDocumentManager.getInstance().saveAllDocuments(); VirtualFile[] childrenAfter = dirExpected.getChildren(); - - if (dirExpected.isInLocalFileSystem() && dirExpected.getFileSystem() != TempFileSystem.getInstance()) { - File[] ioAfter = new File(dirExpected.getPath()).listFiles(); - shallowCompare(childrenAfter, ioAfter); - } + shallowCompare(dirExpected, childrenAfter); VirtualFile[] childrenBefore = dirActual.getChildren(); - if (dirActual.isInLocalFileSystem() && dirActual.getFileSystem() != TempFileSystem.getInstance()) { - File[] ioBefore = new File(dirActual.getPath()).listFiles(); - shallowCompare(childrenBefore, ioBefore); - } + shallowCompare(dirActual, childrenBefore); - HashMap mapAfter = buildNameToFileMap(childrenAfter, fileFilter); - HashMap mapBefore = buildNameToFileMap(childrenBefore, fileFilter); + Map mapAfter = buildNameToFileMap(childrenAfter, fileFilter); + Map mapBefore = buildNameToFileMap(childrenBefore, fileFilter); Set keySetAfter = mapAfter.keySet(); Set keySetBefore = mapBefore.keySet(); @@ -690,30 +689,13 @@ public class PlatformTestUtil { } } - private static void shallowCompare(VirtualFile[] vfs, @Nullable File[] io) { - List vfsPaths = new ArrayList<>(); - for (VirtualFile file : vfs) { - vfsPaths.add(file.getPath()); + private static void shallowCompare(VirtualFile dir, VirtualFile[] vfs) { + if (dir.isInLocalFileSystem() && dir.getFileSystem() != TempFileSystem.getInstance()) { + String vfsPaths = Stream.of(vfs).map(VirtualFile::getPath).sorted().collect(Collectors.joining("\n")); + File[] io = notNull(new File(dir.getPath()).listFiles()); + String ioPaths = Stream.of(io).map(f -> FileUtil.toSystemIndependentName(f.getPath())).sorted().collect(Collectors.joining("\n")); + assertEquals(vfsPaths, ioPaths); } - - List ioPaths = new ArrayList<>(); - if (io != null) { - for (File file : io) { - ioPaths.add(file.getPath().replace(File.separatorChar, '/')); - } - } - - assertEquals(sortAndJoin(vfsPaths), sortAndJoin(ioPaths)); - } - - private static String sortAndJoin(List strings) { - Collections.sort(strings); - StringBuilder buf = new StringBuilder(); - for (String string : strings) { - buf.append(string); - buf.append('\n'); - } - return buf.toString(); } public static void assertFilesEqual(VirtualFile fileExpected, VirtualFile fileActual) throws IOException { @@ -721,35 +703,28 @@ public class PlatformTestUtil { assertJarFilesEqual(VfsUtilCore.virtualToIoFile(fileExpected), VfsUtilCore.virtualToIoFile(fileActual)); } catch (IOException e) { - FileDocumentManager manager = FileDocumentManager.getInstance(); - - Document docBefore = manager.getDocument(fileActual); - boolean canLoadBeforeText = !fileActual.getFileType().isBinary() || fileActual.getFileType() == FileTypes.UNKNOWN; - String textB = docBefore != null - ? docBefore.getText() - : !canLoadBeforeText - ? null - : LoadTextUtil.getTextByBinaryPresentation(fileActual.contentsToByteArray(false), fileActual).toString(); - - Document docAfter = manager.getDocument(fileExpected); - boolean canLoadAfterText = !fileActual.getFileType().isBinary() || fileActual.getFileType() == FileTypes.UNKNOWN; - String textA = docAfter != null - ? docAfter.getText() - : !canLoadAfterText - ? null - : LoadTextUtil.getTextByBinaryPresentation(fileExpected.contentsToByteArray(false), fileExpected).toString(); - - if (textA != null && textB != null) { - if (!StringUtil.equals(textA, textB)) { - throw new FileComparisonFailure("Text mismatch in file " + fileExpected.getName(), textA, textB, fileExpected.getPath()); - } + String actual = fileText(fileActual); + String expected = fileText(fileExpected); + if (expected == null || actual == null) { + assertArrayEquals(fileExpected.getPath(), fileExpected.contentsToByteArray(), fileActual.contentsToByteArray()); } - else { - Assert.assertArrayEquals(fileExpected.getPath(), fileExpected.contentsToByteArray(), fileActual.contentsToByteArray()); + else if (!StringUtil.equals(expected, actual)) { + throw new FileComparisonFailure("Text mismatch in file " + fileExpected.getName(), expected, actual, fileExpected.getPath()); } } } + private static String fileText(VirtualFile file) throws IOException { + Document doc = FileDocumentManager.getInstance().getDocument(file); + if (doc != null) { + return doc.getText(); + } + if (!file.getFileType().isBinary() || file.getFileType() == FileTypes.UNKNOWN) { + return LoadTextUtil.getTextByBinaryPresentation(file.contentsToByteArray(false), file).toString(); + } + return null; + } + private static void assertJarFilesEqual(File file1, File file2) throws IOException { final File tempDir = FileUtilRt.createTempDirectory("assert_jar_tmp", null, false); try { @@ -766,9 +741,9 @@ public class PlatformTestUtil { } final VirtualFile dirAfter = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDirectory1); - Assert.assertNotNull(tempDirectory1.toString(), dirAfter); + assertNotNull(tempDirectory1.toString(), dirAfter); final VirtualFile dirBefore = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempDirectory2); - Assert.assertNotNull(tempDirectory2.toString(), dirBefore); + assertNotNull(tempDirectory2.toString(), dirBefore); getApplication().runWriteAction(() -> { dirAfter.refresh(false, true); dirBefore.refresh(false, true); @@ -802,7 +777,7 @@ public class PlatformTestUtil { @NotNull public static T notNull(@Nullable T t) { - Assert.assertNotNull(t); + assertNotNull(t); return t; } @@ -944,7 +919,6 @@ public class PlatformTestUtil { } } - public static void assertComparisonContractNotViolated(@NotNull List values, @NotNull Comparator comparator, @NotNull Equality equality) { @@ -960,10 +934,10 @@ public class PlatformTestUtil { assertEquals(String.format("Equal, but not 0: '%s' - '%s'", value2, value1), 0, result21); } else { - if (result12 == 0) Assert.fail(String.format("Not equal, but 0: '%s' - '%s'", value1, value2)); - if (result21 == 0) Assert.fail(String.format("Not equal, but 0: '%s' - '%s'", value2, value1)); + if (result12 == 0) fail(String.format("Not equal, but 0: '%s' - '%s'", value1, value2)); + if (result21 == 0) fail(String.format("Not equal, but 0: '%s' - '%s'", value2, value1)); if (Integer.signum(result12) == Integer.signum(result21)) { - Assert.fail(String.format("Not symmetrical: '%s' - '%s'", value1, value2)); + fail(String.format("Not symmetrical: '%s' - '%s'", value1, value2)); } } @@ -974,7 +948,7 @@ public class PlatformTestUtil { int result31 = comparator.compare(value3, value1); if (!isTransitive(result12, result23, result31)) { - Assert.fail(String.format("Not transitive: '%s' - '%s' - '%s'", value1, value2, value3)); + fail(String.format("Not transitive: '%s' - '%s' - '%s'", value1, value2, value3)); } } }