Skip to content
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

[vividus] Allow to escape commas in common expressions #3391

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 55 additions & 0 deletions docs/modules/commons/pages/expressions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,55 @@ NOTE: The expression parameters containing commas or empty values must be surrou
|Remove all numbers from the string
|`#{replaceAllByRegExp(\d, """""", a1b2c3d)}`
|`abcd`

|Replace all whitespaces with commas
|`#{replaceAllByRegExp(\s, """,""", string with spaces)}`
|`string,with,spaces`

|Replace all whitespaces with commas
|`#{replaceAllByRegExp(\s, \,, string with spaces)}`
|`string,with,spaces`

|Replace all commas with underscores
|`#{replaceAllByRegExp(\,, _, string,with,commas)}`
|`string_with_commas`

|Replace all commas with underscores
|`#{replaceAllByRegExp(""",""", _, string,with,commas)}`
|`string_with_commas`

|Replace all commas with underscores
|`#{replaceAllByRegExp(\,, _, """string,with,commas""")}`
|`string_with_commas`

|Replace all commas with underscores
|`#{replaceAllByRegExp(\,, _, string\,with\,commas)}`
|`string_with_commas`

|Replace the first whitespace with comma
|`#{replaceFirstByRegExp(\s, """,""", string with spaces)}`
|`string,with spaces`

|Replace the first whitespace with comma
|`#{replaceFirstByRegExp(\s, \,, string with spaces)}`
|`string,with spaces`

|Replace the first comma with underscore
|`#{replaceFirstByRegExp(\,, _, string,with,commas)}`
|`string_with,commas`

|Replace the first comma with underscore
|`#{replaceFirstByRegExp(""",""", _, string,with,commas)}`
|`string_with,commas`

|Replace the first comma with underscore
|`#{replaceFirstByRegExp(\,, _, """string,with,commas""")}`
|`string_with,commas`

|Replace the first comma with underscore
|`#{replaceFirstByRegExp(\,, _, string\,with\,commas)}`
|`string_with,commas`

|===

=== `toLowerCase`
Expand Down Expand Up @@ -838,6 +887,12 @@ Calculates the hash using the specified hashing algorithm

|`#{calculateHash(MD5, vividus)}`
|`0a05ba6064ae7e5d6ee9818f85b666ad`

|`#{calculateHash(SHA256, with,comma)}`
|`f93dcfd6fc4c91c058177af3b5de34090073f2cc4a658010609ed8778d6d89f3`

|`#{calculateHash(SHA256, with\,comma)}`
|`f93dcfd6fc4c91c058177af3b5de34090073f2cc4a658010609ed8778d6d89f3`
|===

=== `calculateFileHash`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus.expression;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class NormalizingArgumentsExpressionTests
{
private final TestExpressionProcessor expressionProcessor = new TestExpressionProcessor();

@ParameterizedTest
@CsvSource(delimiter = '|', value = {
"expression(input) | input",
"expression(a,b,c) | a,b,c",
"expression(a\\,b) | a,b"
})
void shouldEvaluateExpression(String expression, String expectedResult)
{
var actualResult = expressionProcessor.execute(expression);
assertEquals(Optional.of(expectedResult), actualResult);
}

private static class TestExpressionProcessor extends AbstractExpressionProcessor<String>
implements NormalizingArguments
{
protected TestExpressionProcessor()
{
super(Pattern.compile("^expression\\((.*)\\)$", Pattern.CASE_INSENSITIVE | Pattern.DOTALL));
}

@Override
protected String evaluateExpression(Matcher expressionMatcher)
{
return normalize(expressionMatcher.group(1));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,7 @@
import org.vividus.util.ResourceUtils;

@Named
public class HashExpressionProcessor extends AbstractExpressionProcessor<String>
public class HashExpressionProcessor extends AbstractExpressionProcessor<String> implements NormalizingArguments
{
private static final Pattern HASH_PATTERN = Pattern.compile("^(calculate(?:File)?Hash)\\((.+), (.+)\\)$",
Pattern.CASE_INSENSITIVE);
Expand All @@ -52,7 +52,7 @@ protected String evaluateExpression(Matcher expressionMatcher)
String data = expressionMatcher.group(INPUT_DATA_GROUP);
if ("calculateHash".equalsIgnoreCase(expressionMatcher.group(INPUT_CALCULATE_TYPE_GROUP)))
{
return hashAlgorithmType.getHash(data);
return hashAlgorithmType.getHash(normalize(data));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import javax.inject.Named;

@Named
public class ReplaceByRegExpProcessor implements IExpressionProcessor<String>
public class ReplaceByRegExpProcessor implements IExpressionProcessor<String>, NormalizingArguments
{
@SuppressWarnings("checkstyle:SingleSpaceSeparator")
private static final Map<Pattern, Function<Matcher, Function<String, String>>> EVALUATE_REG_EXP = Map.of(
Expand Down Expand Up @@ -58,7 +58,7 @@ public Optional<String> execute(String expression)
{
List<String> groupValues = processMatchExpressionResult(expressionMatcher);
String regExp = groupValues.get(REG_EXP_INDEX);
String input = groupValues.get(INPUT_INDEX);
String input = normalize(groupValues.get(INPUT_INDEX));
String replacement = groupValues.get(REPLACEMENT_INDEX);
return Optional.of(entry.getValue().apply(Pattern.compile(regExp, Pattern.DOTALL).matcher(input))
.apply(replacement));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,40 +31,42 @@
import org.vividus.converter.FluentTrimmedEnumConverter;
import org.vividus.util.ResourceUtils;

public class HashExpressionProcessorTests
class HashExpressionProcessorTests
{
private final HashExpressionProcessor processor = new HashExpressionProcessor(new FluentTrimmedEnumConverter());

@ParameterizedTest
@CsvSource({
// CHECKSTYLE:OFF
"'calculateHash(SHA-1, a_tylmarande@gmail.com)', 025623becec96e8be6b88a7db6e0dc97fa0033ba",
"'calculateHash(Sha-256, a_tylmarande@gmail.com)', b4e73a7c815bb0eff534ba8ef5f7dbfe9d4f51f449cd9b2ba87fc57ee9de5fc7",
"'calculateHash(md2, a_tylmarande@gmail.com)', 69ccf551ef37e2cf0181c97fcb7db030",
"'calculateHash(MD5, a_tylmarande@gmail.com)', 7b2378863e837c51c83bb04e66a5876d",
"'calculateHash(SHA-384, a_tylmarande@gmail.com)', 397bf1d877dead00a0e91cf27a2b561eff0736fbc05640580c2e541a42f99a61cc7bdd4f3fdc6c1ef7a614e6cac06415",
"'calculateHash(Sha512, a_tylmarande@gmail.com)', b9b06852f7f209e03d0704ce6fa95283f65f7e4558b043b951bb503f23b4521afb21c1a51576dc11965f6bca91e5ed25dc30da765d803b3d4ff811ddfc4e0b5d"
// CHECKSTYLE:ON
// CHECKSTYLE:OFF
"'calculateHash(SHA-1, a_tylmarande@gmail.com)', 025623becec96e8be6b88a7db6e0dc97fa0033ba",
"'calculateHash(Sha-256, a_tylmarande@gmail.com)', b4e73a7c815bb0eff534ba8ef5f7dbfe9d4f51f449cd9b2ba87fc57ee9de5fc7",
"'calculateHash(SHA256, with,comma)', f93dcfd6fc4c91c058177af3b5de34090073f2cc4a658010609ed8778d6d89f3",
"'calculateHash(SHA256, with\\,comma)', f93dcfd6fc4c91c058177af3b5de34090073f2cc4a658010609ed8778d6d89f3",
"'calculateHash(md2, a_tylmarande@gmail.com)', 69ccf551ef37e2cf0181c97fcb7db030",
"'calculateHash(MD5, a_tylmarande@gmail.com)', 7b2378863e837c51c83bb04e66a5876d",
"'calculateHash(SHA-384, a_tylmarande@gmail.com)', 397bf1d877dead00a0e91cf27a2b561eff0736fbc05640580c2e541a42f99a61cc7bdd4f3fdc6c1ef7a614e6cac06415",
"'calculateHash(Sha512, a_tylmarande@gmail.com)', b9b06852f7f209e03d0704ce6fa95283f65f7e4558b043b951bb503f23b4521afb21c1a51576dc11965f6bca91e5ed25dc30da765d803b3d4ff811ddfc4e0b5d"
// CHECKSTYLE:ON
})
void checkHash(String expression, String expected)
void shouldCalculateStringHash(String expression, String expected)
{
assertEquals(processor.execute(expression), Optional.of(expected));
assertEquals(Optional.of(expected), processor.execute(expression));
}

@ParameterizedTest
@CsvSource({
// CHECKSTYLE:OFF
"'calculateFileHash(SHA-1, org/vividus/expressions/resource.txt)', 1ae4969aa6712c516be2e62c652760e8abfaee07",
"'calculateFileHash(Sha-256, org/vividus/expressions/resource.txt)', 8a0a675375c2f15e3789b63a40ffd1963bb11cd0349d8f08f081dcb0bbe489fe",
"'calculateFileHash(md2, org/vividus/expressions/resource.txt)', 63090590b6d8e241bca791640a96e9fb",
"'calculateFileHash(MD5, org/vividus/expressions/resource.txt)', 44d6c918d7a7157f7c0905df17b25496",
"'calculateFileHash(SHA-384, org/vividus/expressions/resource.txt)', ed19ca22a3f70e8deb3c3ecd8e814b3663b042e3f9b70f0eea2bf1baa3b2c3ffecc4ab295cc51266173bab23eb6decc4",
"'calculateFileHash(Sha512, org/vividus/expressions/resource.txt)', 73e68881ac4f06fe5f62fdf3cf0acc90e830c02f8ffe2d9728ceef40bc4eb62dd235ab13aa5214931141d9d721e16e7bba3e69ac4d8a5f9c12261d9941bb65e1"
// CHECKSTYLE:ON
// CHECKSTYLE:OFF
"'calculateFileHash(SHA-1, org/vividus/expressions/resource.txt)', 1ae4969aa6712c516be2e62c652760e8abfaee07",
"'calculateFileHash(Sha-256, org/vividus/expressions/resource.txt)', 8a0a675375c2f15e3789b63a40ffd1963bb11cd0349d8f08f081dcb0bbe489fe",
"'calculateFileHash(md2, org/vividus/expressions/resource.txt)', 63090590b6d8e241bca791640a96e9fb",
"'calculateFileHash(MD5, org/vividus/expressions/resource.txt)', 44d6c918d7a7157f7c0905df17b25496",
"'calculateFileHash(SHA-384, org/vividus/expressions/resource.txt)', ed19ca22a3f70e8deb3c3ecd8e814b3663b042e3f9b70f0eea2bf1baa3b2c3ffecc4ab295cc51266173bab23eb6decc4",
"'calculateFileHash(Sha512, org/vividus/expressions/resource.txt)', 73e68881ac4f06fe5f62fdf3cf0acc90e830c02f8ffe2d9728ceef40bc4eb62dd235ab13aa5214931141d9d721e16e7bba3e69ac4d8a5f9c12261d9941bb65e1"
// CHECKSTYLE:ON
})
void checkFileHash(String expression, String expected)
void shouldCalculateFileHash(String expression, String expected)
{
assertEquals(processor.execute(expression), Optional.of(expected));
assertEquals(Optional.of(expected), processor.execute(expression));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,6 +41,19 @@ static Stream<Arguments> expressionInput()
of("formatDate()", Optional.empty()),
of("replaceFirstByRegExp(\"\"\".*three, (\\d)\"\"\", $1.0, \"\"\"two, three, 4\"\"\")", Optional.of("4.0")),
of("replaceAllByRegExp(\\s, _, string with spaces)", Optional.of("string_with_spaces")),
of("replaceAllByRegExp(\\s, \"\"\",\"\"\", string with spaces)", Optional.of("string,with,spaces")),
of("replaceAllByRegExp(\\s, \\,, string with spaces)", Optional.of("string,with,spaces")),
of("replaceAllByRegExp(\\,, _, string,with,commas)", Optional.of("string_with_commas")),
of("replaceAllByRegExp(\"\"\",\"\"\", _, string,with,commas)", Optional.of("string_with_commas")),
of("replaceAllByRegExp(\\,, _, \"\"\"string,with,commas\"\"\")", Optional.of("string_with_commas")),
of("replaceAllByRegExp(\\,, _, string\\,with\\,commas)", Optional.of("string_with_commas")),
of("replaceFirstByRegExp(\\s, _, string with spaces)", Optional.of("string_with spaces")),
of("replaceFirstByRegExp(\\s, \"\"\",\"\"\", string with spaces)", Optional.of("string,with spaces")),
of("replaceFirstByRegExp(\\s, \\,, string with spaces)", Optional.of("string,with spaces")),
of("replaceFirstByRegExp(\\,, _, string,with,commas)", Optional.of("string_with,commas")),
of("replaceFirstByRegExp(\"\"\",\"\"\", _, string,with,commas)", Optional.of("string_with,commas")),
of("replaceFirstByRegExp(\\,, _, \"\"\"string,with,commas\"\"\")", Optional.of("string_with,commas")),
of("replaceFirstByRegExp(\\,, _, string\\,with\\,commas)", Optional.of("string_with,commas")),
of("replaceAllByRegExp(test, TEST, this\nis\ntest\nvalue)", Optional.of("this\nis\nTEST\nvalue")),
of("replaceAllByRegExp(.test, TEST, this\nis\ntest\nvalue)", Optional.of("this\nisTEST\nvalue")),
of("replaceFirstByRegExp(.*(te[a-z]+).*, $1, this\nis\ntest\nvalue)", Optional.of("test"))
Expand Down