-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-18980. Invalid inputs for getTrimmedStringCollectionSplitByEquals (ADDENDUM) #6546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
241095a
98952d7
74fcd16
4211c55
7c18bb2
3bf1656
8768390
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,16 @@ public class StringUtils { | |
| public static final Pattern ENV_VAR_PATTERN = Shell.WINDOWS ? | ||
| WIN_ENV_VAR_PATTERN : SHELL_ENV_VAR_PATTERN; | ||
|
|
||
| /** | ||
| * {@link #getTrimmedStringCollectionSplitByEquals(String)} throws | ||
| * {@link IllegalArgumentException} with error message starting with this string | ||
| * if the argument provided is not valid representation of non-empty key-value | ||
| * pairs. | ||
| */ | ||
| public static final String STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG = | ||
| "Trimmed string split by equals does not correctly represent " | ||
| + "non-empty key-value pairs."; | ||
|
|
||
| /** | ||
| * Make a string representation of the exception. | ||
| * @param e The exception to stringify | ||
|
|
@@ -494,9 +504,22 @@ public static Map<String, String> getTrimmedStringCollectionSplitByEquals( | |
| String[] trimmedList = getTrimmedStrings(str); | ||
| Map<String, String> pairs = new HashMap<>(); | ||
| for (String s : trimmedList) { | ||
| if (s.length() == 0) { | ||
| continue; | ||
| } | ||
| String[] splitByKeyVal = getTrimmedStringsSplitByEquals(s); | ||
| if (splitByKeyVal.length == 2) { | ||
| pairs.put(splitByKeyVal[0], splitByKeyVal[1]); | ||
| boolean emptyKey = org.apache.commons.lang3.StringUtils.isEmpty(splitByKeyVal[0]); | ||
| boolean emptyVal = org.apache.commons.lang3.StringUtils.isEmpty(splitByKeyVal[1]); | ||
| if (!emptyKey && !emptyVal) { | ||
| pairs.put(splitByKeyVal[0], splitByKeyVal[1]); | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
|
||
| STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG + " Input: " + str); | ||
| } | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
|
||
| STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG + " Input: " + str); | ||
| } | ||
| } | ||
| return pairs; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ | |
| package org.apache.hadoop.util; | ||
|
|
||
| import java.util.Locale; | ||
|
|
||
| import static org.apache.hadoop.util.StringUtils.STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG; | ||
| import static org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix.long2String; | ||
| import static org.apache.hadoop.util.StringUtils.TraditionalBinaryPrefix.string2long; | ||
| import static org.junit.Assert.assertArrayEquals; | ||
|
|
@@ -566,6 +568,86 @@ public void testStringCollectionSplitByEquals() { | |
| .containsEntry("element.xyz.key5", "element.abc.val5") | ||
| .containsEntry("element.xyz.key6", "element.abc.val6") | ||
| .containsEntry("element.xyz.key7", "element.abc.val7"); | ||
|
|
||
| try { | ||
| StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| " = element.abc.val1"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| splitMap = StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| "element.first.key1 = element.first.val2 ,element.first.key1 =element.first.val1"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(1) | ||
| .containsEntry("element.first.key1", "element.first.val1"); | ||
|
|
||
| splitMap = StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| ",,, , ,, ,element.first.key1 = element.first.val2 ," | ||
| + "element.first.key1 = element.first.val1 , ,,, ,"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(1) | ||
| .containsEntry("element.first.key1", "element.first.val1"); | ||
|
|
||
| try { | ||
| StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
|
||
| "element.abc.key1="); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| try { | ||
| StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| "="); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| try { | ||
| StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| "== = = ="); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| splitMap = StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| ",, , , ,, ,"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(0); | ||
|
|
||
| try { | ||
| StringUtils.getTrimmedStringCollectionSplitByEquals( | ||
| ",="); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // Benchmark for StringUtils split | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,6 +70,7 @@ | |
| import static org.apache.hadoop.fs.s3a.test.PublicDatasetTestUtils.getExternalData; | ||
| import static org.apache.hadoop.test.LambdaTestUtils.intercept; | ||
| import static org.apache.hadoop.test.LambdaTestUtils.interceptFuture; | ||
| import static org.apache.hadoop.util.StringUtils.STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG; | ||
|
|
||
| /** | ||
| * Unit tests for {@link Constants#AWS_CREDENTIALS_PROVIDER} logic. | ||
|
|
@@ -722,7 +723,7 @@ public void testV2ClassNotFound() throws Throwable { | |
| * Tests for the string utility that will be used by S3A credentials provider. | ||
| */ | ||
| @Test | ||
| public void testStringCollectionSplitByEquals() { | ||
| public void testStringCollectionSplitByEquals1() { | ||
| final Configuration configuration = new Configuration(); | ||
| configuration.set("custom_key", ""); | ||
| Map<String, String> splitMap = | ||
|
|
@@ -792,6 +793,105 @@ public void testStringCollectionSplitByEquals() { | |
| .containsEntry("element.xyz.key7", "element.abc.val7"); | ||
| } | ||
|
|
||
| /** | ||
| * Tests for the string utility that will be used by S3A credentials provider. | ||
| */ | ||
| @Test | ||
| public void testStringCollectionSplitByEquals2() { | ||
| final Configuration configuration = new Configuration(); | ||
|
||
| configuration.set("custom_key", " = element.abc.val1"); | ||
| try { | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
|
||
| configuration, "custom_key"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| configuration.set( | ||
|
||
| "custom_key", | ||
| "element.first.key1 = element.first.val2 ,element.first.key1 =element.first.val1"); | ||
| Map<String, String> splitMap = | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(1) | ||
| .containsEntry("element.first.key1", "element.first.val1"); | ||
|
|
||
| configuration.set("custom_key", | ||
| ",,, , ,, ,element.first.key1 = element.first.val2 ," | ||
| + "element.first.key1 = element.first.val1 , ,,, ,"); | ||
| splitMap = S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(1) | ||
| .containsEntry("element.first.key1", "element.first.val1"); | ||
|
|
||
| configuration.set("custom_key", "element.abc.key1="); | ||
| try { | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| configuration.set("custom_key", "="); | ||
| try { | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| configuration.set("custom_key", "== = = ="); | ||
| try { | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| configuration.set("custom_key", ",, , , ,, ,"); | ||
| splitMap = S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| Assertions | ||
| .assertThat(splitMap) | ||
| .describedAs("Map of key value pairs split by equals(=) and comma(,)") | ||
| .hasSize(0); | ||
|
|
||
| configuration.set("custom_key", ", = "); | ||
| try { | ||
| S3AUtils.getTrimmedStringCollectionSplitByEquals( | ||
| configuration, "custom_key"); | ||
| throw new RuntimeException("Expected to throw IllegalArgumentException"); | ||
| } catch (IllegalArgumentException e) { | ||
| Assertions | ||
| .assertThat(e) | ||
| .describedAs("Exception thrown due to illegal arguments") | ||
| .hasMessageStartingWith(STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * V2 credentials which raises an instantiation exception in | ||
| * the factory method. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.