KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbc4 > AutoloadTest


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

21 /**
22  * <p>
23  * This JUnit test verifies the autoloading of the jdbc driver under JDBC4.
24  * This test must be run in its own VM because we want to verify that the
25  * driver was not accidentally loaded by some other test.
26  * </p>
27  *
28  * @author Rick
29  */

30
31 package org.apache.derbyTesting.functionTests.tests.jdbc4;
32
33 import java.sql.*;
34 import java.util.*;
35 import junit.framework.*;
36
37 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
38 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
39 import org.apache.derbyTesting.junit.JDBC;
40 import org.apache.derbyTesting.junit.TestConfiguration;
41
42 public class AutoloadTest extends BaseJDBCTestCase
43 {
44     /////////////////////////////////////////////////////////////
45
//
46
// CONSTANTS
47
//
48
/////////////////////////////////////////////////////////////
49

50     private static final String JavaDoc NONEXISTENT_DATABASE = "nonexistentDatabase";
51     
52     /////////////////////////////////////////////////////////////
53
//
54
// STATE
55
//
56
/////////////////////////////////////////////////////////////
57

58     /////////////////////////////////////////////////////////////
59
//
60
// CONSTRUCTOR
61
//
62
/////////////////////////////////////////////////////////////
63

64     public AutoloadTest( String JavaDoc name ) { super( name ); }
65
66     /////////////////////////////////////////////////////////////
67
//
68
// ENTRY POINT
69
//
70
/////////////////////////////////////////////////////////////
71

72     /////////////////////////////////////////////////////////////
73
//
74
// JUnit BEHAVIOR
75
//
76
/////////////////////////////////////////////////////////////
77

78     /**
79      * Only run a test if the driver will be auto-loaded.
80      */

81     public static Test suite()
82     {
83         TestSuite suite = new TestSuite();
84         
85         // need DriverManager at least and Derby drivers
86
// no interest in testing DB2's client.
87
if (JDBC.vmSupportsJDBC2() &&
88                 (usingEmbedded() || usingDerbyNetClient()))
89         {
90
91             boolean autoloadingCurrentDriver = false;
92             
93             // Autoloading if the current driver is defined in the
94
// system property jdbc.drivers, see java.sql.DriverManager.
95
try {
96                 String JavaDoc jdbcDrivers = getSystemProperty("jdbc.drivers");
97                 if (jdbcDrivers != null)
98                 {
99                     // Simple test to see if the driver class is
100
// in the value. Could get fancy and see if it is
101
// correctly formatted but not worth it.
102
String JavaDoc driver =
103                         TestConfiguration.getCurrent().getJDBCClient().getJDBCDriverName();
104                     
105                     if (jdbcDrivers.indexOf(driver) != -1)
106                         autoloadingCurrentDriver = true;
107                 }
108                 
109             } catch (SecurityException JavaDoc e) {
110                 // can't read property, assume not autoloading.
111
}
112                         
113             // Also auto loading if this is JDBC 4 and loading from the
114
// jar files, due to the required manifest entry.
115
if (JDBC.vmSupportsJDBC4() &&
116                     TestConfiguration.loadingFromJars())
117                 autoloadingCurrentDriver = true;
118           
119             if (autoloadingCurrentDriver)
120                 suite.addTestSuite(AutoloadTest.class);
121
122         }
123                
124         return suite;
125     }
126
127     /////////////////////////////////////////////////////////////
128
//
129
// TEST ENTRY POINTS
130
//
131
/////////////////////////////////////////////////////////////
132

133     /**
134      * <p>
135      * Tests the autoloading of the client driver by JDBC 4. This behavior
136      * is described in section 10.2.1 of the JDBC 4 spec. The driver is
137      * autoloaded if we are running under jdk1.6 or later and one of the
138      * following is true:
139      * </p>
140      *
141      * <ul>
142      * <li>Classes are being loaded out of the Derby jar files.</li>
143      * <li>OR the system property jdbc.drivers names the drivers.</li>
144      * </ul>
145      */

146     public void testAutoloading()
147         throws Exception JavaDoc
148     {
149         //CONFIG.setVerbosity( true );
150

151         //
152
// We expect that the connection to the database will fail for
153
// one reason or another.
154
//
155

156         println( "We ARE autoloading..." );
157
158         //
159
// The DriverManager should have autoloaded the client driver.
160
// This means that the connection request is passed on to the
161
// server. The server then determines that the database does
162
// not exist. This raises a different error depending on whether
163
// we're running embedded or with the Derby client.
164
//
165
String JavaDoc expectedError =
166             usingEmbedded() ? "XJ004" : "08004";
167         
168         failToConnect(expectedError);
169        
170         // Test we can connect successfully to a database!
171
String JavaDoc url = getTestConfiguration().getJDBCUrl();
172         url = url.concat(";create=true");
173         String JavaDoc user = getTestConfiguration().getUserName();
174         String JavaDoc password = getTestConfiguration().getUserPassword();
175         DriverManager.getConnection(url, user, password).close();
176
177     }
178
179     /**
180      * <p>
181      * Verify that we fail to connect to the database for the expected
182      * reason.
183      * </p>
184      */

185     private void failToConnect( String JavaDoc expectedSQLState )
186         throws Exception JavaDoc
187     {
188         String JavaDoc connectionURL = getTestConfiguration().getJDBCUrl( NONEXISTENT_DATABASE );
189         Properties properties = new Properties();
190         SQLException se = null;
191
192         properties.put( "user", getTestConfiguration().getUserName() );
193         properties.put( "password", getTestConfiguration().getUserPassword() );
194
195         try {
196             println( "Attempting to connect with this URL: '" + connectionURL + "'" );
197             
198             DriverManager.getConnection( connectionURL, properties );
199             
200             fail("Connection succeed, expected to fail.");
201         }
202         catch ( SQLException e ) { se = e; }
203
204         println( "Caught expected SQLException: " + se );
205
206         assertSQLState( expectedSQLState, expectedSQLState, se );
207     }
208
209
210 }
211
212
Popular Tags