diff --git a/agent/pom.xml b/agent/pom.xml index d9ab58d..4cb85ce 100644 --- a/agent/pom.xml +++ b/agent/pom.xml @@ -1,13 +1,11 @@ - + 4.0.0 com.consol.citrus citrus-db - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT ../pom.xml diff --git a/demo/pom.xml b/demo/pom.xml index ffef42f..e8802b5 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -1,11 +1,10 @@ - + 4.0.0 com.consol.citrus citrus-db-demo - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT citrus-db-demo @@ -116,6 +115,7 @@ org.apache.maven.plugins maven-deploy-plugin + 2.8.2 true diff --git a/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java b/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java index 091add1..9c12967 100644 --- a/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java +++ b/demo/src/main/java/com/consol/citrus/demo/controller/CityController.java @@ -4,13 +4,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.CallableStatementCreator; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.SqlParameter; import org.springframework.stereotype.Controller; +import org.springframework.util.LinkedCaseInsensitiveMap; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; +import java.sql.CallableStatement; +import java.sql.JDBCType; +import java.sql.Types; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; /** @@ -23,16 +32,22 @@ public class CityController { /** Logger */ private static Logger log = LoggerFactory.getLogger(CityController.class); + private final JdbcTemplate jdbcTemplate; + @Autowired - private JdbcTemplate jdbcTemplate; + public CityController(final JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } @GetMapping(path="/add") public @ResponseBody - String addNewProjects (@RequestParam String names) { - List splitUpNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray(names)); + String addNewProjects (@RequestParam final String names) { + final List splitUpNames = Arrays.asList(StringUtils.commaDelimitedListToStringArray(names)); - for(Object name : splitUpNames) { - log.info(String.format("Inserting city %s", name)); + for(final Object name : splitUpNames) { + if(log.isDebugEnabled()){ + log.debug(String.format("Inserting city %s", name)); + } } if (splitUpNames.size() > 1) { @@ -46,7 +61,7 @@ String addNewProjects (@RequestParam String names) { } @GetMapping(path="/all") - public @ResponseBody Iterable getAllProjects(@RequestParam(required = false, defaultValue = "") String name) { + public @ResponseBody Iterable getAllProjects(@RequestParam(required = false, defaultValue = "") final String name) { if (StringUtils.hasText(name)) { return jdbcTemplate.query( "SELECT id, name FROM cities WHERE name = ?", new Object[]{ name }, @@ -59,4 +74,30 @@ String addNewProjects (@RequestParam String names) { ); } } + + @GetMapping(path="/findId") + public @ResponseBody Iterable findCityByName(@RequestParam(defaultValue = "") final String name) { + if (StringUtils.hasText(name)) { + final Map call = jdbcTemplate.call(createCallableStatement(name), createParameters()); + final ArrayList resultSet = (ArrayList) call.get("#result-set-1"); + return resultSet.stream().map(linkedCaseInsensitiveMap -> + new City(Long.valueOf((Integer) linkedCaseInsensitiveMap.get("id")), (String) linkedCaseInsensitiveMap.get("name")) + ).collect(Collectors.toList()); + } + + return Collections.emptyList(); + } + + private List createParameters() { + return Collections.singletonList(new SqlParameter("name", Types.VARCHAR)); + } + + private CallableStatementCreator createCallableStatement(final String name) { + return con ->{ + final CallableStatement callableStatement = con.prepareCall("CALL findCityByName(?,?)"); + callableStatement.registerOutParameter(2, JDBCType.INTEGER); + callableStatement.setString("name", name); + return callableStatement; + }; + } } diff --git a/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java b/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java index 207b647..ee12f67 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java @@ -80,4 +80,18 @@ public void testCity() { .response(HttpStatus.OK) .payload(new ClassPathResource("cities.json"))); } + + @Test + @CitrusTest + public void testGetIdByCityName() { + http(action -> action.client(httpClient) + .send() + .get("/city/findId") + .queryParam("name", "Munich")); + + http(action -> action.client(httpClient) + .receive() + .response(HttpStatus.OK) + .payload("[{ \"name\": \"Munich\", \"id\": 1 }]")); + } } \ No newline at end of file diff --git a/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java b/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java index 0c86df7..3b8ae4d 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java @@ -13,8 +13,8 @@ */ public class DemoJdbcServer { - public static void main(String[] args) throws IOException, SQLException { - JdbcServer dbServer = new JdbcServer(args); + public static void main(final String[] args) throws IOException, SQLException { + final JdbcServer dbServer = new JdbcServer(args); dbServer.when() .statement() @@ -41,6 +41,12 @@ public static void main(String[] args) throws IOException, SQLException { .executeQuery(Pattern.compile("SELECT id, name FROM cities.*")) .thenReturn(new JsonDataSetProducer(new ClassPathResource("cities.json").getFile()).produce()); + dbServer.when() + .statement() + .execute(Pattern.compile("CALL findCityByName\\(\\?,\\?\\) - \\(\\?,Munich\\)")) + .thenReturn(new JsonDataSetProducer("[{ \"name\": \"Munich\", \"id\": 1 }]").produce()); + + dbServer.start(); } } diff --git a/docs/pom.xml b/docs/pom.xml index 444ded2..d87708b 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -1,12 +1,11 @@ - + 4.0.0 com.consol.citrus citrus-db - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT ../pom.xml @@ -38,10 +37,10 @@ index.adoc font - + ${project.version} ${project.basedir}/plugin - + - true @@ -103,7 +102,7 @@ pdf rouge - + diff --git a/driver/pom.xml b/driver/pom.xml index 5340b61..4069797 100644 --- a/driver/pom.xml +++ b/driver/pom.xml @@ -1,13 +1,11 @@ - + 4.0.0 com.consol.citrus citrus-db - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT ../pom.xml @@ -35,6 +33,10 @@ + + commons-beanutils + commons-beanutils + com.fasterxml.jackson.core @@ -83,6 +85,10 @@ org.slf4j org.shaded.slf4j + + commons-beanutils + org.shaded.commons-beanutils + diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java index e4c7ecb..1cd77cf 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcCallableStatement.java @@ -21,6 +21,7 @@ import java.io.InputStream; import java.io.Reader; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URL; import java.sql.Array; import java.sql.Blob; @@ -31,6 +32,7 @@ import java.sql.Ref; import java.sql.RowId; import java.sql.SQLException; +import java.sql.SQLType; import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; @@ -39,98 +41,116 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement { - public JdbcCallableStatement(final HttpClient httpClient, final String callableStatement, final String serverUrl, JdbcConnection connection) { + public JdbcCallableStatement(final HttpClient httpClient, + final String callableStatement, + final String serverUrl, + final JdbcConnection connection) { super(httpClient, callableStatement, serverUrl, connection); } @Override public void registerOutParameter(final int parameterIndex, final int sqlType) throws SQLException { - + setOutParameter(parameterIndex); } @Override public void registerOutParameter(final int parameterIndex, final int sqlType, final int scale) throws SQLException { - + registerOutParameter(parameterIndex, sqlType); } @Override public boolean wasNull() throws SQLException { - return false; + prepareResultSet(); + return resultSet.wasNull(); } @Override public String getString(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getString(parameterIndex); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return false; + prepareResultSet(); + return resultSet.getBoolean(parameterIndex); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getByte(parameterIndex); } @Override public short getShort(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getShort(parameterIndex); } @Override public int getInt(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getInt(parameterIndex); } @Override public long getLong(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getLong(parameterIndex); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getFloat(parameterIndex); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getDouble(parameterIndex); } @Override public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throws SQLException { - return null; + return getBigDecimal(parameterIndex).setScale(scale, RoundingMode.HALF_UP); } @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - return new byte[0]; + prepareResultSet(); + return resultSet.getBytes(parameterIndex); } @Override public Date getDate(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getDate(parameterIndex); } @Override public Time getTime(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getTime(parameterIndex); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getTimestamp(parameterIndex); } @Override public Object getObject(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getObject(parameterIndex); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getBigDecimal(parameterIndex); } @Override @@ -175,22 +195,22 @@ public Timestamp getTimestamp(final int parameterIndex, final Calendar cal) thro @Override public void registerOutParameter(final int parameterIndex, final int sqlType, final String typeName) throws SQLException { - + registerOutParameter(parameterIndex, sqlType); } @Override public void registerOutParameter(final String parameterName, final int sqlType) throws SQLException { - + setOutParameter(parameterName); } @Override public void registerOutParameter(final String parameterName, final int sqlType, final int scale) throws SQLException { - + registerOutParameter(parameterName, sqlType); } @Override public void registerOutParameter(final String parameterName, final int sqlType, final String typeName) throws SQLException { - + registerOutParameter(parameterName, sqlType); } @Override @@ -200,77 +220,77 @@ public URL getURL(final int parameterIndex) throws SQLException { @Override public void setURL(final String parameterName, final URL val) throws SQLException { - + setParameter(parameterName, val); } @Override public void setNull(final String parameterName, final int sqlType) throws SQLException { - + setParameter(parameterName, "null"); } @Override public void setBoolean(final String parameterName, final boolean x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setByte(final String parameterName, final byte x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setShort(final String parameterName, final short x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setInt(final String parameterName, final int x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setLong(final String parameterName, final long x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setFloat(final String parameterName, final float x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setDouble(final String parameterName, final double x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setBigDecimal(final String parameterName, final BigDecimal x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setString(final String parameterName, final String x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setBytes(final String parameterName, final byte[] x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setDate(final String parameterName, final Date x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setTime(final String parameterName, final Time x) throws SQLException { - + setParameter(parameterName, x); } @Override public void setTimestamp(final String parameterName, final Timestamp x) throws SQLException { - + setParameter(parameterName, x); } @Override @@ -325,72 +345,86 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getString(parameterName); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return false; + prepareResultSet(); + return resultSet.getBoolean(parameterName); } @Override public byte getByte(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getByte(parameterName); } @Override public short getShort(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getShort(parameterName); } @Override public int getInt(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getInt(parameterName); } @Override public long getLong(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getLong(parameterName); } @Override public float getFloat(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getFloat(parameterName); } @Override public double getDouble(final String parameterName) throws SQLException { - return 0; + prepareResultSet(); + return resultSet.getDouble(parameterName); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return new byte[0]; + prepareResultSet(); + return resultSet.getBytes(parameterName); } @Override public Date getDate(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getDate(parameterName); } @Override public Time getTime(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getTime(parameterName); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getTimestamp(parameterName); } @Override public Object getObject(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getObject(parameterName); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return null; + prepareResultSet(); + return resultSet.getBigDecimal(parameterName); } @Override @@ -607,4 +641,48 @@ public T getObject(final int parameterIndex, final Class type) throws SQL public T getObject(final String parameterName, final Class type) throws SQLException { return null; } + + @Override + public void registerOutParameter(final int parameterIndex, final SQLType sqlType){ + setOutParameter(parameterIndex); + } + + @Override + public void registerOutParameter(final int parameterIndex, final SQLType sqlType, final int scale){ + registerOutParameter(parameterIndex, sqlType); + } + + @Override + public void registerOutParameter(final int parameterIndex, final SQLType sqlType, final String typeName){ + registerOutParameter(parameterIndex, sqlType); + } + + @Override + public void registerOutParameter(final String parameterName, final SQLType sqlType){ + setOutParameter(parameterName); + } + + @Override + public void registerOutParameter(final String parameterName, final SQLType sqlType, final int scale){ + registerOutParameter(parameterName, sqlType); + } + + @Override + public void registerOutParameter(final String parameterName, final SQLType sqlType, final String typeName){ + registerOutParameter(parameterName, sqlType); + } + + private void setOutParameter(final int parameterIndex) { + setParameter(parameterIndex, "?"); + } + + private void setOutParameter(final String parameterName) { + setParameter(parameterName, "?"); + } + + private void prepareResultSet() throws SQLException { + if(resultSet.getRow() == 0){ + resultSet.next(); + } + } } diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcConnection.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcConnection.java index 6747fa8..a46460d 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcConnection.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcConnection.java @@ -42,6 +42,7 @@ import java.sql.Statement; import java.sql.Struct; import java.util.Map; +import java.util.Objects; import java.util.Properties; import java.util.concurrent.Executor; @@ -439,4 +440,28 @@ public T unwrap(final Class iface) throws SQLException { public boolean isWrapperFor(final Class iface) throws SQLException { return false; } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof JdbcConnection)) return false; + final JdbcConnection that = (JdbcConnection) o; + return closed == that.closed && + Objects.equals(httpClient, that.httpClient) && + Objects.equals(serverUrl, that.serverUrl); + } + + @Override + public int hashCode() { + return Objects.hash(httpClient, serverUrl, closed); + } + + @Override + public String toString() { + return "JdbcConnection{" + + "httpClient=" + httpClient + + ", serverUrl='" + serverUrl + '\'' + + ", closed=" + closed + + '}'; + } } \ No newline at end of file diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java index 2b480e6..5a0673f 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcPreparedStatement.java @@ -37,9 +37,10 @@ import java.sql.SQLXML; import java.sql.Time; import java.sql.Timestamp; -import java.util.ArrayList; import java.util.Calendar; -import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; import java.util.stream.Collectors; /** @@ -48,7 +49,7 @@ public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement { private final String preparedStatement; - private List parameters = new ArrayList<>(); + private Map parameters = new TreeMap<>(); public JdbcPreparedStatement(final HttpClient httpClient, final String preparedStatement, final String serverUrl, JdbcConnection connection) { super(httpClient, serverUrl, connection); @@ -201,7 +202,7 @@ public void setArray(final int parameterIndex, final Array x) throws SQLExceptio @Override public ResultSetMetaData getMetaData() throws SQLException { - return new JdbcResultSetMetaData(dataSet); + return resultSet.getMetaData(); } @Override @@ -330,19 +331,42 @@ public void setNClob(final int parameterIndex, final Reader reader) throws SQLEx } void setParameter(final int parameterIndex, final Object value){ - if (parameters.size() > parameterIndex - 1) { - parameters.set(parameterIndex - 1,value); - }else { - parameters.add(parameterIndex - 1, value); - } + setParameter(String.valueOf(parameterIndex - 1), value); + } + + void setParameter(final String parameterName, final Object value){ + parameters.put(parameterName, value); } private String composeStatement() { - return preparedStatement + " - (" + parameters.stream().map(param -> param != null ? param.toString() : "null").collect(Collectors.joining(",")) + ")"; + return preparedStatement + " - (" + parameters.values().stream().map(param -> param != null ? param.toString() : "null").collect(Collectors.joining(",")) + ")"; } - List getParameters() { + Map getParameters() { return parameters; } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof JdbcPreparedStatement)) return false; + if (!super.equals(o)) return false; + final JdbcPreparedStatement that = (JdbcPreparedStatement) o; + return Objects.equals(preparedStatement, that.preparedStatement) && + Objects.equals(parameters, that.parameters); + } + + @Override + public int hashCode() { + return Objects.hash(preparedStatement, parameters); + } + + @Override + public String toString() { + return "JdbcPreparedStatement{" + + "preparedStatement='" + preparedStatement + '\'' + + ", parameters=" + parameters + + '}'; + } } 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 3fae87b..8a671c2 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 @@ -16,19 +16,35 @@ package com.consol.citrus.db.driver; -import com.consol.citrus.db.driver.dataset.DataSet; import com.consol.citrus.db.driver.data.Row; +import com.consol.citrus.db.driver.dataset.DataSet; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URL; -import java.sql.*; +import java.sql.Array; +import java.sql.Blob; +import java.sql.Clob; +import java.sql.Date; +import java.sql.NClob; +import java.sql.Ref; +import java.sql.ResultSetMetaData; +import java.sql.RowId; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.SQLXML; +import java.sql.Statement; +import java.sql.Time; +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 */ @@ -38,358 +54,199 @@ public class JdbcResultSet implements java.sql.ResultSet { //The current ResultSet data row private Row row; + /** Indicates that this data set is closed */ + private boolean closed = false; + /** * Constructor using remote result set. */ - public JdbcResultSet(DataSet dataSet, JdbcStatement statement) throws SQLException { + public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) { this.dataSet = dataSet; this.statement = statement; } @Override public boolean next() throws SQLException { - try { - row = dataSet.getNextRow(); - } catch(SQLException ex) { - throw ex; - } catch(Exception ex) { - return false; - } - + ensureNotClosed(); + row = dataSet.getNextRow(); return row != null; } @Override public void close() throws SQLException { - dataSet.close(); + closed = true; } - 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(final int columnIndex) throws SQLException { + validateAccess(); + return (String) row.getValue(columnIndex-1, String.class); } - public String getString(int columnIndex) throws SQLException { - return convert(row.getValue(columnIndex-1), String.class); + public String getString(final String columnName) throws SQLException { + validateAccess(); + return (String) row.getValue(columnName, String.class); } - public String getString(String columnName) throws SQLException { - return convert(row.getValue(columnName), String.class); + public float getFloat(final int columnIndex) throws SQLException { + validateAccess(); + return (float) row.getValue(columnIndex-1, float.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 String columnName) throws SQLException { + validateAccess(); + return (float) row.getValue(columnName, 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 int getInt(final int columnIndex) throws SQLException { + validateAccess(); + return (int) row.getValue(columnIndex-1, int.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 String columnName) throws SQLException { + validateAccess(); + return (int) row.getValue(columnName, 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 boolean getBoolean(final int columnIndex) throws SQLException { + validateAccess(); + return (boolean) row.getValue(columnIndex-1, boolean.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 byte getByte(final int columnIndex) throws SQLException { + validateAccess(); + return (byte) row.getValue(columnIndex-1, byte.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 short getShort(final int columnIndex) throws SQLException { + validateAccess(); + return (short) row.getValue(columnIndex-1, short.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 long getLong(final int columnIndex) throws SQLException { + validateAccess(); + return (long) row.getValue(columnIndex-1, long.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 double getDouble(final int columnIndex) throws SQLException { + validateAccess(); + return (double) row.getValue(columnIndex-1, double.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 BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { + return getBigDecimal(columnIndex).setScale(scale, RoundingMode.HALF_UP); } - public BigDecimal getBigDecimal(int columnIndex,int scale) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getBigDecimal'"); + public byte[] getBytes(final int columnIndex) throws SQLException { + validateAccess(); + return (byte[]) row.getValue(columnIndex-1, byte[].class); } - 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 Date getDate(final int columnIndex) throws SQLException { + validateAccess(); + return (Date) row.getValue(columnIndex-1, Date.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 Time getTime(final int columnIndex) throws SQLException { + validateAccess(); + return (Time) row.getValue(columnIndex-1, Time.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 Timestamp getTimestamp(final int columnIndex) throws SQLException { + validateAccess(); + return (Timestamp) row.getValue(columnIndex-1, Timestamp.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 InputStream getAsciiStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getString(columnIndex).getBytes()); } - 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 getUnicodeStream(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 getBinaryStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getBytes(columnIndex)); } - 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 Object getObject(int columnIndex) throws SQLException { + public Object getObject(final int columnIndex) throws SQLException { + validateAccess(); 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + 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 { + validateAccess(); + return (Timestamp) row.getValue(columnName, Timestamp.class); } - public Object getObject(String columnName) throws SQLException { + public Object getObject(final String columnName) throws SQLException { + validateAccess(); 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 { + validateAccess(); + 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 { @@ -397,6 +254,7 @@ public SQLWarning getWarnings() throws SQLException { } public void clearWarnings() throws SQLException { + //currently not required } public String getCursorName() throws SQLException { @@ -407,26 +265,17 @@ public ResultSetMetaData getMetaData() throws SQLException { return new JdbcResultSetMetaData(dataSet); } - public int findColumn(String columnName) throws SQLException { - return row.getColumns().indexOf(columnName); + public int findColumn(final String columnName) throws SQLException { + validateAccess(); + return row.getColumns().indexOf(columnName)+1; } - 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 { @@ -446,9 +295,11 @@ public boolean isLast() throws SQLException { } public void beforeFirst() throws SQLException { + //currently not required } public void afterLast() throws SQLException { + //currently not required } public boolean first() throws SQLException { @@ -460,14 +311,15 @@ public boolean last() throws SQLException { } public int getRow() throws SQLException { - return dataSet.getCursor() + 1; + ensureNotClosed(); + return dataSet.getCursor(); } - 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'"); } @@ -475,14 +327,16 @@ 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 { + //currently not required } 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 { + //currently not required } public int getFetchSize() throws SQLException { @@ -498,236 +352,287 @@ public int getConcurrency() throws SQLException { } public boolean rowUpdated() throws SQLException { - return dataSet.getRows().size() > 0; + return rowModified(); } public boolean rowInserted() throws SQLException { - return dataSet.getRows().size() > 0; + return rowModified(); } public boolean rowDeleted() throws SQLException { - return dataSet.getRows().size() > 0; + return rowModified(); } - public void updateNull(int columnIndex) throws SQLException { + public void updateNull(final int columnIndex) throws SQLException { + //currently not required } - public void updateBoolean(int columnIndex,boolean x) throws SQLException { + public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { + //currently not required } - public void updateByte(int columnIndex, byte x) throws SQLException { + public void updateByte(final int columnIndex, final byte x) throws SQLException { + //currently not required } - public void updateShort(int columnIndex,short x) throws SQLException { + public void updateShort(final int columnIndex, final short x) throws SQLException { + //currently not required } - public void updateInt(int columnIndex,int x) throws SQLException { + public void updateInt(final int columnIndex, final int x) throws SQLException { + //currently not required } - public void updateLong(int columnIndex,long x) throws SQLException { + public void updateLong(final int columnIndex, final long x) throws SQLException { + //currently not required } - public void updateFloat(int columnIndex,float x) throws SQLException { + public void updateFloat(final int columnIndex, final float x) throws SQLException { + //currently not required } - public void updateDouble(int columnIndex,double x) throws SQLException { + public void updateDouble(final int columnIndex, final double x) throws SQLException { + //currently not required } - public void updateBigDecimal(int columnIndex,BigDecimal x) throws SQLException { + public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { + //currently not required } - public void updateString(int columnIndex,String x) throws SQLException { + public void updateString(final int columnIndex, final String x) throws SQLException { + //currently not required } - public void updateBytes(int columnIndex,byte[] x) throws SQLException { + public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { + //currently not required } - public void updateDate(int columnIndex,Date x) throws SQLException { + public void updateDate(final int columnIndex, final Date x) throws SQLException { + //currently not required } - public void updateTime(int columnIndex,Time x) throws SQLException { + public void updateTime(final int columnIndex, final Time x) throws SQLException { + //currently not required } - public void updateTimestamp(int columnIndex,Timestamp x) throws SQLException { + public void updateTimestamp(final int columnIndex, final Timestamp x) throws SQLException { + //currently not required } - 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 { + //currently not required } - 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 { + //currently not required } - 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 { + //currently not required } - public void updateObject(int columnIndex,Object x) throws SQLException { + public void updateObject(final int columnIndex, final Object x) throws SQLException { + //currently not required } - public void updateNull(String columnName) throws SQLException { + public void updateNull(final String columnName) throws SQLException { + //currently not required } - public void updateByte(String columnName, byte x) throws SQLException { + public void updateByte(final String columnName, final byte x) throws SQLException { + //currently not required } - public void updateShort(String columnName, short x) throws SQLException { + public void updateShort(final String columnName, final short x) throws SQLException { + //currently not required } - public void updateInt(String columnName,int x) throws SQLException { + public void updateInt(final String columnName, final int x) throws SQLException { + //currently not required } - public void updateLong(String columnName,long x) throws SQLException { + public void updateLong(final String columnName, final long x) throws SQLException { + //currently not required } - public void updateFloat(String columnName, float x) throws SQLException { + public void updateFloat(final String columnName, final float x) throws SQLException { + //currently not required } - public void updateDouble(String columnName,double x) throws SQLException { + public void updateDouble(final String columnName, final double x) throws SQLException { + //currently not required } - public void updateBigDecimal(String columnName,BigDecimal x) throws SQLException { + public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { + //currently not required } - public void updateString(String columnName,String x) throws SQLException { + public void updateString(final String columnName, final String x) throws SQLException { + //currently not required } - public void updateBytes(String columnName,byte[] x) throws SQLException { + public void updateBytes(final String columnName, final byte[] x) throws SQLException { + //currently not required } - public void updateDate(String columnName,Date x) throws SQLException { + public void updateDate(final String columnName, final Date x) throws SQLException { + //currently not required } - public void updateTime(String columnName, Time x) throws SQLException { + public void updateTime(final String columnName, final Time x) throws SQLException { + //currently not required } - public void updateTimestamp(String columnName,Timestamp x) throws SQLException { + public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException { + //currently not required } - 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 { + //currently not required } - 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 { + //currently not required } - 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 { + //currently not required } - 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 { + //currently not required } - public void updateObject(String columnName,Object x) throws SQLException { + public void updateObject(final String columnName, final Object x) throws SQLException { + //currently not required } public void insertRow() throws SQLException { + //currently not required } public void updateRow()throws SQLException { + //currently not required } public void deleteRow() throws SQLException { + //currently not required } public void refreshRow() throws SQLException { + //currently not required } public void cancelRowUpdates() throws SQLException { + //currently not required } public void moveToInsertRow() throws SQLException { + //currently not required } public void moveToCurrentRow() throws SQLException { + //currently not required } public Statement getStatement() throws SQLException { - if (isClosed()) { - throw new SQLException("ResultSet has already been closed."); - } + ensureNotClosed(); return statement; } - 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 { + //currently not required } @Override - public void updateRef(String columnLabel, Ref x) throws SQLException { + public void updateRef(final String columnLabel, final Ref x) throws SQLException { + //currently not required } @Override - public void updateBlob(int columnIndex, Blob x) throws SQLException { + public void updateBlob(final int columnIndex, final Blob x) throws SQLException { + //currently not required } @Override - public void updateBlob(String columnLabel, Blob x) throws SQLException { + public void updateBlob(final String columnLabel, final Blob x) throws SQLException { + //currently not required } @Override - public void updateClob(int columnIndex, Clob x) throws SQLException { + public void updateClob(final int columnIndex, final Clob x) throws SQLException { + //currently not required } @Override - public void updateClob(String columnLabel, Clob x) throws SQLException { + public void updateClob(final String columnLabel, final Clob x) throws SQLException { + //currently not required } @Override - public void updateArray(int columnIndex, Array x) throws SQLException { + public void updateArray(final int columnIndex, final Array x) throws SQLException { + //currently not required } @Override - public void updateArray(String columnLabel, Array x) throws SQLException { + public void updateArray(final String columnLabel, final Array x) throws SQLException { + //currently not required } @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 { + //currently not required } @Override - public void updateRowId(String columnLabel, RowId x) throws SQLException { + public void updateRowId(final String columnLabel, final RowId x) throws SQLException { + //currently not required } @Override @@ -737,252 +642,335 @@ public int getHoldability() throws SQLException { @Override public boolean isClosed() throws SQLException { - return dataSet.isClosed(); + return closed; } @Override - public void updateNString(int columnIndex, String nString) throws SQLException { + public void updateNString(final int columnIndex, final String nString) throws SQLException { + //currently not required } @Override - public void updateNString(String columnLabel, String nString) throws SQLException { + public void updateNString(final String columnLabel, final String nString) throws SQLException { + //currently not required } @Override - public void updateNClob(int columnIndex, NClob nClob) throws SQLException { + public void updateNClob(final int columnIndex, final NClob nClob) throws SQLException { + //currently not required } @Override - public void updateNClob(String columnLabel, NClob nClob) throws SQLException { + public void updateNClob(final String columnLabel, final NClob nClob) throws SQLException { + //currently not required } @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 { + //currently not required } @Override - public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { + public void updateSQLXML(final String columnLabel, final SQLXML xmlObject) throws SQLException { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @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 { + //currently not required } @Override - public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateNCharacterStream(final int columnIndex, final Reader x) throws SQLException { + //currently not required } @Override - public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override - public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x) throws SQLException { + //currently not required } @Override - public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x) throws SQLException { + //currently not required } @Override - public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x) throws SQLException { + //currently not required } @Override - public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { + public void updateAsciiStream(final String columnLabel, final InputStream x) throws SQLException { + //currently not required } @Override - public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { + public void updateBinaryStream(final String columnLabel, final InputStream x) throws SQLException { + //currently not required } @Override - public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override - public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { + public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { + //currently not required } @Override - public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { + public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { + //currently not required } @Override - public void updateClob(int columnIndex, Reader reader) throws SQLException { + public void updateClob(final int columnIndex, final Reader reader) throws SQLException { + //currently not required } @Override - public void updateClob(String columnLabel, Reader reader) throws SQLException { + public void updateClob(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override - public void updateNClob(int columnIndex, Reader reader) throws SQLException { + public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { + //currently not required } @Override - public void updateNClob(String columnLabel, Reader reader) throws SQLException { + public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override - public T getObject(int columnIndex, Class type) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + public T getObject(final int columnIndex, final Class type) throws SQLException { + throw new SQLException("Not supported JDBC result set function 'getObject(int, Class)'"); } @Override - public T getObject(String columnLabel, Class type) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + public T getObject(final String columnLabel, final Class type) throws SQLException { + throw new SQLException("Not supported JDBC result set function '(String, Class)'"); } public boolean wasNull()throws SQLException { + validateAccess(); return row.getLastValue() == null; } - public void updateBoolean(String columnName, boolean x) throws SQLException { + public void updateBoolean(final String columnName, final boolean x) throws SQLException { + //currently not required } - 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 { + //currently not required } - public Object getObject(int i, Map map) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + public Object getObject(final int i, final Map map) throws SQLException { + throw new SQLException("Not supported JDBC result set function '(int, Map)'"); } - 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'"); } + private void validateAccess() throws SQLException { + ensureValidCursor(); + ensureNotClosed(); + } + + private void ensureValidCursor() throws SQLException { + if(row == null){ + throw new SQLException("No further data - Cursor position is after last row"); + } + } + + private void ensureNotClosed() throws SQLException { + if(isClosed()){ + throw new SQLException("Result set already closed"); + } + } + + private boolean rowModified() throws SQLException { + ensureNotClosed(); + return !dataSet.getRows().isEmpty(); + } + @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/main/java/com/consol/citrus/db/driver/JdbcStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcStatement.java index fddb590..7b88ddc 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcStatement.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcStatement.java @@ -28,19 +28,19 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; -import java.sql.*; +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.SQLWarning; +import java.sql.Statement; import java.util.Objects; -/** - * @author Christoph Deppisch - */ public class JdbcStatement implements Statement { private final HttpClient httpClient; private final String serverUrl; private final JdbcConnection connection; - protected DataSet dataSet = new DataSet(); + protected JdbcResultSet resultSet; /** * Default constructor using remote client reference. @@ -66,9 +66,10 @@ public java.sql.ResultSet executeQuery(final String sqlQuery) throws SQLExceptio throw new SQLException("Failed to execute query: " + sqlQuery); } - dataSet = new JsonDataSetProducer(response.getEntity().getContent()).produce(); + DataSet dataSet = new JsonDataSetProducer(response.getEntity().getContent()).produce(); + resultSet = new JdbcResultSet(dataSet, this); - return new JdbcResultSet(dataSet, this); + return resultSet; } catch (final IOException e) { throw new SQLException(e); } finally { @@ -110,7 +111,8 @@ public boolean execute(final String sql) throws SQLException { } if (response.getEntity().getContentType().getValue().equals("application/json")) { - dataSet = new JsonDataSetProducer(response.getEntity().getContent()).produce(); + final DataSet produce = new JsonDataSetProducer(response.getEntity().getContent()).produce(); + resultSet = new JdbcResultSet(produce, this); } return true; @@ -190,7 +192,7 @@ public void setCursorName(final String name) throws SQLException { @Override public java.sql.ResultSet getResultSet() throws SQLException { - return new JdbcResultSet(dataSet, this); + return resultSet; } @Override @@ -377,10 +379,26 @@ public boolean isWrapperFor(final Class iface) throws SQLException { @Override public boolean equals(final Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof JdbcStatement)) return false; final JdbcStatement that = (JdbcStatement) o; return Objects.equals(httpClient, that.httpClient) && Objects.equals(serverUrl, that.serverUrl) && - Objects.equals(dataSet, that.dataSet); + Objects.equals(connection, that.connection) && + Objects.equals(resultSet, that.resultSet); + } + + @Override + public int hashCode() { + return Objects.hash(httpClient, serverUrl, connection, resultSet); + } + + @Override + public String toString() { + return "JdbcStatement{" + + "httpClient=" + httpClient + + ", serverUrl='" + serverUrl + '\'' + + ", connection=" + connection + + ", resultSet=" + resultSet + + '}'; } } diff --git a/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java b/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java index 5dbcb28..79b02bd 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/data/Row.java @@ -16,15 +16,15 @@ package com.consol.citrus.db.driver.data; +import org.apache.commons.beanutils.ConvertUtils; + import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.SortedMap; -/** - * @author Christoph Deppisch - */ public class Row { /** Row values with column name as key */ @@ -34,36 +34,60 @@ public class Row { /** * Gets set of column names available in this row. - * @return + * @return A list of column values */ public List getColumns() { - return Arrays.asList(values.keySet().toArray(new String[values.size()])); + return Arrays.asList(values.keySet().toArray(new String[0])); } /** * Gets the row value identified by its column name. - * @param columnName - * @return + * @param columnName The name to get the value for + * @return The value of that column */ - public Object getValue(String columnName) { + public Object getValue(final String columnName) { lastValue = values.get(columnName); return lastValue; } + /** + * Gets the row value identified by its column name converted into the given type + * @param parameterName The name to get the value for + * @param clazz The class to convert the value to + * @param Generic parameter to ensure correct conversion + * @return The converted object + */ + public Object getValue(final String parameterName, final Class clazz) { + final Object value = getValue(parameterName); + return convertData(value, clazz); + } + /** * Gets the row value identified by its column index. - * @param columnIndex - * @return + * @param columnIndex The column index to get the value from + * @return The object of that index */ - public Object getValue(int columnIndex) { + public Object getValue(final int columnIndex) { lastValue = values.values().toArray()[columnIndex]; return lastValue; } + /** + * Gets the row value identified by its column index converted into the given type + * @param columnIndex The index to get the value from + * @param clazz The class to convert the value to + * @param Generic parameter to ensure correct conversion + * @return The converted object + */ + public Object getValue(final int columnIndex, final Class clazz){ + final Object value = getValue(columnIndex); + return convertData(value, clazz); + } + /** * Gets the values. * - * @return + * @return The values of the row identified by column manes */ public Map getValues() { return values; @@ -72,17 +96,21 @@ public Map getValues() { /** * Sets the values. * - * @param values + * @param values The values to set in the Row */ - public void setValues(Map values) { + public void setValues(final SortedMap values) { this.values = values; } + private Object convertData(final Object value, final Class clazz) { + return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz); + } + @Override - public boolean equals(Object o) { + public boolean equals(final Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; - Row row = (Row) o; + final Row row = (Row) o; return Objects.equals(values, row.values); } @@ -93,7 +121,7 @@ public int hashCode() { /** * Gets the lastValue. - * @return + * @return The last value as object */ public Object getLastValue() { return lastValue; diff --git a/driver/src/main/java/com/consol/citrus/db/driver/dataset/DataSet.java b/driver/src/main/java/com/consol/citrus/db/driver/dataset/DataSet.java index 4bc42b4..45b2d3f 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/dataset/DataSet.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/dataset/DataSet.java @@ -18,40 +18,36 @@ import com.consol.citrus.db.driver.data.Row; -import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -/** - * @author Christoph Deppisch - */ public class DataSet { /** Rows in this data set */ private final List rows = new ArrayList<>(); - /** Indicates that this data set is closed */ - private boolean closed = false; - /** Cursor position on selected row */ private AtomicInteger cursor = new AtomicInteger(0); /** - * Gets next row in this result set based on cursor position. - * @return - * @throws SQLException + * Gets next row in this data set based on cursor position. + * @return The next row of the dataset or null if no further row is available */ - public Row getNextRow() throws SQLException { - checkNotClosed(); - return rows.get(cursor.getAndIncrement()); + public Row getNextRow(){ + final int index = cursor.getAndIncrement(); + if(rows.size() <= index){ + return null; + } + + return rows.get(index); } /** * Gets list of columns in this dataset. - * @return + * @return The list of columns of the dataset */ public List getColumns() { return rows.stream().flatMap(row -> row.getColumns().stream()).distinct().collect(Collectors.toList()); @@ -59,54 +55,38 @@ public List getColumns() { /** * Gets all rows in this dataset. - * @return - * @throws SQLException + * @return The rows of the Dataset */ - public List getRows() throws SQLException { - checkNotClosed(); + public List getRows() { return rows; } /** * Gets current row index. - * @return + * @return The current curser of the DataSet */ public int getCursor() { return cursor.get(); } - /** - * Close result set - no further access to rows and columns allowed. - */ - public void close() { - this.closed = true; - } - - /** - * If closed throws new SQLException. - * @throws SQLException - */ - private void checkNotClosed() throws SQLException { - if (closed) { - throw new SQLException("Result set already closed"); - } + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof DataSet)) return false; + final DataSet dataSet = (DataSet) o; + return Objects.equals(rows, dataSet.rows); } - /** - * Returns true if the data set is already closed, otherwise false. - * @return - */ - public boolean isClosed() { - return closed; + @Override + public int hashCode() { + return Objects.hash(rows); } @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - DataSet dataSet = (DataSet) o; - return closed == dataSet.closed && - Objects.equals(rows, dataSet.rows) && - Objects.equals(cursor.get(), dataSet.cursor.get()); + public String toString() { + return "DataSet{" + + "rows=" + rows + + ", cursor=" + cursor + + '}'; } } diff --git a/driver/src/main/java/com/consol/citrus/db/driver/json/JsonDataSetWriter.java b/driver/src/main/java/com/consol/citrus/db/driver/json/JsonDataSetWriter.java index 944594b..cd3c63b 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/json/JsonDataSetWriter.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/json/JsonDataSetWriter.java @@ -23,8 +23,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; /** * @author Christoph Deppisch @@ -40,7 +41,7 @@ public String write(DataSet dataSet) { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); return mapper.writeValueAsString(rawDataSet); - } catch (JsonProcessingException | SQLException e) { + } catch (JsonProcessingException e) { throw new JdbcDriverException("Failed to write json dataset", e); } } diff --git a/driver/src/main/java/com/consol/citrus/db/driver/xml/XmlDataSetWriter.java b/driver/src/main/java/com/consol/citrus/db/driver/xml/XmlDataSetWriter.java index d14adc2..e2fceef 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/xml/XmlDataSetWriter.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/xml/XmlDataSetWriter.java @@ -16,13 +16,10 @@ package com.consol.citrus.db.driver.xml; -import com.consol.citrus.db.driver.JdbcDriverException; import com.consol.citrus.db.driver.dataset.DataSet; import com.consol.citrus.db.driver.dataset.DataSetWriter; import org.w3c.dom.Node; -import java.sql.SQLException; - /** * @author Christoph Deppisch */ @@ -36,35 +33,31 @@ public class XmlDataSetWriter implements DataSetWriter { @Override public String write(DataSet dataSet) { - try { - StringBuilder xmlOutput = new StringBuilder(); - - xmlOutput.append("<").append(DATASET).append(">\n"); - - dataSet.getRows().forEach(row -> { - if (mode == Node.ELEMENT_NODE) { - xmlOutput.append(spaces).append("<").append(ROW).append(">\n"); - row.getValues().forEach((key, value) -> { - xmlOutput.append(spaces).append(spaces).append("<").append(key).append(">"); - xmlOutput.append(value); - xmlOutput.append("\n"); - }); - xmlOutput.append(spaces).append("\n"); - } else if (mode == Node.ATTRIBUTE_NODE) { - xmlOutput.append(spaces).append("<").append(ROW).append("\n"); - row.getValues().forEach((key, value) -> { - xmlOutput.append(spaces).append(spaces).append(key).append("=\"").append(value).append("\"").append("\n"); - }); - xmlOutput.append(spaces).append(spaces).append("/>\n"); - } - }); - - xmlOutput.append(""); - - return xmlOutput.toString(); - } catch (SQLException e) { - throw new JdbcDriverException("Failed to write xml dataset", e); - } + StringBuilder xmlOutput = new StringBuilder(); + + xmlOutput.append("<").append(DATASET).append(">\n"); + + dataSet.getRows().forEach(row -> { + if (mode == Node.ELEMENT_NODE) { + xmlOutput.append(spaces).append("<").append(ROW).append(">\n"); + row.getValues().forEach((key, value) -> { + xmlOutput.append(spaces).append(spaces).append("<").append(key).append(">"); + xmlOutput.append(value); + xmlOutput.append("\n"); + }); + xmlOutput.append(spaces).append("\n"); + } else if (mode == Node.ATTRIBUTE_NODE) { + xmlOutput.append(spaces).append("<").append(ROW).append("\n"); + row.getValues().forEach((key, value) -> { + xmlOutput.append(spaces).append(spaces).append(key).append("=\"").append(value).append("\"").append("\n"); + }); + xmlOutput.append(spaces).append(spaces).append("/>\n"); + } + }); + + xmlOutput.append(""); + + return xmlOutput.toString(); } /** diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java new file mode 100644 index 0000000..61f94c0 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -0,0 +1,758 @@ +package com.consol.citrus.db.driver; + +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.http.client.HttpClient; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.net.MalformedURLException; +import java.net.URL; +import java.sql.Date; +import java.sql.JDBCType; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.sql.Types; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.mock; +import static org.powermock.api.mockito.PowerMockito.when; +import static org.testng.Assert.assertEquals; + +public class JdbcCallableStatementTest{ + + private HttpClient httpClient; + private String serverUrl = "localhost"; + private JdbcConnection jdbcConnection; + private final int TEST_VALUE_INDEX = 2; + private final String TEST_VALUE_NAME = "col2"; + private JdbcCallableStatement callableStatement; + + private JdbcResultSet resultSetSpy; + + @BeforeMethod + public void setup(){ + httpClient = mock(HttpClient.class); + jdbcConnection = mock(JdbcConnection.class); + + callableStatement = generateCallableStatement(); + resultSetSpy = mock(JdbcResultSet.class); + callableStatement.resultSet = resultSetSpy; + } + + @Test + public void testRegisterOutParameter() throws SQLException { + + //GIVEN + final int index = 2; + + //WHEN + callableStatement.registerOutParameter(index, Types.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get("1"), "?"); + } + + @Test + public void testRegisterOutParameterWithScale() throws SQLException { + + //GIVEN + final int index = 2; + + //WHEN + callableStatement.registerOutParameter(index, Types.INTEGER, 2); + + //THEN + assertEquals(callableStatement.getParameters().get("1"), "?"); + } + + @Test + public void testRegisterOutParameterWithTypeName() throws SQLException { + + //GIVEN + final int index = 2; + + //WHEN + callableStatement.registerOutParameter(index, Types.INTEGER, "STRUCT"); + + //THEN + assertEquals(callableStatement.getParameters().get("1"), "?"); + } + + @Test + public void testRegisterOutParameterByName() throws SQLException { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, Types.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testRegisterOutParameterByNameWithScale() throws SQLException { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, Types.INTEGER, 2); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testRegisterOutParameterByNameWithTypeName() throws SQLException { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, Types.INTEGER, "STRUCT"); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testRegisterOutParameterWithSqlType() { + + //GIVEN + + //WHEN + callableStatement.registerOutParameter(1, JDBCType.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get("0"), "?"); + } + + @Test + public void testRegisterOutParameterWithSqlTypeAndScale() { + + //GIVEN + + //WHEN + callableStatement.registerOutParameter(1, JDBCType.INTEGER, 2); + + //THEN + assertEquals(callableStatement.getParameters().get("0"), "?"); + } + + @Test + public void testRegisterOutParameterWithSqlTypeAndTypeName() { + + //GIVEN + //WHEN + callableStatement.registerOutParameter(1, JDBCType.INTEGER, "STRUCT"); + + //THEN + assertEquals(callableStatement.getParameters().get("0"), "?"); + } + + @Test + public void testRegisterOutParameterByNameWithSqlType() { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, JDBCType.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testRegisterOutParameterByNameWithSqlTypeAndScale() { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, JDBCType.INTEGER, 2); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testRegisterOutParameterByNameWithSqlTypeAndTypeName() { + + //GIVEN + final String parameterName = "foo"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.registerOutParameter(parameterName, JDBCType.INTEGER, "STRUCT"); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "?"); + } + + @Test + public void testSetUrlWithName() throws SQLException, MalformedURLException { + + //GIVEN + final String parameterName = "MyUrl"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final URL url = new URL("http://www.example.com/datalink/"); + + //WHEN + callableStatement.setURL(parameterName, url); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), url); + } + + @Test + public void testSetNullWithName() throws SQLException { + + //GIVEN + final String parameterName = "myNull"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.setNull(parameterName, Types.NULL); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), "null"); + } + + @Test + public void testSetBooleanWithName() throws SQLException { + + //GIVEN + final String parameterName = "myBoolean"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.setBoolean(parameterName, true); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), true); + } + + @Test + public void testSetByteWithName() throws SQLException { + + //GIVEN + final String parameterName = "myByte"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final byte myByte = 8; + + //WHEN + callableStatement.setByte(parameterName, myByte); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myByte); + } + + @Test + public void testSetShortWithName() throws SQLException { + + //GIVEN + final String parameterName = "myShort"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final short myShort = 8; + + //WHEN + callableStatement.setShort(parameterName, myShort); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myShort); + } + + @Test + public void testSetIntWithName() throws SQLException { + + //GIVEN + final String parameterName = "myInt"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final int myInt = 42; + + //WHEN + callableStatement.setInt(parameterName, myInt); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myInt); + } + + @Test + public void testSetLongWithName() throws SQLException { + + //GIVEN + final String parameterName = "myLong"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final long myLong = 42L; + + //WHEN + callableStatement.setLong(parameterName, myLong); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myLong); + } + + @Test + public void testSetFloatWithName() throws SQLException { + + //GIVEN + final String parameterName = "myFloat"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final float myFloat = 4.2F; + + //WHEN + callableStatement.setFloat(parameterName, myFloat); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myFloat); + } + + @Test + public void testSetDoubleWithName() throws SQLException { + + //GIVEN + final String parameterName = "myDouble"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final double myDouble = 4.2; + + //WHEN + callableStatement.setDouble(parameterName, myDouble); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myDouble); + } + + @Test + public void testSetBigDecimalWithName() throws SQLException { + + //GIVEN + final String parameterName = "myBigDecimal"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final BigDecimal myBigDecimal = new BigDecimal(42); + + //WHEN + callableStatement.setBigDecimal(parameterName, myBigDecimal); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myBigDecimal); + } + + @Test + public void testSetStringWithName() throws SQLException { + + //GIVEN + final String parameterName = "myString"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + + //WHEN + callableStatement.setString(parameterName, parameterName); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), parameterName); + } + + @Test + public void testSetBytesWithName() throws SQLException { + + //GIVEN + final String parameterName = "myBytes"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final byte[] myBytes = parameterName.getBytes(); + + //WHEN + callableStatement.setBytes(parameterName, myBytes); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myBytes); + } + + @Test + public void testSetDateWithName() throws SQLException { + + //GIVEN + final String parameterName = "myDate"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final Date myDate = new Date(619912800000L); + + //WHEN + callableStatement.setDate(parameterName, myDate); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myDate); + } + + @Test + public void testSetTimeWithName() throws SQLException { + + //GIVEN + final String parameterName = "myTime"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final Time myDate = new Time(619912812345L); + + //WHEN + callableStatement.setTime(parameterName, myDate); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myDate); + } + + @Test + public void testSetTimeStampWithName() throws SQLException { + + //GIVEN + final String parameterName = "myTimeStamp"; + final JdbcCallableStatement callableStatement = generateCallableStatementWithParameter(parameterName); + final Timestamp myTimeStamp = new Timestamp(619912812345L); + + //WHEN + callableStatement.setTimestamp(parameterName, myTimeStamp); + + //THEN + assertEquals(callableStatement.getParameters().get(parameterName), myTimeStamp); + } + + @Test + void testGetStringByIndex() throws SQLException { + + //WHEN + callableStatement.getString(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getString(TEST_VALUE_INDEX); + } + + @Test + void testGetBooleanByIndex() throws SQLException { + + //WHEN + callableStatement.getBoolean(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getBoolean(TEST_VALUE_INDEX); + } + + @Test + void testGetByteByIndex() throws SQLException { + + //WHEN + callableStatement.getByte(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getByte(TEST_VALUE_INDEX); + } + + @Test + void testGetBytesByIndex() throws SQLException { + + //WHEN + callableStatement.getBytes(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getBytes(TEST_VALUE_INDEX); + } + + @Test + void testGetShortByIndex() throws SQLException { + + //WHEN + callableStatement.getShort(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getShort(TEST_VALUE_INDEX); + } + + @Test + void testGetIntByIndex() throws SQLException { + + //WHEN + callableStatement.getInt(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getInt(TEST_VALUE_INDEX); + } + + @Test + void testGetLongByIndex() throws SQLException { + + //WHEN + callableStatement.getLong(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getLong(TEST_VALUE_INDEX); + } + + @Test + void testGetFloatByIndex() throws SQLException { + + //WHEN + callableStatement.getFloat(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getFloat(TEST_VALUE_INDEX); + } + + @Test + void testGetDoubleByIndex() throws SQLException { + + //WHEN + callableStatement.getDouble(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getDouble(TEST_VALUE_INDEX); + } + + @Test + void testGetBigDecimalByIndexWithScale() throws Exception { + + //GIVEN + final BigDecimal bigDecimalMock = mock(BigDecimal.class); + when(resultSetSpy.getBigDecimal(TEST_VALUE_INDEX)).thenReturn(bigDecimalMock); + + //WHEN + callableStatement.getBigDecimal(TEST_VALUE_INDEX, 2); + + //THEN + //noinspection ResultOfMethodCallIgnored + verify(bigDecimalMock).setScale(2, RoundingMode.HALF_UP); + } + + @Test + void testGetStringByName() throws SQLException { + + //WHEN + callableStatement.getString(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getString(TEST_VALUE_NAME); + } + + @Test + void testGetBooleanByName() throws SQLException { + + //WHEN + callableStatement.getBoolean(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getBoolean(TEST_VALUE_NAME); + } + + @Test + void testGetByteByName() throws SQLException { + + //WHEN + callableStatement.getByte(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getByte(TEST_VALUE_NAME); + } + + @Test + void testGetShortByName() throws SQLException { + + //WHEN + callableStatement.getShort(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getShort(TEST_VALUE_NAME); + } + + @Test + void testGetIntByName() throws SQLException { + + //WHEN + callableStatement.getInt(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getInt(TEST_VALUE_NAME); + } + + @Test + void testGetLongByName() throws SQLException { + + //WHEN + callableStatement.getLong(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getLong(TEST_VALUE_NAME); + } + + @Test + void testGetFloatByName() throws SQLException { + + //WHEN + callableStatement.getFloat(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getFloat(TEST_VALUE_NAME); + } + + @Test + void testGetDoubleByName() throws SQLException { + + //WHEN + callableStatement.getDouble(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getDouble(TEST_VALUE_NAME); + } + + @Test + void testGetBytesByName() throws SQLException { + + //WHEN + callableStatement.getBytes(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getBytes(TEST_VALUE_NAME); + } + + @Test + void testGetObjectByName() throws SQLException { + + //WHEN + callableStatement.getObject(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getObject(TEST_VALUE_NAME); + } + + @Test + void testGetBigDecimalByName() throws SQLException { + + //WHEN + callableStatement.getBigDecimal(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getBigDecimal(TEST_VALUE_NAME); + } + + @Test + void testDelegateWasNull() throws SQLException { + + + //WHEN + callableStatement.wasNull(); + + //THEN + verify(resultSetSpy).wasNull(); + } + + @Test + void testGetDateByIndex() throws SQLException { + + //WHEN + callableStatement.getDate(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getDate(TEST_VALUE_INDEX); + } + + @Test + void testGetTimeByIndex() throws SQLException { + + //WHEN + callableStatement.getTime(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getTime(TEST_VALUE_INDEX); + } + + @Test + void testGetTimestampByIndex() throws SQLException { + + //WHEN + callableStatement.getTimestamp(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getTimestamp(TEST_VALUE_INDEX); + } + + @Test + void testGetObjectByIndex() throws SQLException { + + //WHEN + callableStatement.getObject(TEST_VALUE_INDEX); + + //THEN + verify(resultSetSpy).getObject(TEST_VALUE_INDEX); + } + + @Test + void testGetDateByName() throws SQLException { + + //WHEN + callableStatement.getDate(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getDate(TEST_VALUE_NAME); + } + + @Test + void testGetTimeByName() throws SQLException { + + //WHEN + callableStatement.getTime(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getTime(TEST_VALUE_NAME); + } + + @Test + void testGetTimestampByName() throws SQLException { + + //WHEN + callableStatement.getTimestamp(TEST_VALUE_NAME); + + //THEN + verify(resultSetSpy).getTimestamp(TEST_VALUE_NAME); + } + + @Test + void verifyResultSetHandling() throws SQLException { + + //GIVEN + when(resultSetSpy.getRow()).thenReturn(0).thenReturn(1); + + //WHEN + callableStatement.getTimestamp(1); + callableStatement.getString(2); + + + //THEN + verify(resultSetSpy, times(2)).getRow(); + verify(resultSetSpy, times(1)).next(); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcCallableStatement.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcCallableStatement.class); + } + + private JdbcCallableStatement generateCallableStatement() { + final String statement = "CALL myFunction(?,?)"; + return new JdbcCallableStatement(httpClient, statement, serverUrl, jdbcConnection); + } + private JdbcCallableStatement generateCallableStatementWithParameter(final String parameterName) { + final String statement = "CALL myFunction("+parameterName+",?)"; + return new JdbcCallableStatement(httpClient,statement, serverUrl, jdbcConnection); + } + +} \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcConnectionTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcConnectionTest.java index bb4a828..597194a 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcConnectionTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcConnectionTest.java @@ -16,6 +16,8 @@ package com.consol.citrus.db.driver; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; @@ -330,4 +332,14 @@ public void testPrepareStatementIoExceptionIsWrappedInSqlException() throws Exce //THEN //Exception is thrown } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcStatement.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcStatement.class); + } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java index e0e04b7..3ac0392 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java @@ -1,5 +1,7 @@ package com.consol.citrus.db.driver; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; import org.apache.http.client.HttpClient; import org.testng.Assert; import org.testng.annotations.BeforeMethod; @@ -26,7 +28,7 @@ public void setUp() { @Test - public void testSetParameter() throws Exception { + public void testSetParameter() { //GIVEN @@ -34,11 +36,11 @@ public void testSetParameter() throws Exception { jdbcPreparedStatement.setParameter(1, 2); //THEN - Assert.assertEquals(jdbcPreparedStatement.getParameters().get(0), 2); + Assert.assertEquals(jdbcPreparedStatement.getParameters().get("0"), 2); } @Test - public void testSetParameterAddAnotherValue() throws Exception { + public void testSetParameterAddAnotherValue(){ //GIVEN @@ -48,12 +50,12 @@ public void testSetParameterAddAnotherValue() throws Exception { //THEN Assert.assertEquals(jdbcPreparedStatement.getParameters().size(), 2); - Assert.assertEquals(jdbcPreparedStatement.getParameters().get(0), 2); - Assert.assertEquals(jdbcPreparedStatement.getParameters().get(1), 42); + Assert.assertEquals(jdbcPreparedStatement.getParameters().get("0"), 2); + Assert.assertEquals(jdbcPreparedStatement.getParameters().get("1"), 42); } @Test - public void testSetParameterOverwritesValue() throws Exception { + public void testSetParameterOverwritesValue() { //GIVEN @@ -63,6 +65,16 @@ public void testSetParameterOverwritesValue() throws Exception { //THEN Assert.assertEquals(jdbcPreparedStatement.getParameters().size(), 1); - Assert.assertEquals(jdbcPreparedStatement.getParameters().get(0), 42); + Assert.assertEquals(jdbcPreparedStatement.getParameters().get("0"), 42); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcPreparedStatement.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcPreparedStatement.class); } } \ 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..81ab53c --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java @@ -0,0 +1,1778 @@ +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.Statement; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.mockito.Mockito.mock; +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.assertFalse; +import static org.testng.Assert.assertTrue; + +@PrepareForTest(ConvertUtils.class) +public class JdbcResultSetTest { + + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedString); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedString); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedFloat); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedFloat); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedInt); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedInt); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedBoolean); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedByte); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedShort); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedLong); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedDouble); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedBigDecimal); + 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(); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedDate); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedTime); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedTimestamp); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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(); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedObject); + resultSet.next(); + + //WHEN + final Object anObject = resultSet.getObject(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(anObject, expectedObject); + } + + @Test + void testGetObjectByName() throws SQLException { + + //GIVEN + final Object expectedObject = new Timestamp(619912812345L); + final JdbcResultSet resultSet = generateResultSet(expectedObject); + resultSet.next(); + + //WHEN + final Object anObject = resultSet.getObject(TEST_VALUE_NAME); + + //THEN + assertEquals(anObject, expectedObject); + } + + @Test + void testGetBooleanByName() throws SQLException { + + //GIVEN + final boolean expectedBoolean = true; + final JdbcResultSet resultSet = generateResultSet(expectedBoolean); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedByte); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedShort); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedLong); + 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; + final JdbcResultSet resultSet = generateResultSet(expectedDouble); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedBigDecimal); + 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(); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedDate); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedTime); + 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); + final JdbcResultSet resultSet = generateResultSet(expectedTimestamp); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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(); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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"; + final JdbcResultSet resultSet = generateResultSet(expectedText); + 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 + final JdbcResultSet resultSet = generateResultSet(); + + //WHEN + final boolean updated = resultSet.rowUpdated(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowInserted() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSet(); + + //WHEN + final boolean updated = resultSet.rowInserted(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowDeleted() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSet(); + + //WHEN + final boolean updated = resultSet.rowDeleted(); + + //THEN + assertTrue(updated); + } + + @Test(expectedExceptions = SQLException.class) + void nextThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.next(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getStringByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getString(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getStringByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getString(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getStringByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getString(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getStringByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getString(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getFloatByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getFloat(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getFloatByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getFloat(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getFloatByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getFloat(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getFloatByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getFloat(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getIntByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getInt(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getIntByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getInt(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getIntByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getInt(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getIntByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getInt(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBooleanByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBoolean(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBooleanByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBoolean(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBooleanByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBoolean(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBooleanByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBoolean(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getByteByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getByteByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getByteByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getByteByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getShortByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getShort(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getShortByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getShort(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getShortByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getShort(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getShortByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getShort(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getLongByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getLong(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getLongByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getLong(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getLongByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getLong(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getLongByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getLong(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDoubleByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getDouble(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDoubleByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getDouble(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDoubleByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getDouble(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBigDecimalByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBigDecimal(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBigDecimalByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBigDecimal(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBigDecimalByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBigDecimal(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBigDecimalByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBigDecimal(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBytesByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBytesByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBytesByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBytesByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDateByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getDate(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDateByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getDate(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDateByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getDate(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getDateByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getDate(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimeByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getTime(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimeByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getTime(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimeByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getTime(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimeByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getTime(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimestampByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getTimestamp(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimestampByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getTimestamp(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimestampByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getTimestamp(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getTimestampByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getTimestamp(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getAsciiStreamByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getAsciiStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getAsciiStreamByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getAsciiStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getAsciiStreamByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getAsciiStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getAsciiStreamByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getAsciiStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getUnicodeStreamByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getUnicodeStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getUnicodeStreamByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getUnicodeStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getUnicodeStreamByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getUnicodeStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getUnicodeStreamByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getUnicodeStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBinaryStreamByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBinaryStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBinaryStreamByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBinaryStream(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBinaryStreamByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getBinaryStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getBinaryStreamByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getBinaryStream(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getObjectByNameThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getObject(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getObjectByNameThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getObject(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getObjectByIndexThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getObject(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void getObjectByIndexThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.getObject(TEST_VALUE_INDEX_JDBC); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void findColumnThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.findColumn(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void findColumnThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.findColumn(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testGetRowThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.findColumn(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testGetRowThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.findColumn(TEST_VALUE_NAME); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testRowUpdatedThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.rowUpdated(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testRowInsertedThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.rowInserted(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testRowDeletedThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.rowDeleted(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testGetStatementThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.getStatement(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testWasNullThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.wasNull(); + + //THEN + //exception + } + + @Test(expectedExceptions = SQLException.class) + void testWasNullThrowsExceptionIfCalledWithInvalidCursor() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSetWithInvalidCursor(); + + //WHEN + resultSet.wasNull(); + + //THEN + //exception + } + + @Test + void testWasNullIsFalse() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSet(); + resultSet.next(); + resultSet.getString(1); + + //WHEN + final boolean wasNull = resultSet.wasNull(); + + //THEN + assertFalse(wasNull); + } + + @Test + void testWasNullIsTrue() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSet(); + resultSet.next(); + resultSet.getString(2); + + //WHEN + final boolean wasNull = resultSet.wasNull(); + + //THEN + assertTrue(wasNull); + } + + @Test + void testGetStatement() throws SQLException { + + //GIVEN + final JdbcStatement expectedStatement = mock(JdbcStatement.class); + final JdbcResultSet resultSet = new JdbcResultSet(null, expectedStatement); + + //WHEN + final Statement statement = resultSet.getStatement(); + + //THEN + assertEquals(statement, expectedStatement); + } + + @Test + void testFindColumn() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateResultSet(); + resultSet.next(); + + //WHEN + final int column = resultSet.findColumn(TEST_VALUE_NAME); + + //THEN + assertEquals(column, TEST_VALUE_INDEX_JDBC); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcResultSet.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcResultSet.class); + } + + private JdbcResultSet generateClosedResultSet() throws SQLException { + final JdbcResultSet jdbcResultSet = new JdbcResultSet(generateTestDataSet(), null); + jdbcResultSet.next(); + jdbcResultSet.close(); + return jdbcResultSet; + } + + private JdbcResultSet generateResultSetWithInvalidCursor() { + final DataSet dataSet = new DataSet(); + dataSet.getRows().add(null); + return new JdbcResultSet(dataSet, null); + } + + private JdbcResultSet generateResultSet(final Object testValue) throws SQLException { + return new JdbcResultSet(generateTestDataSet(testValue), null); + } + + private JdbcResultSet generateResultSet() throws SQLException { + return new JdbcResultSet(generateTestDataSet(), null); + } + + 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/driver/src/test/java/com/consol/citrus/db/driver/JdbcStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcStatementTest.java index 0aa83b3..859fcdd 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcStatementTest.java @@ -16,7 +16,12 @@ package com.consol.citrus.db.driver; -import org.apache.http.*; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHeaders; +import org.apache.http.HttpResponse; +import org.apache.http.StatusLine; import org.apache.http.client.HttpClient; import org.apache.http.message.BasicHeader; import org.mockito.Mockito; @@ -29,7 +34,9 @@ import java.sql.SQLException; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; @@ -241,4 +248,14 @@ public void testCloseIoExceptionIsWrappedInSqlException() throws Exception{ //THEN //Exception is thrown } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcStatement.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcStatement.class); + } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java b/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java new file mode 100644 index 0000000..f036d34 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java @@ -0,0 +1,136 @@ +package com.consol.citrus.db.driver.data; + +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.commons.beanutils.ConvertUtils; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.util.List; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.mockito.ArgumentMatchers.eq; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyStatic; +import static org.testng.Assert.assertEquals; + +@PrepareForTest(ConvertUtils.class) +public class RowTest extends PowerMockTestCase { + + private Row row ; + private final String COLUMN_1 = "col1"; + private final String COLUMN_2 = "col2"; + private final String COLUMN_3 = "col3"; + + private final Object VALUE_1 = 42; + private final Object VALUE_2 = "84"; + + @BeforeMethod + public void setUp(){ + spy(ConvertUtils.class); + row = generateRow(); + } + + @Test + public void testGetColumns(){ + + //WHEN + final List columns = row.getColumns(); + + //THEN + assertEquals(columns.get(0), COLUMN_1); + assertEquals(columns.get(1), COLUMN_2); + assertEquals(columns.get(2), COLUMN_3); + } + + @Test + public void testGetValueByColumnName(){ + + //WHEN + final Object value = row.getValue(COLUMN_1); + + //THEN + assertEquals(value, VALUE_1); + + } + + @Test + public void testGetValueByColumnNameWithConversion(){ + + //WHEN + final Object value = row.getValue(COLUMN_2, int.class); + + //THEN + assertEquals(value, Integer.valueOf((String) VALUE_2)); + verifyConversion(VALUE_2, int.class); + + } + + @Test + public void testGetValueByColumnIndex(){ + + //WHEN + final Object value = row.getValue(1); + + //THEN + assertEquals(value, VALUE_2); + + } + + @Test + public void testGetValueByColumnIndexWithConversion(){ + + //WHEN + final Object value = row.getValue(1, double.class); + + //THEN + assertEquals(value, Double.valueOf((String) VALUE_2)); + verifyConversion(VALUE_2, double.class); + + } + + @Test + public void getGetValues(){ + + //WHEN + final Map values = row.getValues(); + + //THEN + assertEquals(values, generateRowValues()); + + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(Row.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(Row.class); + } + + private Row generateRow() { + final SortedMap values = generateRowValues(); + final Row row = new Row(); + row.setValues(values); + return row; + } + + private SortedMap generateRowValues() { + final SortedMap values = new TreeMap<>(); + values.put(COLUMN_1, VALUE_1); + values.put(COLUMN_2, VALUE_2); + values.put(COLUMN_3, null); + return values; + } + + private void verifyConversion(final Object toBeConverted, final Class clazz) { + verifyStatic(ConvertUtils.class); + ConvertUtils.convert(eq(toBeConverted), eq(clazz)); + } +} \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/dataset/DataSetTest.java b/driver/src/test/java/com/consol/citrus/db/driver/dataset/DataSetTest.java new file mode 100644 index 0000000..2f469c7 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/dataset/DataSetTest.java @@ -0,0 +1,35 @@ +package com.consol.citrus.db.driver.dataset; + +import com.consol.citrus.db.driver.data.Row; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.testng.annotations.Test; + +import java.sql.SQLException; + +import static org.testng.Assert.assertNull; + +public class DataSetTest { + + private DataSet dataSet = new DataSet(); + + @Test + public void emptyDataSetReturnsNullOnGetNextRow() throws SQLException { + + //WHEN + final Row nextRow = dataSet.getNextRow(); + + //THEN + assertNull(nextRow); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(DataSet.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(DataSet.class); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index b141557..bbb4133 100644 --- a/pom.xml +++ b/pom.xml @@ -1,13 +1,11 @@ - + 4.0.0 com.consol.citrus citrus-db ${project.artifactId} - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT pom Citrus JDBC database simulator http://www.citrusframework.org @@ -27,9 +25,22 @@ 4.5.7 4.4.11 2.8.0 - 1.7.9 + + 1.9.9 6.14.3 - 2.8.47 + + 2.24.0 + + 1.9.3 + + 2.6 + + 1.4.5 + + 3.1.4 + + + 2.0.0 1.11.1 @@ -39,7 +50,11 @@ 3.1.1 2.20.1 1.6 + 2.5.3 0.12 + 3.1.1 + 3.0.0-M1 + 3.1.1 2.7 @@ -207,6 +222,13 @@ ${asciidoctor.version} + + + commons-beanutils + commons-beanutils + ${apache.commons.beanutils.version} + + org.testng @@ -226,6 +248,42 @@ ${logback.classic.version} test + + com.jparams + to-string-verifier + ${to.string.verifier.version} + test + + + nl.jqno.equalsverifier + equalsverifier + ${equals.verifier.version} + test + + + org.powermock + powermock-module-testng + ${powermock.version} + + + org.testng + testng + + + test + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + test + + + commons-io + commons-io + ${apache.commons.io.version} + test + @@ -247,6 +305,31 @@ logback-classic test + + com.jparams + to-string-verifier + test + + + nl.jqno.equalsverifier + equalsverifier + test + + + org.powermock + powermock-module-testng + test + + + org.powermock + powermock-api-mockito2 + test + + + commons-io + commons-io + test + @@ -260,6 +343,7 @@ org.apache.maven.plugins maven-jar-plugin + ${maven.jar.plugin.version} true @@ -285,6 +369,16 @@ + + org.apache.maven.plugins + maven-deploy-plugin + ${maven.deploy.plugin.version} + + + org.apache.maven.plugins + maven-assembly-plugin + ${maven.assembly.plugin.version} + org.apache.maven.plugins @@ -363,7 +457,7 @@ true - christoph.deppisch@consol.de + dev@citrusframework.org ${skip.gpg} --allow-weak-digest-algos @@ -382,6 +476,12 @@ false + + + org.apache.maven.plugins + maven-release-plugin + ${maven.release.plugin.version} + @@ -401,6 +501,10 @@ org.apache.maven.plugins maven-failsafe-plugin + + org.apache.maven.plugins + maven-release-plugin + diff --git a/server/pom.xml b/server/pom.xml index ca52b39..3302056 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -1,13 +1,11 @@ - + 4.0.0 com.consol.citrus citrus-db - 0.1.5-SNAPSHOT + 0.2.0-SNAPSHOT ../pom.xml diff --git a/server/src/main/java/com/consol/citrus/db/server/controller/AbstractJdbcController.java b/server/src/main/java/com/consol/citrus/db/server/controller/AbstractJdbcController.java index e53fb2f..f087404 100644 --- a/server/src/main/java/com/consol/citrus/db/server/controller/AbstractJdbcController.java +++ b/server/src/main/java/com/consol/citrus/db/server/controller/AbstractJdbcController.java @@ -21,7 +21,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.sql.SQLException; import java.util.Map; import java.util.stream.Collectors; @@ -88,12 +87,8 @@ public DataSet executeQuery(final String sql) throws JdbcServerException { log.info("EXECUTE QUERY: " + sql); final DataSet dataSet = handleQuery(sql); - try { - if(log.isDebugEnabled()){ - log.debug(String.format("RESULT SET with %s rows", dataSet.getRows().size())); - } - } catch (final SQLException e) { - throw new JdbcServerException("Failed to access dataSet", e); + if(log.isDebugEnabled()){ + log.debug(String.format("RESULT SET with %s rows", dataSet.getRows().size())); } log.info("QUERY EXECUTION SUCCESSFUL"); @@ -105,12 +100,8 @@ public DataSet executeStatement(final String sql) throws JdbcServerException { log.info("EXECUTE STATEMENT: " + sql); final DataSet dataSet = handleExecute(sql); - try { - if(log.isDebugEnabled()){ - log.debug(String.format("RESULT SET with %s rows", dataSet.getRows().size())); - } - } catch (final SQLException e) { - throw new JdbcServerException("Failed to access dataSet", e); + if(log.isDebugEnabled()){ + log.debug(String.format("RESULT SET with %s rows", dataSet.getRows().size())); } log.info("STATEMENT EXECUTION SUCCESSFUL"); diff --git a/sonar-project.properties b/sonar-project.properties index 273f2a5..180ca2e 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -15,4 +15,5 @@ # -sonar.projectKey=citrusframework:citrus-db \ No newline at end of file +sonar.projectKey=citrusframework:citrus-db +sonar.exclusions=demo/** \ No newline at end of file