Skip to content

Commit cf32380

Browse files
committed
spring-projects#282 - Add some error tests for 'PartTreeR2dbcQuery'
1 parent 6358eed commit cf32380

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryIntegrationTests.java

+44
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import io.r2dbc.spi.ConnectionFactoryMetadata;
2020
import lombok.Data;
2121
import org.junit.Before;
22+
import org.junit.Rule;
2223
import org.junit.Test;
24+
import org.junit.rules.ExpectedException;
2325
import org.junit.runner.RunWith;
2426
import org.mockito.Mock;
2527
import org.mockito.junit.MockitoJUnitRunner;
@@ -66,6 +68,9 @@ public class PartTreeR2dbcQueryIntegrationTests {
6668
@Mock
6769
private R2dbcConverter r2dbcConverter;
6870

71+
@Rule
72+
public ExpectedException thrown = ExpectedException.none();
73+
6974
private RelationalMappingContext mappingContext;
7075
private ReactiveDataAccessStrategy dataAccessStrategy;
7176
private DatabaseClient databaseClient;
@@ -439,6 +444,39 @@ public void createsQueryToFindAllEntitiesByStringAttributeIgnoringCase() throws
439444
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
440445
}
441446

447+
@Test
448+
public void throwsExceptionWhenIgnoringCaseIsImpossible() throws Exception {
449+
thrown.expect(IllegalStateException.class);
450+
thrown.expectMessage("Unable to ignore case of java.lang.Long type, " +
451+
"the property 'id' must reference a string");
452+
R2dbcQueryMethod queryMethod = getQueryMethod("findByIdIgnoringCase", Long.class);
453+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
454+
dataAccessStrategy);
455+
r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{1L}));
456+
}
457+
458+
@Test
459+
public void throwsExceptionWhenInPredicateHasNonIterableParameter() throws Exception {
460+
thrown.expect(IllegalArgumentException.class);
461+
thrown.expectMessage("Operator IN on id requires a Collection argument, " +
462+
"found class java.lang.Long in method findAllByIdIn.");
463+
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByIdIn", Long.class);
464+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
465+
dataAccessStrategy);
466+
r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{1L}));
467+
}
468+
469+
@Test
470+
public void throwsExceptionWhenSimplePropertyPredicateHasIterableParameter() throws Exception {
471+
thrown.expect(IllegalArgumentException.class);
472+
thrown.expectMessage("Operator SIMPLE_PROPERTY on id requires a scalar argument, " +
473+
"found interface java.util.Collection in method findAllById.");
474+
R2dbcQueryMethod queryMethod = getQueryMethod("findAllById", Collection.class);
475+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
476+
dataAccessStrategy);
477+
r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{Collections.singleton(1L)}));
478+
}
479+
442480
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
443481
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
444482
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
@@ -501,6 +539,12 @@ private interface UserRepository extends Repository<User, Long> {
501539
Flux<User> findAllByActiveFalse();
502540

503541
Flux<User> findAllByFirstNameIgnoreCase(String firstName);
542+
543+
Mono<User> findByIdIgnoringCase(Long id);
544+
545+
Flux<User> findAllByIdIn(Long id);
546+
547+
Flux<User> findAllById(Collection<Long> ids);
504548
}
505549

506550
@Table("users")

0 commit comments

Comments
 (0)