Elffuss BraidHub

kikocis/java-driver public

Merge pull request #284 from datastax/java575

Authorize Null parameter in Accessor method (JAVA-575).
Changeseta5e878a53851cc3b92725a797c9e73b78a1e581f0048d7416237a9acc50793a1
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 11:50 -08:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 11:50 -08:00
Parents8dbbb33ac272, e02defd2efe5

Browse the files at this changeset

5 files changed

modified driver-core/CHANGELOG.rst +2 −0
@@ -4,6 +4,8 @@
44 2.1.5:
55 ------
66
7+- [bug] Authorize Null parameter in Accessor method (JAVA-575)
8+
79 Merged from 2.0 branch:
810
911 - [new feature] Add AddressTranslater for EC2 multi-region deployment (JAVA-518)
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +13 −4
@@ -95,7 +95,7 @@
9595 }
9696
9797 @SuppressWarnings("rawtypes")
98- private void mapType(MappingManager manager, Class<?> fullReturnType, Type type) {
98+ private void mapType(MappingManager manager, Class<?> fullReturnType, Type type) {
9999
100100 if (type instanceof ParameterizedType) {
101101 ParameterizedType pt = (ParameterizedType)type;
@@ -167,9 +167,14 @@
167167 }
168168
169169 void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) {
170- if (arg != null) {
171- if (paramName == null)
170+ if (paramName == null) {
171+ if (arg == null)
172+ boundStatement.setToNull(paramIdx);
173+ else
172174 boundStatement.setBytesUnsafe(paramIdx, DataType.serializeValue(arg, protocolVersion));
175+ } else {
176+ if (arg == null)
177+ boundStatement.setToNull(paramName);
173178 else
174179 boundStatement.setBytesUnsafe(paramName, DataType.serializeValue(arg, protocolVersion));
175180 }
@@ -188,7 +193,8 @@
188193 void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) {
189194 @SuppressWarnings("unchecked")
190195 V entity = (V) arg;
191- super.setValue(boundStatement, udtMapper.toUDT(entity), protocolVersion);
196+ UDTValue udtArg = arg != null ? udtMapper.toUDT(entity) : null;
197+ super.setValue(boundStatement, udtArg, protocolVersion);
192198 }
193199 }
194200
@@ -258,6 +264,9 @@
258264
259265 @SuppressWarnings("rawtypes")
260266 private Object convert(Object arg) {
267+ if(arg == null)
268+ return arg;
269+
261270 switch (enumType) {
262271 case STRING:
263272 return arg.toString();
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorTest.java +37 −2
@@ -9,13 +9,16 @@
99
1010 import com.datastax.driver.core.CCMBridge;
1111 import com.datastax.driver.core.ResultSet;
12+import com.datastax.driver.core.Row;
1213 import com.datastax.driver.mapping.annotations.Accessor;
14+import com.datastax.driver.mapping.annotations.Param;
1315 import com.datastax.driver.mapping.annotations.Query;
1416
1517 public class MapperAccessorTest extends CCMBridge.PerClassSingleNodeCluster {
1618
17- @Override protected Collection<String> getTableDefinitions() {
18- return Lists.newArrayList("CREATE TABLE foo (i int primary key)");
19+ @Override
20+ protected Collection<String> getTableDefinitions() {
21+ return Lists.newArrayList("CREATE TABLE foo (k int primary key, v text)");
1922 }
2023
2124 @Test(groups = "short")
@@ -36,9 +39,41 @@
3639 assertThat(accessor.hashCode()).isEqualTo(System.identityHashCode(accessor));
3740 }
3841
42+ @Test(groups = "short")
43+ public void should_allow_null_argument_in_accessor_when_set_by_name() {
44+ FooAccessor accessor = new MappingManager(session)
45+ .createAccessor(FooAccessor.class);
46+
47+ accessor.insert(1, null);
48+ Row row = session.execute("select * from foo where k = 1").one();
49+
50+ assertThat(row.getString("v")).isNull();
51+ }
52+
53+ @Test(groups = "short")
54+ public void should_allow_null_argument_in_accessor_when_set_by_index() {
55+ FooAccessor accessor = new MappingManager(session)
56+ .createAccessor(FooAccessor.class);
57+
58+ accessor.insertArg(1, null);
59+
60+ Row row = session.execute("select * from foo where k = 1").one();
61+
62+ assertThat(row.getString("v")).isNull();
63+ }
64+
3965 @Accessor
4066 public interface SystemAccessor {
4167 @Query("select release_version from system.local")
4268 ResultSet getCassandraVersion();
4369 }
70+
71+ @Accessor
72+ public interface FooAccessor {
73+ @Query("insert into foo (k, v) values (:k, :v)")
74+ ResultSet insert(@Param("k") int k, @Param("v") String v);
75+
76+ @Query("insert into foo (k, v) values (?, ?)")
77+ ResultSet insertArg(int k, String v);
78+ }
4479 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +4 −0
@@ -372,5 +372,9 @@
372372 userAccessor.updateNameAndGender("Paule", User.Gender.FEMALE, u1.getUserId());
373373 Mapper<User> userMapper = manager.mapper(User.class);
374374 assertEquals(userMapper.get(u1.getUserId()).getGender(), User.Gender.FEMALE);
375+
376+ // Test that an enum value can be unassigned through an accessor (set to null).
377+ userAccessor.updateNameAndGender("Paule", null, u1.getUserId());
378+ assertEquals(userMapper.get(u1.getUserId()).getGender(), null);
375379 }
376380 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +28 −2
@@ -17,16 +17,20 @@
1717
1818 import java.util.*;
1919
20+import com.google.common.collect.Maps;
2021 import org.testng.annotations.BeforeMethod;
2122
2223 import com.google.common.base.Objects;
2324 import org.testng.annotations.Test;
25+
26+import static org.assertj.core.api.Assertions.assertThat;
2427 import static org.testng.Assert.assertEquals;
2528 import static org.testng.Assert.assertNull;
2629 import static org.testng.Assert.assertTrue;
2730
2831 import com.datastax.driver.core.CCMBridge;
2932 import com.datastax.driver.core.ResultSet;
33+import com.datastax.driver.core.Row;
3034 import com.datastax.driver.core.utils.UUIDs;
3135
3236 import com.datastax.driver.mapping.annotations.*;
@@ -58,7 +62,7 @@
5862
5963 @Column(name = "other_addresses")
6064 @FrozenValue
61- private Map<String, Address> otherAddresses;
65+ private Map<String, Address> otherAddresses = Maps.newHashMap();
6266
6367 public User() {
6468 }
@@ -201,6 +205,9 @@
201205 @Query("UPDATE ks.users SET other_addresses[:name]=:address WHERE user_id=:id")
202206 ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address);
203207
208+ @Query("UPDATE ks.users SET other_addresses=:addresses where user_id=:id")
209+ ResultSet setOtherAddresses(@Param("id") UUID id, @Param("addresses") Map<String, Address> addresses);
210+
204211 @Query("SELECT * FROM ks.users")
205212 public Result<User> getAll();
206213 }
@@ -242,9 +249,28 @@
242249 User u2 = userAccessor.getOne(u1.getUserId());
243250 assertEquals(workAddress, u2.getOtherAddresses().get("work"));
244251
252+ // Adding a null value should remove it from the list.
253+ userAccessor.addAddress(u1.getUserId(), "work", null);
254+ User u3 = userAccessor.getOne(u1.getUserId());
255+ assertThat(u3.getOtherAddresses()).doesNotContainKey("work");
256+
257+ // Add a bunch of other addresses
258+ Map<String, Address> otherAddresses = Maps.newHashMap();
259+ otherAddresses.put("work", workAddress);
260+ otherAddresses.put("cabin", new Address("42 Middle of Nowhere", "Lake of the Woods", 49553, "8675309"));
261+
262+ userAccessor.setOtherAddresses(u1.getUserId(), otherAddresses);
263+ User u4 = userAccessor.getOne(u1.getUserId());
264+ assertThat(u4.getOtherAddresses()).isEqualTo(otherAddresses);
265+
266+ // Nullify other addresses
267+ userAccessor.setOtherAddresses(u1.getUserId(), null);
268+ User u5 = userAccessor.getOne(u1.getUserId());
269+ assertThat(u5.getOtherAddresses()).isEmpty();
270+
245271 // No argument call
246272 Result<User> u = userAccessor.getAll();
247- assertEquals(u.one(), u2);
273+ assertEquals(u.one(), u5);
248274 assertTrue(u.isExhausted());
249275 }
250276 }