db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r425385 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
Date Tue, 25 Jul 2006 12:21:38 GMT
Author: kahatlen
Date: Tue Jul 25 05:21:38 2006
New Revision: 425385

URL: http://svn.apache.org/viewvc?rev=425385&view=rev
Log:
DERBY-1586 (partial) The attributesAsPassword property doesn't work
correctly with EmbeddedConnectionPoolDataSource

This patch adds a JUnit test which reproduces the bug. The test is not
run in any suite since the bug is not fixed.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
  (with props)

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java?rev=425385&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
Tue Jul 25 05:21:38 2006
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.derbyTesting.functionTests.tests.jdbcapi;
+
+import java.lang.reflect.Method;
+import java.sql.Connection;
+import java.sql.SQLException;
+import javax.sql.ConnectionPoolDataSource;
+import javax.sql.DataSource;
+import javax.sql.PooledConnection;
+import javax.sql.XAConnection;
+import javax.sql.XADataSource;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.functionTests.util.BaseJDBCTestCase;
+import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
+import org.apache.derbyTesting.functionTests.util.TestUtil;
+
+/**
+ * This class tests that properties of data sources are handled correctly.
+ */
+public class DataSourcePropertiesTest extends BaseJDBCTestCase {
+
+    /**
+     * Creates a new test case.
+     * @param name name of test method
+     */
+    public DataSourcePropertiesTest(String name) {
+        super(name);
+    }
+
+    // SETUP
+
+    /** Creates a test suite with all test cases. */
+    public static Test suite() {
+        TestSuite suite = new TestSuite();
+        Method[] methods = DataSourcePropertiesTest.class.getMethods();
+        // Add all methods starting with 'test'.
+        // When using embedded, add all methods starting with 'embedded'.
+        for (int i = 0; i < methods.length; i++) {
+            Method m = methods[i];
+            if (m.getParameterTypes().length > 0 ||
+                    m.getReturnType() != Void.TYPE) {
+                continue;
+            }
+            String name = m.getName();
+            if (name.startsWith("test") ||
+                    (name.startsWith("embedded") && usingEmbedded())) {
+                suite.addTest(new DataSourcePropertiesTest(name));
+            }
+        }
+        return suite;
+    }
+
+    // HELPER METHODS
+
+    /**
+     * Sets a property of a data source object.
+     *
+     * @param dataSource the data source
+     * @param name name of the property to set
+     * @param value property value
+     * @param type property type (useful for setting <code>null</code> or
+     * primitive types)
+     */
+    private void setDataSourceProperty(Object dataSource, String name,
+                                       Object value, Class type)
+        throws Exception
+    {
+        Method setter = dataSource.getClass().
+            getMethod(TestUtil.getSetterName(name), new Class[] { type });
+        setter.invoke(dataSource, new Object[] { value });
+    }
+
+    /**
+     * Sets a property of a data source object.
+     *
+     * @param dataSource the data source
+     * @param name name of the property to set
+     * @param value property value
+     */
+    private void setDataSourceProperty(Object dataSource, String name,
+                                       Object value) throws Exception {
+        setDataSourceProperty(dataSource, name, value, value.getClass());
+    }
+
+    // TEST METHODS
+
+    /**
+     * Tests that the default password is not sent as an attribute string when
+     * <code>attributesAsPassword</code> is <code>true</code>. The
test is run
+     * with a <code>DataSource</code>.
+     */
+    public void embeddedTestAttributesAsPasswordWithoutPassword_ds()
+        throws Exception
+    {
+        DataSource ds = TestDataSourceFactory.getDataSource();
+        setDataSourceProperty(ds, "password", "mypassword");
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        Connection c = ds.getConnection();
+        c.close();
+    }
+
+    /**
+     * Tests that the default password is not sent as an attribute string when
+     * <code>attributesAsPassword</code> is <code>true</code>. The
test is run
+     * with a <code>ConnectionPoolDataSource</code>.
+     */
+    public void embeddedTestAttributesAsPasswordWithoutPassword_pooled()
+        throws Exception
+    {
+        ConnectionPoolDataSource ds =
+            TestDataSourceFactory.getConnectionPoolDataSource();
+        setDataSourceProperty(ds, "password", "mypassword");
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        // DERBY-1586 caused a malformed url error here
+        PooledConnection pc = ds.getPooledConnection();
+        Connection c = pc.getConnection();
+        c.close();
+    }
+
+    /**
+     * Tests that the default password is not sent as an attribute string when
+     * <code>attributesAsPassword</code> is <code>true</code>. The
test is run
+     * with an <code>XADataSource</code>.
+     */
+    public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
+        throws Exception
+    {
+        XADataSource ds = TestDataSourceFactory.getXADataSource();
+        setDataSourceProperty(ds, "password", "mypassword");
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        XAConnection xa = ds.getXAConnection();
+        Connection c = xa.getConnection();
+        c.close();
+    }
+
+    /**
+     * Tests that the <code>attributesAsPassword</code> property of a
+     * <code>DataSource</code> causes an explicitly specified password to be
+     * sent as a property string.
+     */
+    public void embeddedTestAttributesAsPasswordWithPassword_ds()
+        throws Exception
+    {
+        DataSource ds = TestDataSourceFactory.getDataSource();
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        try {
+            Connection c = ds.getConnection("username", "mypassword");
+            fail("Expected getConnection to fail.");
+        } catch (SQLException e) {
+            // expect error because of malformed url
+            assertSQLState("XJ028", e);
+        }
+    }
+
+    /**
+     * Tests that the <code>attributesAsPassword</code> property of a
+     * <code>ConnectionPoolDataSource</code> causes an explicitly specified
+     * password to be sent as a property string.
+     */
+    public void embeddedTestAttributesAsPasswordWithPassword_pooled()
+        throws Exception
+    {
+        ConnectionPoolDataSource ds =
+            TestDataSourceFactory.getConnectionPoolDataSource();
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        try {
+            PooledConnection pc =
+                ds.getPooledConnection("username", "mypassword");
+            fail("Expected getPooledConnection to fail.");
+        } catch (SQLException e) {
+            // expect error because of malformed url
+            assertSQLState("XJ028", e);
+        }
+    }
+
+    /**
+     * Tests that the <code>attributesAsPassword</code> property of an
+     * <code>XADataSource</code> causes an explicitly specified password to be
+     * sent as a property string.
+     */
+    public void embeddedTestAttributesAsPasswordWithPassword_xa()
+        throws Exception
+    {
+        XADataSource ds = TestDataSourceFactory.getXADataSource();
+        setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
+                              Boolean.TYPE);
+        try {
+            XAConnection xa = ds.getXAConnection("username", "mypassword");
+            fail("Expected getXAConnection to fail.");
+        } catch (SQLException e) {
+            // expect error because of malformed url
+            assertSQLState("XJ028", e);
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourcePropertiesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message