Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Preconditions.checkArgument(!emptyKey && !emptyVal with its built in string formatting.

STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG + " Input: " + str);
}
} else {
throw new IllegalArgumentException(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, make a precondition on L511

STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG + " Input: " + str);
}
}
return pairs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LambdaTestUtils.intercept(). It's predictable I will insist on that, so save time in future. and it includes a string value check. so all you need is

intercept(IllegalArgumentException.class,
  STRING_COLLECTION_SPLIT_EQUALS_INVALID_ARG, () ->
    StringUtils.getTrimmedStringCollectionSplitByEquals(
          "element.abc.key1="));

See: much better. and if the closure doesn't fail, will even include the string value of the result in the exception raised.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, that's already an amazing utility

"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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use new Configuration(false); ensures no core-site/auth-keys values can interferer

configuration.set("custom_key", " = element.abc.val1");
try {
S3AUtils.getTrimmedStringCollectionSplitByEquals(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intercept(), and again, consider a factored out assertion to call repeatedly

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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pull the expected-to-succeed tests into their own test case.

"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.
Expand Down