com.spotify.hamcrest.future.SuccessfullyCompletedBlockingFuture Maven / Gradle / Ivy
/*-
* -\-\-
* hamcrest-future
* --
* Copyright (C) 2016 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/
package com.spotify.hamcrest.future;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
class SuccessfullyCompletedBlockingFuture
extends TypeSafeDiagnosingMatcher> {
private final Matcher matcher;
/**
* Creates a new SuccessfullyCompletedBlockingFuture that matches a completed future where the
* value matches the given matcher.
*/
SuccessfullyCompletedBlockingFuture(final Matcher matcher) {
this.matcher = Objects.requireNonNull(matcher);
}
@Override
protected boolean matchesSafely(final Future extends T> future,
final Description mismatchDescription) {
try {
final T item = future.get();
if (matcher.matches(item)) {
return true;
} else {
mismatchDescription.appendText("a future that completed with a value that ");
matcher.describeMismatch(item, mismatchDescription);
return false;
}
} catch (InterruptedException e) {
mismatchDescription.appendText("a future that was interrupted");
return false;
} catch (ExecutionException e) {
mismatchDescription
.appendText("a future that completed exceptionally with ")
.appendText(Utils.getStackTraceAsString(e.getCause()));
return false;
}
}
@Override
public void describeTo(final Description description) {
description
.appendText("a future that completed with a value that ")
.appendDescriptionOf(matcher);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy