From 2da829c3d1266112326139a7b94a362bfb27f132 Mon Sep 17 00:00:00 2001 From: Col-E Date: Sun, 20 Aug 2023 03:32:39 -0400 Subject: [PATCH] Add additional try-catch test cases Most are working, still a few odd failures, but much better off than before. --- .../accuracy/AccuracyTests.java | 41 ++++++++++++++++--- .../java/sample/EmptyCatchBlock.java | 12 ++++++ .../java/sample/MultiCatchInCatchInCatch.java | 22 ++++++++++ .../java/sample/MultiCatchInUnusedCatch.java | 17 ++++++++ .../java/sample/MultiCatchInUsedCatch.java | 17 ++++++++ .../java/sample/RethrownCatchEx1Block.java | 14 +++++++ .../java/sample/RethrownCatchEx2Block.java | 18 ++++++++ .../java/sample/RethrownTryInUnusedTry.java | 19 +++++++++ .../RethrownTryInUnusedTryOfSameType.java | 17 ++++++++ .../RethrownTryInUsedTryOfSameType.java | 17 ++++++++ .../java/sample/UnusedCatchEx.java | 13 ++++++ .../java/sample/UnusedCatchEx2Block.java | 2 +- .../java/sample/UnusedMultiCatch.java | 14 +++++++ .../java/sample/UnusedTryInUnusedTry.java | 19 +++++++++ .../java/sample/UnusedTryInUsedTry.java | 19 +++++++++ .../java/sample/UsedCatchEx2Block.java | 18 ++++++++ .../java/sample/UsedCatchEx3Block.java | 22 ++++++++++ .../java/sample/UsedMultiCatchBoth.java | 14 +++++++ .../java/sample/UsedMultiCatchFirst.java | 14 +++++++ .../java/sample/UsedMultiCatchSecond.java | 14 +++++++ .../java/sample/UsedTryInUnusedTry.java | 19 +++++++++ .../java/sample/UsedTryInUsedTry.java | 19 +++++++++ 22 files changed, 374 insertions(+), 7 deletions(-) create mode 100644 translator-core/src/testFixtures/java/sample/EmptyCatchBlock.java create mode 100644 translator-core/src/testFixtures/java/sample/MultiCatchInCatchInCatch.java create mode 100644 translator-core/src/testFixtures/java/sample/MultiCatchInUnusedCatch.java create mode 100644 translator-core/src/testFixtures/java/sample/MultiCatchInUsedCatch.java create mode 100644 translator-core/src/testFixtures/java/sample/RethrownCatchEx1Block.java create mode 100644 translator-core/src/testFixtures/java/sample/RethrownCatchEx2Block.java create mode 100644 translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTry.java create mode 100644 translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTryOfSameType.java create mode 100644 translator-core/src/testFixtures/java/sample/RethrownTryInUsedTryOfSameType.java create mode 100644 translator-core/src/testFixtures/java/sample/UnusedCatchEx.java create mode 100644 translator-core/src/testFixtures/java/sample/UnusedMultiCatch.java create mode 100644 translator-core/src/testFixtures/java/sample/UnusedTryInUnusedTry.java create mode 100644 translator-core/src/testFixtures/java/sample/UnusedTryInUsedTry.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedCatchEx2Block.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedCatchEx3Block.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedMultiCatchBoth.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedMultiCatchFirst.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedMultiCatchSecond.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedTryInUnusedTry.java create mode 100644 translator-core/src/testFixtures/java/sample/UsedTryInUsedTry.java diff --git a/translator-core/src/test/java/software/coley/dextransformer/accuracy/AccuracyTests.java b/translator-core/src/test/java/software/coley/dextransformer/accuracy/AccuracyTests.java index aebbac6..e66a6dd 100644 --- a/translator-core/src/test/java/software/coley/dextransformer/accuracy/AccuracyTests.java +++ b/translator-core/src/test/java/software/coley/dextransformer/accuracy/AccuracyTests.java @@ -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; @@ -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 classMap = Map.of(c.getName().replace('.', '/'), filterDebug(getRuntimeClassBytes(c))); AbstractConversionStep step = AbstractConversionStep.initJvmStep(ClassFilter.PASS_ALL, classMap); iterate(step); @@ -112,6 +136,11 @@ private static void dumpDiffFromPrior(@Nonnull AbstractConversionStep step) { } } } + } else { + SortedMap disassembledCurrent = step.disassemble(); + for (String disassemble : disassembledCurrent.values()) { + System.out.println("\n\n\n" + disassemble); + } } } catch (IOException ex) { fail("Failed to disassemble", ex); diff --git a/translator-core/src/testFixtures/java/sample/EmptyCatchBlock.java b/translator-core/src/testFixtures/java/sample/EmptyCatchBlock.java new file mode 100644 index 0000000..ab52b77 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/EmptyCatchBlock.java @@ -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 + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/MultiCatchInCatchInCatch.java b/translator-core/src/testFixtures/java/sample/MultiCatchInCatchInCatch.java new file mode 100644 index 0000000..b671c34 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/MultiCatchInCatchInCatch.java @@ -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"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/MultiCatchInUnusedCatch.java b/translator-core/src/testFixtures/java/sample/MultiCatchInUnusedCatch.java new file mode 100644 index 0000000..907b927 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/MultiCatchInUnusedCatch.java @@ -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 + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/MultiCatchInUsedCatch.java b/translator-core/src/testFixtures/java/sample/MultiCatchInUsedCatch.java new file mode 100644 index 0000000..ecc644c --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/MultiCatchInUsedCatch.java @@ -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(); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/RethrownCatchEx1Block.java b/translator-core/src/testFixtures/java/sample/RethrownCatchEx1Block.java new file mode 100644 index 0000000..d65dbf6 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/RethrownCatchEx1Block.java @@ -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; + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/RethrownCatchEx2Block.java b/translator-core/src/testFixtures/java/sample/RethrownCatchEx2Block.java new file mode 100644 index 0000000..e079687 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/RethrownCatchEx2Block.java @@ -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; + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTry.java b/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTry.java new file mode 100644 index 0000000..3f2411d --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTry.java @@ -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"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTryOfSameType.java b/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTryOfSameType.java new file mode 100644 index 0000000..b320479 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/RethrownTryInUnusedTryOfSameType.java @@ -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(); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/RethrownTryInUsedTryOfSameType.java b/translator-core/src/testFixtures/java/sample/RethrownTryInUsedTryOfSameType.java new file mode 100644 index 0000000..02bf3fa --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/RethrownTryInUsedTryOfSameType.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UnusedCatchEx.java b/translator-core/src/testFixtures/java/sample/UnusedCatchEx.java new file mode 100644 index 0000000..3526e13 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UnusedCatchEx.java @@ -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(); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UnusedCatchEx2Block.java b/translator-core/src/testFixtures/java/sample/UnusedCatchEx2Block.java index ee9ad6b..aa58ec2 100644 --- a/translator-core/src/testFixtures/java/sample/UnusedCatchEx2Block.java +++ b/translator-core/src/testFixtures/java/sample/UnusedCatchEx2Block.java @@ -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"); diff --git a/translator-core/src/testFixtures/java/sample/UnusedMultiCatch.java b/translator-core/src/testFixtures/java/sample/UnusedMultiCatch.java new file mode 100644 index 0000000..30a38f9 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UnusedMultiCatch.java @@ -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"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UnusedTryInUnusedTry.java b/translator-core/src/testFixtures/java/sample/UnusedTryInUnusedTry.java new file mode 100644 index 0000000..5f352bd --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UnusedTryInUnusedTry.java @@ -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"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UnusedTryInUsedTry.java b/translator-core/src/testFixtures/java/sample/UnusedTryInUsedTry.java new file mode 100644 index 0000000..4561086 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UnusedTryInUsedTry.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedCatchEx2Block.java b/translator-core/src/testFixtures/java/sample/UsedCatchEx2Block.java new file mode 100644 index 0000000..b7ce7c6 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedCatchEx2Block.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedCatchEx3Block.java b/translator-core/src/testFixtures/java/sample/UsedCatchEx3Block.java new file mode 100644 index 0000000..a559ab3 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedCatchEx3Block.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedMultiCatchBoth.java b/translator-core/src/testFixtures/java/sample/UsedMultiCatchBoth.java new file mode 100644 index 0000000..4f45ae1 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedMultiCatchBoth.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedMultiCatchFirst.java b/translator-core/src/testFixtures/java/sample/UsedMultiCatchFirst.java new file mode 100644 index 0000000..3daed60 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedMultiCatchFirst.java @@ -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"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedMultiCatchSecond.java b/translator-core/src/testFixtures/java/sample/UsedMultiCatchSecond.java new file mode 100644 index 0000000..cd76c6c --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedMultiCatchSecond.java @@ -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()); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedTryInUnusedTry.java b/translator-core/src/testFixtures/java/sample/UsedTryInUnusedTry.java new file mode 100644 index 0000000..6bb5fcc --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedTryInUnusedTry.java @@ -0,0 +1,19 @@ +package sample; + +import java.io.IOException; + +public class UsedTryInUnusedTry { + public static void foo(int i) { + try { + try { + Class.forName(""); + if (i > 0) + throw new IOException(); + } catch (ClassNotFoundException e) { + System.out.println("fail class " + e.getMessage()); + } + } catch (IOException e) { + System.out.println("fail io"); + } + } +} diff --git a/translator-core/src/testFixtures/java/sample/UsedTryInUsedTry.java b/translator-core/src/testFixtures/java/sample/UsedTryInUsedTry.java new file mode 100644 index 0000000..2fc2ac8 --- /dev/null +++ b/translator-core/src/testFixtures/java/sample/UsedTryInUsedTry.java @@ -0,0 +1,19 @@ +package sample; + +import java.io.IOException; + +public class UsedTryInUsedTry { + public static void foo(int i) { + try { + try { + Class.forName(""); + if (i > 0) + throw new IOException(); + } catch (ClassNotFoundException e) { + System.out.println("fail class " + e.getMessage()); + } + } catch (IOException e) { + System.out.println("fail io " + e.getMessage()); + } + } +}