Browse Source

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
pull/1166/head
Jens Schauder 4 years ago
parent
commit
cd3d0b19b0
No known key found for this signature in database
GPG Key ID: 45CC872F17423DBF
  1. 2
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/Lock.java
  2. 1
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java
  3. 22
      spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/JdbcQueryMethodUnitTests.java

2
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. * Defines which type of {@link LockMode} we want to use.
*/ */
LockMode value() default LockMode.PESSIMISTIC_READ; LockMode value();
} }

1
spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/JdbcRepositoryIntegrationTests.java

@ -335,6 +335,7 @@ public class JdbcRepositoryIntegrationTests {
@Test @Test
void findAllByFirstnameWithLock() { void findAllByFirstnameWithLock() {
DummyEntity dummyEntity = createDummyEntity(); DummyEntity dummyEntity = createDummyEntity();
repository.save(dummyEntity); repository.save(dummyEntity);
assertThat(repository.findAllByName(dummyEntity.getName())).hasSize(1); assertThat(repository.findAllByName(dummyEntity.getName())).hasSize(1);

22
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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
import org.springframework.data.projection.ProjectionFactory; import org.springframework.data.projection.ProjectionFactory;
import org.springframework.data.relational.core.sql.LockMode; import org.springframework.data.relational.core.sql.LockMode;
@ -123,28 +122,31 @@ public class JdbcQueryMethodUnitTests {
} }
@Test // GH-1041 @Test // GH-1041
void returnsQueryMethodWithLock() throws NoSuchMethodException { void returnsQueryMethodWithCorrectLockTypeWriteLock() throws NoSuchMethodException {
JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodWithWriteLock"); JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodWithWriteLock");
JdbcQueryMethod queryMethodWithReadLock = createJdbcQueryMethod("queryMethodWithReadLock");
assertThat(queryMethodWithWriteLock.hasLockMode()).isTrue(); assertThat(queryMethodWithWriteLock.lookupLockAnnotation()).isPresent();
assertThat(queryMethodWithReadLock.hasLockMode()).isTrue(); assertThat(queryMethodWithWriteLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_WRITE);
} }
@Test // GH-1041 @Test // GH-1041
void returnsQueryMethodWithCorrectLockType() throws NoSuchMethodException { void returnsQueryMethodWithCorrectLockTypeReadLock() throws NoSuchMethodException {
JdbcQueryMethod queryMethodWithWriteLock = createJdbcQueryMethod("queryMethodWithWriteLock");
JdbcQueryMethod queryMethodWithReadLock = createJdbcQueryMethod("queryMethodWithReadLock"); JdbcQueryMethod queryMethodWithReadLock = createJdbcQueryMethod("queryMethodWithReadLock");
assertThat(queryMethodWithWriteLock.lookupLockAnnotation()).isPresent();
assertThat(queryMethodWithReadLock.lookupLockAnnotation()).isPresent(); assertThat(queryMethodWithReadLock.lookupLockAnnotation()).isPresent();
assertThat(queryMethodWithWriteLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_WRITE);
assertThat(queryMethodWithReadLock.lookupLockAnnotation().get().value()).isEqualTo(LockMode.PESSIMISTIC_READ); 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) @Lock(LockMode.PESSIMISTIC_WRITE)
@Query @Query
private void queryMethodWithWriteLock() {} private void queryMethodWithWriteLock() {}

Loading…
Cancel
Save