Elffuss BraidHub

kikocis/java-driver public

JAVA-965: Improve error handling of non-type 1 UUID used for timeuuid.

This commit also applies the same behavior to AsciiCodec,
when given a non-ASCII string.
Changeset7df6cb642d75ba2dd870860b014f8ad0832aea37cac2a0df9c107154e895ee1e
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-10-22 15:21 -05:00
CommitterAlexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-03 09:07 +01:00
Parents82f0202f1f0c

Browse the files at this changeset

4 files changed

modified changelog/README.md +3 −2
@@ -2,8 +2,9 @@
22
33 ### 3.0.0-beta1 (in progress)
44
5- - [improvement] JAVA-958: Make TableOrView.Order visible.
6- - [improvement] JAVA-968: Update metrics to the latest version.
5+- [improvement] Make TableOrView.Order visible (JAVA-958)
6+- [improvement] Update metrics to the latest version (JAVA-968)
7+- [improvement] Improve error handling for when a non-type 1 UUID is given to bind() on a timeuuid column (JAVA-965)
78
89 ### 3.0.0-alpha4
910
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +12 −8
@@ -860,10 +860,20 @@
860860 }
861861
862862 @Override
863- public boolean accepts(Object value) {
864- return value instanceof String && ASCII_PATTERN.matcher((String)value).matches();
863+ public ByteBuffer serialize(String value, ProtocolVersion protocolVersion) {
864+ if (value != null && !ASCII_PATTERN.matcher(value).matches()) {
865+ throw new InvalidTypeException(String.format("%s is not a valid ASCII String", value));
866+ }
867+ return super.serialize(value, protocolVersion);
865868 }
866869
870+ @Override
871+ public String format(String value) {
872+ if (value != null && !ASCII_PATTERN.matcher(value).matches()) {
873+ throw new InvalidTypeException(String.format("%s is not a valid ASCII String", value));
874+ }
875+ return super.format(value);
876+ }
867877 }
868878
869879 /**
@@ -1768,12 +1778,6 @@
17681778 super(timeuuid());
17691779 }
17701780
1771- @Override
1772- public boolean accepts(Object value) {
1773- // only accept time-based uuids
1774- return super.accepts(value) && ((UUID)value).version() == 1;
1775- }
1776-
17771781 @Override
17781782 public String format(UUID value) {
17791783 if (value == null)
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecAssert.java +13 −3
@@ -83,12 +83,22 @@
8383 public TypeCodecAssert cannotSerialize(Object value) {
8484 if(version == null) fail("ProtocolVersion cannot be null");
8585 try {
86- assertThat(actual.deserialize(actual.serialize(value, version), version))
87- .describedAs(String.format("Codec is not supposed to serialize this value but it actually does: %s", value))
88- .isNotEqualTo(value);
86+ actual.serialize(value, version);
87+ fail("Should not have been able to serialize " + value + " with " + actual);
8988 } catch (Exception e) {
9089 //ok
9190 }
9291 return this;
9392 }
93+
94+ @SuppressWarnings("unchecked")
95+ public TypeCodecAssert cannotFormat(Object value) {
96+ try {
97+ actual.format(value);
98+ fail("Should not have been able to format " + value + " with " + actual);
99+ } catch (Exception e) {
100+ // ok
101+ }
102+ return this;
103+ }
94104 }
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +18 −1
@@ -123,7 +123,6 @@
123123 .doesNotAccept(varchar())
124124 .doesNotAccept(text())
125125 .accepts(ascii)
126- .doesNotAccept(utf8)
127126 .canSerialize(ascii)
128127 .cannotSerialize(utf8);
129128 assertThat(utf8Codec)
@@ -251,6 +250,24 @@
251250 assertThat(actual).isEqualTo(expected);
252251 }
253252
253+ /**
254+ * Ensures that {@link TypeCodec#timeUUID()} is resolved for all UUIDs and throws an
255+ * {@link InvalidTypeException} when attempting to serialize or format a non-type 1
256+ * UUID.
257+ *
258+ * @jira_ticket JAVA-965
259+ */
260+ @Test(groups = "unit")
261+ public void should_resolve_timeuuid_codec_for_all_uuids_and_fail_to_serialize_non_type1_uuid() {
262+ UUID type4UUID = UUID.randomUUID();
263+ TypeCodec<UUID> codec = codecRegistry.codecFor(DataType.timeuuid(), type4UUID);
264+ // Should resolve the TimeUUIDCodec, but not serialize/format a type4 uuid with it.
265+ assertThat(codec).isSameAs(TypeCodec.timeUUID())
266+ .accepts(UUID.class)
267+ .cannotSerialize(type4UUID)
268+ .cannotFormat(type4UUID);
269+ }
270+
254271
255272 private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> {
256273