|
| 1 | +/* |
| 2 | + * Copyright 2008-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.domain.support; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.context.annotation.ImportResource; |
| 28 | +import org.springframework.data.domain.Example; |
| 29 | +import org.springframework.data.jpa.domain.sample.UserWithOptionalField; |
| 30 | +import org.springframework.data.jpa.domain.sample.UserWithOptionalFieldRepository; |
| 31 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 32 | +import org.springframework.test.context.ContextConfiguration; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration test for {@link org.springframework.data.repository.query.QueryByExampleExecutor} involving |
| 37 | + * {@link Optional#empty()}. |
| 38 | + * |
| 39 | + * @author Greg Turnquist |
| 40 | + */ |
| 41 | +@ExtendWith(SpringExtension.class) |
| 42 | +@ContextConfiguration |
| 43 | +public class QueryByExampleWithOptionalEmptyTests { |
| 44 | + |
| 45 | + @Autowired UserWithOptionalFieldRepository repository; |
| 46 | + UserWithOptionalField user; |
| 47 | + |
| 48 | + @Test |
| 49 | + void queryByExampleTreatsEmptyOptionalsLikeNulls() { |
| 50 | + |
| 51 | + // given |
| 52 | + UserWithOptionalField user = new UserWithOptionalField(); |
| 53 | + user.setName("Greg"); |
| 54 | + repository.saveAndFlush(user); |
| 55 | + |
| 56 | + // when |
| 57 | + UserWithOptionalField probe = new UserWithOptionalField(); |
| 58 | + probe.setName("Greg"); |
| 59 | + Example<UserWithOptionalField> example = Example.of(probe); |
| 60 | + |
| 61 | + // then |
| 62 | + List<UserWithOptionalField> results = repository.findAll(example); |
| 63 | + |
| 64 | + assertThat(results).hasSize(1); |
| 65 | + assertThat(results).extracting(UserWithOptionalField::getName).containsExactly("Greg"); |
| 66 | + } |
| 67 | + |
| 68 | + @Configuration |
| 69 | + @EnableJpaRepositories(basePackageClasses = UserWithOptionalFieldRepository.class) |
| 70 | + @ImportResource("classpath:infrastructure.xml") |
| 71 | + static class JpaRepositoryConfig {} |
| 72 | + |
| 73 | +} |
0 commit comments