From bba00f1cbd531058d01a67ee4a533d6e78651675 Mon Sep 17 00:00:00 2001 From: Shautvast Date: Mon, 31 Jul 2023 15:39:46 +0200 Subject: [PATCH] test for exceptions --- .../shautvast/reflective/InvokerFactory.java | 6 ++++++ .../java/com/github/shautvast/rusty/Result.java | 4 ++++ .../shautvast/reflective/ReflectiveTest.java | 17 ++++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/shautvast/reflective/InvokerFactory.java b/src/main/java/com/github/shautvast/reflective/InvokerFactory.java index e37e2fe..bb08a32 100644 --- a/src/main/java/com/github/shautvast/reflective/InvokerFactory.java +++ b/src/main/java/com/github/shautvast/reflective/InvokerFactory.java @@ -75,6 +75,12 @@ public class InvokerFactory { classNode.accept(classWriter); byte[] byteArray = classWriter.toByteArray(); +// try (FileOutputStream out = new FileOutputStream("C.class")) { +// out.write(byteArray); +// } catch (IOException e) { +// e.printStackTrace(); +// } + // load it into the JVM ByteClassLoader.INSTANCE.addClass(className, byteArray); return getInvoker(classNode.name); diff --git a/src/main/java/com/github/shautvast/rusty/Result.java b/src/main/java/com/github/shautvast/rusty/Result.java index 35a6569..6f0219d 100644 --- a/src/main/java/com/github/shautvast/rusty/Result.java +++ b/src/main/java/com/github/shautvast/rusty/Result.java @@ -65,4 +65,8 @@ public class Result { return new Result<>(null, error); } } + + public boolean isOk() { + return error == null; + } } diff --git a/src/test/java/com/github/shautvast/reflective/ReflectiveTest.java b/src/test/java/com/github/shautvast/reflective/ReflectiveTest.java index 429c4b7..534de92 100644 --- a/src/test/java/com/github/shautvast/reflective/ReflectiveTest.java +++ b/src/test/java/com/github/shautvast/reflective/ReflectiveTest.java @@ -1,6 +1,7 @@ package com.github.shautvast.reflective; import com.github.shautvast.rusty.Panic; +import com.github.shautvast.rusty.Result; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,7 +26,7 @@ public class ReflectiveTest { Set methods = metaDummy.getMethods(); assertFalse(methods.isEmpty()); - assertEquals(5, methods.size()); + assertEquals(6, methods.size()); MetaMethod equals = metaDummy.getMethod("equals").orElseGet(Assertions::fail); assertEquals(boolean.class, equals.getReturnParameter().getType()); @@ -68,6 +69,16 @@ public class ReflectiveTest { assertEquals("foo", dummy.getName()); // after invoke } + @Test + void testInvocationExceptionHappened() { + Dummy dummy = new Dummy("bar"); + MetaClass metaForClass = Reflective.getMetaClass(dummy.getClass()); + MetaMethod throwEx = metaForClass.getMethod("throwEx").orElseGet(Assertions::fail); + + Result result = throwEx.invoke(dummy, "foo"); + assertFalse(result.isOk()); + } + public static class Dummy { private String name; @@ -84,6 +95,10 @@ public class ReflectiveTest { this.name = name; } + public void throwEx() throws Exception { + throw new Exception("something must have gone wrong"); + } + @Override public boolean equals(Object obj) { return obj instanceof Dummy;