1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.xml.secure;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertSame;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26
27 import java.lang.invoke.MethodHandle;
28
29 import javax.xml.parsers.FactoryConfigurationError;
30 import javax.xml.stream.XMLInputFactory;
31
32 import org.junit.jupiter.api.Test;
33
34
35
36
37
38
39
40
41 class MethodHandleFactoryTest {
42
43 @Test
44 void findStaticReturnsHandleForExistingMethod() {
45 final MethodHandle handle = MethodHandleFactory.findStatic(XMLInputFactory.class, "newInstance");
46 assertNotNull(handle, "an existing static method must resolve to a handle");
47 }
48
49 @Test
50 void findStaticReturnsNullForMissingMethod() {
51 assertNull(MethodHandleFactory.findStatic(XMLInputFactory.class, "noSuchMethod"),
52 "a missing method must resolve to null");
53 }
54
55 @Test
56 void invokeExactRethrowsDeclaredException() {
57 final FactoryConfigurationError declared = new FactoryConfigurationError("boom");
58 final FactoryConfigurationError thrown = assertThrows(FactoryConfigurationError.class, () -> MethodHandleFactory.invokeExact(() -> {
59 throw declared;
60 }, FactoryConfigurationError.class), "an exception of the declared type must be rethrown");
61 assertSame(declared, thrown, "the declared exception must propagate unchanged");
62 }
63
64 @Test
65 void invokeExactRethrowsUndeclaredError() {
66 final OutOfMemoryError error = new OutOfMemoryError("boom");
67 final OutOfMemoryError thrown = assertThrows(OutOfMemoryError.class, () -> MethodHandleFactory.invokeExact(() -> {
68 throw error;
69 }, FactoryConfigurationError.class), "a JVM error must keep its type");
70 assertSame(error, thrown, "the error must propagate unchanged");
71 }
72
73 @Test
74 void invokeExactReturnsValue() {
75 assertEquals("value", MethodHandleFactory.invokeExact(() -> "value", IllegalStateException.class), "the callable's value must be returned");
76 }
77
78 @Test
79 void invokeExactWrapsUnexpectedException() {
80 final IllegalStateException wrapped = assertThrows(IllegalStateException.class, () -> MethodHandleFactory.invokeExact(() -> {
81 throw new IllegalArgumentException("boom");
82 }, FactoryConfigurationError.class), "an undeclared exception must be wrapped");
83 assertInstanceOf(IllegalArgumentException.class, wrapped.getCause(), "the unexpected exception must be the wrapper's cause");
84 }
85 }