2 * Copyright (c) 2014 Robert Varga. All rights reserved.
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
8 package org.opendaylight.yangtools.util.concurrent;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
13 import java.util.concurrent.ExecutionException;
14 import org.junit.Test;
16 public class ReflectiveExceptionMapperTest {
17 static final class NoArgumentCtorException extends Exception {
18 private static final long serialVersionUID = 1L;
20 NoArgumentCtorException() {
24 static final class PrivateCtorException extends Exception {
25 private static final long serialVersionUID = 1L;
27 private PrivateCtorException(final String message, final Throwable cause) {
28 super(message, cause);
32 static final class FailingCtorException extends Exception {
33 private static final long serialVersionUID = 1L;
35 FailingCtorException(final String message, final Throwable cause) {
36 throw new IllegalArgumentException("just for test");
40 public static final class GoodException extends Exception {
41 private static final long serialVersionUID = 1L;
43 public GoodException(final String message, final Throwable cause) {
44 super(message, cause);
50 public void testNoArgumentsContructor() {
51 assertThrows(IllegalArgumentException.class,
52 () -> ReflectiveExceptionMapper.create("no arguments", NoArgumentCtorException.class));
56 public void testPrivateContructor() {
57 assertThrows(IllegalArgumentException.class,
58 () -> ReflectiveExceptionMapper.create("private constructor", PrivateCtorException.class));
62 public void testFailingContructor() {
63 assertThrows(IllegalArgumentException.class,
64 () -> ReflectiveExceptionMapper.create("failing constructor", FailingCtorException.class));
68 public void testInstantiation() {
69 ReflectiveExceptionMapper<GoodException> mapper = ReflectiveExceptionMapper.create("instantiation",
72 final Throwable cause = new Throwable("some test message");
74 GoodException ret = mapper.apply(new ExecutionException("test", cause));
76 assertEquals("instantiation execution failed", ret.getMessage());
77 assertEquals(cause, ret.getCause());