KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > NativeJdbcExtractorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.jdbc.support;
18
19 import java.sql.CallableStatement JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DatabaseMetaData JavaDoc;
22 import java.sql.PreparedStatement JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26
27 import junit.framework.TestCase;
28 import org.easymock.MockControl;
29
30 import org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor;
31 import org.springframework.jdbc.support.nativejdbc.SimpleNativeJdbcExtractor;
32
33 /**
34  * @author Andre Biryukov
35  */

36 public class NativeJdbcExtractorTests extends TestCase {
37
38     public void testSimpleNativeJdbcExtractor() throws SQLException JavaDoc {
39         SimpleNativeJdbcExtractor extractor = new SimpleNativeJdbcExtractor();
40
41         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
42         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
43         MockControl dbmdControl = MockControl.createControl(DatabaseMetaData JavaDoc.class);
44         DatabaseMetaData JavaDoc dbmd = (DatabaseMetaData JavaDoc) dbmdControl.getMock();
45         MockControl con2Control = MockControl.createControl(Connection JavaDoc.class);
46         Connection JavaDoc con2 = (Connection JavaDoc) con2Control.getMock();
47         con.getMetaData();
48         conControl.setReturnValue(dbmd, 2);
49         dbmd.getConnection();
50         dbmdControl.setReturnValue(con2, 2);
51         conControl.replay();
52         dbmdControl.replay();
53         con2Control.replay();
54
55         Connection JavaDoc nativeCon = extractor.getNativeConnection(con);
56         assertEquals(con2, nativeCon);
57
58         MockControl stmtControl = MockControl.createControl(Statement JavaDoc.class);
59         Statement JavaDoc stmt = (Statement JavaDoc) stmtControl.getMock();
60         stmt.getConnection();
61         stmtControl.setReturnValue(con);
62         stmtControl.replay();
63
64         nativeCon = extractor.getNativeConnectionFromStatement(stmt);
65         assertEquals(con2, nativeCon);
66
67         Statement JavaDoc nativeStmt = extractor.getNativeStatement(stmt);
68         assertEquals(nativeStmt, stmt);
69
70         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
71         PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
72         psControl.replay();
73
74         PreparedStatement JavaDoc nativePs = extractor.getNativePreparedStatement(ps);
75         assertEquals(ps, nativePs);
76
77         MockControl csControl = MockControl.createControl(CallableStatement JavaDoc.class);
78         CallableStatement JavaDoc cs = (CallableStatement JavaDoc) csControl.getMock();
79         MockControl rsControl = MockControl.createControl(ResultSet JavaDoc.class);
80         ResultSet JavaDoc rs = (ResultSet JavaDoc) rsControl.getMock();
81         cs.getResultSet();
82         csControl.setReturnValue(rs);
83         csControl.replay();
84         rsControl.replay();
85
86         CallableStatement JavaDoc nativeCs = extractor.getNativeCallableStatement(cs);
87         assertEquals(cs, nativeCs);
88
89         ResultSet JavaDoc nativeRs = extractor.getNativeResultSet(cs.getResultSet());
90         assertEquals(nativeRs, rs);
91
92         conControl.verify();
93         dbmdControl.verify();
94         con2Control.verify();
95         stmtControl.verify();
96         psControl.verify();
97         csControl.verify();
98         rsControl.verify();
99     }
100
101     public void testCommonsDbcpNativeJdbcExtractor() throws SQLException JavaDoc {
102         CommonsDbcpNativeJdbcExtractor extractor = new CommonsDbcpNativeJdbcExtractor();
103         assertFalse(extractor.isNativeConnectionNecessaryForNativeStatements());
104
105         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
106         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
107         MockControl stmtControl = MockControl.createControl(Statement JavaDoc.class);
108         Statement JavaDoc stmt = (Statement JavaDoc) stmtControl.getMock();
109         stmt.getConnection();
110         stmtControl.setReturnValue(con);
111         conControl.replay();
112         stmtControl.replay();
113
114         Connection JavaDoc nativeConnection = extractor.getNativeConnection(con);
115         assertEquals(con, nativeConnection);
116
117         nativeConnection = extractor.getNativeConnectionFromStatement(stmt);
118         assertEquals(con, nativeConnection);
119         assertEquals(stmt, extractor.getNativeStatement(stmt));
120
121         MockControl psControl = MockControl.createControl(PreparedStatement JavaDoc.class);
122         PreparedStatement JavaDoc ps = (PreparedStatement JavaDoc) psControl.getMock();
123         psControl.replay();
124
125         assertEquals(ps, extractor.getNativePreparedStatement(ps));
126
127         CallableStatement JavaDoc mockCallableStmt =
128             (CallableStatement JavaDoc) MockControl.createControl(CallableStatement JavaDoc.class).getMock();
129         assertEquals(mockCallableStmt, extractor.getNativeCallableStatement(mockCallableStmt));
130
131         ResultSet JavaDoc mockResultSet = mockCallableStmt.getResultSet();
132         assertEquals(mockResultSet, extractor.getNativeResultSet(mockResultSet));
133
134         conControl.verify();
135         stmtControl.verify();
136         psControl.verify();
137     }
138
139 }
140
Popular Tags