[java-decompiler] common file comparison method extracted; test data updated

master
Roman Shevchenko 9 years ago
parent 9a06c52a8f
commit aab87019ef
  1. 21
      test/org/jetbrains/java/decompiler/BulkDecompilationTest.java
  2. 27
      test/org/jetbrains/java/decompiler/DecompilerTestFixture.java
  3. 43
      test/org/jetbrains/java/decompiler/SingleClassesTestBase.java
  4. 3
      testData/bulk/META-INF/MANIFEST.MF
  5. 11
      testData/bulk/pkg/Main.java
  6. 25
      testData/bulk/pkg/res/Loader.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,7 +15,6 @@
*/ */
package org.jetbrains.java.decompiler; package org.jetbrains.java.decompiler;
import org.hamcrest.Matchers;
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
import org.jetbrains.java.decompiler.util.InterpreterUtil; import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.junit.After; import org.junit.After;
@ -27,7 +26,7 @@ import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; 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; import static org.junit.Assert.assertTrue;
public class BulkDecompilationTest { public class BulkDecompilationTest {
@ -54,7 +53,7 @@ public class BulkDecompilationTest {
decompiler.addSpace(classes, true); decompiler.addSpace(classes, true);
decompiler.decompileContext(); decompiler.decompileContext();
compareDirectories(new File(fixture.getTestDataDir(), "bulk"), fixture.getTargetDir()); assertFilesEqual(new File(fixture.getTestDataDir(), "bulk"), fixture.getTargetDir());
} }
@Test @Test
@ -66,7 +65,7 @@ public class BulkDecompilationTest {
File unpacked = new File(fixture.getTempDir(), "unpacked"); File unpacked = new File(fixture.getTempDir(), "unpacked");
unpack(new File(fixture.getTargetDir(), "bulk.jar"), 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) { private static void unpack(File archive, File targetDir) {
@ -95,16 +94,4 @@ public class BulkDecompilationTest {
throw new RuntimeException(e); 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));
}
}
}
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,8 +15,10 @@
*/ */
package org.jetbrains.java.decompiler; package org.jetbrains.java.decompiler;
import org.hamcrest.Matchers;
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences; import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -24,6 +26,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public class DecompilerTestFixture { 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("../community/plugins/java-decompiler/engine/testData");
if (!isTestDataDir(testDataDir)) testDataDir = new File("../plugins/java-decompiler/engine/testData"); if (!isTestDataDir(testDataDir)) testDataDir = new File("../plugins/java-decompiler/engine/testData");
assertTrue("current dir: " + new File("").getAbsolutePath(), isTestDataDir(testDataDir)); assertTrue("current dir: " + new File("").getAbsolutePath(), isTestDataDir(testDataDir));
testDataDir = testDataDir.getAbsoluteFile();
//noinspection SSBasedInspection //noinspection SSBasedInspection
tempDir = File.createTempFile("decompiler_test_", "_dir"); tempDir = File.createTempFile("decompiler_test_", "_dir");
@ -96,4 +100,25 @@ public class DecompilerTestFixture {
} }
assertTrue(file.delete()); 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);
}
}
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.jetbrains.java.decompiler; package org.jetbrains.java.decompiler;
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler; import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
import org.jetbrains.java.decompiler.util.InterpreterUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -28,7 +27,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
public abstract class SingleClassesTestBase { public abstract class SingleClassesTestBase {
@ -51,25 +50,20 @@ public abstract class SingleClassesTestBase {
} }
protected void doTest(String testFile) { protected void doTest(String testFile) {
try { File classFile = new File(fixture.getTestDataDir(), "/classes/" + testFile + ".class");
File classFile = new File(fixture.getTestDataDir(), "/classes/" + testFile + ".class"); assertTrue(classFile.isFile());
assertTrue(classFile.isFile()); String testName = classFile.getName().substring(0, classFile.getName().length() - 6);
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); for (File file : collectClasses(classFile)) decompiler.addSpace(file, true);
decompiler.decompileContext(); decompiler.decompileContext();
File decompiledFile = new File(fixture.getTargetDir(), testName + ".java"); File decompiledFile = new File(fixture.getTargetDir(), testName + ".java");
assertTrue(decompiledFile.isFile()); assertTrue(decompiledFile.isFile());
File referenceFile = new File(fixture.getTestDataDir(), "results/" + testName + ".dec"); File referenceFile = new File(fixture.getTestDataDir(), "results/" + testName + ".dec");
assertTrue(referenceFile.isFile()); assertTrue(referenceFile.isFile());
compareContent(decompiledFile, referenceFile); assertFilesEqual(referenceFile, decompiledFile);
}
catch (Exception e) {
throw new RuntimeException(e);
}
} }
private static List<File> collectClasses(File classFile) { private static List<File> collectClasses(File classFile) {
@ -90,15 +84,4 @@ public abstract class SingleClassesTestBase {
return files; 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);
}
} }

@ -0,0 +1,3 @@
Manifest-Version: 1.0
Created-By: 1.8.0_20 (Oracle Corporation)

@ -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);
}
}

@ -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);
}
}
}
}
Loading…
Cancel
Save