From e0dafd29f4027bd2fe40215c8a916e61bff16740 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Mon, 22 Jun 2015 11:33:31 +0200 Subject: [PATCH] DATACMNS-714 - Improved test cases for CompletableFuture support. We now make sure the converter for CompletableFuture is only registered if Spring 4.2 or better is on the classpath. Adapted test cases to only run tests for CompletableFuture support in the very same scenario. --- .../util/QueryExecutionConverters.java | 8 ++++++++ .../RepositoryFactorySupportUnitTests.java | 13 +++++++++---- .../repository/query/QueryMethodUnitTests.java | 8 ++++++++ .../util/QueryExecutionConvertersUnitTests.java | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java index d60161f22..2691c8ece 100644 --- a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java +++ b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java @@ -21,11 +21,13 @@ import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; +import org.springframework.core.SpringVersion; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.converter.GenericConverter; import org.springframework.core.convert.support.ConfigurableConversionService; +import org.springframework.data.util.Version; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; @@ -42,6 +44,9 @@ import com.google.common.base.Optional; */ public abstract class QueryExecutionConverters { + private static final Version SPRING_VERSION = Version.parse(SpringVersion.getVersion()); + private static final Version FOUR_DOT_TWO = new Version(4, 2); + private static final boolean GUAVA_PRESENT = ClassUtils.isPresent("com.google.common.base.Optional", QueryExecutionConverters.class.getClassLoader()); private static final boolean JDK_8_PRESENT = ClassUtils.isPresent("java.util.Optional", @@ -60,6 +65,9 @@ public abstract class QueryExecutionConverters { if (JDK_8_PRESENT) { WRAPPER_TYPES.add(NullableWrapperToJdk8OptionalConverter.getWrapperType()); + } + + if (JDK_8_PRESENT && SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO)) { WRAPPER_TYPES.add(NullableWrapperToCompletableFutureConverter.getWrapperType()); } } diff --git a/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java index 33a30be83..583f127a4 100644 --- a/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java @@ -41,6 +41,7 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.core.SpringVersion; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -52,6 +53,7 @@ import org.springframework.data.repository.core.NamedQueries; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.query.RepositoryQuery; import org.springframework.data.repository.sample.User; +import org.springframework.data.util.Version; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor; import org.springframework.test.util.ReflectionTestUtils; @@ -66,6 +68,9 @@ import org.springframework.util.concurrent.ListenableFuture; @RunWith(MockitoJUnitRunner.class) public class RepositoryFactorySupportUnitTests { + static final Version SPRING_VERSION = Version.parse(SpringVersion.getVersion()); + static final Version FOUR_DOT_TWO = new Version(4, 2); + public @Rule ExpectedException exception = ExpectedException.none(); DummyRepositoryFactory factory; @@ -252,7 +257,7 @@ public class RepositoryFactorySupportUnitTests { @Test public void wrapsExecutionResultIntoCompletableFutureIfConfigured() throws Exception { - assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); User reference = new User(); @@ -265,7 +270,7 @@ public class RepositoryFactorySupportUnitTests { @Test public void wrapsExecutionResultIntoListenableFutureIfConfigured() throws Exception { - assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); User reference = new User(); @@ -278,7 +283,7 @@ public class RepositoryFactorySupportUnitTests { @Test public void wrapsExecutionResultIntoCompletableFutureWithEntityCollectionIfConfigured() throws Exception { - assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); List reference = Arrays.asList(new User()); @@ -291,7 +296,7 @@ public class RepositoryFactorySupportUnitTests { @Test public void wrapsExecutionResultIntoListenableFutureWithEntityCollectionIfConfigured() throws Exception { - assumeThat(ClassUtils.isPresent("org.springframework.transaction.interceptor.TransactionalProxy", null), is(true)); + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); List reference = Arrays.asList(new User()); diff --git a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java index 9e898dd2c..55c1dbf34 100644 --- a/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/QueryMethodUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.repository.query; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.junit.Assume.*; import java.io.Serializable; import java.lang.reflect.Method; @@ -26,12 +27,14 @@ import java.util.concurrent.Future; import java.util.stream.Stream; import org.junit.Test; +import org.springframework.core.SpringVersion; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; +import org.springframework.data.util.Version; /** * Unit tests for {@link QueryMethod}. @@ -41,6 +44,9 @@ import org.springframework.data.repository.core.support.DefaultRepositoryMetadat */ public class QueryMethodUnitTests { + private static final Version SPRING_VERSION = Version.parse(SpringVersion.getVersion()); + private static final Version FOUR_DOT_TWO = new Version(4, 2); + RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class); /** @@ -185,6 +191,8 @@ public class QueryMethodUnitTests { @Test public void doesNotRejectCompletableFutureQueryForEntityCollection() throws Exception { + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); + RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class); Method method = SampleRepository.class.getMethod("returnsCompletableFutureForEntityCollection"); diff --git a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java index 0017a7022..6c804eaaa 100644 --- a/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java +++ b/src/test/java/org/springframework/data/repository/util/QueryExecutionConvertersUnitTests.java @@ -17,13 +17,16 @@ package org.springframework.data.repository.util; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; +import static org.junit.Assume.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import org.junit.Before; import org.junit.Test; +import org.springframework.core.SpringVersion; import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.data.util.Version; import org.springframework.util.concurrent.ListenableFuture; import com.google.common.base.Optional; @@ -35,6 +38,9 @@ import com.google.common.base.Optional; */ public class QueryExecutionConvertersUnitTests { + private static final Version SPRING_VERSION = Version.parse(SpringVersion.getVersion()); + private static final Version FOUR_DOT_TWO = new Version(4, 2); + DefaultConversionService conversionService; @Before @@ -54,6 +60,16 @@ public class QueryExecutionConvertersUnitTests { assertThat(QueryExecutionConverters.supports(java.util.Optional.class), is(true)); assertThat(QueryExecutionConverters.supports(Future.class), is(true)); assertThat(QueryExecutionConverters.supports(ListenableFuture.class), is(true)); + } + + /** + * @see DATACMNS-714 + */ + @Test + public void registersCompletableFutureAsWrapperTypeOnSpring42OrBetter() { + + assumeThat(SPRING_VERSION.isGreaterThanOrEqualTo(FOUR_DOT_TWO), is(true)); + assertThat(QueryExecutionConverters.supports(CompletableFuture.class), is(true)); }