KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Derby - Class StatementTestSetup
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,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20
21 package org.apache.derbyTesting.functionTests.tests.jdbc4;
22
23 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
24 import org.apache.derbyTesting.junit.BaseJDBCTestSetup;
25
26 import junit.framework.Test;
27 import junit.extensions.TestSetup;
28
29 import java.sql.*;
30
31 /**
32  * Create the table necessary for running {@link StatementTest}.
33  *
34  * @see StatementTest
35  */

36 public class StatementTestSetup
37     extends BaseJDBCTestSetup {
38
39     /**
40      * Initialize database schema.
41      * Uses the framework specified by the test harness.
42      *
43      * @see StatementTest
44      */

45     public StatementTestSetup(Test test) {
46         super(test);
47     }
48
49     /**
50      * Create the table and data needed for the test.
51      *
52      * @throws SQLException if database operations fail.
53      *
54      * @see StatementTest
55      */

56     protected void setUp()
57         throws SQLException {
58         Connection con = getConnection();
59         // Create tables used by the test.
60
Statement stmt = con.createStatement();
61         // See if the table is already there, and if so, delete it.
62
try {
63             stmt.execute("select count(*) from stmtTable");
64             // Only get here is the table already exists.
65
stmt.execute("drop table stmtTable");
66         } catch (SQLException sqle) {
67             // Table does not exist, so we can go ahead and create it.
68
assertEquals("Unexpected error when accessing non-existing table.",
69                     "42X05",
70                     sqle.getSQLState());
71         }
72         stmt.execute("create table stmtTable (id int, val varchar(10))");
73         stmt.execute("insert into stmtTable values (1, 'one'),(2,'two')");
74         // Check just to be sure, and to notify developers if the database
75
// contents are changed at a later time.
76
ResultSet rs = stmt.executeQuery("select count(*) from stmtTable");
77         rs.next();
78         assertEquals("Number of rows are not as expected",
79                 2, rs.getInt(1));
80         rs.close();
81         stmt.close();
82         con.commit();
83     }
84
85     /**
86      * Clean up after the tests.
87      * Deletes the table that was created for the tests.
88      *
89      * @throws SQLException if database operations fail.
90      */

91     protected void tearDown()
92         throws Exception JavaDoc {
93         Connection con = getConnection();
94         Statement stmt = con.createStatement();
95         stmt.execute("drop table stmtTable");
96         stmt.close();
97         con.commit();
98         super.tearDown();
99     }
100    
101 } // End class StatementTestSetup
102
Popular Tags