kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 482c2681785f2368ec5525bf11cd4bd9647c79277f842461bb5ac07181381638 |
|---|---|
| Author | Alexandre Dutra <alexdut@gmail.com> · 2015-11-03 09:11 +01:00 |
| Committer | Alexandre Dutra <alexdut@gmail.com> · 2015-11-03 09:11 +01:00 |
| Parents | 82f0202f1f0c, 7df6cb642d75 |
4 files changed
modified changelog/README.md +3 −2
| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | ### 3.0.0-beta1 (in progress) |
| 4 | 4 | |
| 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) | |
| 7 | 8 | |
| 8 | 9 | ### 3.0.0-alpha4 |
| 9 | 10 |
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +12 −8
| @@ -860,10 +860,20 @@ | ||
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | @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); | |
| 865 | 868 | } |
| 866 | 869 | |
| 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 | + } | |
| 867 | 877 | } |
| 868 | 878 | |
| 869 | 879 | /** |
| @@ -1768,12 +1778,6 @@ | ||
| 1768 | 1778 | super(timeuuid()); |
| 1769 | 1779 | } |
| 1770 | 1780 | |
| 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 | - | |
| 1777 | 1781 | @Override |
| 1778 | 1782 | public String format(UUID value) { |
| 1779 | 1783 | if (value == null) |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecAssert.java +13 −3
| @@ -83,12 +83,22 @@ | ||
| 83 | 83 | public TypeCodecAssert cannotSerialize(Object value) { |
| 84 | 84 | if(version == null) fail("ProtocolVersion cannot be null"); |
| 85 | 85 | 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); | |
| 89 | 88 | } catch (Exception e) { |
| 90 | 89 | //ok |
| 91 | 90 | } |
| 92 | 91 | return this; |
| 93 | 92 | } |
| 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 | + } | |
| 94 | 104 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +18 −1
| @@ -123,7 +123,6 @@ | ||
| 123 | 123 | .doesNotAccept(varchar()) |
| 124 | 124 | .doesNotAccept(text()) |
| 125 | 125 | .accepts(ascii) |
| 126 | - .doesNotAccept(utf8) | |
| 127 | 126 | .canSerialize(ascii) |
| 128 | 127 | .cannotSerialize(utf8); |
| 129 | 128 | assertThat(utf8Codec) |
| @@ -251,6 +250,24 @@ | ||
| 251 | 250 | assertThat(actual).isEqualTo(expected); |
| 252 | 251 | } |
| 253 | 252 | |
| 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 | + | |
| 254 | 271 | |
| 255 | 272 | private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> { |
| 256 | 273 |