001: /*
002: * $Id: TestAxionConnection.java,v 1.11 2007/11/13 19:04:01 rwald Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.jdbc;
042:
043: import java.sql.Connection;
044: import java.sql.ResultSet;
045: import java.sql.SQLException;
046: import java.sql.SQLWarning;
047: import java.sql.Statement;
048: import java.util.Collections;
049:
050: import junit.framework.Test;
051: import junit.framework.TestSuite;
052:
053: /**
054: * @version $Revision: 1.11 $ $Date: 2007/11/13 19:04:01 $
055: * @author Rodney Waldhoff
056: * @author Jonathan Giron
057: */
058: public class TestAxionConnection extends AxionTestCaseSupport {
059:
060: public TestAxionConnection(String testName) {
061: super (testName);
062: }
063:
064: public static Test suite() {
065: return new TestSuite(TestAxionConnection.class);
066: }
067:
068: public void testGetURL() throws Exception {
069: assertEquals(CONNECT_STRING, getAxionConnection().getURL());
070: }
071:
072: // "Calling the method close on a Connection object that is already closed is a no-op"
073: public void testDoubleClose() throws Exception {
074: Connection conn = getConnection();
075: assertTrue(!conn.isClosed());
076: conn.close();
077: assertTrue(conn.isClosed());
078: conn.close();
079: assertTrue(conn.isClosed());
080: }
081:
082: public void testCommitAndRollbackWithoutTransaction()
083: throws Exception {
084: Connection conn = getConnection();
085: conn.setAutoCommit(true);
086: try {
087: conn.commit();
088: fail("Expected SQLException");
089: } catch (SQLException e) {
090: // expected
091: }
092: try {
093: conn.rollback();
094: fail("Expected SQLException");
095: } catch (SQLException e) {
096: // expected
097: }
098: conn.close();
099: }
100:
101: public void testAutoCommitTrueByDefault() throws Exception {
102: assertTrue(getConnection().getAutoCommit());
103: }
104:
105: public void testGetCatalog() throws Exception {
106: assertEquals("", getConnection().getCatalog());
107: }
108:
109: public void testGetMetaData() throws Exception {
110: assertNotNull(getConnection().getMetaData());
111: }
112:
113: public void testPrepareCall() throws Exception {
114: try {
115: getConnection().prepareCall("call xyzzy");
116: fail("Expected SQLException");
117: } catch (SQLException e) {
118: // expected
119: }
120: try {
121: getConnection().prepareCall("call xyzzy", 1, 1);
122: fail("Expected SQLException");
123: } catch (SQLException e) {
124: // expected
125: }
126: }
127:
128: public void testPrepareStatement() throws Exception {
129: try {
130: getConnection().prepareStatement(
131: "select * from foo where id = ?", 1, 1);
132: fail("Expected SQLException");
133: } catch (SQLException e) {
134: // expected
135: }
136: }
137:
138: public void testCreateStatement() throws Exception {
139: // Per JDBC spec, createStatement() returns a forward-only, read-only Statement.
140: Statement stmt = getConnection().createStatement();
141: assertEquals(ResultSet.TYPE_FORWARD_ONLY, stmt
142: .getResultSetType());
143: assertEquals(ResultSet.CONCUR_READ_ONLY, stmt
144: .getResultSetConcurrency());
145:
146: // Axion supports forward-only and scroll-sensitive ResultSets.
147: assertExpectedStatementType(ResultSet.TYPE_FORWARD_ONLY,
148: ResultSet.CONCUR_READ_ONLY);
149: assertExpectedStatementType(ResultSet.TYPE_FORWARD_ONLY,
150: ResultSet.CONCUR_UPDATABLE);
151: assertExpectedStatementType(ResultSet.TYPE_SCROLL_SENSITIVE,
152: ResultSet.CONCUR_READ_ONLY);
153: assertExpectedStatementType(ResultSet.TYPE_SCROLL_SENSITIVE,
154: ResultSet.CONCUR_UPDATABLE);
155:
156: // Unsupported but known ResultSet types should return a "best-guess" implementation and
157: // generate "an SQLWarning on the Connection object that is creating the statement." [sic]
158: {
159: Connection conn = getConnection();
160: conn.clearWarnings();
161: stmt = conn.createStatement(
162: ResultSet.TYPE_SCROLL_INSENSITIVE,
163: ResultSet.CONCUR_READ_ONLY);
164: SQLWarning warning = conn.getWarnings();
165: assertNotNull(warning);
166: assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, stmt
167: .getResultSetType());
168: }
169:
170: {
171: Connection conn = getConnection();
172: conn.clearWarnings();
173: stmt = conn.createStatement(
174: ResultSet.TYPE_SCROLL_INSENSITIVE,
175: ResultSet.CONCUR_UPDATABLE);
176: assertNotNull(conn.getWarnings());
177: assertEquals(ResultSet.TYPE_SCROLL_SENSITIVE, stmt
178: .getResultSetType());
179: }
180:
181: // Bogus type and concurrency values should throw a SQLException - unsure whether this
182: // too should instead return a fallback Statement type.
183: try {
184: getConnection().createStatement(
185: ResultSet.TYPE_FORWARD_ONLY, -12324);
186: fail("Expected SQLException");
187: } catch (SQLException e) {
188: // expected
189: }
190:
191: try {
192: getConnection().createStatement(-12324,
193: ResultSet.CONCUR_UPDATABLE);
194: fail("Expected SQLException");
195: } catch (SQLException e) {
196: // expected
197: }
198:
199: try {
200: getConnection().createStatement(-12324, -12324);
201: fail("Expected SQLException");
202: } catch (SQLException e) {
203: // expected
204: }
205: }
206:
207: /**
208: * @param stmt
209: * @param type_forward_only
210: * @param concur_read_only
211: */
212: private void assertExpectedStatementType(int stmtType,
213: int stmtConcur) throws Exception {
214: Statement stmt = getConnection().createStatement(stmtType,
215: stmtConcur);
216: assertNotNull(stmt);
217: assertEquals(stmtType, stmt.getResultSetType());
218: assertEquals(stmtConcur, stmt.getResultSetConcurrency());
219: }
220:
221: public void testSetCatalog() throws Exception {
222: getConnection().setCatalog("");
223: }
224:
225: public void testSetReadOnly() throws Exception {
226: getConnection().setReadOnly(false);
227: }
228:
229: public void testGetWarnings() throws Exception {
230: Connection conn = getConnection();
231: assertNull(conn.getWarnings());
232: conn.clearWarnings();
233: assertNull(conn.getWarnings());
234: conn.close();
235: try {
236: conn.getWarnings();
237: fail("Expected SQLException");
238: } catch (SQLException e) {
239: // expected
240: }
241: }
242:
243: public void testIsReadOnly() throws Exception {
244: assertTrue(!getConnection().isReadOnly());
245: }
246:
247: public void testNativeSQL() throws Exception {
248: String sql = "select * from foo";
249: assertEquals(sql, getConnection().nativeSQL(sql));
250: }
251:
252: public void testGetTypeMap() throws Exception {
253: assertNotNull(getConnection().getTypeMap());
254: assertTrue(getConnection().getTypeMap().isEmpty());
255: }
256:
257: public void testSetAutoCommit() throws Exception {
258: Connection conn = getConnection();
259: assertTrue(conn.getAutoCommit());
260: conn.setAutoCommit(false);
261: assertTrue(!conn.getAutoCommit());
262: conn.setAutoCommit(true);
263: assertTrue(conn.getAutoCommit());
264: }
265:
266: public void testDefaultTransactionIsolation() throws Exception {
267: assertEquals(Connection.TRANSACTION_SERIALIZABLE,
268: getConnection().getTransactionIsolation());
269: }
270:
271: public void testSetTransactionIsolation() throws Exception {
272: Connection conn = getConnection();
273: conn
274: .setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
275: assertEquals(Connection.TRANSACTION_SERIALIZABLE, conn
276: .getTransactionIsolation());
277: try {
278: conn
279: .setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
280: fail("Expected SQLException");
281: } catch (SQLException e) {
282: // expected
283: }
284: }
285: }
|