001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
021:
022: import java.lang.reflect.Method;
023: import java.sql.Connection;
024: import java.sql.SQLException;
025: import javax.sql.ConnectionPoolDataSource;
026: import javax.sql.DataSource;
027: import javax.sql.PooledConnection;
028: import javax.sql.XAConnection;
029: import javax.sql.XADataSource;
030: import junit.framework.Test;
031: import junit.framework.TestSuite;
032: import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
033: import org.apache.derbyTesting.functionTests.util.TestUtil;
034: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
035: import org.apache.derbyTesting.junit.JDBC;
036:
037: /**
038: * This class tests that properties of data sources are handled correctly.
039: */
040: public class DataSourcePropertiesTest extends BaseJDBCTestCase {
041:
042: /**
043: * Creates a new test case.
044: * @param name name of test method
045: */
046: public DataSourcePropertiesTest(String name) {
047: super (name);
048: }
049:
050: // SETUP
051:
052: /** Creates a test suite with all test cases. */
053: public static Test suite() {
054:
055: TestSuite suite = new TestSuite();
056:
057: // TODO: Run fixtures in J2ME and JDBC2 (with extensions)
058: // that can be supported there. This disabling matches
059: // the original _app.properties file. Concern was over
060: // XA support (which is supported in JDBC 2 with extensions).
061: if (JDBC.vmSupportsJDBC3()) {
062:
063: // Add all methods starting with 'test'.
064: //suite.addTestSuite(DataSourcePropertiesTest.class);
065:
066: if (usingEmbedded()) {
067:
068: // When using embedded, add all methods starting with 'embedded'.
069: Method[] methods = DataSourcePropertiesTest.class
070: .getMethods();
071: for (int i = 0; i < methods.length; i++) {
072: Method m = methods[i];
073: if (m.getParameterTypes().length > 0
074: || m.getReturnType().equals(Void.TYPE)) {
075: continue;
076: }
077: String name = m.getName();
078: if (name.startsWith("embedded")) {
079: suite
080: .addTest(new DataSourcePropertiesTest(
081: name));
082: }
083: }
084: }
085: }
086: return suite;
087: }
088:
089: // HELPER METHODS
090:
091: /**
092: * Sets a property of a data source object.
093: *
094: * @param dataSource the data source
095: * @param name name of the property to set
096: * @param value property value
097: * @param type property type (useful for setting <code>null</code> or
098: * primitive types)
099: */
100: private void setDataSourceProperty(Object dataSource, String name,
101: Object value, Class type) throws Exception {
102: Method setter = dataSource.getClass().getMethod(
103: TestUtil.getSetterName(name), new Class[] { type });
104: setter.invoke(dataSource, new Object[] { value });
105: }
106:
107: /**
108: * Sets a property of a data source object.
109: *
110: * @param dataSource the data source
111: * @param name name of the property to set
112: * @param value property value
113: */
114: private void setDataSourceProperty(Object dataSource, String name,
115: Object value) throws Exception {
116: setDataSourceProperty(dataSource, name, value, value.getClass());
117: }
118:
119: // TEST METHODS
120:
121: /**
122: * Tests that the default password is not sent as an attribute string when
123: * <code>attributesAsPassword</code> is <code>true</code>. The test is run
124: * with a <code>DataSource</code>.
125: */
126: public void embeddedTestAttributesAsPasswordWithoutPassword_ds()
127: throws Exception {
128: DataSource ds = TestDataSourceFactory.getDataSource();
129: setDataSourceProperty(ds, "password", "mypassword");
130: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
131: Boolean.TYPE);
132: Connection c = ds.getConnection();
133: c.close();
134: }
135:
136: /**
137: * Tests that the default password is not sent as an attribute string when
138: * <code>attributesAsPassword</code> is <code>true</code>. The test is run
139: * with a <code>ConnectionPoolDataSource</code>.
140: */
141: public void embeddedTestAttributesAsPasswordWithoutPassword_pooled()
142: throws Exception {
143: ConnectionPoolDataSource ds = TestDataSourceFactory
144: .getConnectionPoolDataSource();
145: setDataSourceProperty(ds, "password", "mypassword");
146: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
147: Boolean.TYPE);
148: // DERBY-1586 caused a malformed url error here
149: PooledConnection pc = ds.getPooledConnection();
150: Connection c = pc.getConnection();
151: c.close();
152: }
153:
154: /**
155: * Tests that the default password is not sent as an attribute string when
156: * <code>attributesAsPassword</code> is <code>true</code>. The test is run
157: * with an <code>XADataSource</code>.
158: */
159: public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
160: throws Exception {
161: XADataSource ds = TestDataSourceFactory.getXADataSource();
162: setDataSourceProperty(ds, "password", "mypassword");
163: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
164: Boolean.TYPE);
165: XAConnection xa = ds.getXAConnection();
166: Connection c = xa.getConnection();
167: c.close();
168: }
169:
170: /**
171: * Tests that the <code>attributesAsPassword</code> property of a
172: * <code>DataSource</code> causes an explicitly specified password to be
173: * sent as a property string.
174: */
175: public void embeddedTestAttributesAsPasswordWithPassword_ds()
176: throws Exception {
177: DataSource ds = TestDataSourceFactory.getDataSource();
178: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
179: Boolean.TYPE);
180: try {
181: Connection c = ds.getConnection("username", "mypassword");
182: fail("Expected getConnection to fail.");
183: } catch (SQLException e) {
184: // expect error because of malformed url
185: assertSQLState("XJ028", e);
186: }
187: }
188:
189: /**
190: * Tests that the <code>attributesAsPassword</code> property of a
191: * <code>ConnectionPoolDataSource</code> causes an explicitly specified
192: * password to be sent as a property string.
193: */
194: public void embeddedTestAttributesAsPasswordWithPassword_pooled()
195: throws Exception {
196: ConnectionPoolDataSource ds = TestDataSourceFactory
197: .getConnectionPoolDataSource();
198: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
199: Boolean.TYPE);
200: try {
201: PooledConnection pc = ds.getPooledConnection("username",
202: "mypassword");
203: fail("Expected getPooledConnection to fail.");
204: } catch (SQLException e) {
205: // expect error because of malformed url
206: assertSQLState("XJ028", e);
207: }
208: }
209:
210: /**
211: * Tests that the <code>attributesAsPassword</code> property of an
212: * <code>XADataSource</code> causes an explicitly specified password to be
213: * sent as a property string.
214: */
215: public void embeddedTestAttributesAsPasswordWithPassword_xa()
216: throws Exception {
217: XADataSource ds = TestDataSourceFactory.getXADataSource();
218: setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
219: Boolean.TYPE);
220: try {
221: XAConnection xa = ds.getXAConnection("username",
222: "mypassword");
223: fail("Expected getXAConnection to fail.");
224: } catch (SQLException e) {
225: // expect error because of malformed url
226: assertSQLState("XJ028", e);
227: }
228: }
229: }
|