Skip to content

Commit

Permalink
GH-919 - Add @CheckReturnValue and use it in Scenario API.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Oct 29, 2024
1 parent 7320b3b commit 587d5d7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 the original author or authors.
*
* 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
*
* https://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 org.springframework.modulith.core.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Findbugs and IDEA handle any annotation with name "CheckReturnValue" in return value check.
*
* @see <a href=
* "https://github.com/findbugsproject/findbugs/blob/264ae7baf890d2b347d91805c90057062b5dcb1e/findbugs/src/java/edu/umd/cs/findbugs/detect/BuildCheckReturnAnnotationDatabase.java#L120">Findbugs
* source code</a>
* @since 1.3
*/
@Target({
ElementType.CONSTRUCTOR,
ElementType.METHOD,
ElementType.PACKAGE,
ElementType.TYPE,
})
@Retention(RetentionPolicy.CLASS)
public @interface CheckReturnValue {}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
import org.awaitility.core.ConditionFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.lang.Nullable;
import org.springframework.modulith.core.util.CheckReturnValue;
import org.springframework.modulith.test.PublishedEvents.TypedPublishedEvents;
import org.springframework.modulith.test.PublishedEventsAssert.PublishedEventAssert;
import org.springframework.modulith.test.Scenario.When.EventResult;
import org.springframework.modulith.test.Scenario.When.StateChangeResult;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.transaction.support.TransactionOperations;
Expand All @@ -53,6 +52,7 @@
* @author Oliver Drotbohm
* @see ApplicationModuleTest
*/
@CheckReturnValue
public class Scenario {

private static final Predicate<Object> DEFAULT_ACCEPTANCE = it -> {
Expand Down Expand Up @@ -216,6 +216,7 @@ Scenario setDefaultCustomizer(Function<ConditionFactory, ConditionFactory> custo
return this;
}

@CheckReturnValue
public class When<T> {

private final BiFunction<TransactionOperations, ApplicationEventPublisher, T> stimulus;
Expand Down Expand Up @@ -460,6 +461,7 @@ public void andVerifyEvents(Consumer<AssertablePublishedEvents> events) {
* @param eventType must not be {@literal null}.
* @return will never be {@literal null}.
*/
@CheckReturnValue
public <E> EventResult<E> andExpect(Class<E> eventType) {
return new EventResult<>(eventType, Function.identity(), result);
}
Expand Down Expand Up @@ -501,6 +503,7 @@ public class EventResult<E> {
* @param filter must not be {@literal null}.
* @return will never be {@literal null}.
*/
@CheckReturnValue
public EventResult<E> matching(Predicate<? super E> filter) {

Assert.notNull(filter, "Filter must not be null!");
Expand All @@ -517,6 +520,7 @@ public EventResult<E> matching(Predicate<? super E> filter) {
* @param filter must not be {@literal null}.
* @return will never be {@literal null}.
*/
@CheckReturnValue
public <S> EventResult<E> matchingMapped(Function<E, S> extractor, Predicate<? super S> filter) {
return new EventResult<E>(type, createOrAdd(it -> it.matching(extractor, filter)), previousResult);
}
Expand All @@ -529,6 +533,7 @@ public <S> EventResult<E> matchingMapped(Function<E, S> extractor, Predicate<? s
* @param value can be {@literal null}.
* @return will never be {@literal null}.
*/
@CheckReturnValue
public <S> EventResult<E> matchingMappedValue(Function<E, S> extractor, @Nullable S value) {
return new EventResult<E>(type, createOrAdd(it -> it.matching(extractor, value)), previousResult);
}
Expand Down

0 comments on commit 587d5d7

Please sign in to comment.