Skip to content

Commit

Permalink
Add more test cases on LiteralInlineExpressionParser (#33259)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 15, 2024
1 parent efe19fd commit 5642c0d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
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<>()));
}
}

0 comments on commit 5642c0d

Please sign in to comment.