提交 3120065b authored 作者: Thomas Mueller's avatar Thomas Mueller

Issue 573: Add implementation for Methods "isWrapperFor()" and "unwrap()" in other JDBC classes.

上级 7ad78c92
...@@ -39,6 +39,7 @@ public class TestMetaData extends TestBase { ...@@ -39,6 +39,7 @@ public class TestMetaData extends TestBase {
@Override @Override
public void test() throws SQLException { public void test() throws SQLException {
deleteDb("metaData"); deleteDb("metaData");
testUnwrap();
testUnsupportedOperations(); testUnsupportedOperations();
testTempTable(); testTempTable();
testColumnResultSetMeta(); testColumnResultSetMeta();
...@@ -56,16 +57,29 @@ public class TestMetaData extends TestBase { ...@@ -56,16 +57,29 @@ public class TestMetaData extends TestBase {
testSessionsUncommitted(); testSessionsUncommitted();
testQueryStatistics(); testQueryStatistics();
} }
private void testUnwrap() throws SQLException {
Connection conn = getConnection("metaData");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select 1 as x from dual");
ResultSetMetaData meta = rs.getMetaData();
assertTrue(meta.isWrapperFor(Object.class));
assertTrue(meta.isWrapperFor(ResultSetMetaData.class));
assertTrue(meta.isWrapperFor(meta.getClass()));
assertTrue(meta == meta.unwrap(Object.class));
assertTrue(meta == meta.unwrap(ResultSetMetaData.class));
assertTrue(meta == meta.unwrap(meta.getClass()));
assertFalse(meta.isWrapperFor(Integer.class));
assertThrows(ErrorCode.INVALID_VALUE_2, meta).
unwrap(Integer.class);
conn.close();
}
private void testUnsupportedOperations() throws SQLException { private void testUnsupportedOperations() throws SQLException {
Connection conn = getConnection("metaData"); Connection conn = getConnection("metaData");
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select 1 as x from dual"); ResultSet rs = stat.executeQuery("select 1 as x from dual");
ResultSetMetaData meta = rs.getMetaData(); ResultSetMetaData meta = rs.getMetaData();
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, meta).
isWrapperFor(Object.class);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, meta).
unwrap(Object.class);
assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnLabel(0); assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnLabel(0);
assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnName(0); assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnName(0);
assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnType(0); assertThrows(ErrorCode.INVALID_VALUE_2, meta).getColumnType(0);
...@@ -971,6 +985,7 @@ public class TestMetaData extends TestBase { ...@@ -971,6 +985,7 @@ public class TestMetaData extends TestBase {
private void testGeneral() throws SQLException { private void testGeneral() throws SQLException {
Connection conn = getConnection("metaData"); Connection conn = getConnection("metaData");
DatabaseMetaData meta = conn.getMetaData(); DatabaseMetaData meta = conn.getMetaData();
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"); stat.execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))");
......
...@@ -56,6 +56,7 @@ public class TestPreparedStatement extends TestBase { ...@@ -56,6 +56,7 @@ public class TestPreparedStatement extends TestBase {
public void test() throws Exception { public void test() throws Exception {
deleteDb("preparedStatement"); deleteDb("preparedStatement");
Connection conn = getConnection("preparedStatement"); Connection conn = getConnection("preparedStatement");
testUnwrap(conn);
testUnsupportedOperations(conn); testUnsupportedOperations(conn);
testChangeType(conn); testChangeType(conn);
testDateTimeTimestampWithCalendar(conn); testDateTimeTimestampWithCalendar(conn);
...@@ -95,6 +96,17 @@ public class TestPreparedStatement extends TestBase { ...@@ -95,6 +96,17 @@ public class TestPreparedStatement extends TestBase {
testPreparedStatementWithLiteralsNone(); testPreparedStatementWithLiteralsNone();
deleteDb("preparedStatement"); deleteDb("preparedStatement");
} }
private void testUnwrap(Connection conn) throws SQLException {
assertTrue(conn.isWrapperFor(Object.class));
assertTrue(conn.isWrapperFor(Connection.class));
assertTrue(conn.isWrapperFor(conn.getClass()));
assertFalse(conn.isWrapperFor(String.class));
assertTrue(conn == conn.unwrap(Object.class));
assertTrue(conn == conn.unwrap(Connection.class));
assertThrows(ErrorCode.INVALID_VALUE_2, conn).
unwrap(String.class);
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void testUnsupportedOperations(Connection conn) throws Exception { private void testUnsupportedOperations(Connection conn) throws Exception {
...@@ -134,17 +146,6 @@ public class TestPreparedStatement extends TestBase { ...@@ -134,17 +146,6 @@ public class TestPreparedStatement extends TestBase {
ParameterMetaData meta = prep.getParameterMetaData(); ParameterMetaData meta = prep.getParameterMetaData();
assertTrue(meta.toString(), meta.toString().endsWith("parameterCount=1")); assertTrue(meta.toString(), meta.toString().endsWith("parameterCount=1"));
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, meta).
isWrapperFor(Object.class);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, meta).
unwrap(Object.class);
assertTrue(conn.isWrapperFor(Object.class));
assertTrue(conn.isWrapperFor(Connection.class));
assertFalse(conn.isWrapperFor(String.class));
assertTrue(conn == conn.unwrap(Object.class));
assertTrue(conn == conn.unwrap(Connection.class));
assertThrows(ErrorCode.INVALID_VALUE_2, conn).unwrap(String.class);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, conn). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, conn).
createSQLXML(); createSQLXML();
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, conn). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, conn).
......
...@@ -63,6 +63,7 @@ public class TestResultSet extends TestBase { ...@@ -63,6 +63,7 @@ public class TestResultSet extends TestBase {
stat = conn.createStatement(); stat = conn.createStatement();
testUnwrap();
testReuseSimpleResult(); testReuseSimpleResult();
testUnsupportedOperations(); testUnsupportedOperations();
testAmbiguousColumnNames(); testAmbiguousColumnNames();
...@@ -101,6 +102,19 @@ public class TestResultSet extends TestBase { ...@@ -101,6 +102,19 @@ public class TestResultSet extends TestBase {
deleteDb("resultSet"); deleteDb("resultSet");
} }
private void testUnwrap() throws SQLException {
ResultSet rs = stat.executeQuery("select 1");
assertTrue(rs.isWrapperFor(Object.class));
assertTrue(rs.isWrapperFor(ResultSet.class));
assertTrue(rs.isWrapperFor(rs.getClass()));
assertFalse(rs.isWrapperFor(Integer.class));
assertTrue(rs == rs.unwrap(Object.class));
assertTrue(rs == rs.unwrap(ResultSet.class));
assertTrue(rs == rs.unwrap(rs.getClass()));
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
unwrap(Object.class);
}
private void testReuseSimpleResult() throws SQLException { private void testReuseSimpleResult() throws SQLException {
ResultSet rs = stat.executeQuery("select table(x array=((1)))"); ResultSet rs = stat.executeQuery("select table(x array=((1)))");
...@@ -166,10 +180,6 @@ public class TestResultSet extends TestBase { ...@@ -166,10 +180,6 @@ public class TestResultSet extends TestBase {
getCursorName(); getCursorName();
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
setFetchDirection(ResultSet.FETCH_FORWARD); setFetchDirection(ResultSet.FETCH_FORWARD);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
unwrap(Object.class);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, rs).
isWrapperFor(Object.class);
} }
private void testAmbiguousColumnNames() throws SQLException { private void testAmbiguousColumnNames() throws SQLException {
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
package org.h2.test.jdbc; package org.h2.test.jdbc;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLClientInfoException; import java.sql.SQLClientInfoException;
import java.sql.SQLException; import java.sql.SQLException;
...@@ -41,6 +42,7 @@ public class TestStatement extends TestBase { ...@@ -41,6 +42,7 @@ public class TestStatement extends TestBase {
public void test() throws Exception { public void test() throws Exception {
deleteDb("statement"); deleteDb("statement");
conn = getConnection("statement"); conn = getConnection("statement");
testUnwrap();
testUnsupportedOperations(); testUnsupportedOperations();
testTraceError(); testTraceError();
testSavepoint(); testSavepoint();
...@@ -51,14 +53,22 @@ public class TestStatement extends TestBase { ...@@ -51,14 +53,22 @@ public class TestStatement extends TestBase {
conn.close(); conn.close();
deleteDb("statement"); deleteDb("statement");
} }
private void testUnsupportedOperations() throws Exception { private void testUnwrap() throws SQLException {
Statement stat = conn.createStatement(); Statement stat = conn.createStatement();
assertTrue(stat.isWrapperFor(Object.class));
assertTrue(stat.isWrapperFor(DatabaseMetaData.class));
assertTrue(stat.isWrapperFor(stat.getClass()));
assertTrue(stat == stat.unwrap(Object.class));
assertTrue(stat == stat.unwrap(DatabaseMetaData.class));
assertTrue(stat == stat.unwrap(stat.getClass()));
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat).
isWrapperFor(Object.class); isWrapperFor(Object.class);
assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat). assertThrows(ErrorCode.FEATURE_NOT_SUPPORTED_1, stat).
unwrap(Object.class); unwrap(Object.class);
}
private void testUnsupportedOperations() throws Exception {
conn.setTypeMap(null); conn.setTypeMap(null);
HashMap<String, Class<?>> map = New.hashMap(); HashMap<String, Class<?>> map = New.hashMap();
conn.setTypeMap(map); conn.setTypeMap(map);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论