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