/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// Copyright (C) 2001-2002 Kana Software, Inc.
// Copyright (C) 2001-2005 Julian Hyde and others
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jng, 16 April, 2004
*/
package mondrian.rolap;

import junit.framework.TestCase;
import mondrian.olap.MondrianProperties;
import mondrian.olap.Util;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Unit test for {@link RolapConnection}.
 *
 * @author jng
 * @since 16 April, 2004
 * @version $Id$
 */
public class RolapConnectionTest extends TestCase {
    public RolapConnectionTest(String name) {
        super(name);
    }

    public void testPooledConnectionWithProperties() throws SQLException {
        final String connectString =
                MondrianProperties.instance().TestConnectString.get();
        Util.PropertyList properties = Util.parseConnectString(connectString);

        // Only the JDBC-ODBC bridge gives the error necessary for this
        // test to succeed. So trivially succeed for all other JDBC
        // drivers.
        final String jdbc = properties.get("Jdbc");
        if (jdbc != null &&
                !jdbc.startsWith("jdbc:odbc:")) {
            return;
        }

        // JDBC-ODBC driver does not support UTF-16, so this test succeeds
        // because creating the connection from the DataSource will fail.
        properties.put("jdbc.charSet", "UTF-16");
        DataSource dataSource = RolapConnection.createDataSource(properties);
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.close();
            fail("Expected exception");
        } catch (SQLException e) {
            if (e.getClass().getName().equals(
                "org.apache.commons.dbcp.DbcpException")) {
                // This is expected. (We use string-comparison so that the
                // compiler doesn't warn about using a deprecated class.)
            } else if (e.getClass() == SQLException.class &&
                e.getCause() == null &&
                e.getMessage() != null &&
                e.getMessage().equals("")) {
                // This is expected, from a later version of Dbcp.
            } else {
                fail("Expected exception, but got a different one: " + e);
            }
        } finally {
            RolapConnectionPool.instance().clearPool();
        }
    }

    public void testNonPooledConnectionWithProperties() {
        final String connectString =
                MondrianProperties.instance().TestConnectString.get();
        Util.PropertyList properties = Util.parseConnectString(connectString);

        // Only the JDBC-ODBC bridge gives the error necessary for this
        // test to succeed. So trivially succeed for all other JDBC
        // drivers.
        final String jdbc = properties.get("Jdbc");
        if (jdbc != null &&
                !jdbc.startsWith("jdbc:odbc:")) {
            return;
        }

        // This test is just like the test testPooledConnectionWithProperties
        // except with non-pooled connections.
        properties.put("jdbc.charSet", "UTF-16");
        properties.put(RolapConnectionProperties.PoolNeeded, "false");
        DataSource dataSource = RolapConnection.createDataSource(properties);
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.close();
            fail("Expected exception");
        } catch (SQLException se) {
            // this is expected
        }
    }
}

// End RolapConnectionTest.java

@@ -466,34 +466,20 @@
 an.olap.
-MondrianProperties
+Util
 ;%0Aimport
@@ -488,25 +488,32 @@
 ondrian.
-olap.Util
+test.TestContext
 ;%0A%0Aimpor
@@ -946,58 +946,23 @@
 ng =
-%0A                MondrianProperties.instance().Tes
+ TestContext.ge
 tCon
@@ -963,36 +963,32 @@
 getConnectString
-.get
 ();%0A        Util
@@ -1674,39 +1674,32 @@
 ction connection
- = null
 ;%0A        try %7B%0A
@@ -2475,24 +2475,397 @@
           %7D%0A
+        %7D catch (IllegalArgumentException e) %7B%0A            // Workaround Java bug, logged on 2006/12/09 with synopsis%0A            // %22DriverManager.getConnection throws IllegalArgumentException%22.%0A            if (!System.getProperties().getProperty(%22java.version%22).startsWith(%221.6.%22)) %7B%0A                fail(%22Expect IllegalArgumentException only in JDK 1.6%22);%0A            %7D%0A
         %7D fi
@@ -3043,58 +3043,23 @@
 ng =
-%0A                MondrianProperties.instance().Tes
+ TestContext.ge
 tCon
@@ -3068,20 +3068,16 @@
 ctString
-.get
 ();%0A    
@@ -3830,15 +3830,8 @@
 tion
- = null
 ;%0A  
@@ -3999,24 +3999,24 @@
 ption se) %7B%0A
-
             
@@ -4035,16 +4035,389 @@
 xpected%0A
+        %7D catch (IllegalArgumentException e) %7B%0A            // Workaround Java bug, logged on 2006/12/09 with synopsis%0A            // %22DriverManager.getConnection throws IllegalArgumentException%22.%0A            if (!System.getProperties().getProperty(%22java.version%22).startsWith(%221.6.%22)) %7B%0A                fail(%22Expect IllegalArgumentException only in JDK 1.6%22);%0A            %7D%0A
         

Fix RolapConnectionTest to workaround bug in DriverManager.getConnection in JDK 1.6.