Elffuss BraidHub

kikocis/java-driver public

Merge pull request #511 from datastax/java972

JAVA-972: TypeCodec.parse() implementations should be case insensitive when checking for keyword NULL.
Changeset9d5641e1175428708e75588a676243b0ae8a992c1aae46497390fa4c8e72cd40
AuthorAlexandre Dutra <alexdut@gmail.com> · 2015-11-09 09:42 +01:00
CommitterAlexandre Dutra <alexdut@gmail.com> · 2015-11-09 09:42 +01:00
Parents1d6480b23c6f, b9d0634700a6

Browse the files at this changeset

6 files changed

modified changelog/README.md +1 −1
@@ -7,7 +7,7 @@
77 - [improvement] Improve error handling for when a non-type 1 UUID is given to bind() on a timeuuid column (JAVA-965)
88 - [improvement] Pass the authenticator name from the server to the auth provider (JAVA-885)
99 - [improvement] Raise an exception when an older version of guava (<16.01) is found (JAVA-961)
10-
10+- [bug] TypeCodec.parse() implementations should be case insensitive when checking for keyword NULL (JAVA-972)
1111
1212 ### 3.0.0-alpha4
1313
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +21 −21
@@ -763,7 +763,7 @@
763763
764764 @Override
765765 public String parse(String value) {
766- if (value == null || value.isEmpty() || value.equals("NULL"))
766+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
767767 return null;
768768 if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
769769 throw new InvalidTypeException("text or varchar values must be enclosed by single quotes");
@@ -889,7 +889,7 @@
889889 @Override
890890 public Long parse(String value) {
891891 try {
892- return value == null || value.isEmpty() || value.equals("NULL") ? null : Long.parseLong(value);
892+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Long.parseLong(value);
893893 } catch (NumberFormatException e) {
894894 throw new InvalidTypeException(String.format("Cannot parse 64-bits long value from \"%s\"", value));
895895 }
@@ -959,7 +959,7 @@
959959
960960 @Override
961961 public ByteBuffer parse(String value) {
962- return value == null || value.isEmpty() || value.equals("NULL") ? null : Bytes.fromHexString(value);
962+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Bytes.fromHexString(value);
963963 }
964964
965965 @Override
@@ -994,7 +994,7 @@
994994
995995 @Override
996996 public ByteBuffer parse(String value) {
997- return value == null || value.isEmpty() || value.equals("NULL") ? null : Bytes.fromHexString(value);
997+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Bytes.fromHexString(value);
998998 }
999999
10001000 @Override
@@ -1031,7 +1031,7 @@
10311031
10321032 @Override
10331033 public Boolean parse(String value) {
1034- if (value == null || value.isEmpty() || value.equals("NULL"))
1034+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
10351035 return null;
10361036 if (value.equalsIgnoreCase(Boolean.FALSE.toString()))
10371037 return false;
@@ -1078,7 +1078,7 @@
10781078 @Override
10791079 public BigDecimal parse(String value) {
10801080 try {
1081- return value == null || value.isEmpty() || value.equals("NULL") ? null : new BigDecimal(value);
1081+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigDecimal(value);
10821082 } catch (NumberFormatException e) {
10831083 throw new InvalidTypeException(String.format("Cannot parse decimal value from \"%s\"", value));
10841084 }
@@ -1137,7 +1137,7 @@
11371137 @Override
11381138 public Double parse(String value) {
11391139 try {
1140- return value == null || value.isEmpty() || value.equals("NULL") ? null : Double.parseDouble(value);
1140+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Double.parseDouble(value);
11411141 } catch (NumberFormatException e) {
11421142 throw new InvalidTypeException(String.format("Cannot parse 64-bits double value from \"%s\"", value));
11431143 }
@@ -1182,7 +1182,7 @@
11821182 @Override
11831183 public Float parse(String value) {
11841184 try {
1185- return value == null || value.isEmpty() || value.equals("NULL") ? null : Float.parseFloat(value);
1185+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Float.parseFloat(value);
11861186 } catch (NumberFormatException e) {
11871187 throw new InvalidTypeException(String.format("Cannot parse 32-bits float value from \"%s\"", value));
11881188 }
@@ -1226,7 +1226,7 @@
12261226
12271227 @Override
12281228 public InetAddress parse(String value) {
1229- if (value == null || value.isEmpty() || value.equals("NULL"))
1229+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
12301230 return null;
12311231
12321232 value = value.trim();
@@ -1277,7 +1277,7 @@
12771277 @Override
12781278 public Byte parse(String value) {
12791279 try {
1280- return value == null || value.isEmpty() || value.equals("NULL") ? null : Byte.parseByte(value);
1280+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Byte.parseByte(value);
12811281 } catch (NumberFormatException e) {
12821282 throw new InvalidTypeException(String.format("Cannot parse 8-bits int value from \"%s\"", value));
12831283 }
@@ -1322,7 +1322,7 @@
13221322 @Override
13231323 public Short parse(String value) {
13241324 try {
1325- return value == null || value.isEmpty() || value.equals("NULL") ? null : Short.parseShort(value);
1325+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Short.parseShort(value);
13261326 } catch (NumberFormatException e) {
13271327 throw new InvalidTypeException(String.format("Cannot parse 16-bits int value from \"%s\"", value));
13281328 }
@@ -1367,7 +1367,7 @@
13671367 @Override
13681368 public Integer parse(String value) {
13691369 try {
1370- return value == null || value.isEmpty() || value.equals("NULL") ? null : Integer.parseInt(value);
1370+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : Integer.parseInt(value);
13711371 } catch (NumberFormatException e) {
13721372 throw new InvalidTypeException(String.format("Cannot parse 32-bits int value from \"%s\"", value));
13731373 }
@@ -1453,7 +1453,7 @@
14531453
14541454 @Override
14551455 public Date parse(String value) {
1456- if (value == null || value.isEmpty() || value.equals("NULL"))
1456+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
14571457 return null;
14581458
14591459 // strip enclosing single quotes, if any
@@ -1511,7 +1511,7 @@
15111511
15121512 @Override
15131513 public LocalDate parse(String value) {
1514- if (value == null || value.isEmpty() || value.equals("NULL"))
1514+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
15151515 return null;
15161516
15171517 // strip enclosing single quotes, if any
@@ -1655,7 +1655,7 @@
16551655
16561656 @Override
16571657 public Long parse(String value) {
1658- if (value == null || value.isEmpty() || value.equals("NULL"))
1658+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
16591659 return null;
16601660
16611661 // enclosing single quotes required, even for long literals
@@ -1725,7 +1725,7 @@
17251725 @Override
17261726 public UUID parse(String value) {
17271727 try {
1728- return value == null || value.isEmpty() || value.equals("NULL") ? null : UUID.fromString(value);
1728+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : UUID.fromString(value);
17291729 } catch (IllegalArgumentException e) {
17301730 throw new InvalidTypeException(String.format("Cannot parse UUID value from \"%s\"", value));
17311731 }
@@ -1811,7 +1811,7 @@
18111811 @Override
18121812 public BigInteger parse(String value) {
18131813 try {
1814- return value == null || value.isEmpty() || value.equals("NULL") ? null : new BigInteger(value);
1814+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : new BigInteger(value);
18151815 } catch (NumberFormatException e) {
18161816 throw new InvalidTypeException(String.format("Cannot parse varint value from \"%s\"", value));
18171817 }
@@ -1907,7 +1907,7 @@
19071907
19081908 @Override
19091909 public C parse(String value) {
1910- if (value == null || value.isEmpty() || value.equals("NULL"))
1910+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
19111911 return null;
19121912
19131913 int idx = ParseUtils.skipSpaces(value, 0);
@@ -2047,7 +2047,7 @@
20472047
20482048 @Override
20492049 public Map<K, V> parse(String value) {
2050- if (value == null || value.isEmpty() || value.equals("NULL"))
2050+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
20512051 return null;
20522052
20532053 int idx = ParseUtils.skipSpaces(value, 0);
@@ -2188,7 +2188,7 @@
21882188
21892189 @Override
21902190 public UDTValue parse(String value) {
2191- if (value == null || value.isEmpty() || value.equals("NULL"))
2191+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
21922192 return null;
21932193
21942194 UDTValue v = definition.newValue();
@@ -2316,7 +2316,7 @@
23162316
23172317 @Override
23182318 public TupleValue parse(String value) {
2319- if (value == null || value.isEmpty() || value.equals("NULL"))
2319+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
23202320 return null;
23212321
23222322 TupleValue v = definition.newValue();
modified driver-core/src/test/java/com/datastax/driver/core/DataTypeTest.java +31 −23
@@ -21,7 +21,6 @@
2121 import java.nio.ByteBuffer;
2222 import java.util.*;
2323
24-import com.google.common.collect.Lists;
2524 import org.testng.annotations.Test;
2625
2726 import static com.google.common.collect.Lists.newArrayList;
@@ -70,13 +69,13 @@
7069 return new TestValue[]{
7170 new TestValue("foo", "'foo'", "'foo'"),
7271 new TestValue("fo'o", "'fo''o'", "'fo''o'"),
73- new TestValue(null, null, "NULL") };
72+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
7473 case BIGINT:
7574 return new TestValue[]{
7675 new TestValue(42L, "42", "42"),
7776 new TestValue(91294377723L, "91294377723", "91294377723"),
7877 new TestValue(-133L, "-133", "-133"),
79- new TestValue(null, null, "NULL") };
78+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
8079 case TIMESTAMP:
8180 // input: single quotes are optional for long literals, mandatory for date patterns
8281 return new TestValue[]{
@@ -84,7 +83,7 @@
8483 new TestValue(new Date(91294377723L), "91294377723", "91294377723"),
8584 new TestValue(new Date(-133L), "-133", "-133"),
8685 new TestValue(new Date(784041330999L), "'1994-11-05T14:15:30.999+0100'", "784041330999"),
87- new TestValue(null, null, "NULL") };
86+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
8887 case DATE:
8988 // input: single quotes are optional for long literals, mandatory for date patterns
9089 return new TestValue[]{
@@ -92,43 +91,43 @@
9291 new TestValue(LocalDate.fromDaysSinceEpoch(0), "'1970-01-01'", "'1970-01-01'"),
9392 new TestValue(LocalDate.fromDaysSinceEpoch((int)(2147483648L - (1L << 31))), "'2147483648'", "'1970-01-01'"),
9493 new TestValue(LocalDate.fromDaysSinceEpoch((int)(0 - (1L << 31))), "0", "'-5877641-06-23'"),
95- new TestValue(null, null, "NULL") };
94+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
9695 case TIME:
9796 // input: all literals must by enclosed in single quotes
9897 return new TestValue[]{
9998 new TestValue(54012123450000L, "'54012123450000'", "'15:00:12.123450000'"),
10099 new TestValue(0L, "'0'", "'00:00:00.000000000'"),
101100 new TestValue(54012012345000L, "'15:00:12.012345000'", "'15:00:12.012345000'"),
102- new TestValue(null, null, "NULL") };
101+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
103102 case BLOB:
104103 return new TestValue[]{
105104 new TestValue(Bytes.fromHexString("0x2450"), "0x2450", "0x2450"),
106105 new TestValue(ByteBuffer.allocate(0), "0x", "0x"),
107- new TestValue(null, null, "NULL") };
106+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
108107 case BOOLEAN:
109108 return new TestValue[]{
110109 new TestValue(true, "true", "true"),
111110 new TestValue(false, "false", "false"),
112- new TestValue(null, null, "NULL") };
111+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
113112 case DECIMAL:
114113 return new TestValue[]{
115114 new TestValue(new BigDecimal("1.23E+8"), "1.23E+8", "1.23E+8"),
116- new TestValue(null, null, "NULL") };
115+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
117116 case DOUBLE:
118117 return new TestValue[]{
119118 new TestValue(2.39324324, "2.39324324", "2.39324324"),
120119 new TestValue(-12., "-12.0", "-12.0"),
121- new TestValue(null, null, "NULL") };
120+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
122121 case FLOAT:
123122 return new TestValue[]{
124123 new TestValue(2.39f, "2.39", "2.39"),
125124 new TestValue(-12.f, "-12.0", "-12.0"),
126- new TestValue(null, null, "NULL") };
125+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
127126 case INET:
128127 try {
129128 return new TestValue[]{
130129 new TestValue(InetAddress.getByName("128.2.12.3"), "'128.2.12.3'", "'128.2.12.3'"),
131- new TestValue(null, null, "NULL") };
130+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
132131 } catch (java.net.UnknownHostException e) {
133132 throw new RuntimeException();
134133 }
@@ -136,30 +135,30 @@
136135 return new TestValue[]{
137136 new TestValue((byte)-4, "-4", "-4"),
138137 new TestValue((byte)44, "44", "44"),
139- new TestValue(null, null, "NULL") };
138+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
140139 case SMALLINT:
141140 return new TestValue[]{
142141 new TestValue((short)-3, "-3", "-3"),
143142 new TestValue((short)43, "43", "43"),
144- new TestValue(null, null, "NULL") };
143+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
145144 case INT:
146145 return new TestValue[]{
147146 new TestValue(-2, "-2", "-2"),
148147 new TestValue(42, "42", "42"),
149- new TestValue(null, null, "NULL") };
148+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
150149 case TIMEUUID:
151150 return new TestValue[]{
152151 new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66"),
153- new TestValue(null, null, "NULL") };
152+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
154153 case UUID:
155154 return new TestValue[]{
156155 new TestValue(UUID.fromString("FE2B4360-28C6-11E2-81C1-0800200C9A66"), "fe2b4360-28c6-11e2-81c1-0800200c9a66", "fe2b4360-28c6-11e2-81c1-0800200c9a66"),
157156 new TestValue(UUID.fromString("067e6162-3b6f-4ae2-a171-2470b63dff00"), "067e6162-3b6f-4ae2-a171-2470b63dff00", "067e6162-3b6f-4ae2-a171-2470b63dff00"),
158- new TestValue(null, null, "NULL") };
157+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
159158 case VARINT:
160159 return new TestValue[]{
161160 new TestValue(new BigInteger("12387290982347987032483422342432"), "12387290982347987032483422342432", "12387290982347987032483422342432"),
162- new TestValue(null, null, "NULL") };
161+ new TestValue(null, null, "NULL"), new TestValue(null, "null", "NULL"), new TestValue(null, "NULL", "NULL") };
163162 default:
164163 throw new RuntimeException("Missing handling of " + dt);
165164 }
@@ -204,8 +203,11 @@
204203 @Test(groups = "unit")
205204 public void parseFormatSetTest() {
206205 String toParse = "{'Foo','Bar','Foo''bar'}";
207- Set<String> toFormat = new LinkedHashSet<String>() {{ add("Foo");
208- add("Bar"); add("Foo'bar"); }};
206+ Set<String> toFormat = new LinkedHashSet<String>() {{
207+ add("Foo");
208+ add("Bar");
209+ add("Foo'bar");
210+ }};
209211 DataType dt = DataType.set(DataType.text());
210212 assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat);
211213 assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse);
@@ -215,7 +217,11 @@
215217 @Test(groups = "unit")
216218 public void parseFormatMapTest() {
217219 String toParse = "{'Foo':3,'Bar':42,'Foo''bar':-24}";
218- Map<String, Integer> toFormat = new LinkedHashMap<String, Integer>(){{ put("Foo", 3); put("Bar", 42); put("Foo'bar", -24); }};
220+ Map<String, Integer> toFormat = new LinkedHashMap<String, Integer>() {{
221+ put("Foo", 3);
222+ put("Bar", 42);
223+ put("Foo'bar", -24);
224+ }};
219225 DataType dt = DataType.map(DataType.text(), DataType.cint());
220226 assertEquals(codecRegistry.codecFor(dt).parse(toParse), toFormat);
221227 assertEquals(codecRegistry.codecFor(dt).format(toFormat), toParse);
@@ -239,7 +245,9 @@
239245 toFormat.setString("t", "fo'o");
240246 toFormat.setInt("i", 3);
241247 toFormat.setList("l", Arrays.<String>asList("a", "b"));
242- toFormat.setMap("s", new HashMap<Integer, UDTValue>(){{ put(3, udt1.newValue().setBytes("a", ByteBuffer.wrap(new byte[]{1}))); }});
248+ toFormat.setMap("s", new HashMap<Integer, UDTValue>() {{
249+ put(3, udt1.newValue().setBytes("a", ByteBuffer.wrap(new byte[]{ 1 })));
250+ }});
243251
244252 assertEquals(codecRegistry.codecFor(udt2).parse(toParse), toFormat);
245253 assertEquals(codecRegistry.codecFor(udt2).format(toFormat), toParse);
@@ -313,7 +321,7 @@
313321 assertThat(dataTypes).doesNotContainAnyElementsOf(newArrayList(DataType.tinyint(), DataType.smallint()));
314322
315323 // Ensure all values are <= V3.
316- for(DataType dataType : dataTypes) {
324+ for (DataType dataType : dataTypes) {
317325 assertThat(dataType.getName().minProtocolVersion).isLessThanOrEqualTo(ProtocolVersion.V3);
318326 }
319327 }
modified driver-core/src/test/java/com/datastax/driver/core/JsonCodec.java +1 −1
@@ -76,7 +76,7 @@
7676 @Override
7777 @SuppressWarnings("unchecked")
7878 public T parse(String value) throws InvalidTypeException {
79- if (value == null || value.isEmpty() || value.equals("NULL"))
79+ if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL"))
8080 return null;
8181 if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'')
8282 throw new InvalidTypeException("JSON strings must be enclosed by single quotes");
modified driver-core/src/test/java/com/datastax/driver/core/MappingCodec.java +1 −1
@@ -54,7 +54,7 @@
5454
5555 @Override
5656 public T parse(String value) throws InvalidTypeException {
57- return value == null || value.isEmpty() || value.equals("NULL") ? null : deserialize(innerCodec.parse(value));
57+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : deserialize(innerCodec.parse(value));
5858 }
5959
6060 @Override
modified driver-core/src/test/java/com/datastax/driver/core/StringParsingCodec.java +1 −1
@@ -66,7 +66,7 @@
6666
6767 @Override
6868 public T parse(String value) throws InvalidTypeException {
69- return value == null || value.isEmpty() || value.equals("NULL") ? null : fromString(innerCodec.parse(value));
69+ return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : fromString(innerCodec.parse(value));
7070 }
7171
7272 /**