kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | e02defd2efe5b06648daa0e7db9b79eb519ced883ffa9fafd01c85c7706c3b9b |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2015-03-05 12:57 -06:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-05 11:35 -08:00 |
| Parents | 27f1861b6e82 |
4 files changed
modified driver-mapping/src/main/java/com/datastax/driver/mapping/MethodMapper.java +5 −1
| @@ -193,7 +193,8 @@ | ||
| 193 | 193 | void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) { |
| 194 | 194 | @SuppressWarnings("unchecked") |
| 195 | 195 | V entity = (V) arg; |
| 196 | - super.setValue(boundStatement, udtMapper.toUDT(entity), protocolVersion); | |
| 196 | + UDTValue udtArg = arg != null ? udtMapper.toUDT(entity) : null; | |
| 197 | + super.setValue(boundStatement, udtArg, protocolVersion); | |
| 197 | 198 | } |
| 198 | 199 | } |
| 199 | 200 | |
| @@ -263,6 +264,9 @@ | ||
| 263 | 264 | |
| 264 | 265 | @SuppressWarnings("rawtypes") |
| 265 | 266 | private Object convert(Object arg) { |
| 267 | + if(arg == null) | |
| 268 | + return arg; | |
| 269 | + | |
| 266 | 270 | switch (enumType) { |
| 267 | 271 | case STRING: |
| 268 | 272 | return arg.toString(); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorTest.java +16 −1
| @@ -40,7 +40,7 @@ | ||
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | @Test(groups = "short") |
| 43 | - public void should_allow_null_parameter_in_accessor() { | |
| 43 | + public void should_allow_null_argument_in_accessor_when_set_by_name() { | |
| 44 | 44 | FooAccessor accessor = new MappingManager(session) |
| 45 | 45 | .createAccessor(FooAccessor.class); |
| 46 | 46 | |
| @@ -50,6 +50,18 @@ | ||
| 50 | 50 | assertThat(row.getString("v")).isNull(); |
| 51 | 51 | } |
| 52 | 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 | + | |
| 53 | 65 | @Accessor |
| 54 | 66 | public interface SystemAccessor { |
| 55 | 67 | @Query("select release_version from system.local") |
| @@ -60,5 +72,8 @@ | ||
| 60 | 72 | public interface FooAccessor { |
| 61 | 73 | @Query("insert into foo (k, v) values (:k, :v)") |
| 62 | 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); | |
| 63 | 78 | } |
| 64 | 79 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperTest.java +4 −0
| @@ -372,5 +372,9 @@ | ||
| 372 | 372 | userAccessor.updateNameAndGender("Paule", User.Gender.FEMALE, u1.getUserId()); |
| 373 | 373 | Mapper<User> userMapper = manager.mapper(User.class); |
| 374 | 374 | 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); | |
| 375 | 379 | } |
| 376 | 380 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +28 −2
| @@ -17,16 +17,20 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.util.*; |
| 19 | 19 | |
| 20 | +import com.google.common.collect.Maps; | |
| 20 | 21 | import org.testng.annotations.BeforeMethod; |
| 21 | 22 | |
| 22 | 23 | import com.google.common.base.Objects; |
| 23 | 24 | import org.testng.annotations.Test; |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 24 | 27 | import static org.testng.Assert.assertEquals; |
| 25 | 28 | import static org.testng.Assert.assertNull; |
| 26 | 29 | import static org.testng.Assert.assertTrue; |
| 27 | 30 | |
| 28 | 31 | import com.datastax.driver.core.CCMBridge; |
| 29 | 32 | import com.datastax.driver.core.ResultSet; |
| 33 | +import com.datastax.driver.core.Row; | |
| 30 | 34 | import com.datastax.driver.core.utils.UUIDs; |
| 31 | 35 | |
| 32 | 36 | import com.datastax.driver.mapping.annotations.*; |
| @@ -58,7 +62,7 @@ | ||
| 58 | 62 | |
| 59 | 63 | @Column(name = "other_addresses") |
| 60 | 64 | @FrozenValue |
| 61 | - private Map<String, Address> otherAddresses; | |
| 65 | + private Map<String, Address> otherAddresses = Maps.newHashMap(); | |
| 62 | 66 | |
| 63 | 67 | public User() { |
| 64 | 68 | } |
| @@ -201,6 +205,9 @@ | ||
| 201 | 205 | @Query("UPDATE ks.users SET other_addresses[:name]=:address WHERE user_id=:id") |
| 202 | 206 | ResultSet addAddress(@Param("id") UUID id, @Param("name") String addressName, @Param("address") Address address); |
| 203 | 207 | |
| 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 | + | |
| 204 | 211 | @Query("SELECT * FROM ks.users") |
| 205 | 212 | public Result<User> getAll(); |
| 206 | 213 | } |
| @@ -242,9 +249,28 @@ | ||
| 242 | 249 | User u2 = userAccessor.getOne(u1.getUserId()); |
| 243 | 250 | assertEquals(workAddress, u2.getOtherAddresses().get("work")); |
| 244 | 251 | |
| 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 | + | |
| 245 | 271 | // No argument call |
| 246 | 272 | Result<User> u = userAccessor.getAll(); |
| 247 | - assertEquals(u.one(), u2); | |
| 273 | + assertEquals(u.one(), u5); | |
| 248 | 274 | assertTrue(u.isExhausted()); |
| 249 | 275 | } |
| 250 | 276 | } |