kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 732cde7e1c80694b81829dd67e0226fa4e1fa8a7e3e18b901a049b339282618e |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-22 12:14 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-12-22 12:14 +01:00 |
| Parents | 461a090f4994 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +7 −4
| @@ -232,12 +232,12 @@ | ||
| 232 | 232 | * by single quotes, and {@code false} otherwise. |
| 233 | 233 | */ |
| 234 | 234 | public static boolean isQuoted(String value) { |
| 235 | - return value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\''; | |
| 235 | + return value != null && value.length() > 1 && value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\''; | |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | - | |
| 239 | 238 | /** |
| 240 | 239 | * Quote the given string; single quotes are escaped. |
| 240 | + * If the given string is null, this method returns a quoted empty string ({@code ''}). | |
| 241 | 241 | * |
| 242 | 242 | * @param value The value to quote. |
| 243 | 243 | * @return The quoted string. |
| @@ -247,12 +247,15 @@ | ||
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
| 250 | - * Unquote the given string; single quotes are unescaped. | |
| 250 | + * Unquote the given string if it is quoted; single quotes are unescaped. | |
| 251 | + * If the given string is not quoted, it is returned without any modification. | |
| 251 | 252 | * |
| 252 | 253 | * @param value The string to unquote. |
| 253 | 254 | * @return The unquoted string. |
| 254 | 255 | */ |
| 255 | 256 | public static String unquote(String value) { |
| 257 | + if (!isQuoted(value)) | |
| 258 | + return value; | |
| 256 | 259 | return value.substring(1, value.length() - 1).replace("''", "'"); |
| 257 | 260 | } |
| 258 | 261 | |
| @@ -445,7 +448,7 @@ | ||
| 445 | 448 | */ |
| 446 | 449 | private static String replaceChar(String text, char search, String replacement) { |
| 447 | 450 | if (text == null || text.isEmpty()) |
| 448 | - return text; | |
| 451 | + return ""; | |
| 449 | 452 | |
| 450 | 453 | int nbMatch = 0; |
| 451 | 454 | int start = -1; |
added driver-core/src/test/java/com/datastax/driver/core/ParseUtilsTest.java +63 −0
| @@ -0,0 +1,63 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import org.testng.annotations.Test; | |
| 19 | + | |
| 20 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 21 | + | |
| 22 | +public class ParseUtilsTest { | |
| 23 | + | |
| 24 | + @Test(groups = "unit") | |
| 25 | + public void testQuote() { | |
| 26 | + assertThat(ParseUtils.quote(null)).isEqualTo("''"); | |
| 27 | + assertThat(ParseUtils.quote("")).isEqualTo("''"); | |
| 28 | + assertThat(ParseUtils.quote(" ")).isEqualTo("' '"); | |
| 29 | + assertThat(ParseUtils.quote("foo")).isEqualTo("'foo'"); | |
| 30 | + assertThat(ParseUtils.quote(" 'foo' ")).isEqualTo("' ''foo'' '"); | |
| 31 | + } | |
| 32 | + | |
| 33 | + @Test(groups = "unit") | |
| 34 | + public void testUnquote() { | |
| 35 | + assertThat(ParseUtils.unquote(null)).isNull(); | |
| 36 | + assertThat(ParseUtils.unquote("")).isEqualTo(""); | |
| 37 | + assertThat(ParseUtils.unquote(" ")).isEqualTo(" "); | |
| 38 | + assertThat(ParseUtils.unquote("'")).isEqualTo("'"); // malformed string left untouched | |
| 39 | + assertThat(ParseUtils.unquote("foo")).isEqualTo("foo"); | |
| 40 | + assertThat(ParseUtils.unquote("''")).isEqualTo(""); | |
| 41 | + assertThat(ParseUtils.unquote("' '")).isEqualTo(" "); | |
| 42 | + assertThat(ParseUtils.unquote("'foo")).isEqualTo("'foo"); // malformed string left untouched | |
| 43 | + assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo"); | |
| 44 | + assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted | |
| 45 | + assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'"); | |
| 46 | + } | |
| 47 | + | |
| 48 | + @Test(groups = "unit") | |
| 49 | + public void testIsQuoted() { | |
| 50 | + assertThat(ParseUtils.isQuoted(null)).isFalse(); | |
| 51 | + assertThat(ParseUtils.isQuoted("")).isFalse(); | |
| 52 | + assertThat(ParseUtils.isQuoted(" ")).isFalse(); | |
| 53 | + assertThat(ParseUtils.isQuoted("'")).isFalse(); // malformed string considered unquoted | |
| 54 | + assertThat(ParseUtils.isQuoted("foo")).isFalse(); | |
| 55 | + assertThat(ParseUtils.isQuoted("''")).isTrue(); | |
| 56 | + assertThat(ParseUtils.isQuoted("' '")).isTrue(); | |
| 57 | + assertThat(ParseUtils.isQuoted("'foo")).isFalse(); // malformed string considered unquoted | |
| 58 | + assertThat(ParseUtils.isQuoted("'foo'")).isTrue(); | |
| 59 | + assertThat(ParseUtils.isQuoted(" 'foo' ")).isFalse(); // considered unquoted | |
| 60 | + assertThat(ParseUtils.isQuoted("'''foo'''")).isTrue(); | |
| 61 | + } | |
| 62 | + | |
| 63 | +} |