forked from MorphiaOrg/morphia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for ensureIndexes() logic (MorphiaOrg#1211)
* added integration tests to confirm the issue MorphiaOrg#1175 * collect indexes from subclasses only if field is an interface or abstract class * fixed errors reported by checkstyle
- Loading branch information
1 parent
0c78bd5
commit 0d136d8
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
morphia/src/test/java/org/mongodb/morphia/issue1175/EnsureIndexesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package org.mongodb.morphia.issue1175; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
import static org.mongodb.morphia.testutil.IndexMatcher.doesNotHaveIndexNamed; | ||
import static org.mongodb.morphia.testutil.IndexMatcher.hasIndexNamed; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
import org.mongodb.morphia.TestBase; | ||
import org.mongodb.morphia.annotations.Embedded; | ||
import org.mongodb.morphia.annotations.Entity; | ||
import org.mongodb.morphia.annotations.Id; | ||
import org.mongodb.morphia.annotations.Indexed; | ||
import org.mongodb.morphia.utils.IndexDirection; | ||
|
||
import com.mongodb.DBObject; | ||
|
||
public class EnsureIndexesTest extends TestBase { | ||
@Test | ||
public final void ensureIndexesFromSuperclasses() { | ||
// given | ||
getMorphia().map(Person.class, Employee.class, Company.class); | ||
|
||
// when | ||
getAds().ensureIndexes(); | ||
|
||
// then | ||
List<DBObject> indexInfo = getDs().getCollection(Company.class).getIndexInfo(); | ||
assertEquals(4, indexInfo.size()); | ||
assertThat(indexInfo, hasIndexNamed("_id_")); | ||
assertThat(indexInfo, hasIndexNamed("employees.birthday_-1")); | ||
assertThat(indexInfo, hasIndexNamed("employees.fullName_1")); | ||
assertThat(indexInfo, hasIndexNamed("employees.employeeId_1")); | ||
} | ||
|
||
@Test | ||
public final void ensureIndexesWithoutSubclasses() { | ||
// given | ||
getMorphia().map(Person.class, Employee.class, Contract.class); | ||
|
||
// when | ||
getAds().ensureIndexes(); | ||
|
||
// then | ||
List<DBObject> indexInfo = getDs().getCollection(Contract.class).getIndexInfo(); | ||
assertEquals(3, indexInfo.size()); | ||
assertThat(indexInfo, hasIndexNamed("_id_")); | ||
assertThat(indexInfo, hasIndexNamed("person.birthday_-1")); | ||
assertThat(indexInfo, hasIndexNamed("person.fullName_1")); | ||
assertThat(indexInfo, doesNotHaveIndexNamed("person.employeeId_1")); | ||
} | ||
|
||
@Embedded | ||
public static class LivingBeing { | ||
@Indexed(IndexDirection.DESC) | ||
private Date birthday; | ||
} | ||
|
||
@Embedded | ||
public static class Person extends LivingBeing { | ||
@Indexed | ||
private String fullName; | ||
} | ||
|
||
@Embedded | ||
public static class Employee extends Person { | ||
@Indexed | ||
private Long employeeId; | ||
private String title; | ||
} | ||
|
||
@Entity | ||
public static class Contract { | ||
@Id | ||
private Long contractId; | ||
private Long companyId; | ||
@Embedded | ||
private Person person; | ||
} | ||
|
||
@Entity | ||
public static class Company { | ||
@Id | ||
private Long companyId; | ||
@Embedded | ||
private List<Employee> employees; | ||
} | ||
} |