KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.ConnectionTest
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 package org.apache.derbyTesting.functionTests.tests.jdbc4;
22
23 import junit.framework.*;
24
25 import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
26 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
27 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
28
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.OutputStream JavaDoc;
33 import java.sql.*;
34 import java.util.Properties JavaDoc;
35 import javax.sql.*;
36
37 /**
38  * Tests for the JDBC 4.0 specific methods in the connection object(s).
39  *
40  * Which connection implementation is tested, depends on what connection
41  * object the <code>BaseJDBCTestCase.getConnection()</code>-method returns.
42  * Currently, the property <code>derbyTesting.xa.single</code> can be set to
43  * <code>true</code> to test the XA connection object, which happens to be the
44  * same as the one used for poooled connections.
45  * The connection returned also depends on which framework is being used.
46  */

47 public class ConnectionTest
48     extends BaseJDBCTestCase {
49
50     /**
51      * Create a test with the given name.
52      *
53      * @param name name of the test.
54      */

55     public ConnectionTest(String JavaDoc name) {
56         super(name);
57     }
58    
59     //------------------------- T E S T M E T H O D S ------------------------
60

61     /**
62      *
63      * Tests the Embedded implementation for the createBlob method. The Embedded
64      * server does'nt currently have the set methods implemented. Hence the
65      * create methods cannot be tested by inserting data into the empty LOB
66      * object. Here we do a simple test of checking that the length of the
67      * LOB object is 0.
68      *
69      * @throws SQLException upon failure in the createBlob or the length
70      * methods.
71      *
72      */

73     public void embeddedCreateBlob()
74         throws SQLException {
75             Blob blob = getConnection().createBlob();
76             //Check if the blob is empty
77
if(blob.length() > 0)
78                 fail("The new Blob should not have more than zero bytes " +
79                         "contained in it");
80     }
81     
82     /**
83      *
84      * Tests the Embedded implementation for the createClob method. The Embedded
85      * server does'nt currently have the set methods implemented. Hence the
86      * create methods cannot be tested by inserting data into the empty LOB
87      * object. Here we do a simple test of checking that the length of the
88      * LOB object is 0.
89      *
90      * @throws SQLException upon failure in the createClob or the length
91      * methods.
92      *
93      */

94     public void embeddedCreateClob()
95         throws SQLException {
96             Clob clob = getConnection().createClob();
97             //check if the Clob is empty
98
if(clob.length() > 0)
99                 fail("The new Clob should not have a length of greater than " +
100                         "zero");
101     }
102
103     public void testCreateArrayNotImplemented()
104         throws SQLException {
105         try {
106             getConnection().createArrayOf(null, null);
107             fail("createArrayOf(String,Object[]) should not be implemented");
108         } catch (SQLFeatureNotSupportedException sfnse) {
109             // Do nothing, we are fine
110
}
111     }
112
113     public void testCreateNClobNotImplemented()
114         throws SQLException {
115         try {
116             getConnection().createNClob();
117             fail("createNClob() should not be implemented");
118         } catch (SQLFeatureNotSupportedException sfnse) {
119             // Do nothing, we are fine
120
}
121     }
122
123     public void testCreateSQLXMLNotImplemented()
124         throws SQLException {
125         try {
126             getConnection().createSQLXML();
127             fail("createSQLXML() should not be implemented");
128         } catch (SQLFeatureNotSupportedException sfnse) {
129             // Do nothing, we are fine
130
}
131     }
132
133     public void testCreateStructNotImplemented()
134         throws SQLException {
135         try {
136             getConnection().createStruct(null, null);
137             fail("createStruct(String,Object[]) should not be implemented");
138         } catch (SQLFeatureNotSupportedException sfnse) {
139             // Do nothing, we are fine
140
}
141     }
142     
143     public void testGetClientInfo()
144         throws SQLException {
145         assertTrue("getClientInfo() must return an empty Properties object",
146                    getConnection().getClientInfo().isEmpty());
147     }
148     
149     public void testGetClientInfoString()
150         throws SQLException {
151         assertNull("getClientInfo(null) must return null",
152                    getConnection().getClientInfo(null));
153         assertNull("getClientInfo(\"someProperty\") must return null",
154                    getConnection().getClientInfo("someProperty"));
155     }
156
157     /**
158      * Tests that <code>isValid</code> is implemented and returns true
159      * for the connection. This test is very limited but is tested
160      * for all connection types. A more complete test of isValid is
161      * found in the TestConnectionMethods.java test that is run for
162      * embedded and network client connections.
163      */

164     public void testIsValidImplemented() throws SQLException {
165         // Test with an infinite (0) timeout
166
assertTrue(getConnection().isValid(0));
167
168         // Test with a 1 second timeout
169
assertTrue(getConnection().isValid(1));
170
171         // Test with an illegal timeout
172
try {
173             getConnection().isValid(-1);
174         } catch (SQLException sqle) {
175             assertSQLState("Incorrect SQL state when calling isValid(-1)",
176                            "XJ081", sqle);
177         }
178     }
179
180     /**
181      * Tests that <code>getTypeMap()</code> returns an empty map when
182      * no type map has been installed.
183      * @exception SQLException if an error occurs
184      */

185     public void testGetTypeMapReturnsEmptyMap() throws SQLException {
186         assertTrue(getConnection().getTypeMap().isEmpty());
187     }
188
189     public void testIsWrapperReturnsFalse()
190         throws SQLException {
191         assertFalse(getConnection().isWrapperFor(ResultSet.class));
192     }
193
194     public void testIsWrapperReturnsTrue()
195         throws SQLException {
196         assertTrue(getConnection().isWrapperFor(Connection.class));
197     }
198
199     public void testSetClientInfoProperties()
200         throws SQLException {
201         getConnection().setClientInfo(null);
202         Properties JavaDoc p = new Properties JavaDoc();
203         getConnection().setClientInfo(p);
204
205         p.setProperty("prop1", "val1");
206         p.setProperty("prop2", "val2");
207         try {
208             getConnection().setClientInfo(p);
209             fail("setClientInfo(String,String) should throw "+
210                  "SQLClientInfoException");
211         } catch (SQLClientInfoException cie) {
212             assertSQLState("SQLStates must match", "XCY02", cie);
213             assertTrue("Setting property 'prop1' must fail with "+
214                        "REASON_UNKNOWN_PROPERTY",
215                        cie.getFailedProperties().
216                        get("prop1").
217                        equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
218              assertTrue("Setting property 'prop2' must fail with "+
219                         "REASON_UNKNOWN_PROPERTY",
220                         cie.getFailedProperties().
221                         get("prop2").
222                         equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
223         }
224     }
225
226     public void testSetClientInfoString()
227         throws SQLException {
228         getConnection().setClientInfo(null, null);
229
230         try {
231             getConnection().setClientInfo("foo", null);
232             fail("setClientInfo(String, null) should throw "+
233                  "NullPointerException");
234         } catch (NullPointerException JavaDoc npe) {}
235
236         try {
237             getConnection().setClientInfo("name", "value");
238             fail("setClientInfo(String,String) should throw "+
239                  "SQLClientInfoException");
240         } catch (SQLClientInfoException cie) {
241             assertSQLState("SQLState must match 'unsupported'",
242                            "XCY02", cie);
243             assertTrue("Setting property 'name' must fail with "+
244                        "REASON_UNKNOWN_PROPERTY",
245                        cie.getFailedProperties().
246                        get("name").
247                        equals(ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
248         }
249     }
250     
251     public void testUnwrapValid()
252         throws SQLException {
253         Connection unwrappedCon = getConnection().unwrap(Connection.class);
254         assertSame("Unwrap returned wrong object.", getConnection(), unwrappedCon);
255     }
256
257     public void testUnwrapInvalid()
258         throws SQLException {
259         try {
260             ResultSet unwrappedRs = getConnection().unwrap(ResultSet.class);
261             fail("unwrap should have thrown an exception");
262         } catch (SQLException sqle) {
263             assertSQLState("Incorrect SQL state when unable to unwrap",
264                            SQLStateConstants.UNABLE_TO_UNWRAP,
265                            sqle);
266         }
267     }
268         
269     //------------------ E N D O F T E S T M E T H O D S -------------------
270

271     /**
272      * Create suite containing client-only tests.
273      */

274     private static TestSuite clientSuite(String JavaDoc name) {
275         TestSuite clientSuite = new TestSuite(name);
276         return clientSuite;
277     }
278     
279     /**
280      * Create suite containing embedded-only tests.
281      */

282     private static TestSuite embeddedSuite(String JavaDoc name) {
283         TestSuite embeddedSuite = new TestSuite(name);
284         embeddedSuite.addTest(new ConnectionTest(
285                     "embeddedCreateBlob"));
286         embeddedSuite.addTest(new ConnectionTest(
287                     "embeddedCreateClob"));
288         return embeddedSuite;
289     }
290     
291     /**
292      * Create a test suite containing tests for a JDB connection.
293      * In addition, separate suites for embedded- and client-only are added
294      * when appropriate.
295      */

296     public static Test suite() {
297         TestSuite connSuite =
298             new TestSuite(ConnectionTest.class, "ConnectionTest suite");
299         // Add client only tests
300
// NOTE: JCC is excluded
301
if (usingDerbyNetClient()) {
302             connSuite.addTest(
303                     clientSuite("ConnectionTest client-only suite"));
304         }
305         // Add embedded only tests
306
if (usingEmbedded()) {
307             connSuite.addTest(
308                     embeddedSuite("ConnectionTest embedded-only suite"));
309         }
310         return connSuite;
311     }
312     
313 } // End class BaseJDBCTestCase
314
Popular Tags