KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > util > TestUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.c3p0.util;
25
26 import java.sql.*;
27 import javax.sql.*;
28 import java.lang.reflect.*;
29 import java.util.Random JavaDoc;
30 import com.mchange.v2.sql.SqlUtils;
31 import com.mchange.v2.c3p0.C3P0ProxyConnection;
32
33 public final class TestUtils
34 {
35     private final static Method OBJECT_EQUALS;
36     private final static Method IDENTITY_HASHCODE;
37     private final static Method IPCFP;
38
39
40     static
41     {
42     try
43         {
44         OBJECT_EQUALS = Object JavaDoc.class.getMethod("equals", new Class JavaDoc[] { Object JavaDoc.class });
45         IDENTITY_HASHCODE = System JavaDoc.class.getMethod("identityHashCode", new Class JavaDoc[] {Object JavaDoc.class});
46
47         // is this okay? getting a method from a class while it is still being initialized?
48
IPCFP = TestUtils.class.getMethod("isPhysicalConnectionForProxy", new Class JavaDoc[] {Connection.class, C3P0ProxyConnection.class});
49         }
50     catch ( Exception JavaDoc e )
51         {
52         e.printStackTrace();
53         throw new RuntimeException JavaDoc("Huh? Can't reflectively get ahold of expected methods?");
54         }
55     }
56         
57     /**
58      * In general, if this method returns true for two distinct C3P0ProxyConnections, it indicates a c3p0 bug.
59      * Once a proxy Connection is close()ed, it should not permit any sort of operation. Prior to Connection
60      * close(), there should be at most one valid proxy Connection associated with a given physical Connection.
61      */

62     public static boolean samePhysicalConnection( C3P0ProxyConnection con1, C3P0ProxyConnection con2 ) throws SQLException
63     {
64     try
65         {
66         Object JavaDoc out = con1.rawConnectionOperation( IPCFP, null, new Object JavaDoc[] { C3P0ProxyConnection.RAW_CONNECTION, con2 } );
67         return ((Boolean JavaDoc) out).booleanValue();
68         }
69     catch (Exception JavaDoc e)
70         {
71         e.printStackTrace();
72         throw SqlUtils.toSQLException( e );
73         }
74     }
75
76     public static boolean isPhysicalConnectionForProxy( Connection physicalConnection, C3P0ProxyConnection proxy ) throws SQLException
77     {
78     try
79         {
80         Object JavaDoc out = proxy.rawConnectionOperation( OBJECT_EQUALS, physicalConnection, new Object JavaDoc[] { C3P0ProxyConnection.RAW_CONNECTION } );
81         return ((Boolean JavaDoc) out).booleanValue();
82         }
83     catch (Exception JavaDoc e)
84         {
85         e.printStackTrace();
86         throw SqlUtils.toSQLException( e );
87         }
88     }
89
90     public static int physicalConnectionIdentityHashCode( C3P0ProxyConnection conn ) throws SQLException
91     {
92     try
93         {
94         Object JavaDoc out = conn.rawConnectionOperation( IDENTITY_HASHCODE, null, new Object JavaDoc[] { C3P0ProxyConnection.RAW_CONNECTION } );
95         return ((Integer JavaDoc) out).intValue();
96         }
97     catch (Exception JavaDoc e)
98         {
99         e.printStackTrace();
100         throw SqlUtils.toSQLException( e );
101         }
102     }
103
104
105     public static DataSource unreliableCommitDataSource(DataSource ds) throws Exception JavaDoc
106     {
107     return (DataSource) Proxy.newProxyInstance( TestUtils.class.getClassLoader(),
108                             new Class JavaDoc[] { DataSource.class },
109                             new StupidDataSourceInvocationHandler( ds ) );
110     }
111
112     private TestUtils()
113     {}
114
115     static class StupidDataSourceInvocationHandler implements InvocationHandler
116     {
117     DataSource ds;
118     Random JavaDoc r = new Random JavaDoc();
119     
120     StupidDataSourceInvocationHandler(DataSource ds)
121     { this.ds = ds; }
122     
123     public Object JavaDoc invoke(Object JavaDoc proxy, Method method, Object JavaDoc[] args)
124         throws Throwable JavaDoc
125     {
126         if ( "getConnection".equals(method.getName()) )
127         {
128             Connection conn = (Connection) method.invoke( ds, args );
129             return Proxy.newProxyInstance( TestUtils.class.getClassLoader(),
130                            new Class JavaDoc[] { Connection.class },
131                            new StupidConnectionInvocationHandler( conn ) );
132         }
133         else
134         return method.invoke( ds, args );
135     }
136     }
137
138     static class StupidConnectionInvocationHandler implements InvocationHandler
139     {
140     Connection conn;
141     Random JavaDoc r = new Random JavaDoc();
142     
143     boolean invalid = false;
144
145     StupidConnectionInvocationHandler(Connection conn)
146     { this.conn = conn; }
147     
148     public Object JavaDoc invoke(Object JavaDoc proxy, Method method, Object JavaDoc[] args)
149         throws Throwable JavaDoc
150     {
151         if ("close".equals(method.getName()))
152         {
153             if (invalid)
154             {
155                 new Exception JavaDoc("Duplicate close() called on Connection!!!").printStackTrace();
156             }
157             else
158             {
159                 //new Exception("CLOSE CALLED ON UNPOOLED DATASOURCE CONNECTION!").printStackTrace();
160
invalid = true;
161             }
162             return null;
163         }
164         else if ( invalid )
165         throw new SQLException("Connection closed -- cannot " + method.getName());
166         else if ( "commit".equals(method.getName()) && r.nextInt(100) == 0 )
167         {
168             conn.rollback();
169             throw new SQLException("Random commit exception!!!");
170         }
171         else if (r.nextInt(200) == 0)
172         {
173             conn.rollback();
174             conn.close();
175             throw new SQLException("Random Fatal Exception Occurred!!!");
176         }
177         else
178         return method.invoke( conn, args );
179     }
180     }
181
182 }
183
Popular Tags