From bce6e2c78c93dee5ec5f8e7f4fb5671e238193e8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 27 Dec 2015 12:01:45 +0100 Subject: [PATCH] DATAMONGO-1163 - Polishing. Fixed indentation changes in IndexingIntegrationTests. Separated test cases from each other. Original pull request: #325. --- .../core/index/IndexingIntegrationTests.java | 67 +++++++++++++------ ...ersistentEntityIndexResolverUnitTests.java | 2 + 2 files changed, 47 insertions(+), 22 deletions(-) diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java index 7444ed79f..2b748fc8d 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/IndexingIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2014 by the original author(s). + * Copyright 2011-2015 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. @@ -15,65 +15,88 @@ */ package org.springframework.data.mongodb.core.index; -import com.mongodb.DBCollection; -import com.mongodb.DBObject; -import com.mongodb.MongoException; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import static org.hamcrest.CoreMatchers.*; + import org.junit.After; -import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.dao.DataAccessException; +import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.CollectionCallback; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Field; +import org.springframework.test.annotation.DirtiesContext; 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 tests for index handling. * * @author Oliver Gierke * @author Christoph Strobl + * @author Jordi Llach */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:infrastructure.xml") public class IndexingIntegrationTests { @Autowired MongoOperations operations; - - @After + @Autowired MongoDbFactory mongoDbFactory; + @Autowired ConfigurableApplicationContext context; + + @After public void tearDown() { - operations.dropCollection(IndexedPerson.class); + operations.dropCollection(IndexedPerson.class); } - - /** - * @see DATADOC-237 - * @see DATAMONGO-1163 + + /** + * @see DATAMONGO-237 */ @Test + @DirtiesContext public void createsIndexWithFieldName() { - operations.save(new IndexedPerson()); + + operations.getConverter().getMappingContext().getPersistentEntity(IndexedPerson.class); + assertThat(hasIndex("_firstname", IndexedPerson.class), is(true)); - assertThat(hasIndex("_lastname", IndexedPerson.class), is(true)); } - @Target({ElementType.FIELD}) - @Retention(RetentionPolicy.RUNTIME) - @Indexed - @interface IndexedFieldAnnotation {} - + /** + * @see DATAMONGO-1163 + */ + @Test + @DirtiesContext + public void createsIndexFromMetaAnnotation() { + + operations.getConverter().getMappingContext().getPersistentEntity(IndexedPerson.class); + + assertThat(hasIndex("_lastname", IndexedPerson.class), is(true)); + } + + @Target({ ElementType.FIELD }) + @Retention(RetentionPolicy.RUNTIME) + @Indexed + @interface IndexedFieldAnnotation { + } + @Document class IndexedPerson { @Field("_firstname") @Indexed String firstname; - @Field("_lastname") @IndexedFieldAnnotation String lastname; + @Field("_lastname") @IndexedFieldAnnotation String lastname; } /** @@ -96,4 +119,4 @@ public class IndexingIntegrationTests { } }); } -} \ No newline at end of file +} diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java index 53273fd1a..4768db366 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexResolverUnitTests.java @@ -186,8 +186,10 @@ public class MongoPersistentEntityIndexResolverUnitTests { */ @Test public void resolveIndexDefinitionInMetaAnnotatedFields() { + List indexDefinitions = prepareMappingContextAndResolveIndexForType( IndexOnMetaAnnotatedField.class); + assertThat(indexDefinitions, hasSize(1)); assertThat(indexDefinitions.get(0).getCollection(), equalTo("indexOnMetaAnnotatedField")); assertThat(indexDefinitions.get(0).getIndexOptions(),