DATAJPA-984 - Improved translation of one-element tuples in projections.
If a query returns a one-element tuple and that single element returned is type-compatible to the type to be returned we return it as is. Previously, we insisted on creating a Map from the tuple (which usually works for projection interfaces). This is needed to support custom simple types (i.e. types that the JPA provider can convert itself but which are not exposed through the metamodel - usually types managed through JPA AttributeConverters or provider specific conversion mechanisms).
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2016 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
|
||||
*
|
||||
* http://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.repository.query;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.persistence.Tuple;
|
||||
import javax.persistence.TupleElement;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.jpa.repository.query.AbstractJpaQuery.TupleConverter;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.ReturnedType;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link TupleConverter}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @soundtrack James Bay - Let it go (Chaos and the Calm)
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class TupleConverterUnitTests {
|
||||
|
||||
@Mock Tuple tuple;
|
||||
@Mock TupleElement<String> element;
|
||||
@Mock ProjectionFactory factory;
|
||||
|
||||
/**
|
||||
* @see DATAJPA-984
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void returnsSingleTupleElementIfItMatchesExpectedType() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(SampleRepository.class);
|
||||
QueryMethod method = new QueryMethod(SampleRepository.class.getMethod("someMethod"), metadata, factory);
|
||||
ReturnedType type = method.getResultProcessor().getReturnedType();
|
||||
|
||||
doReturn(element).when(tuple).get(0);
|
||||
doReturn(Arrays.asList(element)).when(tuple).getElements();
|
||||
doReturn("Foo").when(tuple).get(element);
|
||||
|
||||
TupleConverter converter = new TupleConverter(type);
|
||||
|
||||
assertThat(converter.convert(tuple), is((Object) "Foo"));
|
||||
}
|
||||
|
||||
static interface SampleRepository extends CrudRepository<Object, Long> {
|
||||
String someMethod();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user