Skip to content

Commit

Permalink
Add additional try-catch test cases
Browse files Browse the repository at this point in the history
Most are working, still a few odd failures, but much better off than before.
  • Loading branch information
Col-E committed Aug 20, 2023
1 parent ff00b90 commit 2da829c
Show file tree
Hide file tree
Showing 22 changed files with 374 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.objectweb.asm.Opcodes;
import sample.UnusedCatchEx1Block;
import sample.*;
import software.coley.dextransformer.TestBase;
import software.coley.dextranslator.Inputs;
import software.coley.dextranslator.Options;
Expand All @@ -34,10 +34,34 @@ public class AccuracyTests extends TestBase implements Opcodes {
private static final int DIFF_COLUMN_WIDTH = 80;
private static final boolean ALWAYS_LOG = false;

@Test
void testSingle() {
Class<?> c = UnusedCatchEx1Block.class;

@ParameterizedTest
@ValueSource(classes = {
EmptyCatchBlock.class,
MultiCatchInUnusedCatch.class,
MultiCatchInUsedCatch.class,
MultiCatchInCatchInCatch.class,
RethrownCatchEx1Block.class,
RethrownCatchEx2Block.class,
RethrownTryInUnusedTry.class,
RethrownTryInUnusedTryOfSameType.class,
RethrownTryInUsedTryOfSameType.class,
UnusedCatchEx.class,
UnusedCatchEx1Block.class,
UnusedCatchEx2Block.class,
UnusedCatchEx3Block.class,
UnusedMultiCatch.class,
UnusedTryInUnusedTry.class,
UnusedTryInUsedTry.class,
UsedCatchEx1Block.class,
UsedCatchEx2Block.class,
UsedCatchEx3Block.class,
UsedMultiCatchBoth.class,
UsedMultiCatchFirst.class,
UsedMultiCatchSecond.class,
UsedTryInUnusedTry.class,
UsedTryInUsedTry.class,
})
void testSingle(Class<?> c) {
Map<String, byte[]> classMap = Map.of(c.getName().replace('.', '/'), filterDebug(getRuntimeClassBytes(c)));
AbstractConversionStep step = AbstractConversionStep.initJvmStep(ClassFilter.PASS_ALL, classMap);
iterate(step);
Expand Down Expand Up @@ -112,6 +136,11 @@ private static void dumpDiffFromPrior(@Nonnull AbstractConversionStep step) {
}
}
}
} else {
SortedMap<String, String> disassembledCurrent = step.disassemble();
for (String disassemble : disassembledCurrent.values()) {
System.out.println("\n\n\n" + disassemble);
}
}
} catch (IOException ex) {
fail("Failed to disassemble", ex);
Expand Down
12 changes: 12 additions & 0 deletions translator-core/src/testFixtures/java/sample/EmptyCatchBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package sample;

public class EmptyCatchBlock {
public static void foo(int i) {
try {
if (i > 0)
throw new Exception();
} catch (Exception ignored) {
// empty, no code at all
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample;

public class MultiCatchInCatchInCatch {
public static void foo(int i) {
try {
try {
try {
if (i > 0)
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex");
} catch (Throwable t) {
System.out.println("fail t");
}
} catch (Exception ignored) {
// empty
}
} catch (Throwable t) {
System.out.println("fail outer");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample;

public class MultiCatchInUnusedCatch {
public static void foo() {
try {
try {
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex");
} catch (Throwable t) {
System.out.println("fail t");
}
} catch (Exception ignored) {
// empty
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample;

public class MultiCatchInUsedCatch {
public static void foo() {
try {
try {
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex");
} catch (Throwable t) {
System.out.println("fail t");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

import java.io.IOException;

public class RethrownCatchEx1Block {
public static void foo(int i) throws IOException {
try {
if (i > 0)
throw new IOException();
} catch (IOException e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sample;

import java.io.IOException;

public class RethrownCatchEx2Block {
public static void foo(int i) throws IOException {
try {
if (i > 1)
throw new UnsupportedOperationException();
if (i > 0)
throw new IOException();
} catch (UnsupportedOperationException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sample;

import java.io.IOException;

public class RethrownTryInUnusedTry {
public static void foo(int i) {
try {
try {
Class.forName("");
if (i > 0)
throw new IOException();
} catch (ClassNotFoundException e) {
throw new IOException(e);
}
} catch (IOException e) {
System.out.println("fail io");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample;

import java.io.IOException;

public class RethrownTryInUnusedTryOfSameType {
public static void foo() {
try {
try {
throw new IOException();
} catch (IOException e) {
throw new IOException(e);
}
} catch (IOException e) {
foo();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sample;

import java.io.IOException;

public class RethrownTryInUsedTryOfSameType {
public static void foo() {
try {
try {
throw new IOException();
} catch (IOException e) {
throw new IOException(e);
}
} catch (IOException e) {
System.out.println("fail " + e.getMessage());
}
}
}
13 changes: 13 additions & 0 deletions translator-core/src/testFixtures/java/sample/UnusedCatchEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample;

import java.io.IOException;

public class UnusedCatchEx {
public static void foo() {
try {
throw new IOException();
} catch (IOException e) {
foo();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static void foo(int i) {
throw new UnsupportedOperationException();
if (i > 0)
throw new IOException();
}catch (UnsupportedOperationException e) {
} catch (UnsupportedOperationException e) {
System.out.println("fail 1");
} catch (IOException e) {
System.out.println("fail 2");
Expand Down
14 changes: 14 additions & 0 deletions translator-core/src/testFixtures/java/sample/UnusedMultiCatch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

public class UnusedMultiCatch {
public static void foo(int i) {
try {
if (i > 0)
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex");
} catch (Throwable t) {
System.out.println("fail t");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sample;

import java.io.IOException;

public class UnusedTryInUnusedTry {
public static void foo(int i) {
try {
try {
Class.forName("");
if (i > 0)
throw new IOException();
} catch (ClassNotFoundException e) {
System.out.println("fail class");
}
} catch (IOException e) {
System.out.println("fail io");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sample;

import java.io.IOException;

public class UnusedTryInUsedTry {
public static void foo(int i) {
try {
try {
Class.forName("");
if (i > 0)
throw new IOException();
} catch (ClassNotFoundException e) {
System.out.println("fail class");
}
} catch (IOException e) {
System.out.println("fail io " + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package sample;

import java.io.IOException;

public class UsedCatchEx2Block {
public static void foo(int i) {
try {
if (i > 1)
throw new UnsupportedOperationException();
if (i > 0)
throw new IOException();
} catch (UnsupportedOperationException e) {
System.out.println("fail 1" + e.getMessage());
} catch (IOException e) {
System.out.println("fail 2" + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample;

import java.io.IOException;

public class UsedCatchEx3Block {
public static void foo(int i) {
try {
if (i > 2)
throw new IllegalArgumentException();
if (i > 1)
throw new UnsupportedOperationException();
if (i > 0)
throw new IOException();
} catch (IllegalArgumentException e) {
System.out.println("fail 1" + e.getMessage());
} catch (UnsupportedOperationException e) {
System.out.println("fail 2" + e.getMessage());
} catch (IOException e) {
System.out.println("fail 3" + e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

public class UsedMultiCatchBoth {
public static void foo(int i) {
try {
if (i > 0)
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex " + ex.getMessage());
} catch (Throwable t) {
System.out.println("fail t" + t.getMessage());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

public class UsedMultiCatchFirst {
public static void foo(int i) {
try {
if (i > 0)
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex " + ex.getMessage());
} catch (Throwable t) {
System.out.println("fail t");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sample;

public class UsedMultiCatchSecond {
public static void foo(int i) {
try {
if (i > 0)
throw new Exception();
} catch (Exception ex) {
System.out.println("fail ex ");
} catch (Throwable t) {
System.out.println("fail t" + t.getMessage());
}
}
}
Loading

0 comments on commit 2da829c

Please sign in to comment.