1   /*
2    * PRELYTIS.
3    * Copyright 2007, PRELYTIS S.A., and individual contributors
4    * as indicated by the @author tags. See the copyright.txt file in the
5    * distribution for a full listing of individual contributors.
6    *
7    * This is free software; you can redistribute it and/or modify it
8    * under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   * @author Dan Rollo
38   * Date: Jul 8, 2008
39   * Time: 5:47:54 PM
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                 // skipping connection tests, so ignore and stop here
153                 printTestWarningServerAccesTestSkippedMsg(OlapDriverTest.class, this);
154                 return;
155             }
156         }
157         assertEquals(null, result[1].value); // don't show current value of current password
158 
159         //info.setProperty(StandardPropertyManager.CATALOG, "someCatalog");
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         //assertEquals(StandardPropertyManager.CATALOG, result[i++].name);
174         //assertEquals(StandardPropertyManager.CUBE, result[i++].name);
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 //    public static final String SQL_SELECT_WITH_TBL_ALIAS = "SELECT "
188 //            + getTestTableAlias() + OlapDatabaseMetaData.CATALOG_SEPARATOR + QT + getTestCube() + OlapDatabaseMetaData.CATALOG_SEPARATOR + getTestColumn() + QT
189 //            + " FROM "
190 //            + QT + getTestCatalog() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
191 //                    + QT + getTestCatalog() + "/" + getTestSchema() + QT + OlapDatabaseMetaData.CATALOG_SEPARATOR
192 //            + QT + getTestCube() + QT
193 //            + " " + getTestTableAlias();
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(); // parse works with or w/out this alias here
202 
203     // @todo finish join sql
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             //+ " " + getTestTableAlias();
211 
212     // NOTE: This test attempts to hit a real olap database
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                 // skipping connection tests, so ignore and stop here
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     // NOTE: This test attempts to hit a real olap database
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             //conn = olapDriver.connect(getOldDriverTestURL(), new Properties());
261         } catch (Exception e) {
262             if (isRunConnectionTests()) {
263                 throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), e);
264             } else {
265                 // skipping connection tests, so ignore and stop here
266                 printTestWarningServerAccesTestSkippedMsg(OlapDriverTest.class, this);
267                 return;
268             }
269         }
270 
271 //        final List<Object[]> allCatalogs;
272 //        final List<Object[]> allSchemas;
273         try {
274             assertNotNull(conn);
275 
276 //            allCatalogs = ((OlapResultSet) conn.getMetaData().getCatalogs()).getData();
277 //            assertTrue("Does the test db have only 1 catalog? count: " + allCatalogs, allCatalogs.size() > 1);
278 //
279 //            allSchemas = ((OlapResultSet) conn.getMetaData().getSchemas()).getData();
280 //            assertTrue("Does the test db have only 1 cube? count: " + allSchemas, allSchemas.size() > 1);
281 //
282 //            assertTrue("Expected more schemas: " + allSchemas.size() + " that catalogs: " + allCatalogs.size(),
283 //                    allCatalogs.size() < allSchemas.size());
284 
285             assertFalse(conn.isClosed());
286         } finally {
287             assert conn != null;
288             conn.close();
289             assertTrue(conn.isClosed());
290         }
291 
292         // try w/ a catalog
293 //        final List<Object[]> schemasInCatalog;
294 //        try {
295 //            info.setProperty(StandardPropertyManager.CATALOG, getTestCatalog());
296 //            conn = olapDriver.connect(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
297 //            assertEquals(null, conn.getCatalog()); // @todo fix this?
298 //            final OlapResultSet cats = (OlapResultSet) conn.getMetaData().getCatalogs();
299 //            assertEquals(1, cats.getData().size());
300 //
301 //            schemasInCatalog = ((OlapResultSet) conn.getMetaData().getSchemas()).getData();
302 ////            assertTrue("Does the test catalog have only 1 or no schemas? count: " + schemasInCatalog, schemasInCatalog.size() > 1);
303 //        } catch (Throwable t) {
304 //            throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), t);
305 //        }
306 
307         // try w/ a catalog and cube
308 //        try {
309 //            info.setProperty(StandardPropertyManager.CUBE, getTestCube());
310 //            conn = olapDriver.connect(OlapDriver.JDBC_URL_PREFIX + getTestUrl(), info);
311 //            final OlapResultSet schemas = (OlapResultSet) conn.getMetaData().getSchemas();
312 //            assertEquals(1, schemas.getData().size());
313 //        } catch (Throwable t) {
314 //            throw new Exception(printTestWarningServerAccesMsg(OlapDriverTest.class, this), t);
315 //        }
316 
317         // @todo Test reset/clear of schema cache after catalog changed with same connection object
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 }