Migrate common/util to JUnit5
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / concurrent / ReflectiveExceptionMapperTest.java
1 /*
2  * Copyright (c) 2014 Robert Varga.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util.concurrent;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12
13 import java.util.concurrent.ExecutionException;
14 import org.junit.jupiter.api.Test;
15
16 public class ReflectiveExceptionMapperTest {
17     public static final class NoArgumentCtorException extends Exception {
18         @java.io.Serial
19         private static final long serialVersionUID = 1L;
20
21         public NoArgumentCtorException() {
22             // Noop
23         }
24     }
25
26     public static final class PrivateCtorException extends Exception {
27         @java.io.Serial
28         private static final long serialVersionUID = 1L;
29
30         private PrivateCtorException(final String message, final Throwable cause) {
31             super(message, cause);
32         }
33     }
34
35     public static final class FailingCtorException extends Exception {
36         @java.io.Serial
37         private static final long serialVersionUID = 1L;
38
39         public FailingCtorException(final String message, final Throwable cause) {
40             throw new IllegalArgumentException("just for test");
41         }
42     }
43
44     public static final class GoodException extends Exception {
45         @java.io.Serial
46         private static final long serialVersionUID = 1L;
47
48         public GoodException(final String message, final Throwable cause) {
49             super(message, cause);
50         }
51     }
52
53
54     @Test
55     void testNoArgumentsContructor() {
56         assertThrows(IllegalArgumentException.class,
57             () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
58     }
59
60     @Test
61     void testPrivateContructor() {
62         assertThrows(IllegalArgumentException.class,
63             () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
64     }
65
66     @Test
67     void testFailingContructor() {
68         assertThrows(IllegalArgumentException.class,
69             () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
70     }
71
72     @Test
73     void testInstantiation() {
74         final var mapper = ReflectiveExceptionMapper.create("instantiation", GoodException.class);
75         final var cause = new Throwable("some test message");
76         final var ret = mapper.apply(new ExecutionException("test", cause));
77
78         assertEquals("instantiation execution failed", ret.getMessage());
79         assertEquals(cause, ret.getCause());
80     }
81 }