-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathCCJSqlParserUtilTest.java
More file actions
188 lines (156 loc) · 6.49 KB
/
CCJSqlParserUtilTest.java
File metadata and controls
188 lines (156 loc) · 6.49 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package net.sf.jsqlparser.parser;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.Statements;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
*
* @author toben
*/
public class CCJSqlParserUtilTest {
public CCJSqlParserUtilTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of parseExpression method, of class CCJSqlParserUtil.
*/
@Test
public void testParseExpression() throws Exception {
Expression result = CCJSqlParserUtil.parseExpression("a+b");
assertEquals("a + b", result.toString());
assertTrue(result instanceof Addition);
Addition add = (Addition) result;
assertTrue(add.getLeftExpression() instanceof Column);
assertTrue(add.getRightExpression() instanceof Column);
}
@Test
public void testParseExpression2() throws Exception {
Expression result = CCJSqlParserUtil.parseExpression("2*(a+6.0)");
assertEquals("2 * (a + 6.0)", result.toString());
assertTrue(result instanceof Multiplication);
Multiplication mult = (Multiplication) result;
assertTrue(mult.getLeftExpression() instanceof LongValue);
assertTrue(mult.getRightExpression() instanceof Parenthesis);
}
@Test(expected = JSQLParserException.class)
public void testParseExpressionNonPartial() throws Exception {
CCJSqlParserUtil.parseExpression("a+", false);
}
@Test(expected = JSQLParserException.class)
public void testParseExpressionFromStringFail() throws Exception {
CCJSqlParserUtil.parse("whatever$");
}
@Test(expected = JSQLParserException.class)
public void testParseExpressionFromRaderFail() throws Exception {
CCJSqlParserUtil.parse(new StringReader("whatever$"));
}
@Test
public void testParseExpressionNonPartial2() throws Exception {
Expression result = CCJSqlParserUtil.parseExpression("a+", true);
assertEquals("a", result.toString());
}
@Test
public void testParseCondExpression() throws Exception {
Expression result = CCJSqlParserUtil.parseCondExpression("a+b>5 and c<3");
assertEquals("a + b > 5 AND c < 3", result.toString());
}
@Test(expected = JSQLParserException.class)
public void testParseCondExpressionFail() throws Exception {
CCJSqlParserUtil.parseCondExpression(";");
}
@Test(expected = JSQLParserException.class)
public void testParseFromStreamFail() throws Exception {
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)));
}
@Test(expected = JSQLParserException.class)
public void testParseFromStreamWithEncodingFail() throws Exception {
CCJSqlParserUtil.parse(new ByteArrayInputStream("BLA".getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8.name());
}
@Test
public void testParseCondExpressionNonPartial() throws Exception {
Expression result = CCJSqlParserUtil.parseCondExpression("x=92 and y=29", false);
assertEquals("x = 92 AND y = 29", result.toString());
}
@Test(expected = JSQLParserException.class)
public void testParseCondExpressionNonPartial2() throws Exception {
Expression result = CCJSqlParserUtil.parseCondExpression("x=92 lasd y=29", false);
System.out.println(result.toString());
}
@Test
public void testParseCondExpressionPartial2() throws Exception {
Expression result = CCJSqlParserUtil.parseCondExpression("x=92 lasd y=29", true);
assertEquals("x = 92", result.toString());
}
@Test
public void testParseCondExpressionIssue471() throws Exception {
Expression result = CCJSqlParserUtil.parseCondExpression("(SSN,SSM) IN ('11111111111111', '22222222222222')");
assertEquals("(SSN, SSM) IN ('11111111111111', '22222222222222')", result.toString());
}
@Test
public void testParseStatementsIssue691() throws Exception {
Statements result = CCJSqlParserUtil.parseStatements(
"select * from dual;\n"
+ "\n"
+ "select\n"
+ "*\n"
+ "from\n"
+ "dual;\n"
+ "\n"
+ "select *\n"
+ "from dual;");
assertEquals("SELECT * FROM dual;\n"
+ "SELECT * FROM dual;\n"
+ "SELECT * FROM dual;\n", result.toString());
}
@Test(expected = JSQLParserException.class)
public void testParseStatementsFail() throws Exception {
CCJSqlParserUtil.parseStatements("select * from dual;WHATEVER!!");
}
@Test(expected = JSQLParserException.class)
public void testParseASTFail() throws Exception {
CCJSqlParserUtil.parseAST("select * from dual;WHATEVER!!");
}
@Test
public void testParseStatementsIssue691_2() throws Exception {
Statements result = CCJSqlParserUtil.parseStatements(
"select * from dual;\n"
+ "---test");
assertEquals("SELECT * FROM dual;\n", result.toString());
}
@Test
public void testParseStatementIssue742() throws Exception {
Statements result = CCJSqlParserUtil.parseStatements("CREATE TABLE `table_name` (\n" +
" `id` bigint(20) NOT NULL AUTO_INCREMENT,\n" +
" `another_column_id` bigint(20) NOT NULL COMMENT 'column id as sent by SYSTEM',\n" +
" PRIMARY KEY (`id`),\n" +
" UNIQUE KEY `uk_another_column_id` (`another_column_id`)\n" +
")");
assertEquals("CREATE TABLE `table_name` (`id` bigint (20) NOT NULL AUTO_INCREMENT, `another_column_id` " +
"bigint (20) NOT NULL COMMENT 'column id as sent by SYSTEM', PRIMARY KEY (`id`), UNIQUE KEY `uk_another_column_id` " +
"(`another_column_id`));\n", result.toString());
}
}