2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.cluster.databroker.actors.dds;
10 import java.util.concurrent.Future;
11 import java.util.concurrent.TimeUnit;
12 import org.junit.Assert;
17 public interface RunnableWithException {
18 void run() throws Exception;
21 private static final long TIMEOUT = 3;
24 * Asserts, that future result when it completes is equal to given object.
25 * Future must complete in {@link TestUtils#TIMEOUT} seconds.
27 * @param expected expected result
28 * @param actual future
30 * @throws Exception exception
32 static <T> void assertFutureEquals(final T expected, final Future<T> actual) throws Exception {
33 Assert.assertEquals(expected, getWithTimeout(actual));
37 * Calls {@link Future#get(long, TimeUnit)} with {@link TestUtils#TIMEOUT} in seconds.
39 * @param future future
41 * @return future result
42 * @throws Exception exception
44 static <T> T getWithTimeout(final Future<T> future) throws Exception {
45 return future.get(TIMEOUT, TimeUnit.SECONDS);
49 * Asserts that given operation invocation, will throw an exception of given class.
51 * @param operation operation
52 * @param expectedException expected exception class
53 * @param message message, when expected exception isn't thrown
54 * @return expected exception instance. Can be used for additional assertions.
55 * @throws Exception unexpected exception.
57 //Throwable is propagated if doesn't match the expected type
58 @SuppressWarnings("checkstyle:IllegalCatch")
59 static Throwable assertOperationThrowsException(final RunnableWithException operation,
60 final Class<? extends Throwable> expectedException,
61 final String message) throws Exception {
64 throw new AssertionError(message + expectedException);
65 } catch (final Throwable e) {
66 if (!e.getClass().equals(expectedException)) {
74 * Asserts, that when given operation is run, exception of given class is thrown.
76 * @param operation operation
77 * @param expectedException expected exception class
78 * @return expected exception instance. Can be used for additional assertions.
79 * @throws Exception unexpected exception.
81 static Throwable assertOperationThrowsException(final RunnableWithException operation,
82 final Class<? extends Throwable> expectedException)
84 return assertOperationThrowsException(operation, expectedException, "Operation should throw exception: ");