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

Add more test cases on LiteralInlineExpressionParser #33259

Merged
merged 1 commit into from
Oct 15, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public void init(final Properties props) {

@Override
public List<String> splitAndEvaluate() {
return Strings.isNullOrEmpty(inlineExpression) ? Collections.emptyList() : split(inlineExpression);
return Strings.isNullOrEmpty(inlineExpression) ? Collections.emptyList() : split();
}

private List<String> split(final String inlineExpression) {
private List<String> split() {
List<String> result = new ArrayList<>(inlineExpression.length());
StringBuilder segment = new StringBuilder();
for (int i = 0; i < inlineExpression.length(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,33 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

class LiteralInlineExpressionParserTest {

@Test
void assertEvaluateForExpressionIsNull() {
void assertEvaluateWithNullExpression() {
InlineExpressionParser parser = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", new Properties());
List<String> expected = parser.splitAndEvaluate();
assertThat(expected, is(Collections.<String>emptyList()));
assertTrue(parser.splitAndEvaluate().isEmpty());
}

@Test
void assertEvaluateForSimpleString() {
List<String> expected = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", PropertiesBuilder.build(
new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, " t_order_0, t_order_1 "))).splitAndEvaluate();
void assertEvaluateWithCommaExpression() {
InlineExpressionParser parser = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, ",")));
assertThat(parser.splitAndEvaluate(), is(Collections.singletonList("")));
}

@Test
void assertEvaluateWithSimpleExpression() {
List<String> expected = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, " t_order_0, t_order_1 "))).splitAndEvaluate();
assertThat(expected.size(), is(2));
assertThat(expected, hasItems("t_order_0", "t_order_1"));
}

@Test
void assertEvaluateForLong() {
void assertEvaluateWithLongExpression() {
StringBuilder expression = new StringBuilder();
for (int i = 0; i < 1024; i++) {
expression.append("ds_");
Expand All @@ -61,25 +68,23 @@ void assertEvaluateForLong() {
expression.append(",");
}
}
List<String> expected = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", PropertiesBuilder.build(
new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, expression.toString()))).splitAndEvaluate();
List<String> expected = TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, expression.toString()))).splitAndEvaluate();
assertThat(expected.size(), is(1024));
assertThat(expected, hasItems("ds_0.t_user_0", "ds_15.t_user_1023"));
}

@Test
void assertHandlePlaceHolder() {
assertThrows(UnsupportedOperationException.class, () -> {
TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", PropertiesBuilder.build(
new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "t_$->{[\"new$->{1+2}\"]}"))).handlePlaceHolder();
TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", PropertiesBuilder.build(
new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "t_${[\"new$->{1+2}\"]}"))).handlePlaceHolder();
});
void assertEvaluateWithPlaceholderExpression() {
assertThrows(UnsupportedOperationException.class, () -> TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "t_$->{[\"new$->{1+2}\"]}"))).handlePlaceHolder());
assertThrows(UnsupportedOperationException.class, () -> TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "t_${[\"new$->{1+2}\"]}"))).handlePlaceHolder());
}

@Test
void assertEvaluateWithArgs() {
assertThrows(UnsupportedOperationException.class, () -> TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL", PropertiesBuilder.build(
new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "${1+2}"))).evaluateWithArgs(new LinkedHashMap<>()));
void assertEvaluateWithArgumentsExpression() {
assertThrows(UnsupportedOperationException.class, () -> TypedSPILoader.getService(InlineExpressionParser.class, "LITERAL",
PropertiesBuilder.build(new PropertiesBuilder.Property(InlineExpressionParser.INLINE_EXPRESSION_KEY, "${1+2}"))).evaluateWithArgs(new LinkedHashMap<>()));
}
}