KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > c3p0 > test > TestRefSerStuff


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.test;
25
26 import java.sql.*;
27 import javax.sql.*;
28 import com.mchange.v2.c3p0.*;
29 import com.mchange.v1.db.sql.*;
30 import javax.naming.Reference JavaDoc;
31 import javax.naming.Referenceable JavaDoc;
32 import com.mchange.v2.naming.ReferenceableUtils;
33 import com.mchange.v2.ser.SerializableUtils;
34 import com.mchange.v2.c3p0.DriverManagerDataSource;
35 import com.mchange.v2.c3p0.PoolBackedDataSource;
36
37
38 public final class TestRefSerStuff
39 {
40     static void create(DataSource ds) throws SQLException
41     {
42     Connection con = null;
43     Statement stmt = null;
44     try
45         {
46         con = ds.getConnection();
47         stmt = con.createStatement();
48         stmt.executeUpdate("CREATE TABLE TRSS_TABLE ( a_col VARCHAR(16) )");
49         }
50     finally
51         {
52         StatementUtils.attemptClose( stmt );
53         ConnectionUtils.attemptClose( con );
54         }
55     }
56
57     static void drop(DataSource ds) throws SQLException
58     {
59     Connection con = null;
60     Statement stmt = null;
61     try
62         {
63         con = ds.getConnection();
64         stmt = con.createStatement();
65         stmt.executeUpdate("DROP TABLE TRSS_TABLE");
66         }
67     finally
68         {
69         StatementUtils.attemptClose( stmt );
70         ConnectionUtils.attemptClose( con );
71         }
72     }
73
74     static void doSomething(DataSource ds) throws SQLException
75     {
76     Connection con = null;
77     Statement stmt = null;
78     try
79         {
80         con = ds.getConnection();
81         stmt = con.createStatement();
82         int i = stmt.executeUpdate("INSERT INTO TRSS_TABLE VALUES ('" +
83                        System.currentTimeMillis() + "')");
84         if (i != 1)
85             throw new SQLException("Insert failed somehow strange!");
86         }
87     finally
88         {
89         StatementUtils.attemptClose( stmt );
90         ConnectionUtils.attemptClose( con );
91         }
92     }
93
94     /*
95     private static void usage()
96     {
97     System.err.println("java " +
98                "-Djdbc.drivers=<comma_sep_list_of_drivers> " +
99                TestRefSerStuff.class.getName() +
100                " <jdbc_url> [<username> <password>]" );
101     System.exit(-1);
102     }
103     */

104
105     static void doTest(DataSource checkMe) throws Exception JavaDoc
106     {
107     doSomething( checkMe );
108     System.err.println("\tcreated: " + checkMe);
109     DataSource afterSer = (DataSource) SerializableUtils.testSerializeDeserialize( checkMe );
110     doSomething( afterSer );
111     System.err.println("\tafter ser: " + afterSer );
112     Reference JavaDoc ref = ((Referenceable JavaDoc) checkMe).getReference();
113 // System.err.println("ref: " + ref);
114
// System.err.println("Factory Class: " + ref.getFactoryClassName());
115
DataSource afterRef = (DataSource) ReferenceableUtils.referenceToObject( ref,
116                                          null,
117                                          null,
118                                          null );
119 // System.err.println("afterRef data source: " + afterRef);
120
doSomething( afterRef );
121     System.err.println("\tafter ref: " + afterRef );
122     }
123
124     public static void main( String JavaDoc[] argv )
125     {
126         if (argv.length > 0)
127         {
128             System.err.println( TestRefSerStuff.class.getName() +
129                                 " now requires no args. Please set everything in standard c3p0 config files.");
130             return;
131         }
132
133         /*
134     String jdbcUrl = null;
135     String username = null;
136     String password = null;
137     if (argv.length == 3)
138         {
139         jdbcUrl = argv[0];
140         username = argv[1];
141         password = argv[2];
142         }
143     else if (argv.length == 1)
144         {
145         jdbcUrl = argv[0];
146         username = null;
147         password = null;
148         }
149     else
150         usage();
151     
152     if (! jdbcUrl.startsWith("jdbc:") )
153         usage();
154     */

155     
156     try
157         {
158         DriverManagerDataSource dmds = new DriverManagerDataSource();
159         //dmds.setJdbcUrl( jdbcUrl );
160
//dmds.setUser( username );
161
//dmds.setPassword( password );
162
try { drop( dmds ); }
163         catch (Exception JavaDoc e)
164             { /* Ignore */ }
165         create( dmds );
166
167         System.err.println("DriverManagerDataSource:");
168         doTest( dmds );
169         
170         WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource();
171         wcpds.setNestedDataSource( dmds );
172         PoolBackedDataSource pbds = new PoolBackedDataSource();
173         pbds.setConnectionPoolDataSource( wcpds );
174         
175         System.err.println("PoolBackedDataSource:");
176         doTest( pbds );
177         
178         ComboPooledDataSource cpds = new ComboPooledDataSource();
179         doTest( cpds );
180         }
181     catch ( Exception JavaDoc e )
182         { e.printStackTrace(); }
183     }
184
185 }
186
Popular Tags