Use @Serial in util
[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.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12
13 import java.io.Serial;
14 import java.util.concurrent.ExecutionException;
15 import org.junit.Test;
16
17 public class ReflectiveExceptionMapperTest {
18     static final class NoArgumentCtorException extends Exception {
19         @Serial
20         private static final long serialVersionUID = 1L;
21
22         NoArgumentCtorException() {
23         }
24     }
25
26     static final class PrivateCtorException extends Exception {
27         @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     static final class FailingCtorException extends Exception {
36         @Serial
37         private static final long serialVersionUID = 1L;
38
39         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         @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     public void testNoArgumentsContructor() {
56         assertThrows(IllegalArgumentException.class,
57             () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
58     }
59
60     @Test
61     public void testPrivateContructor() {
62         assertThrows(IllegalArgumentException.class,
63             () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
64     }
65
66     @Test
67     public void testFailingContructor() {
68         assertThrows(IllegalArgumentException.class,
69             () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
70     }
71
72     @Test
73     public void testInstantiation() {
74         ReflectiveExceptionMapper<GoodException> mapper = ReflectiveExceptionMapper.create("instantiation",
75             GoodException.class);
76
77         final Throwable cause = new Throwable("some test message");
78
79         GoodException ret = mapper.apply(new ExecutionException("test", cause));
80
81         assertEquals("instantiation execution failed", ret.getMessage());
82         assertEquals(cause, ret.getCause());
83     }
84 }