You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by km...@apache.org on 2007/02/12 23:30:54 UTC
svn commit: r506709 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/master/ functionTests/suites/ functionTests/tests/lang/ junit/
Author: kmarsden
Date: Mon Feb 12 14:30:53 2007
New Revision: 506709
URL: http://svn.apache.org/viewvc?view=rev&rev=506709
Log:
DERBY-2299 convert cursor.java to JUnit
Added:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java (with props)
Removed:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/master/cursor.out
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/cursor.java
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall?view=diff&rev=506709&r1=506708&r2=506709
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbylang.runall Mon Feb 12 14:30:53 2007
@@ -23,7 +23,6 @@
lang/connect.sql
lang/consistencyChecker.sql
lang/currentSchema.sql
-lang/cursor.java
lang/cursorerrors.sql
lang/datetime.sql
lang/db2Compatibility.sql
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java?view=auto&rev=506709
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java Mon Feb 12 14:30:53 2007
@@ -0,0 +1,168 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.derbyTesting.functionTests.tests.lang;
+
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Connection;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+
+
+
+/**
+ * Tests cursors
+ */
+public class CursorTest extends BaseJDBCTestCase {
+
+ /**
+ * Creates a new <code>CursorTest</code> instance.
+ *
+ * @param name name of the test
+ */
+ public CursorTest(String name) {
+ super(name);
+ }
+
+
+ /**
+ * Test cursor methods with regular cursor
+ * @throws SQLException
+ */
+ public void testCursor() throws SQLException {
+ PreparedStatement select;
+ ResultSet cursor;
+
+
+ // because there is no order by (nor can there be)
+ // the fact that this test prints out rows may someday
+ // be a problem. When that day comes, the row printing
+ // can (should) be removed from this test.
+
+ select = prepareStatement("select i, c from t for update");
+ cursor = select.executeQuery(); // cursor is now open
+ cursor.next();
+ assertEquals(1956, cursor.getInt(1));
+ assertEquals("hello world", cursor.getString(2).trim());
+ // close and then test that fetch causes an error
+ cursor.close();
+ assertNextError("XCL16", cursor);
+ //restart the query for another test
+ cursor = select.executeQuery();
+ // test next/getInt past the end of table
+ while (cursor.next());
+ cursor.next();
+ assertGetIntError(1, "24000",cursor);
+ }
+
+
+ /**
+ * Test cursor methods with parameter
+ * @throws SQLException
+ */
+ public void testCursorParam() throws SQLException{
+ PreparedStatement select;
+ ResultSet cursor;
+ select = prepareStatement("select i, c from t where ?=1 for update");
+ select.setInt(1,1);
+ cursor = select.executeQuery();
+ //TEST: fetch of a row works
+ assertTrue("FAIL: unable to fetch row.",cursor.next());
+ assertEquals("FAIL: Wrong row on fetch with param", 1956,cursor.getInt(1));
+ // TEST: Close and then fetch gets an error on fetch.
+ cursor.close();
+ assertNextError("XCL16", cursor);
+ // restart the query for another test
+ select.setBoolean(1,false);
+ select.setCursorName("ForCoverageSake");
+ cursor = select.executeQuery();
+ assertEquals(cursor.getCursorName(),"ForCoverageSake");
+ cursor.next();
+ assertGetIntError(1,"24000", cursor);
+ }
+
+
+
+ /**
+ * Test getCursorName with and without update cursor
+ * @throws SQLException
+ */
+ public void testGetCursorName()throws SQLException {
+ Statement s = createStatement();
+ ResultSet rs = s.executeQuery("select * from t");
+ assertNull(rs.getCursorName());
+ rs.close();
+
+ // test Automatic naming of cursor for update
+ rs = s.executeQuery("select * from t for update");
+ assertEquals("SQLCUR0",rs.getCursorName());
+ rs.close();
+
+ rs = s.executeQuery("select * from t for update of i");
+ assertEquals("SQLCUR1", rs.getCursorName());
+ rs.close();
+
+ s.setCursorName("myselect");
+ rs = s.executeQuery("select * from t");
+ assertEquals("myselect", rs.getCursorName());
+ rs.close();
+
+ s.setCursorName("myselect2");
+ rs = s.executeQuery("select * from t for update");
+ assertEquals("myselect2",rs.getCursorName());
+
+ s.setCursorName("myselect3");
+ rs = s.executeQuery("select * from t for update of i");
+ rs.close();
+ s.close();
+ }
+ public static Test suite() {
+ TestSuite suite = new TestSuite("CursorTestJunit");
+ suite.addTestSuite(CursorTest.class);
+ return suite;
+ }
+
+protected void setUp() throws SQLException {
+ getConnection().setAutoCommit(false);
+ Statement stmt = createStatement();
+ stmt.executeUpdate("create table t (i int, c char(50))");
+ stmt.executeUpdate("create table s (i int, c char(50))");
+ stmt.executeUpdate("insert into t values (1956, 'hello world')");
+ stmt.executeUpdate("insert into t values (456, 'hi yourself')");
+ stmt.executeUpdate("insert into t values (180, 'rubber ducky')");
+ stmt.executeUpdate("insert into t values (3, 'you are the one')");
+
+ stmt.close();
+ commit();
+}
+
+protected void tearDown() throws Exception {
+ Statement stmt = createStatement();
+ stmt.executeUpdate("drop table t");
+ stmt.executeUpdate("drop table s");
+ stmt.close();
+ commit();
+ super.tearDown();
+}
+
+}
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/CursorTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?view=diff&rev=506709&r1=506708&r2=506709
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java Mon Feb 12 14:30:53 2007
@@ -75,6 +75,7 @@
suite.addTest(SysDiagVTIMappingTest.suite());
suite.addTest(UpdatableResultSetTest.suite());
suite.addTest(CurrentOfTest.suite());
+ suite.addTest(CursorTest.suite());
// Add the XML tests, which exist as a separate suite
// so that users can "run all XML tests" easily.
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?view=diff&rev=506709&r1=506708&r2=506709
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java Mon Feb 12 14:30:53 2007
@@ -680,6 +680,39 @@
}
/**
+ * Perform a fetch on the ResultSet with an expected failure
+ *
+ * @param sqlState Expected SQLState
+ * @param rs ResultSet upon which next() will be called
+ */
+ public static void assertNextError(String sqlState,ResultSet rs)
+ {
+ try {
+ rs.next();
+ fail("Expected error on next()");
+ }catch (SQLException se){
+ assertSQLState(sqlState,se);
+ }
+ }
+
+ /**
+ * Perform getInt(position) with expected error
+ * @param position position argument to pass to getInt
+ * @param sqlState sqlState of expected error
+ * @param rs ResultSet upon which to call getInt(position)
+ */
+ public static void assertGetIntError(int position, String sqlState, ResultSet rs)
+ {
+ try {
+ rs.getInt(position);
+ fail("Expected exception " + sqlState);
+ } catch (SQLException se){
+ assertSQLState(sqlState,se);
+ }
+
+
+ }
+ /**
* Take a Statement object and a SQL query, execute it
* via the "executeUpdate()" method, and assert that the
* resultant row count matches the received row count.