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.lang.reflect.Method;
026:
027: import com.mchange.v2.c3p0.ComboPooledDataSource;
028: import com.mchange.v2.c3p0.C3P0ProxyConnection;
029: import com.mchange.v2.c3p0.C3P0ProxyStatement;
030: import com.mchange.v2.c3p0.util.TestUtils;
031:
032: public final class RawConnectionOpTest {
033: public static void main(String[] argv) {
034: try {
035: String jdbc_url = null;
036: String username = null;
037: String password = null;
038:
039: if (argv.length == 3) {
040: jdbc_url = argv[0];
041: username = argv[1];
042: password = argv[2];
043: } else if (argv.length == 1) {
044: jdbc_url = argv[0];
045: username = null;
046: password = null;
047: } else
048: usage();
049:
050: if (!jdbc_url.startsWith("jdbc:"))
051: usage();
052:
053: ComboPooledDataSource cpds = new ComboPooledDataSource();
054: cpds.setJdbcUrl(jdbc_url);
055: cpds.setUser(username);
056: cpds.setPassword(password);
057: cpds.setMaxPoolSize(10);
058: // cpds.setUsesTraditionalReflectiveProxies( true );
059:
060: C3P0ProxyConnection conn = (C3P0ProxyConnection) cpds
061: .getConnection();
062: Method toStringMethod = Object.class.getMethod("toString",
063: new Class[] {});
064: Method identityHashCodeMethod = System.class.getMethod(
065: "identityHashCode", new Class[] { Object.class });
066: System.out.println("rawConnection.toString() -> "
067: + conn.rawConnectionOperation(toStringMethod,
068: C3P0ProxyConnection.RAW_CONNECTION,
069: new Object[] {}));
070: Integer ihc = (Integer) conn
071: .rawConnectionOperation(
072: identityHashCodeMethod,
073: null,
074: new Object[] { C3P0ProxyConnection.RAW_CONNECTION });
075: System.out
076: .println("System.identityHashCode( rawConnection ) -> "
077: + Integer.toHexString(ihc.intValue()));
078:
079: C3P0ProxyStatement stmt = (C3P0ProxyStatement) conn
080: .createStatement();
081: System.out.println("rawStatement.toString() -> "
082: + stmt.rawStatementOperation(toStringMethod,
083: C3P0ProxyStatement.RAW_STATEMENT,
084: new Object[] {}));
085: Integer ihc2 = (Integer) stmt.rawStatementOperation(
086: identityHashCodeMethod, null,
087: new Object[] { C3P0ProxyStatement.RAW_STATEMENT });
088: System.out
089: .println("System.identityHashCode( rawStatement ) -> "
090: + Integer.toHexString(ihc2.intValue()));
091:
092: conn.close();
093:
094: for (int i = 0; i < 10; ++i) {
095: C3P0ProxyConnection check = null;
096: try {
097: check = (C3P0ProxyConnection) cpds.getConnection();
098: //System.err.println( TestUtils.samePhysicalConnection( conn, check ) );
099: System.err
100: .println(TestUtils
101: .physicalConnectionIdentityHashCode(check) == ihc
102: .intValue());
103: } finally { /* if (check != null) check.close(); */
104: }
105: }
106: } catch (Exception e) {
107: e.printStackTrace();
108: }
109: }
110:
111: private static void usage() {
112: System.err.println("java "
113: + RawConnectionOpTest.class.getName() + " \\");
114: System.err.println("\t<jdbc_driver_class> \\");
115: System.err.println("\t<jdbc_url> [<username> <password>]");
116: System.exit(-1);
117: }
118: }
|