Organize Imports to be Checkstyle compliant in utils
[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 final class ReflectiveExceptionMapperTest {
16     static final class NoArgumentCtorException extends Exception {
17         private static final long serialVersionUID = 1L;
18
19         public NoArgumentCtorException() {
20             super();
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         public FailingCtorException(final String message, final Throwable cause) {
36             throw new IllegalArgumentException("just for test");
37         }
38     }
39
40     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(expected=IllegalArgumentException.class)
50     public void testNoArgumentsContructor() {
51         ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class);
52     }
53
54     @Test(expected=IllegalArgumentException.class)
55     public void testPrivateContructor() {
56         ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class);
57     }
58
59     @Test(expected=IllegalArgumentException.class)
60     public void testFailingContructor() {
61         ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class);
62     }
63
64     @Test
65     public void testInstantiation() {
66         ReflectiveExceptionMapper<GoodException> mapper = ReflectiveExceptionMapper.create("instantiation", 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 }