001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.ConnectionTest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: package org.apache.derbyTesting.functionTests.tests.jdbc4;
022:
023: import junit.framework.*;
024:
025: import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
026: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
027: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
028:
029: import java.io.FileInputStream;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032: import java.io.OutputStream;
033: import java.sql.*;
034: import java.util.Properties;
035: import javax.sql.*;
036:
037: /**
038: * Tests for the JDBC 4.0 specific methods in the connection object(s).
039: *
040: * Which connection implementation is tested, depends on what connection
041: * object the <code>BaseJDBCTestCase.getConnection()</code>-method returns.
042: * Currently, the property <code>derbyTesting.xa.single</code> can be set to
043: * <code>true</code> to test the XA connection object, which happens to be the
044: * same as the one used for poooled connections.
045: * The connection returned also depends on which framework is being used.
046: */
047: public class ConnectionTest extends BaseJDBCTestCase {
048:
049: /**
050: * Create a test with the given name.
051: *
052: * @param name name of the test.
053: */
054: public ConnectionTest(String name) {
055: super (name);
056: }
057:
058: //------------------------- T E S T M E T H O D S ------------------------
059:
060: /**
061: *
062: * Tests the Embedded implementation for the createBlob method. The Embedded
063: * server does'nt currently have the set methods implemented. Hence the
064: * create methods cannot be tested by inserting data into the empty LOB
065: * object. Here we do a simple test of checking that the length of the
066: * LOB object is 0.
067: *
068: * @throws SQLException upon failure in the createBlob or the length
069: * methods.
070: *
071: */
072: public void embeddedCreateBlob() throws SQLException {
073: Blob blob = getConnection().createBlob();
074: //Check if the blob is empty
075: if (blob.length() > 0)
076: fail("The new Blob should not have more than zero bytes "
077: + "contained in it");
078: }
079:
080: /**
081: *
082: * Tests the Embedded implementation for the createClob method. The Embedded
083: * server does'nt currently have the set methods implemented. Hence the
084: * create methods cannot be tested by inserting data into the empty LOB
085: * object. Here we do a simple test of checking that the length of the
086: * LOB object is 0.
087: *
088: * @throws SQLException upon failure in the createClob or the length
089: * methods.
090: *
091: */
092: public void embeddedCreateClob() throws SQLException {
093: Clob clob = getConnection().createClob();
094: //check if the Clob is empty
095: if (clob.length() > 0)
096: fail("The new Clob should not have a length of greater than "
097: + "zero");
098: }
099:
100: public void testCreateArrayNotImplemented() throws SQLException {
101: try {
102: getConnection().createArrayOf(null, null);
103: fail("createArrayOf(String,Object[]) should not be implemented");
104: } catch (SQLFeatureNotSupportedException sfnse) {
105: // Do nothing, we are fine
106: }
107: }
108:
109: public void testCreateNClobNotImplemented() throws SQLException {
110: try {
111: getConnection().createNClob();
112: fail("createNClob() should not be implemented");
113: } catch (SQLFeatureNotSupportedException sfnse) {
114: // Do nothing, we are fine
115: }
116: }
117:
118: public void testCreateSQLXMLNotImplemented() throws SQLException {
119: try {
120: getConnection().createSQLXML();
121: fail("createSQLXML() should not be implemented");
122: } catch (SQLFeatureNotSupportedException sfnse) {
123: // Do nothing, we are fine
124: }
125: }
126:
127: public void testCreateStructNotImplemented() throws SQLException {
128: try {
129: getConnection().createStruct(null, null);
130: fail("createStruct(String,Object[]) should not be implemented");
131: } catch (SQLFeatureNotSupportedException sfnse) {
132: // Do nothing, we are fine
133: }
134: }
135:
136: public void testGetClientInfo() throws SQLException {
137: assertTrue(
138: "getClientInfo() must return an empty Properties object",
139: getConnection().getClientInfo().isEmpty());
140: }
141:
142: public void testGetClientInfoString() throws SQLException {
143: assertNull("getClientInfo(null) must return null",
144: getConnection().getClientInfo(null));
145: assertNull("getClientInfo(\"someProperty\") must return null",
146: getConnection().getClientInfo("someProperty"));
147: }
148:
149: /**
150: * Tests that <code>isValid</code> is implemented and returns true
151: * for the connection. This test is very limited but is tested
152: * for all connection types. A more complete test of isValid is
153: * found in the TestConnectionMethods.java test that is run for
154: * embedded and network client connections.
155: */
156: public void testIsValidImplemented() throws SQLException {
157: // Test with an infinite (0) timeout
158: assertTrue(getConnection().isValid(0));
159:
160: // Test with a 1 second timeout
161: assertTrue(getConnection().isValid(1));
162:
163: // Test with an illegal timeout
164: try {
165: getConnection().isValid(-1);
166: } catch (SQLException sqle) {
167: assertSQLState(
168: "Incorrect SQL state when calling isValid(-1)",
169: "XJ081", sqle);
170: }
171: }
172:
173: /**
174: * Tests that <code>getTypeMap()</code> returns an empty map when
175: * no type map has been installed.
176: * @exception SQLException if an error occurs
177: */
178: public void testGetTypeMapReturnsEmptyMap() throws SQLException {
179: assertTrue(getConnection().getTypeMap().isEmpty());
180: }
181:
182: public void testIsWrapperReturnsFalse() throws SQLException {
183: assertFalse(getConnection().isWrapperFor(ResultSet.class));
184: }
185:
186: public void testIsWrapperReturnsTrue() throws SQLException {
187: assertTrue(getConnection().isWrapperFor(Connection.class));
188: }
189:
190: public void testSetClientInfoProperties() throws SQLException {
191: getConnection().setClientInfo(null);
192: Properties p = new Properties();
193: getConnection().setClientInfo(p);
194:
195: p.setProperty("prop1", "val1");
196: p.setProperty("prop2", "val2");
197: try {
198: getConnection().setClientInfo(p);
199: fail("setClientInfo(String,String) should throw "
200: + "SQLClientInfoException");
201: } catch (SQLClientInfoException cie) {
202: assertSQLState("SQLStates must match", "XCY02", cie);
203: assertTrue("Setting property 'prop1' must fail with "
204: + "REASON_UNKNOWN_PROPERTY", cie
205: .getFailedProperties().get("prop1").equals(
206: ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
207: assertTrue("Setting property 'prop2' must fail with "
208: + "REASON_UNKNOWN_PROPERTY", cie
209: .getFailedProperties().get("prop2").equals(
210: ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
211: }
212: }
213:
214: public void testSetClientInfoString() throws SQLException {
215: getConnection().setClientInfo(null, null);
216:
217: try {
218: getConnection().setClientInfo("foo", null);
219: fail("setClientInfo(String, null) should throw "
220: + "NullPointerException");
221: } catch (NullPointerException npe) {
222: }
223:
224: try {
225: getConnection().setClientInfo("name", "value");
226: fail("setClientInfo(String,String) should throw "
227: + "SQLClientInfoException");
228: } catch (SQLClientInfoException cie) {
229: assertSQLState("SQLState must match 'unsupported'",
230: "XCY02", cie);
231: assertTrue("Setting property 'name' must fail with "
232: + "REASON_UNKNOWN_PROPERTY", cie
233: .getFailedProperties().get("name").equals(
234: ClientInfoStatus.REASON_UNKNOWN_PROPERTY));
235: }
236: }
237:
238: public void testUnwrapValid() throws SQLException {
239: Connection unwrappedCon = getConnection().unwrap(
240: Connection.class);
241: assertSame("Unwrap returned wrong object.", getConnection(),
242: unwrappedCon);
243: }
244:
245: public void testUnwrapInvalid() throws SQLException {
246: try {
247: ResultSet unwrappedRs = getConnection().unwrap(
248: ResultSet.class);
249: fail("unwrap should have thrown an exception");
250: } catch (SQLException sqle) {
251: assertSQLState("Incorrect SQL state when unable to unwrap",
252: SQLStateConstants.UNABLE_TO_UNWRAP, sqle);
253: }
254: }
255:
256: //------------------ E N D O F T E S T M E T H O D S -------------------
257:
258: /**
259: * Create suite containing client-only tests.
260: */
261: private static TestSuite clientSuite(String name) {
262: TestSuite clientSuite = new TestSuite(name);
263: return clientSuite;
264: }
265:
266: /**
267: * Create suite containing embedded-only tests.
268: */
269: private static TestSuite embeddedSuite(String name) {
270: TestSuite embeddedSuite = new TestSuite(name);
271: embeddedSuite.addTest(new ConnectionTest("embeddedCreateBlob"));
272: embeddedSuite.addTest(new ConnectionTest("embeddedCreateClob"));
273: return embeddedSuite;
274: }
275:
276: /**
277: * Create a test suite containing tests for a JDB connection.
278: * In addition, separate suites for embedded- and client-only are added
279: * when appropriate.
280: */
281: public static Test suite() {
282: TestSuite connSuite = new TestSuite(ConnectionTest.class,
283: "ConnectionTest suite");
284: // Add client only tests
285: // NOTE: JCC is excluded
286: if (usingDerbyNetClient()) {
287: connSuite
288: .addTest(clientSuite("ConnectionTest client-only suite"));
289: }
290: // Add embedded only tests
291: if (usingEmbedded()) {
292: connSuite
293: .addTest(embeddedSuite("ConnectionTest embedded-only suite"));
294: }
295: return connSuite;
296: }
297:
298: } // End class BaseJDBCTestCase
|