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.mdsal.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
34 public class MappingCheckedFutureTest {
36 interface FutureInvoker {
37 void invokeGet(CheckedFuture<?,?> future) throws Exception;
39 Throwable extractWrappedTestEx(Exception from);
42 static class TestException extends Exception {
43 private static final long serialVersionUID = 1L;
45 TestException(final String message, final Throwable cause) {
46 super(message, cause);
50 static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
51 "Test", TestException.class) {
54 protected TestException newWithCause(final String message, final Throwable cause) {
55 return new TestException(message, cause);
59 static final FutureInvoker GET = new FutureInvoker() {
61 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
66 public Throwable extractWrappedTestEx(final Exception from) {
67 if (from instanceof ExecutionException) {
68 return from.getCause();
75 static final FutureInvoker TIMED_GET = new FutureInvoker() {
77 public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
78 future.get(1, TimeUnit.HOURS);
82 public Throwable extractWrappedTestEx(final Exception from) {
83 if (from instanceof ExecutionException) {
84 return from.getCause();
91 static final FutureInvoker CHECKED_GET = new FutureInvoker() {
93 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
98 public Throwable extractWrappedTestEx(final Exception from) {
103 static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
105 public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
106 future.checkedGet(50, TimeUnit.MILLISECONDS);
110 public Throwable extractWrappedTestEx(final Exception from) {
116 public void testGet() throws Exception {
117 SettableFuture<String> delegate = SettableFuture.create();
118 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
119 delegate.set("test");
120 assertEquals("get", "test", future.get());
124 public void testGetWithExceptions() throws Exception {
125 testExecutionException(GET, new RuntimeException());
126 testExecutionException(GET, new TestException("mock", null));
127 testCancellationException(GET);
128 testInterruptedException(GET);
132 public void testTimedGet() throws Exception {
133 SettableFuture<String> delegate = SettableFuture.create();
134 MappingCheckedFuture<String,TestException> future = MappingCheckedFuture.create(delegate, MAPPER);
135 delegate.set("test");
136 assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS));
140 public void testTimedGetWithExceptions() throws Exception {
141 testExecutionException(TIMED_GET, new RuntimeException());
142 testCancellationException(TIMED_GET);
143 testInterruptedException(TIMED_GET);
147 public void testCheckedGetWithExceptions() throws Exception {
148 testExecutionException(CHECKED_GET, new RuntimeException());
149 testCancellationException(CHECKED_GET);
150 testInterruptedException(CHECKED_GET);
154 public void testTimedCheckedWithExceptions() throws Exception {
155 testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
156 testCancellationException(TIMED_CHECKED_GET);
157 testInterruptedException(TIMED_CHECKED_GET);
160 @SuppressWarnings("checkstyle:illegalCatch")
161 private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) {
162 SettableFuture<String> delegate = SettableFuture.create();
163 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
165 delegate.setException(cause);
168 invoker.invokeGet(mappingFuture);
169 fail("Expected exception thrown");
170 } catch (Exception e) {
171 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
172 assertNotNull("Expected returned exception is null", expectedTestEx);
173 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
175 if (cause instanceof TestException) {
176 assertNull("Expected null cause", expectedTestEx.getCause());
178 assertSame("TestException cause", cause, expectedTestEx.getCause());
183 @SuppressWarnings("checkstyle:illegalCatch")
184 private static void testCancellationException(final FutureInvoker invoker) {
185 SettableFuture<String> delegate = SettableFuture.create();
186 MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
188 mappingFuture.cancel(false);
191 invoker.invokeGet(mappingFuture);
192 fail("Expected exception thrown");
193 } catch (Exception e) {
194 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
195 assertNotNull("Expected returned exception is null", expectedTestEx);
196 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
197 assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass());
201 @SuppressWarnings("checkstyle:illegalCatch")
202 private static void testInterruptedException(final FutureInvoker invoker) throws Exception {
203 SettableFuture<String> delegate = SettableFuture.create();
204 final MappingCheckedFuture<String, TestException> mappingFuture = MappingCheckedFuture.create(delegate, MAPPER);
206 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
207 final CountDownLatch doneLatch = new CountDownLatch(1);
208 Thread thread = new Thread() {
213 } catch (AssertionError e) {
216 doneLatch.countDown();
222 invoker.invokeGet(mappingFuture);
223 fail("Expected exception thrown");
224 } catch (Exception e) {
225 Throwable expectedTestEx = invoker.extractWrappedTestEx(e);
226 assertNotNull("Expected returned exception is null", expectedTestEx);
227 assertEquals("Exception type", TestException.class, expectedTestEx.getClass());
228 assertEquals("TestException cause type", InterruptedException.class,
229 expectedTestEx.getCause().getClass());
236 assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
238 if (assertError.get() != null) {
239 throw assertError.get();