Browse Source
Disabled index creation for repository query methods by default. Added property on MongoRepositoryFactoryBean to enable index creation and expose that via 'create-query-indexes' attribute on the namespace.pull/1/head
7 changed files with 240 additions and 2 deletions
@ -0,0 +1,86 @@
@@ -0,0 +1,86 @@
|
||||
/* |
||||
* Copyright 2011 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.document.mongodb.repository; |
||||
|
||||
import static org.mockito.Mockito.*; |
||||
import static org.junit.Assert.*; |
||||
import static org.hamcrest.CoreMatchers.*; |
||||
|
||||
import java.util.List; |
||||
|
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.mockito.Mock; |
||||
import org.mockito.runners.MockitoJUnitRunner; |
||||
import org.springframework.data.document.mongodb.MongoTemplate; |
||||
import org.springframework.data.document.mongodb.convert.MongoConverter; |
||||
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean.IndexEnsuringQueryCreationListener; |
||||
import org.springframework.data.mapping.context.MappingContext; |
||||
import org.springframework.data.repository.core.support.QueryCreationListener; |
||||
import org.springframework.data.repository.core.support.RepositoryFactorySupport; |
||||
import org.springframework.test.util.ReflectionTestUtils; |
||||
|
||||
/** |
||||
* Unit tests for {@link MongoRepositoryFactoryBean}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
@RunWith(MockitoJUnitRunner.class) |
||||
public class MongoRepositoryFactoryBeanUnitTests { |
||||
|
||||
@Mock |
||||
MongoTemplate template; |
||||
|
||||
@Mock |
||||
MongoConverter converter; |
||||
|
||||
@Mock |
||||
@SuppressWarnings("rawtypes") |
||||
MappingContext context; |
||||
|
||||
@Test |
||||
@SuppressWarnings("rawtypes") |
||||
public void addsIndexEnsuringQueryCreationListenerIfConfigured() { |
||||
|
||||
MongoRepositoryFactoryBean factory = new MongoRepositoryFactoryBean(); |
||||
factory.setCreateIndexesForQueryMethods(true); |
||||
|
||||
List<QueryCreationListener<?>> listeners = getListenersFromFactory(factory); |
||||
assertThat(listeners.size(), is(1)); |
||||
assertThat(listeners.get(0), is(instanceOf(IndexEnsuringQueryCreationListener.class))); |
||||
} |
||||
|
||||
@Test |
||||
@SuppressWarnings("rawtypes") |
||||
public void doesNotAddIndexEnsuringQueryCreationListenerByDefault() { |
||||
|
||||
List<QueryCreationListener<?>> listeners = getListenersFromFactory(new MongoRepositoryFactoryBean()); |
||||
assertThat(listeners.isEmpty(), is(true)); |
||||
} |
||||
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" }) |
||||
private List<QueryCreationListener<?>> getListenersFromFactory(MongoRepositoryFactoryBean factoryBean) { |
||||
|
||||
when(template.getConverter()).thenReturn(converter); |
||||
when(converter.getMappingContext()).thenReturn(context); |
||||
|
||||
factoryBean.setTemplate(template); |
||||
factoryBean.afterPropertiesSet(); |
||||
|
||||
RepositoryFactorySupport factory = factoryBean.createRepositoryFactory(); |
||||
return (List<QueryCreationListener<?>>) ReflectionTestUtils.getField(factory, "queryPostProcessors"); |
||||
} |
||||
} |
||||
@ -0,0 +1,78 @@
@@ -0,0 +1,78 @@
|
||||
/* |
||||
* Copyright 2011 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.document.mongodb.repository; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.hamcrest.Matchers.*; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import org.junit.After; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.dao.DataAccessException; |
||||
import org.springframework.data.document.mongodb.CollectionCallback; |
||||
import org.springframework.data.document.mongodb.MongoOperations; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
|
||||
import com.mongodb.DBCollection; |
||||
import com.mongodb.DBObject; |
||||
import com.mongodb.MongoException; |
||||
|
||||
/** |
||||
* Integration test for index creation for query methods. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@ContextConfiguration |
||||
public class RepositoryIndexCreationIntegrationTests { |
||||
|
||||
@Autowired |
||||
MongoOperations operations; |
||||
|
||||
@After |
||||
public void tearDown() { |
||||
operations.dropCollection(Person.class); |
||||
} |
||||
|
||||
@Test |
||||
public void testname() { |
||||
operations.execute(Person.class, new CollectionCallback<Void>() { |
||||
|
||||
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
||||
List<DBObject> indexInfo = collection.getIndexInfo(); |
||||
|
||||
assertThat(indexInfo.isEmpty(), is(false)); |
||||
assertThat(indexInfo.size(), is(greaterThan(2))); |
||||
assertThat(getIndexNamesFrom(indexInfo), hasItems("findByLastname", "findByFirstnameNotIn")); |
||||
|
||||
return null; |
||||
} |
||||
}); |
||||
} |
||||
|
||||
private static List<String> getIndexNamesFrom(List<DBObject> indexes) { |
||||
List<String> result = new ArrayList<String>(); |
||||
for (DBObject dbObject : indexes) { |
||||
result.add(dbObject.get("name").toString()); |
||||
} |
||||
return result; |
||||
} |
||||
} |
||||
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:mongo="http://www.springframework.org/schema/data/mongo" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xmlns:util="http://www.springframework.org/schema/util" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd |
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> |
||||
|
||||
<mongo:db-factory dbname="repositories"/> |
||||
<mongo:mapping-converter base-package="org.springframework.data.document.mongodb.repository"/> |
||||
|
||||
<bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"> |
||||
<constructor-arg ref="mongoDbFactory"/> |
||||
<constructor-arg ref="mappingConverter"/> |
||||
</bean> |
||||
|
||||
<mongo:repositories base-package="org.springframework.data.document.mongodb.repository" |
||||
create-query-indexes="true" /> |
||||
|
||||
</beans> |
||||
Loading…
Reference in new issue