LuciferYang commented on code in PR #7641:
URL: https://github.com/apache/kyuubi/pull/7641#discussion_r3832164705


##########
kyuubi-util/src/test/java/org/apache/kyuubi/util/reflect/DynMethodsTest.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.kyuubi.util.reflect;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.IOException;
+import org.junit.jupiter.api.Test;
+
+public class DynMethodsTest {
+
+  public static class ReflectionTarget {
+
+    /** Kept as a field so a test can assert the very same instance came back 
out of invoke. */
+    final IllegalStateException causelessFailure = new IllegalStateException();
+
+    public Object throwCheckedWithoutCause() throws IOException {
+      throw new IOException("checked failure");
+    }
+
+    public Object throwCheckedWithRuntimeCause() throws IOException {
+      throw new IOException("checked failure", new 
IllegalArgumentException("root"));
+    }
+
+    public Object throwUncheckedWithCause() {
+      throw new IllegalStateException("unchecked failure", new 
IllegalArgumentException("cause"));
+    }
+
+    public Object throwCauselessUnchecked() {
+      throw causelessFailure;
+    }
+
+    public static Object throwStaticUnchecked() {
+      throw new IllegalStateException(
+          "static failure", new IllegalArgumentException("static cause"));
+    }
+
+    public String echo(String input) {
+      return input;
+    }
+
+    public static String staticEcho(String input) {
+      return input;
+    }
+
+    public String concat(String... parts) {
+      return String.join("", parts);
+    }
+  }
+
+  @Test
+  public void testInvokePreservesCheckedExceptionThrownByTarget() {
+    DynMethods.UnboundMethod method =
+        
DynMethods.builder("throwCheckedWithoutCause").impl(ReflectionTarget.class).build();
+
+    RuntimeException thrown =
+        assertThrows(RuntimeException.class, () -> method.invoke(new 
ReflectionTarget()));
+
+    assertTrue(thrown.getCause() instanceof IOException);
+    assertEquals("checked failure", thrown.getCause().getMessage());
+  }
+
+  @Test
+  public void testInvokeKeepsCheckedExceptionCarryingRuntimeCause() {
+    DynMethods.UnboundMethod method =
+        
DynMethods.builder("throwCheckedWithRuntimeCause").impl(ReflectionTarget.class).build();
+
+    RuntimeException thrown =
+        assertThrows(RuntimeException.class, () -> method.invoke(new 
ReflectionTarget()));
+
+    // Before the fix, invoke unwrapped a second time and threw the 
IllegalArgumentException
+    // bare, dropping the target's IOException: a plausible-looking wrong type 
in place of the
+    // real failure. The IOException must now arrive as the wrapper's cause, 
with its own cause
+    // still attached.

Review Comment:
   Removed. I also dropped the javadoc on the `causelessFailure` field, for the 
same reason: it narrated the test's intent, which `assertSame` already shows. I 
kept the note on the varargs `invoke` call because it documents `Method.invoke` 
behavior rather than this bug, but I can remove that one too if you'd rather.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to