2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. 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.controller.md.sal.common.api;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.SettableFuture;
19 import java.util.concurrent.CancellationException;
20 import java.util.concurrent.CountDownLatch;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicReference;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
28 * Unit tests for MappingCheckedFuture.
30 * @author Thomas Pantelis
33 public class MappingCheckedFutureTest {
35 interface FutureInvoker {
36 void invokeGet(CheckedFuture<?,?> future) throws Exception;
38 Throwable extractWrappedTestEx(Exception from);
41 static class TestException extends Exception {
42 private static final long serialVersionUID = 1L;
44 TestException(final String message, final Throwable cause) {
45 super(message, cause);
49 static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
50 "Test", TestException.class) {
53 protected TestException newWithCause(final String message, final Throwable cause) {
54 return new TestException(message, cause);
58 static final FutureInvoker GET = new FutureInvoker() {
60 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
65 public Throwable extractWrappedTestEx(final Exception from) {
66 if (from instanceof ExecutionException) {
67 return from.getCause();
74 static final FutureInvoker TIMED_GET = new FutureInvoker() {
76 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
77 future.get(1, TimeUnit.HOURS);
81 public Throwable extractWrappedTestEx(final Exception from) {
82 if (from instanceof ExecutionException) {
83 return from.getCause();
90 static final FutureInvoker CHECKED_GET = new FutureInvoker() {
92 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
97 public Throwable extractWrappedTestEx(final Exception from) {
102 static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
104 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
105 future.checkedGet(50, TimeUnit.MILLISECONDS);
109 public Throwable extractWrappedTestEx(final Exception from) {
115 public void testGet() throws Exception {
116 SettableFuture<String> delegate = SettableFuture.create();
117 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
118 delegate.set("test");
119 assertEquals("get", "test", future.get());
123 public void testGetWithExceptions() throws Exception {
124 testExecutionException(GET, new RuntimeException());
125 testExecutionException(GET, new TestException("mock", null));
126 testCancellationException(GET);
127 testInterruptedException(GET);
131 public void testTimedGet() throws Exception {
132 SettableFuture<String> delegate = SettableFuture.create();
133 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
134 delegate.set("test");
135 assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS));
139 public void testTimedGetWithExceptions() throws Exception {
140 testExecutionException(TIMED_GET, new RuntimeException());
141 testCancellationException(TIMED_GET);
142 testInterruptedException(TIMED_GET);
146 public void testCheckedGetWithExceptions() throws Exception {
147 testExecutionException(CHECKED_GET, new RuntimeException());
148 testCancellationException(CHECKED_GET);
149 testInterruptedException(CHECKED_GET);
153 public void testTimedCheckedWithExceptions() throws Exception {
154 testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
155 testCancellationException(TIMED_CHECKED_GET);
156 testInterruptedException(TIMED_CHECKED_GET);
159 @SuppressWarnings("checkstyle:illegalCatch")
160 private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) {
161 SettableFuture<String> delegate = SettableFuture.create();
162 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
164 delegate.setException(cause);
167 invoker.invokeGet(mappingFuture);
168 fail("Expected exception thrown");
169 } catch (Exception e) {
170 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
171 assertNotNull("Expected returned exception is null", expectedTestEx);
172 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
174 if (cause instanceof TestException) {
175 assertNull("Expected null cause", expectedTestEx.getCause());
177 assertSame("TestException cause", cause, expectedTestEx.getCause());
182 @SuppressWarnings("checkstyle:illegalCatch")
183 private static void testCancellationException(final FutureInvoker invoker) {
184 SettableFuture<String> delegate = SettableFuture.create();
185 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
187 mappingFuture.cancel(false);
190 invoker.invokeGet(mappingFuture);
191 fail("Expected exception thrown");
192 } catch (Exception e) {
193 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
194 assertNotNull("Expected returned exception is null", expectedTestEx);
195 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
196 assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass());
200 @SuppressWarnings("checkstyle:illegalCatch")
201 private static void testInterruptedException(final FutureInvoker invoker) throws Exception {
202 SettableFuture<String> delegate = SettableFuture.create();
203 final MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
205 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
206 final CountDownLatch doneLatch = new CountDownLatch(1);
207 Thread thread = new Thread() {
212 } catch (AssertionError e) {
215 doneLatch.countDown();
221 invoker.invokeGet(mappingFuture);
222 fail("Expected exception thrown");
223 } catch (Exception e) {
224 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
225 assertNotNull("Expected returned exception is null", expectedTestEx);
226 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
227 assertEquals("TestException cause type", InterruptedException.class,
228 expectedTestEx.getCause().getClass());
235 assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
237 if (assertError.get() != null) {
238 throw assertError.get();