-
Notifications
You must be signed in to change notification settings - Fork 145
/
SqlLineTest.java
237 lines (211 loc) · 8.23 KB
/
SqlLineTest.java
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
// Licensed to Julian Hyde under one or more contributor license
// agreements. See the NOTICE file distributed with this work for
// additional information regarding copyright ownership.
//
// Julian Hyde licenses this file to you under the Modified BSD License
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/BSD-3-Clause
*/
package sqlline;
import java.util.Objects;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Test cases for SQLLine.
*/
public class SqlLineTest {
/**
* Unit test for {@link SqlLine#splitCompound(String)}.
*/
@Test
public void testSplitCompound() {
final SqlLine line = new SqlLine();
String[][] strings;
// simple line
strings = line.splitCompound("abc de fgh");
assertArrayEquals(new String[][] {{"ABC"}, {"DE"}, {"FGH"}}, strings);
// line with double quotes
strings = line.splitCompound("abc \"de fgh\" ijk");
assertArrayEquals(new String[][] {{"ABC"}, {"de fgh"}, {"IJK"}}, strings);
// line with double quotes as first and last
strings = line.splitCompound("\"abc de\" fgh \"ijk\"");
assertArrayEquals(new String[][] {{"abc de"}, {"FGH"}, {"ijk"}}, strings);
// escaped double quotes, and dots inside quoted identifiers
strings = line.splitCompound("\"ab.c \"\"de\" fgh.ij");
assertArrayEquals(new String[][] {{"ab.c \"de"}, {"FGH", "IJ"}}, strings);
// single quotes do not affect parsing
strings = line.splitCompound("'abc de' fgh");
assertArrayEquals(new String[][] {{"'ABC"}, {"DE'"}, {"FGH"}}, strings);
// incomplete double-quoted identifiers are implicitly completed
strings = line.splitCompound("abcdefgh \"ijk");
assertArrayEquals(new String[][] {{"ABCDEFGH"}, {"ijk"}}, strings);
// dot at start of line is illegal, but we are lenient and ignore it
strings = line.splitCompound(".abc def.gh");
assertArrayEquals(new String[][] {{"ABC"}, {"DEF", "GH"}}, strings);
// spaces around dots are fine
strings = line.splitCompound("abc de . gh .i. j");
assertArrayEquals(
new String[][] {{"ABC"}, {"DE", "GH", "I", "J"}}, strings);
// double-quote inside an unquoted identifier is treated like a regular
// character; should be an error, but we are lenient
strings = line.splitCompound("abc\"de \"fg\"");
assertArrayEquals(new String[][] {{"ABC\"DE"}, {"fg"}}, strings);
// null value only if unquoted
strings = line.splitCompound("abc null");
assertArrayEquals(new String[][] {{"ABC"}, {null}}, strings);
strings = line.splitCompound("abc foo.null.bar");
assertArrayEquals(new String[][] {{"ABC"}, {"FOO", null, "BAR"}}, strings);
strings = line.splitCompound("abc foo.\"null\".bar");
assertArrayEquals(
new String[][] {{"ABC"}, {"FOO", "null", "BAR"}}, strings);
strings = line.splitCompound("abc foo.\"NULL\".bar");
assertArrayEquals(
new String[][] {{"ABC"}, {"FOO", "NULL", "BAR"}}, strings);
// trim trailing whitespace and semicolon
strings = line.splitCompound("abc ;\t ");
assertArrayEquals(new String[][] {{"ABC"}}, strings);
// keep semicolon inside line
strings = line.splitCompound("abc\t;def");
assertArrayEquals(new String[][] {{"ABC"}, {";DEF"}}, strings);
}
@Test
public void testRpad() {
assertThat(SqlLine.rpad("x", 1), is("x"));
assertThat(SqlLine.rpad("x", 2), is("x "));
assertThat(SqlLine.rpad("xyz", 2), is("xyz"));
assertThat(SqlLine.rpad(" x ", 5), is(" x "));
assertThat(SqlLine.rpad(null, 2), is(" "));
}
@Test
public void testCenterString() {
assertThat(SqlLine.center("abc", -1), is("abc"));
assertThat(SqlLine.center("abc", 1), is("abc"));
assertThat(SqlLine.center("abc", 4), is("abc "));
assertThat(SqlLine.center("abc", 5), is(" abc "));
// center used to have cartesian performance
assertThat(SqlLine.center("abc", 1234567).length(), is(1234567));
}
@Test
public void testLoadingSystemPropertiesOnCreate() {
System.setProperty("sqlline.Isolation", "TRANSACTION_NONE");
SqlLine line = new SqlLine();
try {
assertEquals(
"TRANSACTION_NONE", line.getOpts().getIsolation());
} finally {
// set back to the default for tests running in the same JVM.
System.setProperty("sqlline.Isolation", "TRANSACTION_REPEATABLE_READ");
}
}
@Test
public void testSplit() {
SqlLine line = new SqlLine();
String[] strings;
// query check
strings = line.split("values (1, cast(null as integer), "
+ "cast(null as varchar(3));", " ");
assertArrayEquals(
new String[]{"values", "(1,", "cast(null", "as", "integer),",
"cast(null", "as", "varchar(3));"}, strings);
// space
strings = line.split("set csvdelimiter ' '", " ");
assertArrayEquals(new String[]{"set", "csvdelimiter", " "}, strings);
// space with double quotes
strings = line.split("set csvdelimiter \" \"", " ");
assertArrayEquals(new String[]{"set", "csvdelimiter", " "}, strings);
// table and space
strings = line.split("set csvdelimiter '\t. '", " ");
assertArrayEquals(new String[]{"set", "csvdelimiter", "\t. "}, strings);
// \n
strings = line.split("set csvdelimiter ' ,\n; '", " ");
assertArrayEquals(new String[]{"set", "csvdelimiter", " ,\n; "}, strings);
// double quote inside singles
strings = line.split("set csvdelimiter ' \"\" \" '", " ");
assertArrayEquals(
new String[] {"set", "csvdelimiter", " \"\" \" "}, strings);
// single quotes inside doubles
strings = line.split("set csvdelimiter \" ' '' \"", " ");
assertArrayEquals(new String[]{"set", "csvdelimiter", " ' '' "}, strings);
// timestamp string
strings = line.split("set timestampformat 01/01/1970T12:32:12", " ");
assertArrayEquals(
new String[]{"set", "timestampformat", "01/01/1970T12:32:12"}, strings);
strings = line.split("?", " ");
assertArrayEquals(new String[]{"?"}, strings);
strings = line.split("#", " ");
assertArrayEquals(new String[]{"#"}, strings);
try {
line.split("set csvdelimiter '", " ");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.split("set csvdelimiter \"", " ");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.split("set csvdelimiter \"'", " ");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.split("set csvdelimiter \"a", " ");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.split("set csvdelimiter q'", " ");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.dequote("\"'");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.dequote("\"a");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
try {
line.dequote("q'");
fail("Non-paired quote is not allowed");
} catch (IllegalArgumentException e) {
//ok
}
}
@Test
public void testNextColorScheme() {
final SqlLine sqlLine = new SqlLine();
final String initialScheme = sqlLine.getOpts().getColorScheme();
String currentScheme = null;
// the artificial limit to check
// if finally we will come to the initial color scheme
final int limit = 10000;
int counter = 0;
while (counter++ < 10000 && !Objects.equals(initialScheme, currentScheme)) {
sqlLine.nextColorSchemeWidget();
currentScheme = sqlLine.getOpts().getColorScheme();
}
assertNotEquals(limit, counter);
assertEquals(initialScheme, currentScheme);
}
}
// End SqlLineTest.java