|
| 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 net.sf.jsqlparser.JSQLParserException; |
| 13 | +import net.sf.jsqlparser.statement.UnsupportedStatement; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.ExecutorService; |
| 18 | +import java.util.concurrent.Executors; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.concurrent.TimeoutException; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +class ParseStatementsFailureTest { |
| 25 | + @Test |
| 26 | + void reportsFailureWhenComplexParsingIsDisabled() { |
| 27 | + JSQLParserException exception = assertThrows(JSQLParserException.class, |
| 28 | + () -> CCJSqlParserUtil.parseStatements("SELECT FROM", |
| 29 | + parser -> parser.withAllowComplexParsing(false))); |
| 30 | + assertNotNull(exception.getCause()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void reportsFailureWhenNestingPreventsRetry() { |
| 35 | + assertThrows(JSQLParserException.class, |
| 36 | + () -> CCJSqlParserUtil.parseStatements("SELECT (( FROM", |
| 37 | + parser -> parser.withAllowComplexParsing(true).withAllowedNestingDepth(0))); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void leavesCallerExecutorUsableAfterFailure() throws Exception { |
| 42 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 43 | + try { |
| 44 | + assertThrows(JSQLParserException.class, |
| 45 | + () -> CCJSqlParserUtil.parseStatements("SELECT FROM", executor, |
| 46 | + parser -> parser.withAllowComplexParsing(false))); |
| 47 | + assertFalse(executor.isShutdown()); |
| 48 | + assertEquals(2, CCJSqlParserUtil.parseStatements("SELECT 1; SELECT 2", executor, |
| 49 | + parser -> parser.withAllowComplexParsing(false)).size()); |
| 50 | + } finally { |
| 51 | + executor.shutdownNow(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void reportsTimeoutWhenComplexParsingIsDisabled() { |
| 57 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 58 | + CountDownLatch release = new CountDownLatch(1); |
| 59 | + executor.submit(() -> release.await(30, TimeUnit.SECONDS)); |
| 60 | + try { |
| 61 | + JSQLParserException exception = assertThrows(JSQLParserException.class, |
| 62 | + () -> CCJSqlParserUtil.parseStatements("SELECT 1", executor, |
| 63 | + parser -> parser.withAllowComplexParsing(false).withTimeOut(50))); |
| 64 | + assertInstanceOf(TimeoutException.class, exception.getCause()); |
| 65 | + } finally { |
| 66 | + release.countDown(); |
| 67 | + executor.shutdownNow(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void preservesEmptyInputAndUnsupportedStatementContracts() throws Exception { |
| 73 | + assertNull(CCJSqlParserUtil.parseStatements((String) null)); |
| 74 | + assertNull(CCJSqlParserUtil.parseStatements("")); |
| 75 | + assertInstanceOf(UnsupportedStatement.class, |
| 76 | + CCJSqlParserUtil.parseStatements("SELECT 1; WHATEVER !", |
| 77 | + parser -> parser.withAllowComplexParsing(false) |
| 78 | + .withUnsupportedStatements(true)) |
| 79 | + .get(1)); |
| 80 | + } |
| 81 | +} |
0 commit comments