Deprecate MappingCheckedFuture
[mdsal.git] / common / mdsal-common-api / src / test / java / org / opendaylight / mdsal / common / api / MappingCheckedFutureTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  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.mdsal.common.api;
9
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;
16
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;
26
27 /**
28  * Unit tests for MappingCheckedFuture.
29  *
30  * @author Thomas Pantelis
31  *
32  */
33 @Deprecated
34 public class MappingCheckedFutureTest {
35
36     interface FutureInvoker {
37         void invokeGet(CheckedFuture<?,?> future) throws Exception;
38
39         Throwable extractWrappedTestEx(Exception from);
40     }
41
42     static class TestException extends Exception {
43         private static final long serialVersionUID = 1L;
44
45         TestException(final String message, final Throwable cause) {
46             super(message, cause);
47         }
48     }
49
50     static final ExceptionMapper<TestException> MAPPER = new ExceptionMapper<TestException>(
51                                                                       "Test", TestException.class) {
52
53         @Override
54         protected TestException newWithCause(final String message, final Throwable cause) {
55             return new TestException(message, cause);
56         }
57     };
58
59     static final FutureInvoker GET = new FutureInvoker() {
60         @Override
61         public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
62             future.get();
63         }
64
65         @Override
66         public Throwable extractWrappedTestEx(final Exception from) {
67             if (from instanceof ExecutionException) {
68                 return from.getCause();
69             }
70
71             return from;
72         }
73     };
74
75     static final FutureInvoker TIMED_GET = new FutureInvoker() {
76         @Override
77         public void invokeGet(final CheckedFuture<?, ?> future) throws Exception {
78             future.get(1, TimeUnit.HOURS);
79         }
80
81         @Override
82         public Throwable extractWrappedTestEx(final Exception from) {
83             if (from instanceof ExecutionException) {
84                 return from.getCause();
85             }
86
87             return from;
88         }
89     };
90
91     static final FutureInvoker CHECKED_GET = new FutureInvoker() {
92         @Override
93         public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
94             future.checkedGet();
95         }
96
97         @Override
98         public Throwable extractWrappedTestEx(final Exception from) {
99             return from;
100         }
101     };
102
103     static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() {
104         @Override
105         public void invokeGet(final CheckedFuture<?,?> future) throws Exception {
106             future.checkedGet(50, TimeUnit.MILLISECONDS);
107         }
108
109         @Override
110         public Throwable extractWrappedTestEx(final Exception from) {
111             return from;
112         }
113     };
114
115     @Test
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());
121     }
122
123     @Test
124     public void testGetWithExceptions() throws Exception {
125         testExecutionException(GET, new RuntimeException());
126         testExecutionException(GET, new TestException("mock", null));
127         testCancellationException(GET);
128         testInterruptedException(GET);
129     }
130
131     @Test
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));
137     }
138
139     @Test
140     public void testTimedGetWithExceptions() throws Exception {
141         testExecutionException(TIMED_GET, new RuntimeException());
142         testCancellationException(TIMED_GET);
143         testInterruptedException(TIMED_GET);
144     }
145
146     @Test
147     public void testCheckedGetWithExceptions() throws Exception {
148         testExecutionException(CHECKED_GET, new RuntimeException());
149         testCancellationException(CHECKED_GET);
150         testInterruptedException(CHECKED_GET);
151     }
152
153     @Test
154     public void testTimedCheckedWithExceptions() throws Exception {
155         testExecutionException(TIMED_CHECKED_GET, new RuntimeException());
156         testCancellationException(TIMED_CHECKED_GET);
157         testInterruptedException(TIMED_CHECKED_GET);
158     }
159
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);
164
165         delegate.setException(cause);
166
167         try {
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());
174
175             if (cause instanceof TestException) {
176                 assertNull("Expected null cause", expectedTestEx.getCause());
177             } else {
178                 assertSame("TestException cause", cause, expectedTestEx.getCause());
179             }
180         }
181     }
182
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);
187
188         mappingFuture.cancel(false);
189
190         try {
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());
198         }
199     }
200
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);
205
206         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
207         final CountDownLatch doneLatch = new CountDownLatch(1);
208         Thread thread = new Thread() {
209             @Override
210             public void run() {
211                 try {
212                     doInvoke();
213                 } catch (AssertionError e) {
214                     assertError.set(e);
215                 } finally {
216                     doneLatch.countDown();
217                 }
218             }
219
220             void doInvoke() {
221                 try {
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());
230                 }
231             }
232         };
233         thread.start();
234
235         thread.interrupt();
236         assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS));
237
238         if (assertError.get() != null) {
239             throw assertError.get();
240         }
241     }
242 }