From 288d253b8bc809d6f8625206c76c7650c36ea446 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 27 Jul 2015 19:23:01 +0200 Subject: [PATCH] Introduce support for conditional lambda execution in Spring's Assume --- .../org/springframework/tests/Assume.java | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/spring-core/src/test/java/org/springframework/tests/Assume.java b/spring-core/src/test/java/org/springframework/tests/Assume.java index c916ea7f56..e7923851a5 100644 --- a/spring-core/src/test/java/org/springframework/tests/Assume.java +++ b/spring-core/src/test/java/org/springframework/tests/Assume.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -21,6 +21,7 @@ import java.lang.reflect.Method; import java.util.Set; import org.apache.commons.logging.Log; + import org.junit.AssumptionViolatedException; import org.springframework.util.ClassUtils; @@ -73,9 +74,11 @@ import static org.junit.Assume.*; * * @author Rob Winch * @author Phillip Webb + * @author Sam Brannen * @since 3.2 * @see #atLeast(JavaVersion) * @see #group(TestGroup) + * @see #group(TestGroup, Executable) */ public abstract class Assume { @@ -83,8 +86,9 @@ public abstract class Assume { /** - * Assume a minimum {@link JavaVersion} is running. + * Assume that a minimum {@link JavaVersion} is running. * @param version the minimum version for the test to run + * @throws AssumptionViolatedException if the assumption fails */ public static void atLeast(JavaVersion version) { if (!JavaVersion.runningVersion().isAtLeast(version)) { @@ -95,7 +99,8 @@ public abstract class Assume { /** * Assume that a particular {@link TestGroup} has been specified. - * @param group the group that must be specified. + * @param group the group that must be specified + * @throws AssumptionViolatedException if the assumption fails */ public static void group(TestGroup group) { if (!GROUPS.contains(group)) { @@ -104,9 +109,25 @@ public abstract class Assume { } } + /** + * Assume that a particular {@link TestGroup} has been specified before + * executing the supplied {@link Executable}. + *

If the assumption fails, the executable will not be executed, but + * no {@link AssumptionViolatedException} will be thrown. + * @param group the group that must be specified + * @param executable the executable to execute if the test group is active + * @since 4.2 + */ + public static void group(TestGroup group, Executable executable) throws Exception { + if (GROUPS.contains(group)) { + executable.execute(); + } + } + /** * Assume that the specified log is not set to Trace or Debug. * @param log the log to test + * @throws AssumptionViolatedException if the assumption fails */ public static void notLogging(Log log) { assumeFalse(log.isTraceEnabled()); @@ -114,7 +135,10 @@ public abstract class Assume { } /** - * Assume that we can load fonts (https://java.net/jira/browse/MACOSX_PORT-355) + * Assume that we can load fonts. + *

See MACOSX_PORT-355 + * issue. + * @throws AssumptionViolatedException if the assumption fails */ public static void canLoadNativeDirFonts() { try { @@ -130,4 +154,12 @@ public abstract class Assume { } } + /** + * @since 4.2 + */ + @FunctionalInterface + public static interface Executable { + void execute() throws Exception; + } + }