kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | a5e878a53851cc3b92725a797c9e73b78a1e581f0048d7416237a9acc50793a1 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 11:50 -08:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-03-05 11:50 -08:00 |
| Parents | 8dbbb33ac272, e02defd2efe5 |
5 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -4,6 +4,8 @@ | ||
| 4 | 4 | 2.1.5: |
| 5 | 5 | ------ |
| 6 | 6 | |
| 7 | +- [bug] Authorize Null parameter in Accessor method (JAVA-575) | |
| 8 | + | |
| 7 | 9 | Merged from 2.0 branch: |
| 8 | 10 | |
| 9 | 11 | - [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 @@ | ||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | @SuppressWarnings("rawtypes") |
| 98 | - private void mapType(MappingManager manager, Class<?> fullReturnType, Type type) { | |
| 98 | + private void mapType(MappingManager manager, Class<?> fullReturnType, Type type) { | |
| 99 | 99 | |
| 100 | 100 | if (type instanceof ParameterizedType) { |
| 101 | 101 | ParameterizedType pt = (ParameterizedType)type; |
| @@ -167,9 +167,14 @@ | ||
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | 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 | |
| 172 | 174 | boundStatement.setBytesUnsafe(paramIdx, DataType.serializeValue(arg, protocolVersion)); |
| 175 | + } else { | |
| 176 | + if (arg == null) | |
| 177 | + boundStatement.setToNull(paramName); | |
| 173 | 178 | else |
| 174 | 179 | boundStatement.setBytesUnsafe(paramName, DataType.serializeValue(arg, protocolVersion)); |
| 175 | 180 | } |
| @@ -188,7 +193,8 @@ | ||
| 188 | 193 | void setValue(BoundStatement boundStatement, Object arg, ProtocolVersion protocolVersion) { |
| 189 | 194 | @SuppressWarnings("unchecked") |
| 190 | 195 | 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); | |
| 192 | 198 | } |
| 193 | 199 | } |
| 194 | 200 | |
| @@ -258,6 +264,9 @@ | ||
| 258 | 264 | |
| 259 | 265 | @SuppressWarnings("rawtypes") |
| 260 | 266 | private Object convert(Object arg) { |
| 267 | + if(arg == null) | |
| 268 | + return arg; | |
| 269 | + | |
| 261 | 270 | switch (enumType) { |
| 262 | 271 | case STRING: |
| 263 | 272 | return arg.toString(); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperAccessorTest.java +37 −2
| @@ -9,13 +9,16 @@ | ||
| 9 | 9 | |
| 10 | 10 | import com.datastax.driver.core.CCMBridge; |
| 11 | 11 | import com.datastax.driver.core.ResultSet; |
| 12 | +import com.datastax.driver.core.Row; | |
| 12 | 13 | import com.datastax.driver.mapping.annotations.Accessor; |
| 14 | +import com.datastax.driver.mapping.annotations.Param; | |
| 13 | 15 | import com.datastax.driver.mapping.annotations.Query; |
| 14 | 16 | |
| 15 | 17 | public class MapperAccessorTest extends CCMBridge.PerClassSingleNodeCluster { |
| 16 | 18 | |
| 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)"); | |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | 24 | @Test(groups = "short") |
| @@ -36,9 +39,41 @@ | ||
| 36 | 39 | assertThat(accessor.hashCode()).isEqualTo(System.identityHashCode(accessor)); |
| 37 | 40 | } |
| 38 | 41 | |
| 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 | + | |
| 39 | 65 | @Accessor |
| 40 | 66 | public interface SystemAccessor { |
| 41 | 67 | @Query("select release_version from system.local") |
| 42 | 68 | ResultSet getCassandraVersion(); |
| 43 | 69 | } |
| 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 | + } | |
| 44 | 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 | } |