From cd3d0b19b080f08de49d6c0b82f6f535f2a423f5 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Wed, 9 Feb 2022 16:16:49 +0100 Subject: [PATCH] Polishing. Refactored the unit tests to include a negative case and to separate the different scenarios tested. Removed the default LockMode from the Lock annotation. I have the feeling that most users will assume an exclusive Lock when none is specified, but also don't want to request stronger locks than required. Original pull request #1158 See #1041 --- .../data/jdbc/repository/query/Lock.java | 2 +- .../JdbcRepositoryIntegrationTests.java | 1 + .../query/JdbcQueryMethodUnitTests.java | 22 ++++++++++--------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Lock.java b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Lock.java index 0ef95ca0..1fd310b8 100644 --- a/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Lock.java +++ b/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Lock.java @@ -34,6 +34,6 @@ public @interface Lock { /** * Defines which type of {@link LockMode} we want to use. */ - LockMode value() default LockMode.PESSIMISTIC_READ; + LockMode value(); } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java index 70391a68..048a78a5 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java @@ -335,6 +335,7 @@ public class JdbcRepositoryIntegrationTests { @Test void findAllByFirstnameWithLock() { + DummyEntity dummyEntity = createDummyEntity(); repository.save(dummyEntity); assertThat(repository.findAllByName(dummyEntity.getName())).hasSize(1); diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethodUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethodUnitTests.java index d30a3cdb..a707f854 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethodUnitTests.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethodUnitTests.java @@ -25,7 +25,6 @@ import java.util.Properties; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.relational.core.sql.LockMode; @@ -123,28 +122,31 @@ public class JdbcQueryMethodUnitTests { } @Test // GH-1041 - void returnsQueryMethodWithLock() throws NoSuchMethodException { + void returnsQueryMethodWithCorrectLockTypeWriteLock() throws NoSuchMethodException { JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodWithWriteLock"); - JdbcQueryMethod queryMethodWithReadLock = createJdbcQueryMethod("queryMethodWithReadLock"); - assertThat(queryMethodWithWriteLock.hasLockMode()).isTrue(); - assertThat(queryMethodWithReadLock.hasLockMode()).isTrue(); + assertThat(queryMethodWithWriteLock.lookupLockAnnotation()).isPresent(); + assertThat(queryMethodWithWriteLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_WRITE); } @Test // GH-1041 - void returnsQueryMethodWithCorrectLockType() throws NoSuchMethodException { + void returnsQueryMethodWithCorrectLockTypeReadLock() throws NoSuchMethodException { - JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodWithWriteLock"); JdbcQueryMethod queryMethodWithReadLock = createJdbcQueryMethod("queryMethodWithReadLock"); - assertThat(queryMethodWithWriteLock.lookupLockAnnotation()).isPresent(); assertThat(queryMethodWithReadLock.lookupLockAnnotation()).isPresent(); - - assertThat(queryMethodWithWriteLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_WRITE); assertThat(queryMethodWithReadLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_READ); } + @Test // GH-1041 + void returnsQueryMethodWithCorrectLockTypeNoLock() throws NoSuchMethodException { + + JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodName"); + + assertThat(queryMethodWithWriteLock.lookupLockAnnotation()).isEmpty(); + } + @Lock(LockMode.PESSIMISTIC_WRITE) @Query private void queryMethodWithWriteLock() {}