1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/*
*
* Derby - Class StatementTestSetup
*
* 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.jdbc4;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
import junit.framework.Test;
import junit.extensions.TestSetup;
import java.sql.*;
/**
* Create the table necessary for running {@link StatementTest}.
*
* @see StatementTest
*/
public class StatementTestSetup
extends BaseJDBCTestSetup {
/**
* Initialize database schema.
* Uses the framework specified by the test harness.
*
* @see StatementTest
*/
public StatementTestSetup(Test test) {
super(test);
}
/**
* Create the table and data needed for the test.
*
* @throws SQLException if database operations fail.
*
* @see StatementTest
*/
protected void setUp()
throws SQLException {
Connection con = getConnection();
// Create tables used by the test.
Statement stmt = con.createStatement();
// See if the table is already there, and if so, delete it.
try {
stmt.execute("select count(*) from stmtTable");
// Only get here is the table already exists.
stmt.execute("drop table stmtTable");
} catch (SQLException sqle) {
// Table does not exist, so we can go ahead and create it.
assertEquals("Unexpected error when accessing non-existing table.",
"42X05",
sqle.getSQLState());
}
try {
stmt.execute("drop function delay_st");
}
catch (SQLException se)
{
// ignore object does not exist error
assertEquals( "42Y55", se.getSQLState() );
}
stmt.execute("create table stmtTable (id int, val varchar(10))");
stmt.execute("insert into stmtTable values (1, 'one'),(2,'two')");
// Check just to be sure, and to notify developers if the database
// contents are changed at a later time.
ResultSet rs = stmt.executeQuery("select count(*) from stmtTable");
rs.next();
assertEquals("Number of rows are not as expected",
2, rs.getInt(1));
rs.close();
stmt.execute
(
"create function delay_st(seconds integer, value integer) returns integer\n" +
"parameter style java no sql language java\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.jdbcapi.SetQueryTimeoutTest.delay'"
);
stmt.close();
con.commit();
}
/**
* Clean up after the tests.
* Deletes the table that was created for the tests.
*
* @throws SQLException if database operations fail.
*/
protected void tearDown()
throws Exception {
Connection con = getConnection();
Statement stmt = con.createStatement();
stmt.execute("drop table stmtTable");
stmt.close();
con.commit();
super.tearDown();
}
} // End class StatementTestSetup
|