1 16 17 package org.springframework.jdbc.support; 18 19 import java.sql.CallableStatement ; 20 import java.sql.Connection ; 21 import java.sql.DatabaseMetaData ; 22 import java.sql.PreparedStatement ; 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Statement ; 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 36 public class NativeJdbcExtractorTests extends TestCase { 37 38 public void testSimpleNativeJdbcExtractor() throws SQLException { 39 SimpleNativeJdbcExtractor extractor = new SimpleNativeJdbcExtractor(); 40 41 MockControl conControl = MockControl.createControl(Connection .class); 42 Connection con = (Connection ) conControl.getMock(); 43 MockControl dbmdControl = MockControl.createControl(DatabaseMetaData .class); 44 DatabaseMetaData dbmd = (DatabaseMetaData ) dbmdControl.getMock(); 45 MockControl con2Control = MockControl.createControl(Connection .class); 46 Connection con2 = (Connection ) 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 nativeCon = extractor.getNativeConnection(con); 56 assertEquals(con2, nativeCon); 57 58 MockControl stmtControl = MockControl.createControl(Statement .class); 59 Statement stmt = (Statement ) 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 nativeStmt = extractor.getNativeStatement(stmt); 68 assertEquals(nativeStmt, stmt); 69 70 MockControl psControl = MockControl.createControl(PreparedStatement .class); 71 PreparedStatement ps = (PreparedStatement ) psControl.getMock(); 72 psControl.replay(); 73 74 PreparedStatement nativePs = extractor.getNativePreparedStatement(ps); 75 assertEquals(ps, nativePs); 76 77 MockControl csControl = MockControl.createControl(CallableStatement .class); 78 CallableStatement cs = (CallableStatement ) csControl.getMock(); 79 MockControl rsControl = MockControl.createControl(ResultSet .class); 80 ResultSet rs = (ResultSet ) rsControl.getMock(); 81 cs.getResultSet(); 82 csControl.setReturnValue(rs); 83 csControl.replay(); 84 rsControl.replay(); 85 86 CallableStatement nativeCs = extractor.getNativeCallableStatement(cs); 87 assertEquals(cs, nativeCs); 88 89 ResultSet 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 { 102 CommonsDbcpNativeJdbcExtractor extractor = new CommonsDbcpNativeJdbcExtractor(); 103 assertFalse(extractor.isNativeConnectionNecessaryForNativeStatements()); 104 105 MockControl conControl = MockControl.createControl(Connection .class); 106 Connection con = (Connection ) conControl.getMock(); 107 MockControl stmtControl = MockControl.createControl(Statement .class); 108 Statement stmt = (Statement ) stmtControl.getMock(); 109 stmt.getConnection(); 110 stmtControl.setReturnValue(con); 111 conControl.replay(); 112 stmtControl.replay(); 113 114 Connection 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 .class); 122 PreparedStatement ps = (PreparedStatement ) psControl.getMock(); 123 psControl.replay(); 124 125 assertEquals(ps, extractor.getNativePreparedStatement(ps)); 126 127 CallableStatement mockCallableStmt = 128 (CallableStatement ) MockControl.createControl(CallableStatement .class).getMock(); 129 assertEquals(mockCallableStmt, extractor.getNativeCallableStatement(mockCallableStmt)); 130 131 ResultSet 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 |