|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.parser; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import net.sf.jsqlparser.JSQLParserException; |
| 23 | +import net.sf.jsqlparser.expression.Expression; |
| 24 | +import net.sf.jsqlparser.parser.feature.Feature; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.EnumSource; |
| 27 | + |
| 28 | +class ExpressionParsingFallbackTest { |
| 29 | + private static final String PARTIAL_EXPRESSION = "[customer].[id] = 1 trailing tokens"; |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @EnumSource(ParserApi.class) |
| 33 | + void configuresCompleteExpressionsOnce(ParserApi api) throws JSQLParserException { |
| 34 | + List<CCJSqlParser> attempts = new ArrayList<>(); |
| 35 | + Expression result = api.parse("[customer].[id] = 1", false, parser -> { |
| 36 | + attempts.add(parser); |
| 37 | + parser.withSquareBracketQuotation(true); |
| 38 | + }); |
| 39 | + |
| 40 | + assertEquals("[customer].[id] = 1", result.toString()); |
| 41 | + assertEquals(1, attempts.size()); |
| 42 | + } |
| 43 | + |
| 44 | + @ParameterizedTest |
| 45 | + @EnumSource(ParserApi.class) |
| 46 | + void reappliesConfigurationToAFreshParserForPartialInput(ParserApi api) |
| 47 | + throws JSQLParserException { |
| 48 | + List<CCJSqlParser> attempts = new ArrayList<>(); |
| 49 | + List<Boolean> modes = new ArrayList<>(); |
| 50 | + Expression result = api.parse(PARTIAL_EXPRESSION, true, parser -> { |
| 51 | + attempts.add(parser); |
| 52 | + modes.add(parser.getAsBoolean(Feature.allowComplexParsing)); |
| 53 | + parser.withSquareBracketQuotation(true); |
| 54 | + }); |
| 55 | + |
| 56 | + assertEquals("[customer].[id] = 1", result.toString()); |
| 57 | + assertEquals(List.of(false, true), modes); |
| 58 | + assertNotSame(attempts.get(0), attempts.get(1)); |
| 59 | + } |
| 60 | + |
| 61 | + @ParameterizedTest |
| 62 | + @EnumSource(ParserApi.class) |
| 63 | + void rejectsTrailingTokensWhenPartialParsingIsDisabled(ParserApi api) { |
| 64 | + JSQLParserException exception = assertThrows(JSQLParserException.class, |
| 65 | + () -> api.parse(PARTIAL_EXPRESSION, false, |
| 66 | + parser -> parser.withSquareBracketQuotation(true))); |
| 67 | + |
| 68 | + assertEquals("could only parse partial expression [customer].[id] = 1", |
| 69 | + exception.getMessage()); |
| 70 | + assertNull(exception.getCause()); |
| 71 | + } |
| 72 | + |
| 73 | + @ParameterizedTest |
| 74 | + @EnumSource(ParserApi.class) |
| 75 | + void preservesSyntaxFailureCauses(ParserApi api) { |
| 76 | + for (boolean partial : new boolean[] {false, true}) { |
| 77 | + JSQLParserException exception = assertThrows(JSQLParserException.class, |
| 78 | + () -> api.parse(";", partial, null)); |
| 79 | + |
| 80 | + ParseException cause = assertInstanceOf(ParseException.class, exception.getCause()); |
| 81 | + assertEquals(cause.getMessage(), exception.getMessage()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @ParameterizedTest |
| 86 | + @EnumSource(ParserApi.class) |
| 87 | + void leavesNullAndEmptyInputUnconfigured(ParserApi api) throws JSQLParserException { |
| 88 | + Consumer<CCJSqlParser> unexpectedConfiguration = parser -> { |
| 89 | + throw new AssertionError("Empty input must not configure a parser"); |
| 90 | + }; |
| 91 | + for (boolean partial : new boolean[] {false, true}) { |
| 92 | + assertNull(api.parse(null, partial, unexpectedConfiguration)); |
| 93 | + assertNull(api.parse("", partial, unexpectedConfiguration)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @ParameterizedTest |
| 98 | + @EnumSource(ParserApi.class) |
| 99 | + void acceptsNullConfiguration(ParserApi api) throws JSQLParserException { |
| 100 | + assertEquals("x = 92", api.parse("x=92", false, null).toString()); |
| 101 | + assertEquals("x = 92", api.parse("x=92 trailing tokens", true, null).toString()); |
| 102 | + } |
| 103 | + |
| 104 | + @ParameterizedTest |
| 105 | + @EnumSource(ParserApi.class) |
| 106 | + void propagatesConfigurationFailuresWithoutRetrying(ParserApi api) { |
| 107 | + IllegalStateException failure = new IllegalStateException("Configuration failed"); |
| 108 | + List<CCJSqlParser> attempts = new ArrayList<>(); |
| 109 | + IllegalStateException exception = assertThrows(IllegalStateException.class, |
| 110 | + () -> api.parse("x=92", false, parser -> { |
| 111 | + attempts.add(parser); |
| 112 | + throw failure; |
| 113 | + })); |
| 114 | + |
| 115 | + assertSame(failure, exception); |
| 116 | + assertEquals(1, attempts.size()); |
| 117 | + } |
| 118 | + |
| 119 | + private enum ParserApi { |
| 120 | + EXPRESSION, CONDITION; |
| 121 | + |
| 122 | + Expression parse(String text, boolean allowPartialParse, Consumer<CCJSqlParser> consumer) |
| 123 | + throws JSQLParserException { |
| 124 | + return this == EXPRESSION |
| 125 | + ? CCJSqlParserUtil.parseExpression(text, allowPartialParse, consumer) |
| 126 | + : CCJSqlParserUtil.parseCondExpression(text, allowPartialParse, consumer); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments