Fork of the Fernflower decompiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fernflower/test/org/jetbrains/java/decompiler/SingleClassesTest.java

138 lines
6.8 KiB

/*
* Copyright 2000-2017 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.
*/
package org.jetbrains.java.decompiler;
import org.jetbrains.java.decompiler.main.decompiler.ConsoleDecompiler;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.*;
import static org.jetbrains.java.decompiler.DecompilerTestFixture.assertFilesEqual;
import static org.junit.Assert.assertTrue;
public class SingleClassesTest {
private DecompilerTestFixture fixture;
@Before
public void setUp() throws IOException {
fixture = new DecompilerTestFixture();
fixture.setUp(IFernflowerPreferences.BYTECODE_SOURCE_MAPPING, "1",
IFernflowerPreferences.DUMP_ORIGINAL_LINES, "1");
}
@After
public void tearDown() {
fixture.tearDown();
fixture = null;
}
@Test public void testClassFields() { doTest("pkg/TestClassFields"); }
@Test public void testInterfaceFields() { doTest("pkg/TestInterfaceFields"); }
@Test public void testClassLambda() { doTest("pkg/TestClassLambda"); }
@Test public void testClassLoop() { doTest("pkg/TestClassLoop"); }
@Test public void testClassSwitch() { doTest("pkg/TestClassSwitch"); }
@Test public void testClassTypes() { doTest("pkg/TestClassTypes"); }
@Test public void testClassVar() { doTest("pkg/TestClassVar"); }
@Test public void testClassNestedInitializer() { doTest("pkg/TestClassNestedInitializer"); }
@Test public void testClassCast() { doTest("pkg/TestClassCast"); }
@Test public void testDeprecations() { doTest("pkg/TestDeprecations"); }
@Test public void testExtendsList() { doTest("pkg/TestExtendsList"); }
@Test public void testMethodParameters() { doTest("pkg/TestMethodParameters"); }
@Test public void testCodeConstructs() { doTest("pkg/TestCodeConstructs"); }
@Test public void testConstants() { doTest("pkg/TestConstants"); }
@Test public void testEnum() { doTest("pkg/TestEnum"); }
@Test public void testDebugSymbols() { doTest("pkg/TestDebugSymbols"); }
@Test public void testInvalidMethodSignature() { doTest("InvalidMethodSignature"); }
@Test public void testInnerClassConstructor() { doTest("pkg/TestInnerClassConstructor"); }
@Test public void testInnerClassConstructor11() { doTest("v11/TestInnerClassConstructor"); }
@Test public void testTryCatchFinally() { doTest("pkg/TestTryCatchFinally"); }
@Test public void testAmbiguousCall() { doTest("pkg/TestAmbiguousCall"); }
@Test public void testAmbiguousCallWithDebugInfo() { doTest("pkg/TestAmbiguousCallWithDebugInfo"); }
@Test public void testSimpleBytecodeMapping() { doTest("pkg/TestClassSimpleBytecodeMapping"); }
@Test public void testSynchronizedMapping() { doTest("pkg/TestSynchronizedMapping"); }
@Test public void testAbstractMethods() { doTest("pkg/TestAbstractMethods"); }
@Test public void testLocalClass() { doTest("pkg/TestLocalClass"); }
@Test public void testAnonymousClass() { doTest("pkg/TestAnonymousClass"); }
@Test public void testThrowException() { doTest("pkg/TestThrowException"); }
@Test public void testInnerLocal() { doTest("pkg/TestInnerLocal"); }
@Test public void testInnerLocalPkg() { doTest("pkg/TestInnerLocalPkg"); }
@Test public void testInnerSignature() { doTest("pkg/TestInnerSignature"); }
@Test public void testAnonymousSignature() { doTest("pkg/TestAnonymousSignature"); }
@Test public void testLocalsSignature() { doTest("pkg/TestLocalsSignature"); }
@Test public void testParameterizedTypes() { doTest("pkg/TestParameterizedTypes"); }
@Test public void testShadowing() { doTest("pkg/TestShadowing", "pkg/Shadow", "ext/Shadow"); }
@Test public void testStringConcat() { doTest("pkg/TestStringConcat"); }
@Test public void testJava9StringConcat() { doTest("java9/TestJava9StringConcat"); }
@Test public void testMethodReferenceSameName() { doTest("pkg/TestMethodReferenceSameName"); }
@Test public void testMethodReferenceLetterClass() { doTest("pkg/TestMethodReferenceLetterClass"); }
@Test public void testMemberAnnotations() { doTest("pkg/TestMemberAnnotations"); }
@Test public void testMoreAnnotations() { doTest("pkg/MoreAnnotations"); }
@Test public void testTypeAnnotations() { doTest("pkg/TypeAnnotations"); }
@Test public void testStaticNameClash() { doTest("pkg/TestStaticNameClash"); }
@Test public void testExtendingSubclass() { doTest("pkg/TestExtendingSubclass"); }
@Test public void testSyntheticAccess() { doTest("pkg/TestSyntheticAccess"); }
@Test public void testIllegalVarName() { doTest("pkg/TestIllegalVarName"); }
@Test public void testKotlinConstructor() { doTest("pkg/TestKotlinConstructorKt"); }
@Test public void testAsserts() { doTest("pkg/TestAsserts"); }
@Test public void testLocalsNames() { doTest("pkg/TestLocalsNames"); }
private void doTest(String testFile, String... companionFiles) {
ConsoleDecompiler decompiler = fixture.getDecompiler();
File classFile = new File(fixture.getTestDataDir(), "/classes/" + testFile + ".class");
assertTrue(classFile.isFile());
for (File file : collectClasses(classFile)) {
decompiler.addSpace(file, true);
}
for (String companionFile : companionFiles) {
File companionClassFile = new File(fixture.getTestDataDir(), "/classes/" + companionFile + ".class");
assertTrue(companionClassFile.isFile());
for (File file : collectClasses(companionClassFile)) {
decompiler.addSpace(file, true);
}
}
decompiler.decompileContext();
String testName = classFile.getName().substring(0, classFile.getName().length() - 6);
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<File> collectClasses(File classFile) {
List<File> files = new ArrayList<>();
files.add(classFile);
File parent = classFile.getParentFile();
if (parent != null) {
final String pattern = classFile.getName().replace(".class", "") + "\\$.+\\.class";
File[] inner = parent.listFiles((dir, name) -> name.matches(pattern));
if (inner != null) Collections.addAll(files, inner);
}
return files;
}
}