1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.jdbc4olap.jdbc;
24
25 import junit.framework.TestCase;
26
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.util.Properties;
31 import java.sql.Connection;
32 import java.sql.DriverPropertyInfo;
33
34 import org.jdbc4olap.xmla.XmlaLogin;
35
36
37
38
39
40
41 public final class OlapDriverTest extends TestCase {
42
43 private static final File TEST_PROPS_FILE = new File("test.properties");
44 private static final Properties TEST_PROPS = new Properties();
45 static {
46 final FileInputStream fis;
47 try {
48 fis = new FileInputStream(TEST_PROPS_FILE);
49 try {
50 TEST_PROPS.load(fis);
51 } finally {
52 fis.close();
53 }
54
55 } catch (IOException e) {
56 System.out.println("Missing unit test properties file: " + TEST_PROPS_FILE.getAbsolutePath());
57 System.out.println("Some unit tests will fail or be skipped.");
58 e.printStackTrace();
59 }
60 }
61
62 private static boolean isRunConnectionTests() {
63 return Boolean.valueOf(TEST_PROPS.getProperty("test.isRunConnectionTests"));
64 }
65
66 private static String getTestUrl() {
67 return TEST_PROPS.getProperty("test.url");
68 }
69
70 private static String getTestUser() {
71 return TEST_PROPS.getProperty("test.user");
72 }
73
74 private static String getTestPwd() {
75 return TEST_PROPS.getProperty("test.pwd");
76 }
77
78 public static String getTestCatalog() {
79 return TEST_PROPS.getProperty("test.catalog");
80 }
81
82 public static String getTestSchema() {
83 return TEST_PROPS.getProperty("test.schema");
84 }
85
86 public static String getTestCube() {
87 return TEST_PROPS.getProperty("test.cube");
88 }
89
90 private static String getTestTableAlias() {
91 return TEST_PROPS.getProperty("test.tablealias");
92 }
93
94 public static String getOldDriverTestURL() {
95 return OlapDriver.JDBC_URL_PREFIX + "http://"
96 + getTestUser() + ":" + getTestPwd() + "@"
97 + getTestUrl().substring("http://".length());
98 }
99
100 public static String getTestColumn() {
101 return TEST_PROPS.getProperty("test.column");
102 }
103
104
105 private OlapDriver olapDriver;
106
107 @Override
108 protected void setUp() throws Exception {
109 olapDriver = new OlapDriver();
110 }
111
112
113 public void testAcceptsURL() throws Exception {
114 assertTrue(olapDriver.acceptsURL(OlapDriver.JDBC_URL_PREFIX));
115 assertTrue(olapDriver.acceptsURL(OlapDriver.JDBC_URL_PREFIX + "x"));
116 assertFalse(olapDriver.acceptsURL(OlapDriver.JDBC_URL_PREFIX.substring(0,
117 OlapDriver.JDBC_URL_PREFIX.length() - 1)));
118 }
119
120 public void testGetDriverInfo() throws Exception {
121
122 DriverPropertyInfo[] result = olapDriver.getPropertyInfo(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), null);
123 assertDriverProps(result);
124
125 result = olapDriver.getPropertyInfo(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), new Properties());
126 assertDriverProps(result);
127
128 final Properties info = new Properties();
129 final String currentValue = "someUser";
130 info.put(XmlaLogin.PROPERTY_NAME_USER, currentValue);
131 result = olapDriver.getPropertyInfo(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
132 assertDriverProps(result);
133 assertEquals(currentValue, result[0].value);
134
135 info.clear();
136 info.setProperty(XmlaLogin.PROPERTY_NAME_USER, getTestUser());
137 info.setProperty(XmlaLogin.PROPERTY_NAME_PASSWORD, getTestPwd());
138
139 DriverPropertyInfo[] driverInfo;
140 try {
141 driverInfo = olapDriver.getPropertyInfo(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
142 } catch (Exception e) {
143 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), e);
144 }
145 assertNotNull(driverInfo);
146 try {
147 assertEquals(0, driverInfo.length);
148 } catch (Throwable t) {
149 if (isRunConnectionTests()) {
150 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), t);
151 } else {
152
153 printTestWarningServerAccesTestSkippedMsg(OlapDriverTest.class, this);
154 return;
155 }
156 }
157 assertEquals(null, result[1].value);
158
159
160 try {
161 driverInfo = olapDriver.getPropertyInfo(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
162 } catch (Exception e) {
163 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), e);
164 }
165 assertNotNull(driverInfo);
166 assertEquals(0, driverInfo.length);
167 }
168
169 private static void assertDriverProps(final DriverPropertyInfo[] result) {
170 int i = 0;
171 assertEquals(XmlaLogin.PROPERTY_NAME_USER, result[i++].name);
172 assertEquals(XmlaLogin.PROPERTY_NAME_PASSWORD, result[i++].name);
173
174
175
176 for (String[] olapVendor : OlapDriver.PROPERTY_VALUE_DRIVER_URL_PATTERNS) {
177 assertEquals(OlapDriver.PROPERTY_NAME_DRIVER_URL_PATTERN, result[i].name);
178 assertTrue(result[i++].description.contains(olapVendor[0]));
179 }
180
181 assertEquals(2 + OlapDriver.PROPERTY_VALUE_DRIVER_URL_PATTERNS.length, result.length);
182 }
183
184
185 private static final String QT = "\"";
186
187
188
189
190
191
192
193
194
195 public static final String SQL_SELECT_WITH_TBL_ALIAS = "SELECT "
196 + QT + getTestCube() + OlapDatabaseMetaData.CATALOG_SEPARATOR + getTestColumn() + QT
197 + " FROM "
198 + QT + getTestCatalog() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
199 + QT + getTestCatalog() + "/" + getTestSchema() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
200 + QT + getTestCube() + QT
201 + " " + getTestTableAlias();
202
203
204 public static final String SQL_SELECT_JOIN = "SELECT "
205 + QT + getTestCube() + OlapDatabaseMetaData.CATALOG_SEPARATOR + getTestColumn() + QT
206 + " FROM "
207 + QT + getTestCatalog() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
208 + QT + getTestCatalog() + "/" + getTestSchema() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
209 + QT + getTestCube() + QT;
210
211
212
213 public void testTableAlias() throws Exception {
214 final Properties info = new Properties();
215 info.setProperty(XmlaLogin.PROPERTY_NAME_USER, getTestUser());
216 info.setProperty(XmlaLogin.PROPERTY_NAME_PASSWORD, getTestPwd());
217
218 final Connection conn;
219 try {
220 conn = olapDriver.connect(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
221 } catch (Exception e) {
222 if (isRunConnectionTests()) {
223 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), e);
224 } else {
225
226 printTestWarningServerAccesTestSkippedMsg(OlapDriverTest.class, this);
227 return;
228 }
229 }
230
231 try {
232 assertNotNull(conn);
233 assertFalse(conn.isClosed());
234
235 final OlapStatement st = (OlapStatement) conn.createStatement();
236 final OlapResultSet rs;
237 try {
238 rs = (OlapResultSet) st.executeQuery(SQL_SELECT_WITH_TBL_ALIAS);
239 } catch (Throwable throwable) {
240 throw new Exception("Error parsing SQL: " + SQL_SELECT_WITH_TBL_ALIAS, throwable);
241 }
242 assertTrue(rs.getData().size() > 0);
243 } finally {
244 assert conn != null;
245 conn.close();
246 assertTrue(conn.isClosed());
247 }
248
249 }
250
251
252 public void testConnect() throws Exception {
253 final Properties info = new Properties();
254 info.setProperty(XmlaLogin.PROPERTY_NAME_USER, getTestUser());
255 info.setProperty(XmlaLogin.PROPERTY_NAME_PASSWORD, getTestPwd());
256
257 Connection conn;
258 try {
259 conn = olapDriver.connect(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
260
261 } catch (Exception e) {
262 if (isRunConnectionTests()) {
263 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), e);
264 } else {
265
266 printTestWarningServerAccesTestSkippedMsg(OlapDriverTest.class, this);
267 return;
268 }
269 }
270
271
272
273 try {
274 assertNotNull(conn);
275
276
277
278
279
280
281
282
283
284
285 assertFalse(conn.isClosed());
286 } finally {
287 assert conn != null;
288 conn.close();
289 assertTrue(conn.isClosed());
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 private static String printTestWarningServerAccesMsg(final Class testClass, final TestCase test) {
321 final String msg = "**** WARNING: The unit test: " + testClass.getName() + "." + test.getName()
322 + "() \n\trequires access to a server. \nBe sure: "
323 + TEST_PROPS_FILE.getAbsolutePath()
324 + " \n\tis setup correctly AND the server is available on the network (vpn).\n"
325 + "Be sure you can connect a web browser to: " + getTestUrl();
326 System.out.println(msg + "\n");
327 return msg;
328 }
329
330 private static void printTestWarningServerAccesTestSkippedMsg(final Class testClass, final TestCase test) {
331 final String msg = "**** WARNING: The unit test: " + testClass.getName() + "." + test.getName()
332 + "() \n\t is being skipped. This test requires access to a server. \nTo run these tests, be sure: "
333 + TEST_PROPS_FILE.getAbsolutePath()
334 + " \n\tis setup correctly (and set test.isRunConnectionTests=true) AND the server is available on the network (vpn).\n"
335 + "Be sure you can connect a web browser to: " + getTestUrl();
336 System.out.println(msg + "\n");
337 }
338 }