Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JDBC-193: Preparation for testing metadata of other methods
- Loading branch information
1 parent
8f9a191
commit 3e42bf7
Showing
2 changed files
with
165 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* Firebird Open Source J2ee connector - jdbc driver | ||
* | ||
* Distributable under LGPL license. | ||
* You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* LGPL License for more details. | ||
* | ||
* This file was created by members of the firebird development team. | ||
* All individual contributions remain the Copyright (C) of those | ||
* individuals. Contributors to this file are either listed here or | ||
* can be obtained from a CVS history command. | ||
* | ||
* All rights reserved. | ||
*/ | ||
package org.firebirdsql.jdbc; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.ResultSetMetaData; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
import static junit.framework.Assert.*; | ||
|
||
/** | ||
* Validator for columns of metadata resultsets. | ||
* | ||
* @author <a href="mailto:mrotteveel@users.sourceforge.net">Mark Rotteveel</a> | ||
* | ||
* @param <T> Enum type, implementing {@link MetaDataInfo} for metadata column information | ||
*/ | ||
public class MetaDataValidator<T extends Enum & MetaDataValidator.MetaDataInfo> { | ||
|
||
private T mdi; | ||
|
||
protected MetaDataValidator(T mdi) { | ||
this.mdi = mdi; | ||
} | ||
|
||
/** | ||
* Asserts the expected position of this column in the resultset. | ||
* | ||
* @param rs ResultSet to use for asserting the column position | ||
* @throws SQLException | ||
*/ | ||
public void assertColumnPosition(ResultSet rs) throws SQLException { | ||
assertEquals(String.format("Unexpected column position for %s", mdi), mdi.getPosition(), rs.findColumn(mdi.name())); | ||
} | ||
|
||
/** | ||
* Asserts the type of this column as reported by the ResultSetMetaData of this ResultSet. | ||
* | ||
* @param rs ResultSet | ||
* @throws SQLException | ||
*/ | ||
public void assertColumnType(ResultSet rs) throws SQLException { | ||
ResultSetMetaData md = rs.getMetaData(); | ||
assertEquals(String.format("Unexpected SQL Type for column %s", mdi), getSqlType(), md.getColumnType(mdi.getPosition())); | ||
} | ||
|
||
/** | ||
* Asserts the value of this column on the current row of the resultset. | ||
* | ||
* @param rs ResultSet | ||
* @param expectedValue Value expected | ||
* @throws SQLException | ||
*/ | ||
public void assertColumnValue(ResultSet rs, Object expectedValue) throws SQLException { | ||
if (mdi.getColumnClass().isInstance(expectedValue) || expectedValue == null) { | ||
if (mdi.getColumnClass().equals(String.class)) { | ||
assertStringColumnValue(rs, (String) expectedValue); | ||
} else if (mdi.getColumnClass().equals(Integer.class)) { | ||
assertIntegerColumnValue(rs, (Integer) expectedValue); | ||
} else if (mdi.getColumnClass().equals(Short.class)) { | ||
assertShortColumnValue(rs, (Short) expectedValue); | ||
} else { | ||
assertObjectColumnValue(rs, expectedValue); | ||
} | ||
} else { | ||
assertObjectColumnValue(rs, expectedValue); | ||
} | ||
} | ||
|
||
private void assertObjectColumnValue(ResultSet rs, Object expectedValue) throws SQLException { | ||
Object value = rs.getObject(mdi.name()); | ||
assertEquals(String.format("Unexpected value for %s", mdi), expectedValue, value); | ||
} | ||
|
||
private void assertShortColumnValue(ResultSet rs, Short expectedValue) throws SQLException { | ||
short value = rs.getShort(mdi.name()); | ||
if (expectedValue != null) { | ||
assertEquals(String.format("Unexpected value for %s", mdi), expectedValue.shortValue(), value); | ||
assertFalse(String.format("%s should not be actual NULL", mdi), rs.wasNull()); | ||
} else { | ||
assertEquals(String.format("Unexpected value for %s (expected NULL/0)", mdi), 0, value); | ||
assertTrue(String.format("%s should be actual NULL", mdi), rs.wasNull()); | ||
} | ||
} | ||
|
||
private void assertIntegerColumnValue(ResultSet rs, Integer expectedValue) throws SQLException { | ||
int value = rs.getInt(mdi.name()); | ||
if (expectedValue != null) { | ||
assertEquals(String.format("Unexpected value for %s", mdi), expectedValue.intValue(), value); | ||
assertFalse(String.format("%s should not be actual NULL", mdi), rs.wasNull()); | ||
} else { | ||
assertEquals(String.format("Unexpected value for %s (expected NULL/0)", mdi), 0, value); | ||
assertTrue(String.format("%s should be actual NULL", mdi), rs.wasNull()); | ||
} | ||
} | ||
|
||
private void assertStringColumnValue(ResultSet rs, String expectedValue) | ||
throws SQLException { | ||
String value = rs.getString(mdi.name()); | ||
assertEquals(String.format("Unexpected value for %s", mdi), expectedValue, value); | ||
} | ||
|
||
private int getSqlType() { | ||
if (mdi.getColumnClass() == String.class) { | ||
return Types.VARCHAR; | ||
} else if (mdi.getColumnClass() == Integer.class) { | ||
return Types.INTEGER; | ||
} else if (mdi.getColumnClass() == Short.class) { | ||
return Types.SMALLINT; | ||
} | ||
return Types.OTHER; | ||
} | ||
|
||
|
||
/** | ||
* Interface for the information enums for metadata columns should be able to provide for the {@link MetaDataValidator}. | ||
*/ | ||
public interface MetaDataInfo { | ||
|
||
/** | ||
* Position of this metadata column in the resultset | ||
* | ||
* @return 1-based position of the column | ||
*/ | ||
int getPosition(); | ||
|
||
/** | ||
* Java class of the expected column type (String=> Varchar, Integer=> Integer, Short=> Smallint) | ||
*/ | ||
Class<?> getColumnClass(); | ||
|
||
/** | ||
* @return MetaDataValidator for this MetaDataInfo instance | ||
*/ | ||
MetaDataValidator<?> getValidator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters