KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > DataSourcePropertiesTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */

19
20 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.sql.Connection JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import javax.sql.ConnectionPoolDataSource JavaDoc;
26 import javax.sql.DataSource JavaDoc;
27 import javax.sql.PooledConnection JavaDoc;
28 import javax.sql.XAConnection JavaDoc;
29 import javax.sql.XADataSource JavaDoc;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
33 import org.apache.derbyTesting.functionTests.util.TestUtil;
34 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
35 import org.apache.derbyTesting.junit.JDBC;
36
37 /**
38  * This class tests that properties of data sources are handled correctly.
39  */

40 public class DataSourcePropertiesTest extends BaseJDBCTestCase {
41
42     /**
43      * Creates a new test case.
44      * @param name name of test method
45      */

46     public DataSourcePropertiesTest(String JavaDoc name) {
47         super(name);
48     }
49
50     // SETUP
51

52     /** Creates a test suite with all test cases. */
53     public static Test suite() {
54         
55         
56         TestSuite suite = new TestSuite();
57         
58         // TODO: Run fixtures in J2ME and JDBC2 (with extensions)
59
// that can be supported there. This disabling matches
60
// the original _app.properties file. Concern was over
61
// XA support (which is supported in JDBC 2 with extensions).
62
if (JDBC.vmSupportsJDBC3()) {
63         
64             // Add all methods starting with 'test'.
65
//suite.addTestSuite(DataSourcePropertiesTest.class);
66

67             if (usingEmbedded()) {
68            
69                 // When using embedded, add all methods starting with 'embedded'.
70
Method JavaDoc[] methods = DataSourcePropertiesTest.class.getMethods();
71                 for (int i = 0; i < methods.length; i++) {
72                     Method JavaDoc m = methods[i];
73                     if (m.getParameterTypes().length > 0 ||
74                             m.getReturnType().equals(Void.TYPE)) {
75                         continue;
76                     }
77                     String JavaDoc name = m.getName();
78                     if (name.startsWith("embedded")) {
79                         suite.addTest(new DataSourcePropertiesTest(name));
80                     }
81                 }
82             }
83         }
84         return suite;
85     }
86
87     // HELPER METHODS
88

89     /**
90      * Sets a property of a data source object.
91      *
92      * @param dataSource the data source
93      * @param name name of the property to set
94      * @param value property value
95      * @param type property type (useful for setting <code>null</code> or
96      * primitive types)
97      */

98     private void setDataSourceProperty(Object JavaDoc dataSource, String JavaDoc name,
99                                        Object JavaDoc value, Class JavaDoc type)
100         throws Exception JavaDoc
101     {
102         Method JavaDoc setter = dataSource.getClass().
103             getMethod(TestUtil.getSetterName(name), new Class JavaDoc[] { type });
104         setter.invoke(dataSource, new Object JavaDoc[] { 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 JavaDoc dataSource, String JavaDoc name,
115                                        Object JavaDoc value) throws Exception JavaDoc {
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 JavaDoc
128     {
129         DataSource JavaDoc ds = TestDataSourceFactory.getDataSource();
130         setDataSourceProperty(ds, "password", "mypassword");
131         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
132                               Boolean.TYPE);
133         Connection JavaDoc c = ds.getConnection();
134         c.close();
135     }
136
137     /**
138      * Tests that the default password is not sent as an attribute string when
139      * <code>attributesAsPassword</code> is <code>true</code>. The test is run
140      * with a <code>ConnectionPoolDataSource</code>.
141      */

142     public void embeddedTestAttributesAsPasswordWithoutPassword_pooled()
143         throws Exception JavaDoc
144     {
145         ConnectionPoolDataSource JavaDoc ds =
146             TestDataSourceFactory.getConnectionPoolDataSource();
147         setDataSourceProperty(ds, "password", "mypassword");
148         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
149                               Boolean.TYPE);
150         // DERBY-1586 caused a malformed url error here
151
PooledConnection JavaDoc pc = ds.getPooledConnection();
152         Connection JavaDoc c = pc.getConnection();
153         c.close();
154     }
155
156     /**
157      * Tests that the default password is not sent as an attribute string when
158      * <code>attributesAsPassword</code> is <code>true</code>. The test is run
159      * with an <code>XADataSource</code>.
160      */

161     public void embeddedTestAttributesAsPasswordWithoutPassword_xa()
162         throws Exception JavaDoc
163     {
164         XADataSource JavaDoc ds = TestDataSourceFactory.getXADataSource();
165         setDataSourceProperty(ds, "password", "mypassword");
166         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
167                               Boolean.TYPE);
168         XAConnection JavaDoc xa = ds.getXAConnection();
169         Connection JavaDoc c = xa.getConnection();
170         c.close();
171     }
172
173     /**
174      * Tests that the <code>attributesAsPassword</code> property of a
175      * <code>DataSource</code> causes an explicitly specified password to be
176      * sent as a property string.
177      */

178     public void embeddedTestAttributesAsPasswordWithPassword_ds()
179         throws Exception JavaDoc
180     {
181         DataSource JavaDoc ds = TestDataSourceFactory.getDataSource();
182         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
183                               Boolean.TYPE);
184         try {
185             Connection JavaDoc c = ds.getConnection("username", "mypassword");
186             fail("Expected getConnection to fail.");
187         } catch (SQLException JavaDoc e) {
188             // expect error because of malformed url
189
assertSQLState("XJ028", e);
190         }
191     }
192
193     /**
194      * Tests that the <code>attributesAsPassword</code> property of a
195      * <code>ConnectionPoolDataSource</code> causes an explicitly specified
196      * password to be sent as a property string.
197      */

198     public void embeddedTestAttributesAsPasswordWithPassword_pooled()
199         throws Exception JavaDoc
200     {
201         ConnectionPoolDataSource JavaDoc ds =
202             TestDataSourceFactory.getConnectionPoolDataSource();
203         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
204                               Boolean.TYPE);
205         try {
206             PooledConnection JavaDoc pc =
207                 ds.getPooledConnection("username", "mypassword");
208             fail("Expected getPooledConnection to fail.");
209         } catch (SQLException JavaDoc e) {
210             // expect error because of malformed url
211
assertSQLState("XJ028", e);
212         }
213     }
214
215     /**
216      * Tests that the <code>attributesAsPassword</code> property of an
217      * <code>XADataSource</code> causes an explicitly specified password to be
218      * sent as a property string.
219      */

220     public void embeddedTestAttributesAsPasswordWithPassword_xa()
221         throws Exception JavaDoc
222     {
223         XADataSource JavaDoc ds = TestDataSourceFactory.getXADataSource();
224         setDataSourceProperty(ds, "attributesAsPassword", Boolean.TRUE,
225                               Boolean.TYPE);
226         try {
227             XAConnection JavaDoc xa = ds.getXAConnection("username", "mypassword");
228             fail("Expected getXAConnection to fail.");
229         } catch (SQLException JavaDoc e) {
230             // expect error because of malformed url
231
assertSQLState("XJ028", e);
232         }
233     }
234 }
235
Popular Tags