Repository navigation
Expand file tree
/
Copy pathEmptyStatementInputTest.java
More file actions
89 lines (82 loc) · 3.74 KB
/
Copy pathEmptyStatementInputTest.java
File metadata and controls
89 lines (82 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.statement.Statements;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.*;
class EmptyStatementInputTest {
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" \t\r\n", "/* nothing */", "-- nothing\n"})
void rejectsInputWithoutASingleStatement(String sql) {
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql));
for (boolean allowComplex : new boolean[] {false, true}) {
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql,
parser -> parser.withAllowComplexParsing(allowComplex)));
}
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" \t\r\n", "/* nothing */", "-- nothing\n"})
void returnsAnEmptyStatementList(String sql) throws Exception {
assertTrue(CCJSqlParserUtil.parseStatements(sql).isEmpty());
for (boolean allowComplex : new boolean[] {false, true}) {
assertTrue(CCJSqlParserUtil.parseStatements(sql,
parser -> parser.withAllowComplexParsing(allowComplex)).isEmpty());
}
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {" \t\r\n", "/* nothing */", "-- nothing\n"})
void preservesTheContractWithACallerExecutor(String sql) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try {
for (boolean allowComplex : new boolean[] {false, true}) {
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(sql, executor,
parser -> parser.withAllowComplexParsing(allowComplex)));
assertTrue(CCJSqlParserUtil.parseStatements(sql, executor,
parser -> parser.withAllowComplexParsing(allowComplex)).isEmpty());
}
assertFalse(executor.isShutdown());
assertEquals("SELECT 1", CCJSqlParserUtil.parse("SELECT 1", executor, null).toString());
assertEquals(2,
CCJSqlParserUtil.parseStatements("SELECT 1; SELECT 2", executor, null).size());
} finally {
executor.shutdownNow();
}
}
@ParameterizedTest
@ValueSource(strings = {"", " \t\r\n", "/* nothing */", "-- nothing\n"})
void agreesWithReaderAndStreamParsing(String sql) {
assertThrows(JSQLParserException.class,
() -> CCJSqlParserUtil.parse(new StringReader(sql)));
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(
new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8))));
assertThrows(JSQLParserException.class, () -> CCJSqlParserUtil.parse(
new ByteArrayInputStream(sql.getBytes(StandardCharsets.UTF_8)), "UTF-8"));
}
@Test
void emptyResultsRemainIndependentAndMutable() throws Exception {
Statements first = CCJSqlParserUtil.parseStatements("");
first.add(CCJSqlParserUtil.parse("SELECT 1"));
assertTrue(CCJSqlParserUtil.parseStatements("").isEmpty());
assertTrue(CCJSqlParserUtil.parseStatements((String) null).isEmpty());
assertEquals(1, first.size());
}
}