From 50a73b9b8a1ce8753ad053b7be44d324875da54e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 4 Aug 2018 02:56:15 +0200 Subject: [PATCH] Remove MappingCheckedFuture This class has been moved to controller. Change-Id: Ib98d2d7c05d16ce66431ef7cb311a571f75e09e0 JIRA: MDSAL-229 Signed-off-by: Robert Varga --- .../common/api/MappingCheckedFuture.java | 98 ------- .../common/api/MappingCheckedFutureTest.java | 242 ------------------ 2 files changed, 340 deletions(-) delete mode 100644 common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/MappingCheckedFuture.java delete mode 100644 common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/MappingCheckedFutureTest.java diff --git a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/MappingCheckedFuture.java b/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/MappingCheckedFuture.java deleted file mode 100644 index 89a669f43b..0000000000 --- a/common/mdsal-common-api/src/main/java/org/opendaylight/mdsal/common/api/MappingCheckedFuture.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.mdsal.common.api; - -import static java.util.Objects.requireNonNull; - -import com.google.common.util.concurrent.AbstractCheckedFuture; -import com.google.common.util.concurrent.ListenableFuture; -import java.util.concurrent.CancellationException; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; -import java.util.function.Function; -import javax.annotation.Nonnull; - -/** - * An implementation of CheckedFuture that provides similar behavior for the get methods - * that the checkedGet methods provide. - * - *

For {@link CancellationException} and {@link InterruptedException}, the specified exception mapper - * is invoked to translate them to the checked exception type. - * - *

For {@link ExecutionException}, the mapper is invoked to translate the cause to the checked exception - * and a new ExecutionException is thrown with the translated cause. - * - * @author Thomas Pantelis - * - * @param The result type returned by this Future's get method - * @param The checked exception type - * @deprecated Use {@code org.opendaylight.controller.md.sal.common.api.MappingCheckedFuture} instead. - */ -@Deprecated -public final class MappingCheckedFuture extends AbstractCheckedFuture { - - private final Function mapper; - - private MappingCheckedFuture(final ListenableFuture delegate, final Function mapper) { - super(delegate); - this.mapper = requireNonNull(mapper); - } - - /** - * Creates a new MappingCheckedFuture that wraps the given {@link ListenableFuture} - * delegate. - * - * @param delegate the {@link ListenableFuture} to wrap - * @param mapper the mapping {@link Function} used to translate exceptions from the delegate - * @return a new MappingCheckedFuture - */ - public static MappingCheckedFuture create( - final ListenableFuture delegate, final Function mapper) { - return new MappingCheckedFuture<>(delegate, mapper); - } - - @Override - @SuppressWarnings("checkstyle:parameterName") - protected X mapException(@Nonnull final Exception e) { - return mapper.apply(e); - } - - private ExecutionException wrapInExecutionException(final String message, final Exception ex) { - return new ExecutionException(message, mapException(ex)); - } - - @Override - public V get() throws InterruptedException, ExecutionException { - try { - return super.get(); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - throw wrapInExecutionException("Operation was interrupted", e); - } catch (final CancellationException e) { - throw wrapInExecutionException("Operation was cancelled", e); - } catch (final ExecutionException e) { - throw wrapInExecutionException(e.getMessage(), e); - } - } - - @Override - public V get(final long timeout, @Nonnull final TimeUnit unit) - throws InterruptedException, ExecutionException, TimeoutException { - try { - return super.get(timeout, unit); - } catch (final InterruptedException e) { - Thread.currentThread().interrupt(); - throw wrapInExecutionException("Operation was interrupted", e); - } catch (final CancellationException e) { - throw wrapInExecutionException("Operation was cancelled", e); - } catch (final ExecutionException e) { - throw wrapInExecutionException(e.getMessage(), e); - } - } -} diff --git a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/MappingCheckedFutureTest.java b/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/MappingCheckedFutureTest.java deleted file mode 100644 index ae3b39a95f..0000000000 --- a/common/mdsal-common-api/src/test/java/org/opendaylight/mdsal/common/api/MappingCheckedFutureTest.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.mdsal.common.api; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import com.google.common.util.concurrent.CheckedFuture; -import com.google.common.util.concurrent.SettableFuture; -import java.util.concurrent.CancellationException; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; -import org.junit.Test; -import org.opendaylight.yangtools.util.concurrent.ExceptionMapper; - -/** - * Unit tests for MappingCheckedFuture. - * - * @author Thomas Pantelis - * - */ -@Deprecated -public class MappingCheckedFutureTest { - - interface FutureInvoker { - void invokeGet(CheckedFuture future) throws Exception; - - Throwable extractWrappedTestEx(Exception from); - } - - static class TestException extends Exception { - private static final long serialVersionUID = 1L; - - TestException(final String message, final Throwable cause) { - super(message, cause); - } - } - - static final ExceptionMapper MAPPER = new ExceptionMapper( - "Test", TestException.class) { - - @Override - protected TestException newWithCause(final String message, final Throwable cause) { - return new TestException(message, cause); - } - }; - - static final FutureInvoker GET = new FutureInvoker() { - @Override - public void invokeGet(final CheckedFuture future) throws Exception { - future.get(); - } - - @Override - public Throwable extractWrappedTestEx(final Exception from) { - if (from instanceof ExecutionException) { - return from.getCause(); - } - - return from; - } - }; - - static final FutureInvoker TIMED_GET = new FutureInvoker() { - @Override - public void invokeGet(final CheckedFuture future) throws Exception { - future.get(1, TimeUnit.HOURS); - } - - @Override - public Throwable extractWrappedTestEx(final Exception from) { - if (from instanceof ExecutionException) { - return from.getCause(); - } - - return from; - } - }; - - static final FutureInvoker CHECKED_GET = new FutureInvoker() { - @Override - public void invokeGet(final CheckedFuture future) throws Exception { - future.checkedGet(); - } - - @Override - public Throwable extractWrappedTestEx(final Exception from) { - return from; - } - }; - - static final FutureInvoker TIMED_CHECKED_GET = new FutureInvoker() { - @Override - public void invokeGet(final CheckedFuture future) throws Exception { - future.checkedGet(50, TimeUnit.MILLISECONDS); - } - - @Override - public Throwable extractWrappedTestEx(final Exception from) { - return from; - } - }; - - @Test - public void testGet() throws Exception { - SettableFuture delegate = SettableFuture.create(); - MappingCheckedFuture future = MappingCheckedFuture.create(delegate, MAPPER); - delegate.set("test"); - assertEquals("get", "test", future.get()); - } - - @Test - public void testGetWithExceptions() throws Exception { - testExecutionException(GET, new RuntimeException()); - testExecutionException(GET, new TestException("mock", null)); - testCancellationException(GET); - testInterruptedException(GET); - } - - @Test - public void testTimedGet() throws Exception { - SettableFuture delegate = SettableFuture.create(); - MappingCheckedFuture future = MappingCheckedFuture.create(delegate, MAPPER); - delegate.set("test"); - assertEquals("get", "test", future.get(50, TimeUnit.MILLISECONDS)); - } - - @Test - public void testTimedGetWithExceptions() throws Exception { - testExecutionException(TIMED_GET, new RuntimeException()); - testCancellationException(TIMED_GET); - testInterruptedException(TIMED_GET); - } - - @Test - public void testCheckedGetWithExceptions() throws Exception { - testExecutionException(CHECKED_GET, new RuntimeException()); - testCancellationException(CHECKED_GET); - testInterruptedException(CHECKED_GET); - } - - @Test - public void testTimedCheckedWithExceptions() throws Exception { - testExecutionException(TIMED_CHECKED_GET, new RuntimeException()); - testCancellationException(TIMED_CHECKED_GET); - testInterruptedException(TIMED_CHECKED_GET); - } - - @SuppressWarnings("checkstyle:illegalCatch") - private static void testExecutionException(final FutureInvoker invoker, final Throwable cause) { - SettableFuture delegate = SettableFuture.create(); - MappingCheckedFuture mappingFuture = MappingCheckedFuture.create(delegate, MAPPER); - - delegate.setException(cause); - - try { - invoker.invokeGet(mappingFuture); - fail("Expected exception thrown"); - } catch (Exception e) { - Throwable expectedTestEx = invoker.extractWrappedTestEx(e); - assertNotNull("Expected returned exception is null", expectedTestEx); - assertEquals("Exception type", TestException.class, expectedTestEx.getClass()); - - if (cause instanceof TestException) { - assertNull("Expected null cause", expectedTestEx.getCause()); - } else { - assertSame("TestException cause", cause, expectedTestEx.getCause()); - } - } - } - - @SuppressWarnings("checkstyle:illegalCatch") - private static void testCancellationException(final FutureInvoker invoker) { - SettableFuture delegate = SettableFuture.create(); - MappingCheckedFuture mappingFuture = MappingCheckedFuture.create(delegate, MAPPER); - - mappingFuture.cancel(false); - - try { - invoker.invokeGet(mappingFuture); - fail("Expected exception thrown"); - } catch (Exception e) { - Throwable expectedTestEx = invoker.extractWrappedTestEx(e); - assertNotNull("Expected returned exception is null", expectedTestEx); - assertEquals("Exception type", TestException.class, expectedTestEx.getClass()); - assertEquals("TestException cause type", CancellationException.class, expectedTestEx.getCause().getClass()); - } - } - - @SuppressWarnings("checkstyle:illegalCatch") - private static void testInterruptedException(final FutureInvoker invoker) throws Exception { - SettableFuture delegate = SettableFuture.create(); - final MappingCheckedFuture mappingFuture = MappingCheckedFuture.create(delegate, MAPPER); - - final AtomicReference assertError = new AtomicReference<>(); - final CountDownLatch doneLatch = new CountDownLatch(1); - Thread thread = new Thread() { - @Override - public void run() { - try { - doInvoke(); - } catch (AssertionError e) { - assertError.set(e); - } finally { - doneLatch.countDown(); - } - } - - void doInvoke() { - try { - invoker.invokeGet(mappingFuture); - fail("Expected exception thrown"); - } catch (Exception e) { - Throwable expectedTestEx = invoker.extractWrappedTestEx(e); - assertNotNull("Expected returned exception is null", expectedTestEx); - assertEquals("Exception type", TestException.class, expectedTestEx.getClass()); - assertEquals("TestException cause type", InterruptedException.class, - expectedTestEx.getCause().getClass()); - } - } - }; - thread.start(); - - thread.interrupt(); - assertTrue("get call completed", doneLatch.await(5, TimeUnit.SECONDS)); - - if (assertError.get() != null) { - throw assertError.get(); - } - } -} -- 2.36.6