001: /* Copyright 2004 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.rdbm;
007:
008: import java.sql.Connection;
009: import java.sql.PreparedStatement;
010: import java.sql.ResultSet;
011: import java.sql.SQLException;
012:
013: import junit.framework.TestCase;
014:
015: /**
016: * Testcase for HsqlDatasource
017: * @author andrew.petro@yale.edu
018: * @version $Revision: 35073 $ $Date: 2004-11-17 12:17:28 -0700 (Wed, 17 Nov 2004) $
019: */
020: public class TransientDatasourceTest extends TestCase {
021:
022: /**
023: * Test basic single usage of a Transient datasource.
024: * @throws SQLException
025: */
026: public void testBasics() throws SQLException {
027: TransientDatasource source = new TransientDatasource();
028:
029: Connection c = source.getConnection();
030:
031: c.prepareStatement("CREATE TABLE foo_table (bar_col INT)")
032: .execute();
033: PreparedStatement inserter = c
034: .prepareStatement("INSERT INTO foo_table (bar_col) VALUES (?)");
035: inserter.setInt(1, 10);
036: inserter.execute();
037: inserter.setInt(1, 11);
038: inserter.execute();
039: inserter.setInt(1, 12);
040: inserter.execute();
041: inserter.setInt(1, 13);
042: inserter.execute();
043:
044: PreparedStatement query = c
045: .prepareStatement("SELECT bar_col FROM foo_table ORDER BY bar_col");
046: ResultSet rs = query.executeQuery();
047:
048: for (int i = 10; i < 14; i++) {
049: rs.next();
050: assertEquals(i, rs.getInt("bar_col"));
051: }
052:
053: c.prepareStatement("DROP TABLE foo_table").execute();
054:
055: c.close();
056:
057: }
058:
059: /**
060: * Test that changes made to the database persist across requests for the
061: * connection.
062: * @throws SQLException
063: */
064: public void testMultiuse() throws SQLException {
065: TransientDatasource source = new TransientDatasource();
066:
067: Connection connectionOne = source.getConnection();
068:
069: connectionOne.prepareStatement(
070: "CREATE TABLE foo_table (bar_col INT)").execute();
071: PreparedStatement inserter = connectionOne
072: .prepareStatement("INSERT INTO foo_table (bar_col) VALUES (?)");
073: inserter.setInt(1, 10);
074: inserter.execute();
075: inserter.setInt(1, 11);
076: inserter.execute();
077: inserter.setInt(1, 12);
078: inserter.execute();
079: inserter.setInt(1, 13);
080: inserter.execute();
081:
082: connectionOne.close();
083:
084: Connection connectionTwo = source.getConnection();
085:
086: PreparedStatement query = connectionTwo
087: .prepareStatement("SELECT bar_col FROM foo_table ORDER BY bar_col");
088: ResultSet rs = query.executeQuery();
089:
090: for (int i = 10; i < 14; i++) {
091: rs.next();
092: assertEquals(i, rs.getInt("bar_col"));
093: }
094:
095: connectionTwo.prepareStatement("DROP TABLE foo_table")
096: .execute();
097:
098: connectionTwo.close();
099:
100: }
101:
102: }
|