Browse Source
We now no longer double-wrap parameters for IN criteria. Previously, collection arguments were wrapped into another collection which caused double-wrapped lists. Original pull request: #217.pull/226/head
2 changed files with 118 additions and 2 deletions
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
/* |
||||
* Copyright 2020 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.relational.repository.query; |
||||
|
||||
import static org.assertj.core.api.Assertions.*; |
||||
|
||||
import lombok.Data; |
||||
import lombok.SneakyThrows; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
||||
import org.springframework.data.relational.core.query.Criteria; |
||||
import org.springframework.data.repository.Repository; |
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
||||
import org.springframework.data.repository.query.QueryMethod; |
||||
import org.springframework.data.repository.query.parser.Part; |
||||
|
||||
/** |
||||
* Unit tests for {@link CriteriaFactory}. |
||||
* |
||||
* @author Mark Paluch |
||||
*/ |
||||
public class CriteriaFactoryUnitTests { |
||||
|
||||
@Test // DATAJDBC-539
|
||||
void shouldConsiderIterableValuesInInOperator() { |
||||
|
||||
QueryMethod queryMethod = getQueryMethod("findAllByNameIn", List.class); |
||||
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, Arrays.asList("foo", "bar")); |
||||
ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor); |
||||
CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata); |
||||
|
||||
Part part = new Part("NameIn", User.class); |
||||
|
||||
Criteria criteria = criteriaFactory.createCriteria(part); |
||||
|
||||
assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar")); |
||||
} |
||||
|
||||
@Test // DATAJDBC-539
|
||||
void shouldConsiderArrayValuesInInOperator() { |
||||
|
||||
QueryMethod queryMethod = getQueryMethod("findAllByNameIn", String[].class); |
||||
|
||||
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, |
||||
new Object[] { new String[] { "foo", "bar" } }); |
||||
ParameterMetadataProvider parameterMetadata = new ParameterMetadataProvider(accessor); |
||||
CriteriaFactory criteriaFactory = new CriteriaFactory(parameterMetadata); |
||||
|
||||
Part part = new Part("NameIn", User.class); |
||||
|
||||
Criteria criteria = criteriaFactory.createCriteria(part); |
||||
|
||||
assertThat(criteria.getValue()).isEqualTo(Arrays.asList("foo", "bar")); |
||||
} |
||||
|
||||
@SneakyThrows |
||||
private QueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) { |
||||
Method method = UserRepository.class.getMethod(methodName, parameterTypes); |
||||
return new QueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class), |
||||
new SpelAwareProxyProjectionFactory()); |
||||
} |
||||
|
||||
private RelationalParametersParameterAccessor getAccessor(QueryMethod queryMethod, Object... values) { |
||||
return new RelationalParametersParameterAccessor(queryMethod, values); |
||||
} |
||||
|
||||
interface UserRepository extends Repository<User, Long> { |
||||
|
||||
User findAllByNameIn(List<String> names); |
||||
|
||||
User findAllByNameIn(String[] names); |
||||
} |
||||
|
||||
@Data |
||||
static class User { |
||||
|
||||
String name; |
||||
} |
||||
} |
||||
Loading…
Reference in new issue