From aab87019efe1249352e0c45f58a8d28aa8732164 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Mon, 7 Sep 2015 19:23:12 +0300 Subject: [PATCH] [java-decompiler] common file comparison method extracted; test data updated --- .../decompiler/BulkDecompilationTest.java | 21 ++------- .../decompiler/DecompilerTestFixture.java | 27 +++++++++++- .../decompiler/SingleClassesTestBase.java | 43 ++++++------------- testData/bulk/META-INF/MANIFEST.MF | 3 ++ testData/bulk/pkg/Main.java | 11 +++++ testData/bulk/pkg/res/Loader.java | 25 +++++++++++ 6 files changed, 82 insertions(+), 48 deletions(-) diff --git a/test/org/jetbrains/java/decompiler/BulkDecompilationTest.java b/test/org/jetbrains/java/decompiler/BulkDecompilationTest.java index 1b5353b..0211a57 100644 --- a/test/org/jetbrains/java/decompiler/BulkDecompilationTest.java +++ b/test/org/jetbrains/java/decompiler/BulkDecompilationTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -15,7 +15,6 @@ */ package org.jetbrains.java.decompiler; -import org.hamcrest.Matchers; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.junit.After; @@ -27,7 +26,7 @@ import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import static org.junit.Assert.assertThat; +import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual; import static org.junit.Assert.assertTrue; public class BulkDecompilationTest { @@ -54,7 +53,7 @@ public class BulkDecompilationTest { decompiler.addSpace(classes, true); decompiler.decompileContext(); - compareDirectories(new File(fixture.getTestDataDir(), "bulk"), fixture.getTargetDir()); + assertFilesEqual(new File(fixture.getTestDataDir(), "bulk"), fixture.getTargetDir()); } @Test @@ -66,7 +65,7 @@ public class BulkDecompilationTest { File unpacked = new File(fixture.getTempDir(), "unpacked"); unpack(new File(fixture.getTargetDir(), "bulk.jar"), unpacked); - compareDirectories(new File(fixture.getTestDataDir(), "bulk"), unpacked); + assertFilesEqual(new File(fixture.getTestDataDir(), "bulk"), unpacked); } private static void unpack(File archive, File targetDir) { @@ -95,16 +94,4 @@ public class BulkDecompilationTest { throw new RuntimeException(e); } } - - private static void compareDirectories(File expected, File actual) { - String[] expectedList = expected.list(); - String[] actualList = actual.list(); - assertThat(actualList, Matchers.arrayContainingInAnyOrder(expectedList)); - for (String name : expectedList) { - File child = new File(expected, name); - if (child.isDirectory()) { - compareDirectories(child, new File(actual, name)); - } - } - } } diff --git a/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java b/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java index d0d869c..ad36548 100644 --- a/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java +++ b/test/org/jetbrains/java/decompiler/DecompilerTestFixture.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -15,8 +15,10 @@ */ package org.jetbrains.java.decompiler; +import org.hamcrest.Matchers; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; +import org.jetbrains.java.decompiler.util.InterpreterUtil; import java.io.File; import java.io.IOException; @@ -24,6 +26,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; public class DecompilerTestFixture { @@ -43,6 +46,7 @@ public class DecompilerTestFixture { if (!isTestDataDir(testDataDir)) testDataDir = new File("../community/plugins/java-decompiler/engine/testData"); if (!isTestDataDir(testDataDir)) testDataDir = new File("../plugins/java-decompiler/engine/testData"); assertTrue("current dir: " + new File("").getAbsolutePath(), isTestDataDir(testDataDir)); + testDataDir = testDataDir.getAbsoluteFile(); //noinspection SSBasedInspection tempDir = File.createTempFile("decompiler_test_", "_dir"); @@ -96,4 +100,25 @@ public class DecompilerTestFixture { } assertTrue(file.delete()); } + + public static void assertFilesEqual(File expected, File actual) { + if (expected.isDirectory()) { + assertThat(actual.list(), Matchers.arrayContainingInAnyOrder(expected.list())); + for (String name : expected.list()) { + assertFilesEqual(new File(expected, name), new File(actual, name)); + } + } + else { + assertThat(getContent(actual), Matchers.equalTo(getContent(expected))); + } + } + + private static String getContent(File expected) { + try { + return new String(InterpreterUtil.getBytes(expected), "UTF-8").replace("\r\n", "\n"); + } + catch (IOException e) { + throw new RuntimeException(e); + } + } } diff --git a/test/org/jetbrains/java/decompiler/SingleClassesTestBase.java b/test/org/jetbrains/java/decompiler/SingleClassesTestBase.java index 165f695..97e98b2 100644 --- a/test/org/jetbrains/java/decompiler/SingleClassesTestBase.java +++ b/test/org/jetbrains/java/decompiler/SingleClassesTestBase.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * 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. @@ -16,7 +16,6 @@ package org.jetbrains.java.decompiler; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; -import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.junit.After; import org.junit.Before; @@ -28,7 +27,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; -import static org.junit.Assert.assertEquals; +import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual; import static org.junit.Assert.assertTrue; public abstract class SingleClassesTestBase { @@ -51,25 +50,20 @@ public abstract class SingleClassesTestBase { } protected void doTest(String testFile) { - try { - File classFile = new File(fixture.getTestDataDir(), "/classes/" + testFile + ".class"); - assertTrue(classFile.isFile()); - String testName = classFile.getName().substring(0, classFile.getName().length() - 6); + File classFile = new File(fixture.getTestDataDir(), "/classes/" + testFile + ".class"); + assertTrue(classFile.isFile()); + String testName = classFile.getName().substring(0, classFile.getName().length() - 6); - ConsoleDecompiler decompiler = fixture.getDecompiler(); + ConsoleDecompiler decompiler = fixture.getDecompiler(); - for (File file : collectClasses(classFile)) decompiler.addSpace(file, true); - decompiler.decompileContext(); + for (File file : collectClasses(classFile)) decompiler.addSpace(file, true); + decompiler.decompileContext(); - File decompiledFile = new File(fixture.getTargetDir(), testName + ".java"); - assertTrue(decompiledFile.isFile()); - File referenceFile = new File(fixture.getTestDataDir(), "results/" + testName + ".dec"); - assertTrue(referenceFile.isFile()); - compareContent(decompiledFile, referenceFile); - } - catch (Exception e) { - throw new RuntimeException(e); - } + File decompiledFile = new File(fixture.getTargetDir(), testName + ".java"); + assertTrue(decompiledFile.isFile()); + File referenceFile = new File(fixture.getTestDataDir(), "results/" + testName + ".dec"); + assertTrue(referenceFile.isFile()); + assertFilesEqual(referenceFile, decompiledFile); } private static List collectClasses(File classFile) { @@ -90,15 +84,4 @@ public abstract class SingleClassesTestBase { return files; } - - private static void compareContent(File decompiledFile, File referenceFile) throws IOException { - String decompiledContent = new String(InterpreterUtil.getBytes(decompiledFile), "UTF-8"); - - String referenceContent = new String(InterpreterUtil.getBytes(referenceFile), "UTF-8"); - if (InterpreterUtil.IS_WINDOWS && !referenceContent.contains("\r\n")) { - referenceContent = referenceContent.replace("\n", "\r\n"); // fix for broken Git checkout on Windows - } - - assertEquals(referenceContent, decompiledContent); - } } diff --git a/testData/bulk/META-INF/MANIFEST.MF b/testData/bulk/META-INF/MANIFEST.MF index e69de29..22f53c8 100644 --- a/testData/bulk/META-INF/MANIFEST.MF +++ b/testData/bulk/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Created-By: 1.8.0_20 (Oracle Corporation) + diff --git a/testData/bulk/pkg/Main.java b/testData/bulk/pkg/Main.java index e69de29..f22c5d2 100644 --- a/testData/bulk/pkg/Main.java +++ b/testData/bulk/pkg/Main.java @@ -0,0 +1,11 @@ +package pkg; + +import pkg.res.Loader; + +public class Main { + public static void main(String[] args) { + Loader loader = new Loader(); + String resource = loader.getResource(); + System.out.println(resource); + } +} diff --git a/testData/bulk/pkg/res/Loader.java b/testData/bulk/pkg/res/Loader.java index e69de29..b0326dd 100644 --- a/testData/bulk/pkg/res/Loader.java +++ b/testData/bulk/pkg/res/Loader.java @@ -0,0 +1,25 @@ +package pkg.res; + +import java.io.File; +import java.io.FileInputStream; +import java.net.URL; + +public class Loader { + public String getResource() { + URL resource = this.getClass().getClassLoader().getResource("pkg/res/resource.txt"); + if(resource == null) { + throw new RuntimeException("Resource missing"); + } else { + try { + File e = new File(resource.toURI()); + byte[] bytes = new byte[(int)e.length()]; + FileInputStream stream = new FileInputStream(e); + stream.read(bytes); + stream.close(); + return new String(bytes, "UTF-8"); + } catch (Exception var5) { + throw new RuntimeException("Resource load failed", var5); + } + } + } +}