Use assertThrows() in common-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.util.concurrent.ExecutionException;
14 import org.junit.Test;
15
16 public class ReflectiveExceptionMapperTest {
17     static final class NoArgumentCtorException extends Exception {
18         private static final long serialVersionUID = 1L;
19
20         NoArgumentCtorException() {
21         }
22     }
23
24     static final class PrivateCtorException extends Exception {
25         private static final long serialVersionUID = 1L;
26
27         private PrivateCtorException(final String message, final Throwable cause) {
28             super(message, cause);
29         }
30     }
31
32     static final class FailingCtorException extends Exception {
33         private static final long serialVersionUID = 1L;
34
35         FailingCtorException(final String message, final Throwable cause) {
36             throw new IllegalArgumentException("just for test");
37         }
38     }
39
40     public static final class GoodException extends Exception {
41         private static final long serialVersionUID = 1L;
42
43         public GoodException(final String message, final Throwable cause) {
44             super(message, cause);
45         }
46     }
47
48
49     @Test
50     public void testNoArgumentsContructor() {
51         assertThrows(IllegalArgumentException.class,
52             () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
53     }
54
55     @Test
56     public void testPrivateContructor() {
57         assertThrows(IllegalArgumentException.class,
58             () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
59     }
60
61     @Test
62     public void testFailingContructor() {
63         assertThrows(IllegalArgumentException.class,
64             () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
65     }
66
67     @Test
68     public void testInstantiation() {
69         ReflectiveExceptionMapper<GoodException> mapper = ReflectiveExceptionMapper.create("instantiation",
70             GoodException.class);
71
72         final Throwable cause = new Throwable("some test message");
73
74         GoodException ret = mapper.apply(new ExecutionException("test", cause));
75
76         assertEquals("instantiation execution failed", ret.getMessage());
77         assertEquals(cause, ret.getCause());
78     }
79 }