diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java index b6c8bc6..c0e38e1 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java @@ -24,6 +24,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URL; import java.sql.Array; import java.sql.Blob; @@ -41,10 +42,9 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Map; +import java.util.Objects; + -/** - * @author Christoph Deppisch - */ public class JdbcResultSet implements java.sql.ResultSet { /** Remote ResultSet */ @@ -57,7 +57,7 @@ public class JdbcResultSet implements java.sql.ResultSet { /** * Constructor using remote result set. */ - public JdbcResultSet(DataSet dataSet, JdbcStatement statement) throws SQLException { + public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) throws SQLException { this.dataSet = dataSet; this.statement = statement; } @@ -73,332 +73,148 @@ public void close() throws SQLException { dataSet.close(); } - private T convert(Object value, Class type) throws SQLException { - if (value == null) { - return null; - } - - if (type.isInstance(value)) { - return type.cast(value); - } - - if (String.class.isAssignableFrom(type)) { - return (T) value.toString(); - } - - if (Byte.class.isAssignableFrom(type)) { - return (T) Byte.valueOf(value.toString()); - } - - if (Boolean.class.isAssignableFrom(type)) { - return (T) Boolean.valueOf(value.toString()); - } - - if (Short.class.isAssignableFrom(type)) { - return (T) Short.valueOf(value.toString()); - } - - if (Integer.class.isAssignableFrom(type)) { - return (T) Integer.valueOf(value.toString()); - } - - if (Long.class.isAssignableFrom(type)) { - return (T) Long.valueOf(value.toString()); - } - - if (Double.class.isAssignableFrom(type)) { - return (T) Double.valueOf(value.toString()); - } - - if (Float.class.isAssignableFrom(type)) { - return (T) Float.valueOf(value.toString()); - } - - if (Timestamp.class.isAssignableFrom(type)) { - return (T) Timestamp.valueOf(value.toString()); - } - - if (Time.class.isAssignableFrom(type)) { - return (T) Time.valueOf(value.toString()); - } - - if (Date.class.isAssignableFrom(type)) { - return (T) Date.valueOf(value.toString()); - } - - throw new SQLException(String.format("Missing conversion strategy for type %s", type)); - } - - public String getString(int columnIndex) throws SQLException { - return convert(row.getValue(columnIndex-1), String.class); + public String getString(final int columnIndex) throws SQLException { + return (String) row.getValue(columnIndex-1, String.class); } - public String getString(String columnName) throws SQLException { - return convert(row.getValue(columnName), String.class); + public String getString(final String columnName) throws SQLException { + return (String) row.getValue(columnName, String.class); } - public float getFloat(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Float.class); - } + public float getFloat(final int columnIndex) throws SQLException { + return (float) row.getValue(columnIndex-1, float.class); } - public float getFloat(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Float.class); - } + public float getFloat(final String columnName) throws SQLException { + return (float) row.getValue(columnName, float.class); } - public int getInt(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Integer.class); - } + public int getInt(final int columnIndex) throws SQLException { + return (int) row.getValue(columnIndex-1, int.class); } - public int getInt(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Integer.class); - } + public int getInt(final String columnName) throws SQLException { + return (int) row.getValue(columnName, int.class); } - public boolean getBoolean(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1) == null) { - return false; - } else { - return convert(row.getValue(columnIndex-1), Boolean.class); - } + public boolean getBoolean(final int columnIndex) throws SQLException { + return (boolean) row.getValue(columnIndex-1, boolean.class); } - public byte getByte(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Byte.class); - } + public byte getByte(final int columnIndex) throws SQLException { + return (byte) row.getValue(columnIndex-1, byte.class); } - public short getShort(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Short.class); - } + public short getShort(final int columnIndex) throws SQLException { + return (short) row.getValue(columnIndex-1, short.class); } - public long getLong(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Long.class); - } + public long getLong(final int columnIndex) throws SQLException { + return (long) row.getValue(columnIndex-1, long.class); } - public double getDouble(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Double.class); - } + public double getDouble(final int columnIndex) throws SQLException { + return (double) row.getValue(columnIndex-1, double.class); } - public BigDecimal getBigDecimal(int columnIndex,int scale) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getBigDecimal'"); + public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { + return getBigDecimal(columnIndex).setScale(scale, RoundingMode.HALF_UP); } - public byte[] getBytes(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), String.class).getBytes(); + public byte[] getBytes(final int columnIndex) throws SQLException { + return (byte[]) row.getValue(columnIndex-1, byte[].class); } - public Date getDate(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Date.class); + public Date getDate(final int columnIndex) throws SQLException { + return (Date) row.getValue(columnIndex-1, Date.class); } - public Time getTime(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Time.class); + public Time getTime(final int columnIndex) throws SQLException { + return (Time) row.getValue(columnIndex-1, Time.class); } - public Timestamp getTimestamp(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Timestamp.class); + public Timestamp getTimestamp(final int columnIndex) throws SQLException { + return (Timestamp) row.getValue(columnIndex-1, Timestamp.class); } - public InputStream getAsciiStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getAsciiStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getString(columnIndex).getBytes()); } - public InputStream getUnicodeStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getUnicodeStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getString(columnIndex).getBytes()); } - public InputStream getBinaryStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getBinaryStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getBytes(columnIndex)); } - public Object getObject(int columnIndex) throws SQLException { + public Object getObject(final int columnIndex) throws SQLException { return row.getValue(columnIndex-1); } - public BigDecimal getBigDecimal(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - Long bigdObj = convert(row.getValue(columnIndex-1), Long.class); - return BigDecimal.valueOf(bigdObj); + public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { + return (BigDecimal) row.getValue(columnIndex-1, BigDecimal.class); } - public boolean getBoolean(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return false; - } else { - return convert(row.getValue(columnName), Boolean.class); - } + public boolean getBoolean(final String columnName) throws SQLException { + return (boolean) row.getValue(columnName, boolean.class); } - public byte getByte(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Byte.class); - } + public byte getByte(final String columnName) throws SQLException { + return (byte) row.getValue(columnName, byte.class); } - public short getShort(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Short.class); - } + public short getShort(final String columnName) throws SQLException { + return (short) row.getValue(columnName, short.class); } - public long getLong(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Long.class); - } + public long getLong(final String columnName) throws SQLException { + return (long) row.getValue(columnName, long.class); } - public double getDouble(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Double.class); - } + public double getDouble(final String columnName) throws SQLException { + return (double) row.getValue(columnName, double.class); } - public BigDecimal getBigDecimal(String columnName,int scale) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getBigDecimal'"); + public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException { + return getBigDecimal(columnName).setScale(scale, RoundingMode.HALF_UP); } - public byte[] getBytes(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } else { - return convert(row.getValue(columnName), String.class).getBytes(); - } + public byte[] getBytes(final String columnName) throws SQLException { + return (byte[]) row.getValue(columnName, byte[].class); } - public Date getDate(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Date.class); + public Date getDate(final String columnName) throws SQLException { + return (Date) row.getValue(columnName, Date.class); } - public Time getTime(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Time.class); + public Time getTime(final String columnName) throws SQLException { + return (Time) row.getValue(columnName, Time.class); } - public Timestamp getTimestamp(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Timestamp.class); + public Timestamp getTimestamp(final String columnName) throws SQLException { + return (Timestamp) row.getValue(columnName, Timestamp.class); } - public Object getObject(String columnName) throws SQLException { + public Object getObject(final String columnName) throws SQLException { return row.getValue(columnName); } - public BigDecimal getBigDecimal(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - Long bigdObj = convert(row.getValue(columnName), Long.class); - return BigDecimal.valueOf(bigdObj); + public BigDecimal getBigDecimal(final String columnName) throws SQLException { + return (BigDecimal) row.getValue(columnName, BigDecimal.class); } - public InputStream getAsciiStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getAsciiStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getString(columnName).getBytes()); } - public InputStream getUnicodeStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getUnicodeStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getString(columnName).getBytes()); } - public InputStream getBinaryStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getBinaryStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getBytes(columnName)); } public SQLWarning getWarnings() throws SQLException { @@ -416,26 +232,16 @@ public ResultSetMetaData getMetaData() throws SQLException { return new JdbcResultSetMetaData(dataSet); } - public int findColumn(String columnName) throws SQLException { + public int findColumn(final String columnName) throws SQLException { return row.getColumns().indexOf(columnName); } - public Reader getCharacterStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new InputStreamReader(new ByteArrayInputStream(byteArray)); + public Reader getCharacterStream(final int columnIndex) throws SQLException { + return new InputStreamReader(new ByteArrayInputStream(getString(columnIndex).getBytes())); } - public Reader getCharacterStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new InputStreamReader(new ByteArrayInputStream(byteArray)); + public Reader getCharacterStream(final String columnName) throws SQLException { + return new InputStreamReader(new ByteArrayInputStream(getString(columnName).getBytes())); } public boolean isBeforeFirst() throws SQLException { @@ -472,11 +278,11 @@ public int getRow() throws SQLException { return dataSet.getCursor() + 1; } - public boolean absolute(int row) throws SQLException { + public boolean absolute(final int row) throws SQLException { throw new SQLException("Not supported JDBC result set function 'absolute'"); } - public boolean relative(int rows) throws SQLException { + public boolean relative(final int rows) throws SQLException { throw new SQLException("Not supported JDBC result set function 'relative'"); } @@ -484,14 +290,14 @@ public boolean previous() throws SQLException { throw new SQLException("Not supported JDBC result set function 'previous'"); } - public void setFetchDirection(int direction) throws SQLException { + public void setFetchDirection(final int direction) throws SQLException { } public int getFetchDirection() throws SQLException { throw new SQLException("Not supported JDBC result set function 'getFetchDirection'"); } - public void setFetchSize(int rows) throws SQLException { + public void setFetchSize(final int rows) throws SQLException { } public int getFetchSize() throws SQLException { @@ -507,123 +313,123 @@ public int getConcurrency() throws SQLException { } public boolean rowUpdated() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } public boolean rowInserted() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } public boolean rowDeleted() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } - public void updateNull(int columnIndex) throws SQLException { + public void updateNull(final int columnIndex) throws SQLException { } - public void updateBoolean(int columnIndex,boolean x) throws SQLException { + public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { } - public void updateByte(int columnIndex, byte x) throws SQLException { + public void updateByte(final int columnIndex, final byte x) throws SQLException { } - public void updateShort(int columnIndex,short x) throws SQLException { + public void updateShort(final int columnIndex, final short x) throws SQLException { } - public void updateInt(int columnIndex,int x) throws SQLException { + public void updateInt(final int columnIndex, final int x) throws SQLException { } - public void updateLong(int columnIndex,long x) throws SQLException { + public void updateLong(final int columnIndex, final long x) throws SQLException { } - public void updateFloat(int columnIndex,float x) throws SQLException { + public void updateFloat(final int columnIndex, final float x) throws SQLException { } - public void updateDouble(int columnIndex,double x) throws SQLException { + public void updateDouble(final int columnIndex, final double x) throws SQLException { } - public void updateBigDecimal(int columnIndex,BigDecimal x) throws SQLException { + public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { } - public void updateString(int columnIndex,String x) throws SQLException { + public void updateString(final int columnIndex, final String x) throws SQLException { } - public void updateBytes(int columnIndex,byte[] x) throws SQLException { + public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { } - public void updateDate(int columnIndex,Date x) throws SQLException { + public void updateDate(final int columnIndex, final Date x) throws SQLException { } - public void updateTime(int columnIndex,Time x) throws SQLException { + public void updateTime(final int columnIndex, final Time x) throws SQLException { } - public void updateTimestamp(int columnIndex,Timestamp x) throws SQLException { + public void updateTimestamp(final int columnIndex, final Timestamp x) throws SQLException { } - public void updateBinaryStream(int columnIndex,InputStream x,int length) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x, final int length) throws SQLException { } - public void updateCharacterStream(int columnIndex,Reader x,int length) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x, final int length) throws SQLException { } - public void updateObject(int columnIndex,Object x,int scale) throws SQLException { + public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { } - public void updateObject(int columnIndex,Object x) throws SQLException { + public void updateObject(final int columnIndex, final Object x) throws SQLException { } - public void updateNull(String columnName) throws SQLException { + public void updateNull(final String columnName) throws SQLException { } - public void updateByte(String columnName, byte x) throws SQLException { + public void updateByte(final String columnName, final byte x) throws SQLException { } - public void updateShort(String columnName, short x) throws SQLException { + public void updateShort(final String columnName, final short x) throws SQLException { } - public void updateInt(String columnName,int x) throws SQLException { + public void updateInt(final String columnName, final int x) throws SQLException { } - public void updateLong(String columnName,long x) throws SQLException { + public void updateLong(final String columnName, final long x) throws SQLException { } - public void updateFloat(String columnName, float x) throws SQLException { + public void updateFloat(final String columnName, final float x) throws SQLException { } - public void updateDouble(String columnName,double x) throws SQLException { + public void updateDouble(final String columnName, final double x) throws SQLException { } - public void updateBigDecimal(String columnName,BigDecimal x) throws SQLException { + public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { } - public void updateString(String columnName,String x) throws SQLException { + public void updateString(final String columnName, final String x) throws SQLException { } - public void updateBytes(String columnName,byte[] x) throws SQLException { + public void updateBytes(final String columnName, final byte[] x) throws SQLException { } - public void updateDate(String columnName,Date x) throws SQLException { + public void updateDate(final String columnName, final Date x) throws SQLException { } - public void updateTime(String columnName, Time x) throws SQLException { + public void updateTime(final String columnName, final Time x) throws SQLException { } - public void updateTimestamp(String columnName,Timestamp x) throws SQLException { + public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException { } - public void updateAsciiStream(String columnName,InputStream x,int length) throws SQLException { + public void updateAsciiStream(final String columnName, final InputStream x, final int length) throws SQLException { } - public void updateBinaryStream(String columnName,InputStream x,int length) throws SQLException { + public void updateBinaryStream(final String columnName, final InputStream x, final int length) throws SQLException { } - public void updateCharacterStream(String columnName,Reader reader,int length) throws SQLException { + public void updateCharacterStream(final String columnName, final Reader reader, final int length) throws SQLException { } - public void updateObject(String columnName,Object x,int scale) throws SQLException { + public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { } - public void updateObject(String columnName,Object x) throws SQLException { + public void updateObject(final String columnName, final Object x) throws SQLException { } public void insertRow() throws SQLException { @@ -655,88 +461,88 @@ public Statement getStatement() throws SQLException { } - public Date getDate(int columnIndex,Calendar cal) throws SQLException { + public Date getDate(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getDate'"); } - public Date getDate(String columnName,Calendar cal) throws SQLException { + public Date getDate(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getDate'"); } - public Time getTime(int columnIndex,Calendar cal) throws SQLException { + public Time getTime(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTime'"); } - public Time getTime(String columnName,Calendar cal) throws SQLException { + public Time getTime(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTime'"); } - public Timestamp getTimestamp(int columnIndex,Calendar cal) throws SQLException { + public Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTimestamp'"); } - public Timestamp getTimestamp(String columnName,Calendar cal) throws SQLException { + public Timestamp getTimestamp(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTimestamp'"); } @Override - public URL getURL(int columnIndex) throws SQLException { + public URL getURL(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getURL'"); } @Override - public URL getURL(String columnLabel) throws SQLException { + public URL getURL(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getURL'"); } @Override - public void updateRef(int columnIndex, Ref x) throws SQLException { + public void updateRef(final int columnIndex, final Ref x) throws SQLException { } @Override - public void updateRef(String columnLabel, Ref x) throws SQLException { + public void updateRef(final String columnLabel, final Ref x) throws SQLException { } @Override - public void updateBlob(int columnIndex, Blob x) throws SQLException { + public void updateBlob(final int columnIndex, final Blob x) throws SQLException { } @Override - public void updateBlob(String columnLabel, Blob x) throws SQLException { + public void updateBlob(final String columnLabel, final Blob x) throws SQLException { } @Override - public void updateClob(int columnIndex, Clob x) throws SQLException { + public void updateClob(final int columnIndex, final Clob x) throws SQLException { } @Override - public void updateClob(String columnLabel, Clob x) throws SQLException { + public void updateClob(final String columnLabel, final Clob x) throws SQLException { } @Override - public void updateArray(int columnIndex, Array x) throws SQLException { + public void updateArray(final int columnIndex, final Array x) throws SQLException { } @Override - public void updateArray(String columnLabel, Array x) throws SQLException { + public void updateArray(final String columnLabel, final Array x) throws SQLException { } @Override - public RowId getRowId(int columnIndex) throws SQLException { + public RowId getRowId(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRowId'"); } @Override - public RowId getRowId(String columnLabel) throws SQLException { + public RowId getRowId(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRowId'"); } @Override - public void updateRowId(int columnIndex, RowId x) throws SQLException { + public void updateRowId(final int columnIndex, final RowId x) throws SQLException { } @Override - public void updateRowId(String columnLabel, RowId x) throws SQLException { + public void updateRowId(final String columnLabel, final RowId x) throws SQLException { } @Override @@ -750,188 +556,188 @@ public boolean isClosed() throws SQLException { } @Override - public void updateNString(int columnIndex, String nString) throws SQLException { + public void updateNString(final int columnIndex, final String nString) throws SQLException { } @Override - public void updateNString(String columnLabel, String nString) throws SQLException { + public void updateNString(final String columnLabel, final String nString) throws SQLException { } @Override - public void updateNClob(int columnIndex, NClob nClob) throws SQLException { + public void updateNClob(final int columnIndex, final NClob nClob) throws SQLException { } @Override - public void updateNClob(String columnLabel, NClob nClob) throws SQLException { + public void updateNClob(final String columnLabel, final NClob nClob) throws SQLException { } @Override - public NClob getNClob(int columnIndex) throws SQLException { + public NClob getNClob(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNClob'"); } @Override - public NClob getNClob(String columnLabel) throws SQLException { + public NClob getNClob(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNClob'"); } @Override - public SQLXML getSQLXML(int columnIndex) throws SQLException { + public SQLXML getSQLXML(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getSQLXML'"); } @Override - public SQLXML getSQLXML(String columnLabel) throws SQLException { + public SQLXML getSQLXML(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getSQLXML'"); } @Override - public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { + public void updateSQLXML(final int columnIndex, final SQLXML xmlObject) throws SQLException { } @Override - public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { + public void updateSQLXML(final String columnLabel, final SQLXML xmlObject) throws SQLException { } @Override - public String getNString(int columnIndex) throws SQLException { + public String getNString(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNString'"); } @Override - public String getNString(String columnLabel) throws SQLException { + public String getNString(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNString'"); } @Override - public Reader getNCharacterStream(int columnIndex) throws SQLException { + public Reader getNCharacterStream(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNCharacterStream'"); } @Override - public Reader getNCharacterStream(String columnLabel) throws SQLException { + public Reader getNCharacterStream(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNCharacterStream'"); } @Override - public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + public void updateNCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { } @Override - public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x, final long length) throws SQLException { } @Override - public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x, final long length) throws SQLException { } @Override - public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { } @Override - public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateAsciiStream(final String columnLabel, final InputStream x, final long length) throws SQLException { } @Override - public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateBinaryStream(final String columnLabel, final InputStream x, final long length) throws SQLException { } @Override - public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { + public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { } @Override - public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { + public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { } @Override - public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { + public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { } @Override - public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { + public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { + public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { } @Override - public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { + public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateNCharacterStream(final int columnIndex, final Reader x) throws SQLException { } @Override - public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x) throws SQLException { } @Override - public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x) throws SQLException { } @Override - public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x) throws SQLException { } @Override - public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { + public void updateAsciiStream(final String columnLabel, final InputStream x) throws SQLException { } @Override - public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { + public void updateBinaryStream(final String columnLabel, final InputStream x) throws SQLException { } @Override - public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { + public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { } @Override - public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { + public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { } @Override - public void updateClob(int columnIndex, Reader reader) throws SQLException { + public void updateClob(final int columnIndex, final Reader reader) throws SQLException { } @Override - public void updateClob(String columnLabel, Reader reader) throws SQLException { + public void updateClob(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateNClob(int columnIndex, Reader reader) throws SQLException { + public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { } @Override - public void updateNClob(String columnLabel, Reader reader) throws SQLException { + public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { } @Override - public T getObject(int columnIndex, Class type) throws SQLException { + public T getObject(final int columnIndex, final Class type) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } @Override - public T getObject(String columnLabel, Class type) throws SQLException { + public T getObject(final String columnLabel, final Class type) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } @@ -939,59 +745,83 @@ public boolean wasNull()throws SQLException { return row.getLastValue() == null; } - public void updateBoolean(String columnName, boolean x) throws SQLException { + public void updateBoolean(final String columnName, final boolean x) throws SQLException { } - public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x, final int length) throws SQLException { } - public Object getObject(int i, Map map) throws SQLException { + public Object getObject(final int i, final Map map) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } - public Ref getRef(int i) throws SQLException { + public Ref getRef(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRef'"); } - public Blob getBlob(int i) throws SQLException { + public Blob getBlob(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getBlob'"); } - public Clob getClob(int i) throws SQLException { + public Clob getClob(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getClob'"); } - public Array getArray(int i) throws SQLException { + public Array getArray(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getArray'"); } - public Object getObject(String colName, Map map) throws SQLException { + public Object getObject(final String colName, final Map map) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } - public Ref getRef(String colName) throws SQLException { + public Ref getRef(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRef'"); } - public Blob getBlob(String colName) throws SQLException { + public Blob getBlob(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getBlob'"); } - public Clob getClob(String colName) throws SQLException { + public Clob getClob(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getClob'"); } - public Array getArray(String colName) throws SQLException { + public Array getArray(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getArray'"); } @Override - public T unwrap(Class iface) throws SQLException { + public T unwrap(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'unwrap'"); } @Override - public boolean isWrapperFor(Class iface) throws SQLException { + public boolean isWrapperFor(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'isWrapperFor'"); } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof JdbcResultSet)) return false; + final JdbcResultSet that = (JdbcResultSet) o; + return Objects.equals(dataSet, that.dataSet) && + Objects.equals(statement, that.statement) && + Objects.equals(row, that.row); + } + + @Override + public int hashCode() { + return Objects.hash(dataSet, statement, row); + } + + @Override + public String toString() { + return "JdbcResultSet{" + + "dataSet=" + dataSet + + ", statement=" + statement + + ", row=" + row + + '}'; + } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java new file mode 100644 index 0000000..36d6f80 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java @@ -0,0 +1,668 @@ +package com.consol.citrus.db.driver; + +import com.consol.citrus.db.driver.data.Row; +import com.consol.citrus.db.driver.dataset.DataSet; +import com.consol.citrus.db.driver.dataset.DataSetBuilder; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.io.IOUtils; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.sql.Date; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +@PrepareForTest(ConvertUtils.class) +public class JdbcResultSetTest { + + private JdbcResultSet resultSet; + private Row rowSpy; + + private final int TEST_VALUE_INDEX_JDBC = 2; + //Because in JDBC, arrays start at 1 + private final int TEST_VALUE_INDEX_INTERNAL = TEST_VALUE_INDEX_JDBC-1; + private final String TEST_VALUE_NAME = "col2"; + + @Test + public void testGetStringByIndex() throws SQLException { + //GIVEN + final String expectedString = "bar"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + resultSet.next(); + + //WHEN + final String string = resultSet.getString(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(string, expectedString); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetStringByName() throws SQLException { + + //GIVEN + final String expectedString = "bar"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + resultSet.next(); + + //WHEN + final String string = resultSet.getString(TEST_VALUE_NAME); + + //THEN + assertEquals(string, expectedString); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetFloatByIndex() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + resultSet.next(); + //WHEN + final float aFloat = resultSet.getFloat(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aFloat, expectedFloat); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, float.class); + } + + @Test + void testGetFloatByName() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + resultSet.next(); + + //WHEN + final float aFloat = resultSet.getFloat(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedFloat); + verify(rowSpy).getValue(TEST_VALUE_NAME, float.class); + } + + @Test + void testGetIntByIndex() throws SQLException { + + //GIVEN + final int expectedInt = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + resultSet.next(); + + //WHEN + final int anInt = resultSet.getInt(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(anInt, expectedInt); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, int.class); + } + + @Test + void testGetIntByName() throws SQLException { + + //GIVEN + final int expectedInt = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + resultSet.next(); + + //WHEN + final int anInt = resultSet.getInt(TEST_VALUE_NAME); + + //THEN + assertEquals(anInt, expectedInt); + verify(rowSpy).getValue(TEST_VALUE_NAME, int.class); + } + + @Test + void testGetBooleanByIndex() throws SQLException { + + //GIVEN + final boolean expectedBoolean = true; + resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + resultSet.next(); + + //WHEN + final boolean aBoolean = resultSet.getBoolean(TEST_VALUE_INDEX_JDBC); + + //THEN + assertTrue(aBoolean); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, boolean.class); + } + + @Test + void testGetByteByIndex() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + resultSet.next(); + + //WHEN + final byte aByte = resultSet.getByte(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aByte, expectedByte); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte.class); + } + + @Test + void testGetShortByIndex() throws SQLException { + + //GIVEN + final short expectedShort = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + resultSet.next(); + + //WHEN + final short aShort = resultSet.getShort(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aShort, expectedShort); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, short.class); + } + + @Test + void testGetLongByIndex() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + resultSet.next(); + + //WHEN + final long aLong = resultSet.getLong(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aLong, expectedLong); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, long.class); + } + + @Test + void testGetDoubleByIndex() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + resultSet.next(); + + //WHEN + final double aDouble = resultSet.getDouble(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aDouble, expectedDouble); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, double.class); + } + + @Test + void testGetBigDecimalByIndexWithScale() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + resultSet.next(); + + //WHEN + final BigDecimal aBigDecimal = resultSet.getBigDecimal(TEST_VALUE_INDEX_JDBC, 2); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, BigDecimal.class); + } + + @Test + void testGetBytesByIndex() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final byte[] aByte = resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aByte, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); + } + + @Test + void testGetDateByIndex() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + resultSet.next(); + + //WHEN + final Date aDate = resultSet.getDate(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aDate, expectedDate); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Date.class); + } + + @Test + void testGetTimeByIndex() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + resultSet.next(); + + //WHEN + final Time aTime = resultSet.getTime(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aTime, expectedTime); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Time.class); + } + + @Test + void testGetTimestampByIndex() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + resultSet.next(); + + //WHEN + final Timestamp aTimestamp = resultSet.getTimestamp(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Timestamp.class); + } + + @Test + void testGetAsciiStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getAsciiStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(inputStream, "ISO-8859-1"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetUnicodeStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getUnicodeStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(inputStream, "UTF-8"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetBinaryStreamByIndex() throws SQLException, IOException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getBinaryStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final byte[] text = IOUtils.toByteArray(inputStream); + assertEquals(text, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); + } + + @Test + void testGetObjectByIndex() throws SQLException { + + //GIVEN + final Object expectedObject = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedObject), null); + resultSet.next(); + + //WHEN + final Object anObject = resultSet.getObject(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(anObject, expectedObject); + } + + @Test + void testGetBooleanByName() throws SQLException { + + //GIVEN + final boolean expectedBoolean = true; + resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + resultSet.next(); + + //WHEN + final boolean aBoolean = resultSet.getBoolean(TEST_VALUE_NAME); + + //THEN + assertTrue(aBoolean); + verify(rowSpy).getValue(TEST_VALUE_NAME, boolean.class); + } + + @Test + void testGetByteByName() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + resultSet.next(); + + //WHEN + final byte aByte = resultSet.getByte(TEST_VALUE_NAME); + + //THEN + assertEquals(aByte, expectedByte); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte.class); + } + + @Test + void testGetShortByName() throws SQLException { + + //GIVEN + final short expectedShort = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + resultSet.next(); + + //WHEN + final short aShort = resultSet.getShort(TEST_VALUE_NAME); + + //THEN + assertEquals(aShort, expectedShort); + verify(rowSpy).getValue(TEST_VALUE_NAME, short.class); + } + + @Test + void testGetLongByName() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + resultSet.next(); + + //WHEN + final long aLong = resultSet.getLong(TEST_VALUE_NAME); + + //THEN + assertEquals(aLong, expectedLong); + verify(rowSpy).getValue(TEST_VALUE_NAME, long.class); + } + + @Test + void testGetDoubleByName() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + resultSet.next(); + + //WHEN + final double aFloat = resultSet.getDouble(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedDouble); + verify(rowSpy).getValue(TEST_VALUE_NAME, double.class); + } + + @Test + void testGetBigDecimalByNameWithScale() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + resultSet.next(); + + //WHEN + final BigDecimal aBigDecimal = resultSet.getBigDecimal(TEST_VALUE_NAME, 2); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + verify(rowSpy).getValue(TEST_VALUE_NAME, BigDecimal.class); + } + + @Test + void testGetBytesByName() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "Foobar".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final byte[] bytes = resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + assertEquals(bytes, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); + } + + @Test + void testGetDateByName() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + resultSet.next(); + + //WHEN + final Date aDate = resultSet.getDate(TEST_VALUE_NAME); + + //THEN + assertEquals(aDate, expectedDate); + verify(rowSpy).getValue(TEST_VALUE_NAME, Date.class); + } + + @Test + void testGetTimeByName() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + resultSet.next(); + + //WHEN + final Time aTime = resultSet.getTime(TEST_VALUE_NAME); + + //THEN + assertEquals(aTime, expectedTime); + verify(rowSpy).getValue(TEST_VALUE_NAME, Time.class); + } + + @Test + void testGetTimestampByName() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + resultSet.next(); + + //WHEN + final Timestamp aTimestamp = resultSet.getTimestamp(TEST_VALUE_NAME); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verify(rowSpy).getValue(TEST_VALUE_NAME, Timestamp.class); + } + + @Test + void testGetAsciiStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getAsciiStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(inputStream, "ISO-8859-1"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetUnicodeStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getUnicodeStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(inputStream, "UTF-8"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetBinaryStreamByName() throws SQLException, IOException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getBinaryStream(TEST_VALUE_NAME); + + //THEN + final byte[] text = IOUtils.toByteArray(inputStream); + assertEquals(text, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); + } + + @Test + void testGetCharacterStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final Reader reader = resultSet.getCharacterStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(reader); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetCharacterStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final Reader reader = resultSet.getCharacterStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(reader); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testRowUpdated() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowUpdated(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowInserted() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowInserted(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowDeleted() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowDeleted(); + + //THEN + assertTrue(updated); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcResultSet.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcResultSet.class); + } + + private DataSet generateTestDataSet() throws SQLException { + return generateTestDataSet(null); + } + + private DataSet generateTestDataSet(final Object testValue) throws SQLException { + final SortedMap testData = new TreeMap<>(); + testData.put("col1", "dummyValue"); + testData.put("col2", testValue); + + rowSpy = spy(new Row()); + rowSpy.setValues(testData); + return new DataSetBuilder().add(rowSpy).build(); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3c67b6f..87690fc 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,8 @@ 2.24.0 1.9.3 + + 2.6 1.4.5 @@ -275,6 +277,12 @@ ${powermock.version} test + + commons-io + commons-io + ${apache.commons.io.version} + test + @@ -316,6 +324,11 @@ powermock-api-mockito2 test + + commons-io + commons-io + test +