001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.c3p0.test;
024:
025: import java.sql.*;
026: import javax.sql.*;
027: import com.mchange.v2.c3p0.*;
028: import com.mchange.v1.db.sql.*;
029: import javax.naming.Reference;
030: import javax.naming.Referenceable;
031: import com.mchange.v2.naming.ReferenceableUtils;
032: import com.mchange.v2.ser.SerializableUtils;
033: import com.mchange.v2.c3p0.DriverManagerDataSource;
034: import com.mchange.v2.c3p0.PoolBackedDataSource;
035:
036: public final class TestRefSerStuff {
037: static void create(DataSource ds) throws SQLException {
038: Connection con = null;
039: Statement stmt = null;
040: try {
041: con = ds.getConnection();
042: stmt = con.createStatement();
043: stmt
044: .executeUpdate("CREATE TABLE TRSS_TABLE ( a_col VARCHAR(16) )");
045: } finally {
046: StatementUtils.attemptClose(stmt);
047: ConnectionUtils.attemptClose(con);
048: }
049: }
050:
051: static void drop(DataSource ds) throws SQLException {
052: Connection con = null;
053: Statement stmt = null;
054: try {
055: con = ds.getConnection();
056: stmt = con.createStatement();
057: stmt.executeUpdate("DROP TABLE TRSS_TABLE");
058: } finally {
059: StatementUtils.attemptClose(stmt);
060: ConnectionUtils.attemptClose(con);
061: }
062: }
063:
064: static void doSomething(DataSource ds) throws SQLException {
065: Connection con = null;
066: Statement stmt = null;
067: try {
068: con = ds.getConnection();
069: stmt = con.createStatement();
070: int i = stmt
071: .executeUpdate("INSERT INTO TRSS_TABLE VALUES ('"
072: + System.currentTimeMillis() + "')");
073: if (i != 1)
074: throw new SQLException("Insert failed somehow strange!");
075: } finally {
076: StatementUtils.attemptClose(stmt);
077: ConnectionUtils.attemptClose(con);
078: }
079: }
080:
081: /*
082: private static void usage()
083: {
084: System.err.println("java " +
085: "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
086: TestRefSerStuff.class.getName() +
087: " <jdbc_url> [<username> <password>]" );
088: System.exit(-1);
089: }
090: */
091:
092: static void doTest(DataSource checkMe) throws Exception {
093: doSomething(checkMe);
094: System.err.println("\tcreated: " + checkMe);
095: DataSource afterSer = (DataSource) SerializableUtils
096: .testSerializeDeserialize(checkMe);
097: doSomething(afterSer);
098: System.err.println("\tafter ser: " + afterSer);
099: Reference ref = ((Referenceable) checkMe).getReference();
100: // System.err.println("ref: " + ref);
101: // System.err.println("Factory Class: " + ref.getFactoryClassName());
102: DataSource afterRef = (DataSource) ReferenceableUtils
103: .referenceToObject(ref, null, null, null);
104: // System.err.println("afterRef data source: " + afterRef);
105: doSomething(afterRef);
106: System.err.println("\tafter ref: " + afterRef);
107: }
108:
109: public static void main(String[] argv) {
110: if (argv.length > 0) {
111: System.err
112: .println(TestRefSerStuff.class.getName()
113: + " now requires no args. Please set everything in standard c3p0 config files.");
114: return;
115: }
116:
117: /*
118: String jdbcUrl = null;
119: String username = null;
120: String password = null;
121: if (argv.length == 3)
122: {
123: jdbcUrl = argv[0];
124: username = argv[1];
125: password = argv[2];
126: }
127: else if (argv.length == 1)
128: {
129: jdbcUrl = argv[0];
130: username = null;
131: password = null;
132: }
133: else
134: usage();
135:
136: if (! jdbcUrl.startsWith("jdbc:") )
137: usage();
138: */
139:
140: try {
141: DriverManagerDataSource dmds = new DriverManagerDataSource();
142: //dmds.setJdbcUrl( jdbcUrl );
143: //dmds.setUser( username );
144: //dmds.setPassword( password );
145: try {
146: drop(dmds);
147: } catch (Exception e) { /* Ignore */
148: }
149: create(dmds);
150:
151: System.err.println("DriverManagerDataSource:");
152: doTest(dmds);
153:
154: WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource();
155: wcpds.setNestedDataSource(dmds);
156: PoolBackedDataSource pbds = new PoolBackedDataSource();
157: pbds.setConnectionPoolDataSource(wcpds);
158:
159: System.err.println("PoolBackedDataSource:");
160: doTest(pbds);
161:
162: ComboPooledDataSource cpds = new ComboPooledDataSource();
163: doTest(cpds);
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: }
168:
169: }
|