Skip to content

Commit

Permalink
JDBC-193: Preparation for testing metadata of other methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Jan 5, 2012
1 parent 8f9a191 commit 3e42bf7
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 89 deletions.
155 changes: 155 additions & 0 deletions src/test/org/firebirdsql/jdbc/MetaDataValidator.java
@@ -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();
}
}
99 changes: 10 additions & 89 deletions src/test/org/firebirdsql/jdbc/TestFBDatabaseMetaDataColumns.java
Expand Up @@ -23,7 +23,6 @@
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collections;
Expand All @@ -33,6 +32,7 @@
import java.util.Set;

import org.firebirdsql.common.FBTestBase;
import org.firebirdsql.jdbc.MetaDataValidator.MetaDataInfo;

/**
* Tests for {@link FBDatabaseMetaData} for column related metadata.
Expand Down Expand Up @@ -120,8 +120,9 @@ protected void tearDown() throws Exception {
public void testColumnMetaDataColumns() throws SQLException {
ResultSet columns = dbmd.getColumns(null, null, null, null);
for (ColumnMetaData column : getValidColumnMetaData()) {
column.assertColumnPosition(columns);
column.assertColumnType(columns);
MetaDataValidator<?> validator = column.getValidator();
validator.assertColumnPosition(columns);
validator.assertColumnType(columns);
}
}

Expand Down Expand Up @@ -826,7 +827,7 @@ private void validate(String tableName, String columnName, Map<ColumnMetaData, O
try {
assertTrue("Expected row in column metadata", columns.next());
for (Map.Entry<ColumnMetaData, Object> rule : validationRules.entrySet()) {
rule.getKey().assertColumnValue(columns, rule.getValue());
rule.getKey().getValidator().assertColumnValue(columns, rule.getValue());
}
assertFalse("Expected only one row in resultset", columns.next());
} finally {
Expand Down Expand Up @@ -916,7 +917,7 @@ private Set<ColumnMetaData> getValidColumnMetaData() throws SQLException {
/**
* Columns defined for the getColumns() metadata.
*/
private enum ColumnMetaData {
private enum ColumnMetaData implements MetaDataInfo {
TABLE_CAT(1, String.class),
TABLE_SCHEM(2, String.class),
TABLE_NAME(3, String.class),
Expand Down Expand Up @@ -955,92 +956,12 @@ public int getPosition() {
return position;
}

/**
* 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", this), getPosition(), rs.findColumn(name()));
public Class<?> getColumnClass() {
return columnClass;
}

/**
* Asserts the type of this column as reported by the ResultSetMetaData.
*
* @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", this), getSqlType(), md.getColumnType(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 (columnClass.isInstance(expectedValue) || expectedValue == null) {
if (columnClass.equals(String.class)) {
assertStringColumnValue(rs, (String) expectedValue);
} else if (columnClass.equals(Integer.class)) {
assertIntegerColumnValue(rs, (Integer) expectedValue);
} else if (columnClass.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(name());
assertEquals(String.format("Unexpected value for %s", this), expectedValue, value);
}

private void assertShortColumnValue(ResultSet rs, Short expectedValue) throws SQLException {
short value = rs.getShort(name());
if (expectedValue != null) {
assertEquals(String.format("Unexpected value for %s", this), expectedValue.shortValue(), value);
assertFalse(String.format("%s should not be actual NULL", name()), rs.wasNull());
} else {
assertEquals(String.format("Unexpected value for %s (expected NULL/0)", this), 0, value);
assertTrue(String.format("%s should be actual NULL", name()), rs.wasNull());
}
}

private void assertIntegerColumnValue(ResultSet rs, Integer expectedValue) throws SQLException {
int value = rs.getInt(name());
if (expectedValue != null) {
assertEquals(String.format("Unexpected value for %s", this), expectedValue.intValue(), value);
assertFalse(String.format("%s should not be actual NULL", name()), rs.wasNull());
} else {
assertEquals(String.format("Unexpected value for %s (expected NULL/0)", this), 0, value);
assertTrue(String.format("%s should be actual NULL", name()), rs.wasNull());
}
}

private void assertStringColumnValue(ResultSet rs, String expectedValue)
throws SQLException {
String value = rs.getString(name());
assertEquals(String.format("Unexpected value for %s", this), expectedValue, value);
}

private int getSqlType() {
if (columnClass == String.class) {
return Types.VARCHAR;
} else if (columnClass == Integer.class) {
return Types.INTEGER;
} else if (columnClass == Short.class) {
return Types.SMALLINT;
}
return Types.OTHER;
public MetaDataValidator<?> getValidator() {
return new MetaDataValidator<ColumnMetaData>(this);
}
}
}

0 comments on commit 3e42bf7

Please sign in to comment.