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