/
TestJDBCURL.java
340 lines (290 loc) · 16.3 KB
/
TestJDBCURL.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.jdbc;
import java.net.URLEncoder;
import java.util.Properties;
import junit.framework.TestCase;
import org.teiid.net.TeiidURL;
/**
* @since 4.3
*/
public class TestJDBCURL extends TestCase {
// Need to allow embedded spaces and ='s within optional properties
public final void testCredentials() throws Exception {
String credentials = URLEncoder.encode("defaultToLogon,(system=BQT1 SQL Server 2000 Simple Cap,user=xyz,password=xyz)", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$
JDBCURL url = new JDBCURL("jdbc:teiid:QT_sqls2kds@mm://slwxp136:43100;credentials="+credentials); //$NON-NLS-1$
Properties p = url.getProperties();
assertEquals("defaultToLogon,(system=BQT1 SQL Server 2000 Simple Cap,user=xyz,password=xyz)", p.getProperty("credentials")); //$NON-NLS-1$//$NON-NLS-2$
}
public void testJDBCURLWithProperties() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=%25xyz;password=***;logLevel=1;configFile=testdata/bqt/dqp_stmt_e2e.xmi;disableLocalTxn=true;autoFailover=false"; //$NON-NLS-1$
Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "%xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("logLevel", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", "testdata/bqt/dqp_stmt_e2e.xmi"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty(ExecutionProperties.DISABLE_LOCAL_TRANSACTIONS, "true"); //$NON-NLS-1$
expectedProperties.setProperty(TeiidURL.CONNECTION.AUTO_FAILOVER, "false"); //$NON-NLS-1$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
assertTrue(url.getJDBCURL().contains("user=%25xyz"));
}
public void testJDBCURLWithoutProperties() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345"; //$NON-NLS-1$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(new Properties(), url.getProperties());
}
public void testCaseConversion() {
// Different case ------------------------------------HERE -v ----------------and HERE -v
String URL = "jdbc:teiid:bqt@mm://localhost:12345;VERSION=1;user=xyz;password=***;configFile=testdata/bqt/dqp_stmt_e2e.xmi"; //$NON-NLS-1$
Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", "testdata/bqt/dqp_stmt_e2e.xmi"); //$NON-NLS-1$ //$NON-NLS-2$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
}
public void testWithExtraSemicolons() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password=***;logLevel=1;;;configFile=testdata/bqt/dqp_stmt_e2e.xmi;;"; //$NON-NLS-1$
Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("logLevel", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", "testdata/bqt/dqp_stmt_e2e.xmi"); //$NON-NLS-1$ //$NON-NLS-2$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
}
public void testWithWhitespace() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345; version =1;user= xyz ;password=***; logLevel = 1 ; configFile=testdata/bqt/dqp_stmt_e2e.xmi ;"; //$NON-NLS-1$
Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("logLevel", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", "testdata/bqt/dqp_stmt_e2e.xmi"); //$NON-NLS-1$ //$NON-NLS-2$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
}
public void testNoPropertyValue() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password=***;logLevel=;configFile="; //$NON-NLS-1$
Properties expectedProperties = new Properties();
expectedProperties.setProperty("version", "1"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("user", "xyz"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("password", "***"); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("logLevel", ""); //$NON-NLS-1$ //$NON-NLS-2$
expectedProperties.setProperty("configFile", ""); //$NON-NLS-1$ //$NON-NLS-2$
JDBCURL url = new JDBCURL(URL);
assertEquals("bqt", url.getVDBName()); //$NON-NLS-1$
assertEquals("mm://localhost:12345", url.getConnectionURL()); //$NON-NLS-1$
assertEquals(expectedProperties, url.getProperties());
}
public void testInvalidProtocol() {
String URL = "jdbc:monkeymatrix:bqt@mm://localhost:12345;version=1;user=xyz;password=***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
}
public void testNoVDBName() {
String URL = "jdbc:teiid:@mm://localhost:12345;version=1;user=xyz;password=***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
}
public void testNoAtSignInURL() {
String URL = "jdbc:teiid:bqt!mm://localhost:12345;version=1;user=xyz;password=***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
// No @ sign is llowed as part of embedded driver now,
// but this form of URL rejected in the acceptURL
//fail("Illegal argument should have failed.");
} catch (IllegalArgumentException e) {
// expected
}
}
public void testMoreThanOneAtSign() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xy@;password=***;logLevel=1"; //$NON-NLS-1$
try {
// this allowed as customer properties can have @ in their properties
new JDBCURL(URL);
} catch (IllegalArgumentException e) {
// expected
}
}
public void testNoEqualsInProperty() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
}
public void testMoreThanOneEqualsInProperty() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password==***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;password=***=;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;=password=***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
}
public void testNoKeyInProperty() {
String URL = "jdbc:teiid:bqt@mm://localhost:12345;version=1;user=xyz;=***;logLevel=1"; //$NON-NLS-1$
try {
new JDBCURL(URL);
fail("Illegal argument should have failed."); //$NON-NLS-1$
} catch (IllegalArgumentException e) {
// expected
}
}
public void testConstructor() {
JDBCURL url = new JDBCURL("myVDB", "mm://myhost:12345",null); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("jdbc:teiid:myVDB@mm://myhost:12345", url.getJDBCURL()); //$NON-NLS-1$
Properties props = new Properties();
props.setProperty(BaseDataSource.USER_NAME, "myuser"); //$NON-NLS-1$
props.setProperty(BaseDataSource.PASSWORD, "mypassword"); //$NON-NLS-1$
props.put("ClieNTtOKeN", new Integer(1)); //$NON-NLS-1$
url = new JDBCURL("myVDB", "mm://myhost:12345", props); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("jdbc:teiid:myVDB@mm://myhost:12345;user=myuser;password=mypassword", url.getJDBCURL()); //$NON-NLS-1$
}
public void testConstructor_Exception() {
try {
new JDBCURL(null, "myhost", null); //$NON-NLS-1$
fail("Should have failed."); //$NON-NLS-1$
} catch (Exception e) {
}
try {
new JDBCURL(" ", "myhost", null); //$NON-NLS-1$ //$NON-NLS-2$
fail("Should have failed."); //$NON-NLS-1$
} catch (Exception e) {
}
try {
// in embedded situation there is no connection url
new JDBCURL("myVDB", " ", null); //$NON-NLS-1$ //$NON-NLS-2$
} catch (Exception e) {
}
}
public void testNormalize() {
Properties props = new Properties();
props.setProperty("UsEr", "myuser"); //$NON-NLS-1$ //$NON-NLS-2$
props.setProperty("pAssWOrD", "mypassword"); //$NON-NLS-1$ //$NON-NLS-2$
props.put("ClieNTtOKeN", new Integer(1)); //$NON-NLS-1$
JDBCURL.normalizeProperties(props);
assertEquals("myuser", props.getProperty(BaseDataSource.USER_NAME)); //$NON-NLS-1$
assertEquals("mypassword", props.getProperty(BaseDataSource.PASSWORD)); //$NON-NLS-1$
}
public final void testEncodedPropertyProperties() throws Exception {
String password = "=@#^&*()+!%$^%@#_-)_~{}||\\`':;,./<>?password has = & %"; //$NON-NLS-1$
Properties props = new Properties();
props.setProperty("UsEr", "foo"); //$NON-NLS-1$ //$NON-NLS-2$
props.setProperty("PASswoRd", password); //$NON-NLS-1$
JDBCURL.normalizeProperties(props);
assertEquals(password, props.getProperty("password")); //$NON-NLS-1$
assertEquals("foo", props.getProperty("user")); //$NON-NLS-1$//$NON-NLS-2$
}
public final void testEncodedPropertyInURL() throws Exception {
String password = "=@#^&*()+!%$^%@#_-)_~{}||\\`':;,./<>?password has = & %"; //$NON-NLS-1$
String encPassword = URLEncoder.encode(password, "UTF-8"); //$NON-NLS-1$
JDBCURL url = new JDBCURL("jdbc:teiid:QT_sqls2kds@mm://slwxp136:43100;PASswoRd="+encPassword); //$NON-NLS-1$
Properties p = url.getProperties();
assertEquals(password, p.getProperty("password")); //$NON-NLS-1$
}
public void testGetServerURL_NoProperties() {
String result = new JDBCURL("jdbc:teiid:designtimecatalog@mm://slwxp172:44401;user=ddifranco;password=mm").getConnectionURL(); //$NON-NLS-1$
assertEquals("mm://slwxp172:44401", result); //$NON-NLS-1$
}
public void testGetServerURL_Properties() {
String result = new JDBCURL("jdbc:teiid:designtimecatalog@mm://slwxp172:44401;user=ddifranco;password=mm").getConnectionURL(); //$NON-NLS-1$
assertEquals("mm://slwxp172:44401", result); //$NON-NLS-1$
}
/**
* Test getServerURL with a valid URL and password that contains at least
* one ASCII character in the range of 32 to 126 excluding the ; and = sign.
*
* @since 5.0.2
*/
public void testGetServerURL_PasswordProperties() throws Exception {
String result = null;
String srcURL = "jdbc:teiid:designtimecatalog@mm://slwxp172:44401;user=ddifranco;password="; //$NON-NLS-1$
String password = null;
String tgtURL = "mm://slwxp172:44401"; //$NON-NLS-1$
for ( char ch = 32; ch <= 126; ch++ ) {
//exclude URL reserved characters
if ( ch != ';' && ch != '=' && ch != '%') {
password = ch+"mm"; //$NON-NLS-1$
result = new JDBCURL(srcURL+URLEncoder.encode(password, "UTF-8")).getConnectionURL(); //$NON-NLS-1$
assertEquals("Failed to obtain correct ServerURL when using password "+password,tgtURL, result); //$NON-NLS-1$
}
}
}
public void testGetServerURL_2Servers() {
String result = new JDBCURL("jdbc:teiid:designtimecatalog@mm://slwxp172:44401,slabc123:12345;user=ddifranco;password=mm").getConnectionURL(); //$NON-NLS-1$
assertEquals("mm://slwxp172:44401,slabc123:12345", result); //$NON-NLS-1$
}
public void testBuildEmbeedURL() {
JDBCURL url = new JDBCURL("vdb", "/home/foo/deploy.properties", new Properties()); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("jdbc:teiid:vdb@/home/foo/deploy.properties", url.getJDBCURL()); //$NON-NLS-1$
Properties p = new Properties();
p.setProperty("user", "test"); //$NON-NLS-1$ //$NON-NLS-2$
p.setProperty("password", "pass"); //$NON-NLS-1$ //$NON-NLS-2$
p.setProperty("autoFailover", "true"); //$NON-NLS-1$ //$NON-NLS-2$
p.setProperty("any", "thing"); //$NON-NLS-1$ //$NON-NLS-2$
url = new JDBCURL("vdb", "/home/foo/deploy.properties", p); //$NON-NLS-1$ //$NON-NLS-2$
assertTrue(url.getJDBCURL().startsWith("jdbc:teiid:vdb@/home/foo/deploy.properties;user=test;")); //$NON-NLS-1$
assertTrue(url.getJDBCURL().indexOf("any=thing")!=-1); //$NON-NLS-1$
assertTrue(url.getJDBCURL().indexOf("password=pass")!=-1); //$NON-NLS-1$
assertTrue(url.getJDBCURL().indexOf("autoFailover=true")!=-1); //$NON-NLS-1$
}
}