Elffuss BraidHub

kikocis/java-driver public

Handle null enum fields in object mapper (JAVA-487).
Changeset1eb65a0596f62a60883e1acc11b6965c7f2d4ca5d12f6041cbbebeb460072888
Authorolim7t <omichallat+github@gmail.com> · 2014-10-07 11:42 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-07 11:42 +02:00
Parents91df7d5540af

Browse the files at this changeset

3 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -15,6 +15,7 @@
1515 - [improvement] Throw an exception when DCAwareRoundRobinPolicy is built with
1616 an explicit but null or empty local datacenter (JAVA-451)
1717 - [improvement] Add backwards-compatible DataType.serialize methods (JAVA-469)
18+- [bug] Handle null enum fields in object mapper (JAVA-487)
1819
1920 Merged from 2.0 branch:
2021
modified driver-mapping/src/main/java/com/datastax/driver/mapping/ReflectionMapper.java +2 −2
@@ -119,9 +119,9 @@
119119 Object value = super.getValue(entity);
120120 switch (enumType) {
121121 case STRING:
122- return value.toString();
122+ return (value == null) ? null : value.toString();
123123 case ORDINAL:
124- return ((Enum)value).ordinal();
124+ return (value == null) ? null : ((Enum)value).ordinal();
125125 }
126126 throw new AssertionError();
127127 }
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperEnumTest.java +115 −0
@@ -0,0 +1,115 @@
1+package com.datastax.driver.mapping;
2+
3+import java.util.Collection;
4+
5+import com.datastax.driver.mapping.annotations.Enumerated;
6+import com.datastax.driver.mapping.annotations.PartitionKey;
7+import com.datastax.driver.mapping.annotations.Table;
8+import com.google.common.collect.Lists;
9+import org.testng.annotations.Test;
10+
11+import static org.junit.Assert.assertNull;
12+
13+import com.datastax.driver.core.CCMBridge;
14+
15+/**
16+ * Covers handling of enum fields by the mapper.
17+ */
18+public class MapperEnumTest extends CCMBridge.PerClassSingleNodeCluster {
19+
20+ @Override
21+ protected Collection<String> getTableDefinitions() {
22+ return Lists.newArrayList("CREATE TABLE asOrdinal (k int primary key, v int)",
23+ "CREATE TABLE asString (k int primary key, v text)");
24+ }
25+
26+ public enum Enum {
27+ FOO, BAR
28+ }
29+
30+ @Test(groups = "short")
31+ public void should_handle_null_enum_as_ordinal() {
32+ Mapper<AsOrdinal> mapper = new MappingManager(session).mapper(AsOrdinal.class);
33+
34+ mapper.save(new AsOrdinal(1, null));
35+ AsOrdinal o = mapper.get(1);
36+
37+ assertNull(o.getV());
38+ }
39+
40+ @Test(groups = "short")
41+ public void should_handle_null_enum_as_string() {
42+ Mapper<AsString> mapper = new MappingManager(session).mapper(AsString.class);
43+
44+ mapper.save(new AsString(1, null));
45+ AsString o = mapper.get(1);
46+
47+ assertNull(o.getV());
48+ }
49+
50+ @Table(name = "asOrdinal")
51+ public static class AsOrdinal {
52+ @PartitionKey
53+ private int k;
54+
55+ @Enumerated(EnumType.ORDINAL)
56+ private Enum v;
57+
58+ public AsOrdinal(Integer k, Enum v) {
59+ this.k = k;
60+ this.v = v;
61+ }
62+
63+ public AsOrdinal() {
64+ }
65+
66+ public int getK() {
67+ return k;
68+ }
69+
70+ public void setK(int k) {
71+ this.k = k;
72+ }
73+
74+ public Enum getV() {
75+ return v;
76+ }
77+
78+ public void setV(Enum v) {
79+ this.v = v;
80+ }
81+ }
82+
83+ @Table(name = "asString")
84+ public static class AsString {
85+ @PartitionKey
86+ private int k;
87+
88+ @Enumerated(EnumType.STRING)
89+ private Enum v;
90+
91+ public AsString(Integer k, Enum v) {
92+ this.k = k;
93+ this.v = v;
94+ }
95+
96+ public AsString() {
97+ }
98+
99+ public int getK() {
100+ return k;
101+ }
102+
103+ public void setK(int k) {
104+ this.k = k;
105+ }
106+
107+ public Enum getV() {
108+ return v;
109+ }
110+
111+ public void setV(Enum v) {
112+ this.v = v;
113+ }
114+ }
115+}