
com.oracle.tools.deferred.Eventually Maven / Gradle / Ivy
/*
* File: Eventually.java
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* The contents of this file are subject to the terms and conditions of
* the Common Development and Distribution License 1.0 (the "License").
*
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the License by consulting the LICENSE.txt file
* distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL
*
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file LICENSE.txt.
*
* MODIFICATIONS:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*/
package com.oracle.tools.deferred;
import com.oracle.tools.runtime.java.JavaApplication;
import org.hamcrest.Matcher;
import static com.oracle.tools.deferred.DeferredHelper.ensure;
import static com.oracle.tools.deferred.DeferredHelper.eventually;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
/**
* A helper class that defines commonly used "assertThat" methods for the
* purposes of unit, functional and integration testing, most of which support
* working with {@link Deferred}s.
*
* Copyright (c) 2013. All Rights Reserved. Oracle Corporation.
* Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
*
* @author Brian Oliver
*/
public class Eventually
{
/**
* Asserts that a value will eventually satisfy the specified {@link Matcher}.
*
* Should the value be the result of a call to {@link DeferredHelper#invoking(Deferred)}
* the result is unwrapped into a {@link Deferred} that is then used for the assert.
*
* @param the type of value produced by the {@link Deferred}
*
* @param value the value
* @param matcher the {@link Matcher}
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(T value,
Matcher> matcher) throws AssertionError
{
assertThat(null, eventually(value), matcher);
}
/**
* Asserts that a value will eventually satisfy the specified {@link Matcher}.
*
* Should the value be the result of a call to {@link DeferredHelper#invoking(Deferred)}
* the result is unwrapped into a {@link Deferred} that is then used for the assert.
*
* @param the type of value produced by the {@link Deferred}
*
* @param message the message for the AssertionError (null
ok)
* @param value the value
* @param matcher the {@link Matcher}
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(String message,
T value,
Matcher> matcher) throws AssertionError
{
assertThat(message, eventually(value), matcher);
}
/**
* Asserts that a {@link Deferred}, when it becomes available,
* will eventually satisfy the specified {@link Matcher}.
*
* @param the type of value produced by the {@link Deferred}
*
* @param deferred the {@link Deferred}
* @param matcher the {@link Matcher}
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(Deferred deferred,
Matcher> matcher) throws AssertionError
{
assertThat(null, deferred, matcher);
}
/**
* Asserts that a {@link Deferred}, when it becomes available,
* will eventually (after the default amount of time) satisfy the
* specified {@link Matcher}.
*
* @param the type of value produced by the {@link Deferred}
*
* @param message the message for the AssertionError (null
ok)
* @param deferred the {@link Deferred}
* @param matcher the {@link Matcher}
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(String message,
Deferred deferred,
Matcher> matcher) throws AssertionError
{
assertThat(message,
deferred,
matcher,
DeferredHelper.ORACLETOOLS_DEFERRED_RETRY_TIMEOUT_SECS,
TimeUnit.SECONDS);
}
/**
* Asserts that a {@link Deferred}, when it becomes available,
* will eventually (after the specified amount of time) satisfy the
* specified {@link Matcher}.
*
* @param the type of value produced by the {@link Deferred}
*
* @param deferred the {@link Deferred}
* @param matcher the {@link Matcher}
* @param totalRetryDuration the maximum duration for retrying
* @param totalRetryDurationUnits the {@link TimeUnit}s for the duration
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(Deferred deferred,
Matcher> matcher,
long totalRetryDuration,
TimeUnit totalRetryDurationUnits) throws AssertionError
{
assertThat(null, deferred, matcher, totalRetryDuration, totalRetryDurationUnits);
}
/**
* Asserts that a {@link Deferred}, when it becomes available,
* will eventually (after the specified amount of time) satisfy the
* specified {@link Matcher}.
*
* @param the type of value produced by the {@link Deferred}
*
* @param message the message for the AssertionError (null
ok)
* @param deferred the {@link Deferred}
* @param matcher the {@link Matcher}
* @param totalRetryDuration the maximum duration for retrying
* @param totalRetryDurationUnits the {@link TimeUnit}s for the duration
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(String message,
Deferred deferred,
Matcher> matcher,
long totalRetryDuration,
TimeUnit totalRetryDurationUnits) throws AssertionError
{
// a DeferredMatcher does the heavy lifting
DeferredMatch deferredMatch = new DeferredMatch(deferred, matcher);
try
{
ensure(deferredMatch, totalRetryDuration, totalRetryDurationUnits);
}
catch (UnresolvableInstanceException e)
{
AssertionError error;
if (deferredMatch.getLastUsedMatchValue() == null)
{
error = new AssertionError((message == null ? "" : message + ": ") + "Failed to resolve a value for ["
+ deferredMatch.getDeferred() + "] to evaluate with matcher ["
+ deferredMatch.getMatcher() + "]");
error.initCause(e);
}
else
{
error = new AssertionError((message == null ? "" : message + ": ") + "Matcher [" + matcher
+ "] failed to match last resolved value ["
+ deferredMatch.getLastUsedMatchValue() + "] for ["
+ deferredMatch.getDeferred() + "]");
error.initCause(e);
}
throw error;
}
catch (Exception e)
{
AssertionError error = new AssertionError((message == null ? "" : message + ": ")
+ "Unexpected exception when attempting to resolve a value for ["
+ deferredMatch.getDeferred() + "] to evaluate with matcher ["
+ deferredMatch.getMatcher() + "]");
error.initCause(e);
throw error;
}
}
/**
* Asserts that the specified {@link Callable} submitted to the {@link JavaApplication}
* will eventually match the specified matcher.
*
* @param the type of value produced by the {@link Deferred}
*
* @param application the {@link JavaApplication} to which the {@link Callable} will be submitted
* @param callable the {@link Callable}
* @param matcher the {@link Matcher} representing the desire condition to match
*
* @throws AssertionError if the assertion fails
*/
public static void assertThat(JavaApplication> application,
Callable callable,
Matcher> matcher) throws AssertionError
{
assertThat(new DeferredRemoteExecution(application, callable), matcher);
}
}