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.
mapping tweak: pass the MappedField's "subclass" (terrible name) when…
… decoding from the database fixes MorphiaOrg#753
- Loading branch information
Justin Lee
committed
Apr 20, 2015
1 parent
b0d9fda
commit 5169824
Showing
2 changed files
with
120 additions
and
98 deletions.
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
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 |
---|---|---|
|
@@ -18,20 +18,96 @@ | |
* @author Uwe Schaefer, ([email protected]) | ||
*/ | ||
public class EnumMappingTest extends TestBase { | ||
public static class ContainsEnum { | ||
@Id | ||
private ObjectId id; | ||
private Foo foo = Foo.BAR; | ||
@Test | ||
public void getMapOfEnum() throws Exception { | ||
Class1 entity = new Class1(); | ||
entity.getMap().put("key", Foo.BAR); | ||
getDs().save(entity); | ||
|
||
@PreSave | ||
void testMapping() { | ||
getMorphia().map(Class1.class); | ||
|
||
} | ||
entity = getDs().find(Class1.class).get(); | ||
final Map<String, Foo> map = entity.getMap(); | ||
Foo b = map.get("key"); | ||
Assert.assertNotNull(b); | ||
} | ||
|
||
@Test | ||
public void testCustomer() { | ||
Customer customer = new Customer(); | ||
customer.add(WebTemplateType.CrewContract, new WebTemplate("template #1")); | ||
customer.add(WebTemplateType.CrewContractHeader, new WebTemplate("template #2")); | ||
|
||
getDs().save(customer); | ||
Customer loaded = getDs().get(customer); | ||
Assert.assertEquals(customer.map, loaded.map); | ||
} | ||
|
||
@Test | ||
public void testCustomerWithArrayList() { | ||
getMorphia().getMapper().getOptions().setStoreEmpties(true); | ||
getMorphia().getMapper().getOptions().setStoreNulls(true); | ||
getMorphia().map(CustomerWithArrayList.class); | ||
|
||
CustomerWithArrayList customer = new CustomerWithArrayList(); | ||
|
||
List<WebTemplate> templates1 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #1.1")); | ||
templates1.add(new WebTemplate("template #1.2")); | ||
customer.add(WebTemplateType.CrewContract, templates1); | ||
|
||
List<WebTemplate> templates2 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #2.1")); | ||
templates1.add(new WebTemplate("template #2.2")); | ||
customer.add(WebTemplateType.CrewContractHeader, templates2); | ||
|
||
getDs().save(customer); | ||
CustomerWithArrayList loaded = getDs().get(customer); | ||
|
||
Assert.assertEquals(customer.mapWithArrayList, loaded.mapWithArrayList); | ||
} | ||
|
||
@Test | ||
public void testCustomerWithList() { | ||
getMorphia().getMapper().getOptions().setStoreEmpties(true); | ||
getMorphia().getMapper().getOptions().setStoreNulls(true); | ||
getMorphia().map(CustomerWithArrayList.class); | ||
CustomerWithList customer = new CustomerWithList(); | ||
|
||
List<WebTemplate> templates1 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #1.1")); | ||
templates1.add(new WebTemplate("template #1.2")); | ||
customer.add(WebTemplateType.CrewContract, templates1); | ||
|
||
List<WebTemplate> templates2 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #2.1")); | ||
templates1.add(new WebTemplate("template #2.2")); | ||
customer.add(WebTemplateType.CrewContractHeader, templates2); | ||
|
||
getDs().save(customer); | ||
CustomerWithList loaded = getDs().get(customer); | ||
|
||
Assert.assertEquals(customer.mapWithList, loaded.mapWithList); | ||
} | ||
|
||
@Test | ||
public void testEnumMapping() throws Exception { | ||
getDs().getDB().dropDatabase(); | ||
|
||
getMorphia().map(ContainsEnum.class); | ||
|
||
getDs().save(new ContainsEnum()); | ||
Assert.assertEquals(1, getDs().createQuery(ContainsEnum.class).field("foo").equal(Foo.BAR) | ||
.countAll()); | ||
Assert.assertEquals(1, getDs().createQuery(ContainsEnum.class).filter("foo", Foo.BAR) | ||
.countAll()); | ||
Assert.assertEquals(1, | ||
getDs().createQuery(ContainsEnum.class).disableValidation().filter("foo", Foo.BAR) | ||
.countAll()); | ||
} | ||
|
||
enum Foo { | ||
BAR() { | ||
}, | ||
BAR, | ||
BAZ | ||
} | ||
|
||
|
@@ -51,6 +127,28 @@ public String getText() { | |
|
||
} | ||
|
||
@Entity("user") | ||
public static class Class1 { | ||
@Id | ||
private ObjectId id; | ||
private Map<String, Foo> map = new HashMap<String, Foo>(); | ||
|
||
public Map<String, Foo> getMap() { | ||
return map; | ||
} | ||
} | ||
|
||
public static class ContainsEnum { | ||
@Id | ||
private ObjectId id; | ||
private Foo foo = Foo.BAR; | ||
|
||
@PreSave | ||
void testMapping() { | ||
|
||
} | ||
} | ||
|
||
@Embedded | ||
public static class WebTemplate { | ||
private ObjectId id = new ObjectId(); | ||
|
@@ -64,6 +162,14 @@ public WebTemplate(final String content) { | |
this.content = content; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = id != null ? id.hashCode() : 0; | ||
result = 31 * result + (templateName != null ? templateName.hashCode() : 0); | ||
result = 31 * result + (content != null ? content.hashCode() : 0); | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
|
@@ -88,21 +194,13 @@ public boolean equals(final Object o) { | |
|
||
return true; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = id != null ? id.hashCode() : 0; | ||
result = 31 * result + (templateName != null ? templateName.hashCode() : 0); | ||
result = 31 * result + (content != null ? content.hashCode() : 0); | ||
return result; | ||
} | ||
} | ||
|
||
@Entity(noClassnameStored = true) | ||
public static class Customer { | ||
private final Map<WebTemplateType, WebTemplate> map = new HashMap<WebTemplateType, WebTemplate>(); | ||
@Id | ||
private ObjectId id; | ||
private final Map<WebTemplateType, WebTemplate> map = new HashMap<WebTemplateType, WebTemplate>(); | ||
|
||
public void add(final WebTemplateType type, final WebTemplate template) { | ||
map.put(type, template); | ||
|
@@ -112,101 +210,25 @@ public void add(final WebTemplateType type, final WebTemplate template) { | |
|
||
@Entity(noClassnameStored = true) | ||
public static class CustomerWithList { | ||
private final Map<WebTemplateType, List<WebTemplate>> mapWithList = new HashMap<WebTemplateType, List<WebTemplate>>(); | ||
@Id | ||
private ObjectId id; | ||
|
||
private final Map<WebTemplateType, List<WebTemplate>> mapWithList = new HashMap<WebTemplateType, List<WebTemplate>>(); | ||
|
||
public void add(final WebTemplateType type, final List<WebTemplate> templates) { | ||
mapWithList.put(type, templates); | ||
} | ||
} | ||
|
||
@Entity(noClassnameStored = true) | ||
public static class CustomerWithArrayList { | ||
@Id | ||
private ObjectId id; | ||
|
||
private final Map<WebTemplateType, List<WebTemplate>> mapWithArrayList | ||
= new HashMap<WebTemplateType, List<WebTemplate>>(); | ||
@Id | ||
private ObjectId id; | ||
|
||
public void add(final WebTemplateType type, final List<WebTemplate> templates) { | ||
mapWithArrayList.put(type, templates); | ||
} | ||
} | ||
|
||
@Test | ||
public void testEnumMapping() throws Exception { | ||
getDs().getDB().dropDatabase(); | ||
|
||
getMorphia().map(ContainsEnum.class); | ||
|
||
getDs().save(new ContainsEnum()); | ||
Assert.assertEquals(1, getDs().createQuery(ContainsEnum.class).field("foo").equal(Foo.BAR) | ||
.countAll()); | ||
Assert.assertEquals(1, getDs().createQuery(ContainsEnum.class).filter("foo", Foo.BAR) | ||
.countAll()); | ||
Assert.assertEquals(1, | ||
getDs().createQuery(ContainsEnum.class).disableValidation().filter("foo", Foo.BAR) | ||
.countAll()); | ||
} | ||
|
||
@Test | ||
public void testCustomer() { | ||
Customer customer = new Customer(); | ||
customer.add(WebTemplateType.CrewContract, new WebTemplate("template #1")); | ||
customer.add(WebTemplateType.CrewContractHeader, new WebTemplate("template #2")); | ||
|
||
getDs().save(customer); | ||
Customer loaded = getDs().get(customer); | ||
Assert.assertEquals(customer.map, loaded.map); | ||
} | ||
|
||
@Test | ||
public void testCustomerWithList() { | ||
getMorphia().getMapper().getOptions().setStoreEmpties(true); | ||
getMorphia().getMapper().getOptions().setStoreNulls(true); | ||
getMorphia().map(CustomerWithArrayList.class); | ||
CustomerWithList customer = new CustomerWithList(); | ||
|
||
List<WebTemplate> templates1 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #1.1")); | ||
templates1.add(new WebTemplate("template #1.2")); | ||
customer.add(WebTemplateType.CrewContract, templates1); | ||
|
||
List<WebTemplate> templates2 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #2.1")); | ||
templates1.add(new WebTemplate("template #2.2")); | ||
customer.add(WebTemplateType.CrewContractHeader, templates2); | ||
|
||
getDs().save(customer); | ||
CustomerWithList loaded = getDs().get(customer); | ||
|
||
Assert.assertEquals(customer.mapWithList, loaded.mapWithList); | ||
} | ||
|
||
@Test | ||
public void testCustomerWithArrayList() { | ||
getMorphia().getMapper().getOptions().setStoreEmpties(true); | ||
getMorphia().getMapper().getOptions().setStoreNulls(true); | ||
getMorphia().map(CustomerWithArrayList.class); | ||
|
||
CustomerWithArrayList customer = new CustomerWithArrayList(); | ||
|
||
List<WebTemplate> templates1 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #1.1")); | ||
templates1.add(new WebTemplate("template #1.2")); | ||
customer.add(WebTemplateType.CrewContract, templates1); | ||
|
||
List<WebTemplate> templates2 = new ArrayList<WebTemplate>(); | ||
templates1.add(new WebTemplate("template #2.1")); | ||
templates1.add(new WebTemplate("template #2.2")); | ||
customer.add(WebTemplateType.CrewContractHeader, templates2); | ||
|
||
getDs().save(customer); | ||
CustomerWithArrayList loaded = getDs().get(customer); | ||
|
||
Assert.assertEquals(customer.mapWithArrayList, loaded.mapWithArrayList); | ||
} | ||
|
||
} |