-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Query by Example to property handle Optional types that are empty.
The Auditable interface introduces Optional getters, which when combined with Query by Example result in cryptic errors. By ignoring a probe's field that contains an Optional.empty, Query by Example works properly. Closes #2176.
- Loading branch information
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/test/java/org/springframework/data/jpa/domain/support/AuditableQueryByExampleTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2008-2021 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jpa.domain.support; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.Example; | ||
import org.springframework.data.jpa.domain.sample.AuditableUser; | ||
import org.springframework.data.jpa.domain.sample.AuditorAwareStub; | ||
import org.springframework.data.jpa.repository.sample.AnnotatedAuditableUserRepository; | ||
import org.springframework.data.jpa.repository.sample.AuditableUserRepository; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
|
||
/** | ||
* Integration test for {@link org.springframework.data.repository.query.QueryByExampleExecutor} involving | ||
* {@link java.util.Optional}, using {@link org.springframework.data.domain.Auditable} as the means to verify it works | ||
* properly. | ||
* | ||
* @author Greg Turnquist | ||
*/ | ||
@ExtendWith(SpringExtension.class) | ||
@ContextConfiguration("classpath:auditing/auditing-query-by-example.xml") | ||
public class AuditableQueryByExampleTests { | ||
|
||
@Autowired AuditableUserRepository repository; | ||
@Autowired AnnotatedAuditableUserRepository annotatedUserRepository; | ||
|
||
@Autowired AuditorAwareStub auditorAware; | ||
|
||
private AuditableUser user; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
|
||
user = new AuditableUser(); | ||
auditorAware.setAuditor(user); | ||
|
||
repository.saveAndFlush(user); | ||
} | ||
|
||
@Test | ||
void queryByExampleTreatsEmptyOptionalsLikeNulls() { | ||
|
||
user.setFirstname("Greg"); | ||
user = repository.saveAndFlush(user); | ||
|
||
AuditableUser probe = new AuditableUser(); | ||
probe.setFirstname("Greg"); | ||
Example<AuditableUser> example = Example.of(probe); | ||
|
||
List<AuditableUser> results = repository.findAll(example); | ||
|
||
assertThat(results).hasSize(1); | ||
assertThat(results).extracting(AuditableUser::getFirstname).containsExactly("Greg"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:jpa="http://www.springframework.org/schema/data/jpa" | ||
xmlns:context="http://www.springframework.org/schema/context" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd | ||
http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> | ||
|
||
<import resource="../infrastructure.xml" /> | ||
|
||
<jpa:auditing auditor-aware-ref="auditorAware" /> | ||
|
||
<bean id="auditorAware" class="org.springframework.data.jpa.domain.sample.AuditorAwareStub"> | ||
<constructor-arg ref="auditableUserRepository" /> | ||
</bean> | ||
|
||
<jpa:repositories base-package="org.springframework.data.jpa.repository.sample" /> | ||
|
||
</beans> |