Skip to content

Commit

Permalink
Add more test cases on ShadowExtractor (#33516)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 3, 2024
1 parent 0c8d337 commit f361c51
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
public final class ShadowExtractor {

/**
* Get values in expression segment.
* Extract values.
*
* @param expression expression segment
* @param params parameters
* @return values
* @return extracted values
*/
public static Optional<Collection<Comparable<?>>> extractValues(final ExpressionSegment expression, final List<Object> params) {
Collection<Comparable<?>> result = new LinkedList<>();
Expand All @@ -54,33 +54,33 @@ public static Optional<Collection<Comparable<?>>> extractValues(final Expression
extractValues(((InExpression) expression).getRight(), params).ifPresent(result::addAll);
}
if (expression instanceof ListExpression) {
((ListExpression) expression).getItems().forEach(each -> extractValueInSimpleExpressionSegment(each, params).ifPresent(result::add));
((ListExpression) expression).getItems().forEach(each -> extractSimpleExpressionValue(each, params).ifPresent(result::add));
}
if (expression instanceof SimpleExpressionSegment) {
extractValueInSimpleExpressionSegment(expression, params).ifPresent(result::add);
extractSimpleExpressionValue(expression, params).ifPresent(result::add);
}
return result.isEmpty() ? Optional.empty() : Optional.of(result);
}

private static Optional<Comparable<?>> extractValueInSimpleExpressionSegment(final ExpressionSegment expression, final List<Object> params) {
private static Optional<Comparable<?>> extractSimpleExpressionValue(final ExpressionSegment expression, final List<Object> params) {
if (expression instanceof LiteralExpressionSegment) {
return extractValueInLiteralExpressionSegment((LiteralExpressionSegment) expression);
return extractLiteralExpressionValue((LiteralExpressionSegment) expression);
}
if (expression instanceof ParameterMarkerExpressionSegment) {
return extractValueInParameterMarkerExpressionSegment((ParameterMarkerExpressionSegment) expression, params);
return extractParameterMarkerExpressionValue((ParameterMarkerExpressionSegment) expression, params);
}
return Optional.empty();
}

private static Optional<Comparable<?>> extractValueInParameterMarkerExpressionSegment(final ParameterMarkerExpressionSegment expression, final List<Object> params) {
return castToComparable(params.get(expression.getParameterMarkerIndex()));
private static Optional<Comparable<?>> extractLiteralExpressionValue(final LiteralExpressionSegment expression) {
return castToComparable(expression.getLiterals());
}

private static Optional<Comparable<?>> extractValueInLiteralExpressionSegment(final LiteralExpressionSegment expression) {
return castToComparable(expression.getLiterals());
private static Optional<Comparable<?>> extractParameterMarkerExpressionValue(final ParameterMarkerExpressionSegment expression, final List<Object> params) {
return castToComparable(params.get(expression.getParameterMarkerIndex()));
}

private static Optional<Comparable<?>> castToComparable(final Object object) {
return object instanceof Comparable<?> ? Optional.of((Comparable<?>) object) : Optional.empty();
private static Optional<Comparable<?>> castToComparable(final Object value) {
return value instanceof Comparable<?> ? Optional.of((Comparable<?>) value) : Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

class ColumnRegexMatchedShadowAlgorithmTest {

private static final String SHADOW_TABLE = "t_user";
private static final String SHADOW_TABLE = "foo_tbl";

private static final String SHADOW_COLUMN = "shadow";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

class ColumnValueMatchedShadowAlgorithmTest {

private static final String SHADOW_TABLE = "t_user";
private static final String SHADOW_TABLE = "foo_tbl";

private static final String SHADOW_COLUMN = "shadow";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,95 @@

package org.apache.shardingsphere.shadow.route.engine.util;

import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ListExpression;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.simple.SimpleExpressionSegment;
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.subquery.SubqueryExpressionSegment;
import org.junit.jupiter.api.Test;

import java.util.LinkedList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ShadowExtractorTest {

@Test
void assertExtractValuesSimpleExpressionSegment() {
void assertExtractValuesWithBinaryOperationExpression() {
SimpleExpressionSegment leftSegment = new LiteralExpressionSegment(1, 2, "left");
SimpleExpressionSegment rightSegment = new LiteralExpressionSegment(1, 2, "right");
ExpressionSegment expressionSegment = new BinaryOperationExpression(1, 2, leftSegment, rightSegment, "=", "text");
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(expressionSegment, Collections.emptyList());
assertTrue(actual.isPresent());
assertThat(actual.get().iterator().next(), is("right"));
}

@Test
void assertExtractValuesWithInExpression() {
SimpleExpressionSegment leftSegment = new LiteralExpressionSegment(1, 2, "left");
SimpleExpressionSegment rightSegment = new LiteralExpressionSegment(1, 2, "right");
ExpressionSegment expressionSegment = new InExpression(1, 2, leftSegment, rightSegment, false);
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(expressionSegment, Collections.emptyList());
assertTrue(actual.isPresent());
assertThat(actual.get().iterator().next(), is("right"));
}

@Test
void assertExtractValuesWithListExpression() {
SimpleExpressionSegment segment0 = new LiteralExpressionSegment(1, 2, "expect0");
SimpleExpressionSegment segment1 = new LiteralExpressionSegment(1, 2, "expect1");
ListExpression expressionSegment = new ListExpression(1, 2);
expressionSegment.getItems().add(segment0);
expressionSegment.getItems().add(segment1);
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(expressionSegment, Collections.emptyList());
assertTrue(actual.isPresent());
assertThat(actual.get().iterator().next(), is("expect0"));
}

@Test
void assertExtractValuesWithLiteralExpressionSegment() {
SimpleExpressionSegment simpleExpressionSegment = new LiteralExpressionSegment(1, 2, "expected");
List<Object> params = new LinkedList<>();
assertTrue(ShadowExtractor.extractValues(simpleExpressionSegment, params).isPresent());
assertThat(ShadowExtractor.extractValues(simpleExpressionSegment, params).get().iterator().next(), is("expected"));
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(simpleExpressionSegment, Collections.emptyList());
assertTrue(actual.isPresent());
assertThat(actual.get().iterator().next(), is("expected"));
}

@Test
void assertExtractValuesWithInvalidLiteralExpressionSegment() {
SimpleExpressionSegment simpleExpressionSegment = new LiteralExpressionSegment(1, 2, new Object());
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(simpleExpressionSegment, Collections.emptyList());
assertFalse(actual.isPresent());
}

@Test
void assertExtractValuesWithParameterMarkerExpressionSegment() {
SimpleExpressionSegment simpleExpressionSegment = new ParameterMarkerExpressionSegment(1, 2, 0);
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(simpleExpressionSegment, Collections.singletonList(10));
assertTrue(actual.isPresent());
assertThat(actual.get().iterator().next(), is(10));
}

@Test
void assertExtractValuesWithInvalidParameterMarkerExpressionSegment() {
SimpleExpressionSegment simpleExpressionSegment = new ParameterMarkerExpressionSegment(1, 2, 0);
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(simpleExpressionSegment, Collections.singletonList(new Object()));
assertFalse(actual.isPresent());
}

@Test
void assertExtractValuesWithSubqueryExpressionSegment() {
SimpleExpressionSegment simpleExpressionSegment = new SubqueryExpressionSegment(null);
List<Object> params = Collections.singletonList(new Object());
Optional<Collection<Comparable<?>>> actual = ShadowExtractor.extractValues(simpleExpressionSegment, params);
assertFalse(actual.isPresent());
}
}

0 comments on commit f361c51

Please sign in to comment.