Browse Source
In case a repository query method returns a domain type not assignable to the repository domain type we have to use the repositories domain type to determine the collection but still use the returned domain type to hand to the unmarshalling. Thus, we need to set up a custom MongoEntityInformation to reflect this scenario. Extended MappingMongoEntityInformation to allow manually defining a custom collection name. EntityInformationCreator was extended accordingly and MongoQueryMethod now sets up the EntityInformation accordingly.pull/1/head
5 changed files with 177 additions and 21 deletions
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* 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.mongodb.repository; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
import static org.mockito.Mockito.*; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.runners.MockitoJUnitRunner; |
||||
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
||||
|
||||
/** |
||||
* Unit tests for {@link MappingMongoEntityInformation}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class MappingMongoEntityInformationUnitTests { |
||||
|
||||
@Mock |
||||
MongoPersistentEntity<Person> info; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
when(info.getType()).thenReturn(Person.class); |
||||
when(info.getCollection()).thenReturn("Person"); |
||||
} |
||||
|
||||
@Test |
||||
public void usesEntityCollectionIfNoCustomOneGiven() { |
||||
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info); |
||||
assertThat(information.getCollectionName(), is("Person")); |
||||
} |
||||
|
||||
@Test |
||||
public void usesCustomCollectionIfGiven() { |
||||
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info, "foobar"); |
||||
assertThat(information.getCollectionName(), is("foobar")); |
||||
} |
||||
} |
||||
@ -0,0 +1,81 @@
@@ -0,0 +1,81 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* 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.mongodb.repository; |
||||
|
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.hamcrest.Matchers.*; |
||||
|
||||
import java.lang.reflect.Method; |
||||
import java.util.List; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; |
||||
import org.springframework.data.mongodb.repository.MongoRepositoryFactoryBean.EntityInformationCreator; |
||||
import org.springframework.data.repository.Repository; |
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; |
||||
|
||||
/** |
||||
* Unit test for {@link MongoQueryMethod}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class MongoQueryMethodUnitTests { |
||||
|
||||
EntityInformationCreator creator; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
MongoMappingContext context = new MongoMappingContext(); |
||||
creator = new MongoRepositoryFactoryBean.EntityInformationCreator(context); |
||||
} |
||||
|
||||
@Test |
||||
public void detectsCollectionFromRepoTypeIfReturnTypeNotAssignable() throws Exception { |
||||
|
||||
Method method = SampleRepository.class.getMethod("method"); |
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator); |
||||
MongoEntityInformation<?,?> entityInformation = queryMethod.getEntityInformation(); |
||||
|
||||
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Address.class))); |
||||
assertThat(entityInformation.getCollectionName(), is("contact")); |
||||
} |
||||
|
||||
@Test |
||||
public void detectsCollectionFromReturnTypeIfReturnTypeAssignable() throws Exception { |
||||
|
||||
Method method = SampleRepository2.class.getMethod("method"); |
||||
|
||||
MongoQueryMethod queryMethod = new MongoQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), creator); |
||||
MongoEntityInformation<?,?> entityInformation = queryMethod.getEntityInformation(); |
||||
|
||||
assertThat(entityInformation.getJavaType(), is(typeCompatibleWith(Person.class))); |
||||
assertThat(entityInformation.getCollectionName(), is("person")); |
||||
} |
||||
|
||||
|
||||
interface SampleRepository extends Repository<Contact, Long> { |
||||
|
||||
List<Address> method(); |
||||
} |
||||
|
||||
interface SampleRepository2 extends Repository<Contact, Long> { |
||||
|
||||
List<Person> method(); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue