From 1ecfe46a816e33415272b8832a90f400347a53fc Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Wed, 30 Jan 2019 13:18:27 +0100 Subject: [PATCH 01/27] (#29) Added maven release plugin --- pom.xml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pom.xml b/pom.xml index 2456269..f4917d2 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,7 @@ 3.1.1 2.20.1 1.6 + 2.5.3 0.12 @@ -382,6 +383,12 @@ false + + + org.apache.maven.plugins + maven-release-plugin + ${maven.release.plugin.version} + @@ -401,6 +408,10 @@ org.apache.maven.plugins maven-failsafe-plugin + + org.apache.maven.plugins + maven-release-plugin + From d05243cdf2c0c3e061a793254f9093f38d4496f8 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Wed, 30 Jan 2019 13:19:53 +0100 Subject: [PATCH 02/27] (#29) Set version to 0.2.0-SNAPSHOT --- agent/pom.xml | 6 ++---- demo/pom.xml | 5 ++--- docs/pom.xml | 11 +++++------ driver/pom.xml | 6 ++---- pom.xml | 6 ++---- server/pom.xml | 6 ++---- 6 files changed, 15 insertions(+), 25 deletions(-) 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..a1926b3 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 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..a7c71b0 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 diff --git a/pom.xml b/pom.xml index f4917d2..24681c1 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 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 From d7472e0c18469aaac0a851d705bfece2b974b40e Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Wed, 30 Jan 2019 14:14:15 +0100 Subject: [PATCH 03/27] (#29) Added end-2-end test for callable statements with outParameter --- demo/pom.xml | 1 + .../demo/controller/CityController.java | 50 ++++++++++++++++--- .../consol/citrus/demo/DemoApplicationIT.java | 14 ++++++ .../consol/citrus/demo/DemoJdbcServer.java | 10 +++- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/demo/pom.xml b/demo/pom.xml index a1926b3..e8802b5 100644 --- a/demo/pom.xml +++ b/demo/pom.xml @@ -115,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..b117d93 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,12 +4,18 @@ 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.StringUtils; import org.springframework.web.bind.annotation.*; +import java.sql.CallableStatement; +import java.sql.JDBCType; +import java.sql.Types; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -23,16 +29,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 +58,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 +71,30 @@ String addNewProjects (@RequestParam String names) { ); } } + + @GetMapping(path="/findId") + public @ResponseBody Iterable findCityByName(@RequestParam(defaultValue = "") final String name) { + if (StringUtils.hasText(name)) { + return jdbcTemplate.call(createCallableStatement(name), createParameters()) + .values() + .stream() + .map(o -> (City) o ) + .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..5209dfa 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..a6af59d 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(); } } From d9811f51d400eae0d11812120aa0cb09bd66f5ed Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 5 Feb 2019 10:20:07 +0100 Subject: [PATCH 04/27] (#29) Improved Demo and E2E-Test --- .../citrus/demo/controller/CityController.java | 13 ++++++++----- .../com/consol/citrus/demo/DemoApplicationIT.java | 2 +- .../java/com/consol/citrus/demo/DemoJdbcServer.java | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) 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 b117d93..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 @@ -8,15 +8,18 @@ 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; /** @@ -75,11 +78,11 @@ String addNewProjects (@RequestParam final String names) { @GetMapping(path="/findId") public @ResponseBody Iterable findCityByName(@RequestParam(defaultValue = "") final String name) { if (StringUtils.hasText(name)) { - return jdbcTemplate.call(createCallableStatement(name), createParameters()) - .values() - .stream() - .map(o -> (City) o ) - .collect(Collectors.toList()); + 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(); 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 5209dfa..ee12f67 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoApplicationIT.java @@ -92,6 +92,6 @@ public void testGetIdByCityName() { http(action -> action.client(httpClient) .receive() .response(HttpStatus.OK) - .payload("{ \"name\": \"Munich\", \"id\": 1 }")); + .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 a6af59d..3b8ae4d 100644 --- a/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java +++ b/demo/src/test/java/com/consol/citrus/demo/DemoJdbcServer.java @@ -43,7 +43,7 @@ public static void main(final String[] args) throws IOException, SQLException { dbServer.when() .statement() - .execute(Pattern.compile("CALL findCityByName \\(Munich, .*")) + .execute(Pattern.compile("CALL findCityByName\\(\\?,\\?\\) - \\(\\?,Munich\\)")) .thenReturn(new JsonDataSetProducer("[{ \"name\": \"Munich\", \"id\": 1 }]").produce()); From 1891b6e82e62cc59bc0334d543ab7629eab62274 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 5 Feb 2019 10:43:08 +0100 Subject: [PATCH 05/27] (#29) Added registerOutParameter and setter implementations --- .../db/driver/JdbcCallableStatement.java | 83 +++- .../db/driver/JdbcPreparedStatement.java | 20 +- .../db/driver/JdbcCallableStatementTest.java | 421 ++++++++++++++++++ .../db/driver/JdbcPreparedStatementTest.java | 14 +- 4 files changed, 499 insertions(+), 39 deletions(-) create mode 100644 driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java 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..1aadf07 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 @@ -31,6 +31,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,18 +40,18 @@ 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 @@ -175,22 +176,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 +201,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 @@ -607,4 +608,42 @@ 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, "?"); + } } 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..1029059 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,9 @@ 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.TreeMap; import java.util.stream.Collectors; /** @@ -48,7 +48,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); @@ -330,18 +330,18 @@ 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; } } 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..0b82d19 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -0,0 +1,421 @@ +package com.consol.citrus.db.driver; + +import org.apache.http.client.HttpClient; +import org.testng.annotations.Test; + +import java.math.BigDecimal; +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.mock; +import static org.testng.Assert.assertEquals; + +public class JdbcCallableStatementTest { + + private HttpClient httpClient = mock(HttpClient.class); + private String serverUrl = "localhost"; + private JdbcConnection jdbcConnection = mock(JdbcConnection.class); + + @Test + public void testRegisterOutParameter() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + final int index = 2; + + //WHEN + callableStatement.registerOutParameter(index, Types.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get("1"), "?"); + } + + @Test + public void testRegisterOutParameterWithScale() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + 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 JdbcCallableStatement callableStatement = generateCallableStatement(); + 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 + final JdbcCallableStatement callableStatement = generateCallableStatement(); + + //WHEN + callableStatement.registerOutParameter(1, JDBCType.INTEGER); + + //THEN + assertEquals(callableStatement.getParameters().get("0"), "?"); + } + + @Test + public void testRegisterOutParameterWithSqlTypeAndScale() { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + + //WHEN + callableStatement.registerOutParameter(1, JDBCType.INTEGER, 2); + + //THEN + assertEquals(callableStatement.getParameters().get("0"), "?"); + } + + @Test + public void testRegisterOutParameterWithSqlTypeAndTypeName() { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + + //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); + } + + 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/JdbcPreparedStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcPreparedStatementTest.java index e0e04b7..1e97848 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 @@ -26,7 +26,7 @@ public void setUp() { @Test - public void testSetParameter() throws Exception { + public void testSetParameter() { //GIVEN @@ -34,11 +34,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 +48,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 +63,6 @@ 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); } } \ No newline at end of file From d297108abc7092ba53c3ff5b2b62fdd1b6e237de Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 5 Feb 2019 14:07:15 +0100 Subject: [PATCH 06/27] (#29) Added sonar exclusion for demo project --- sonar-project.properties | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From b0dadc7a0b88ab3af9df0d18521c2fa17811f52d Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 5 Feb 2019 18:27:23 +0100 Subject: [PATCH 07/27] (#29) Added getter Tests --- .../db/driver/JdbcCallableStatement.java | 62 ++-- .../com/consol/citrus/db/driver/data/Row.java | 15 +- .../db/driver/JdbcCallableStatementTest.java | 327 +++++++++++++++++- 3 files changed, 369 insertions(+), 35 deletions(-) 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 1aadf07..49ba3b4 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 @@ -16,11 +16,13 @@ package com.consol.citrus.db.driver; +import com.consol.citrus.db.driver.data.Row; import org.apache.http.client.HttpClient; 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; @@ -37,9 +39,12 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Map; +import java.util.Objects; public class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement { + private Row resultRow; + public JdbcCallableStatement(final HttpClient httpClient, final String callableStatement, final String serverUrl, final JdbcConnection connection) { super(httpClient, callableStatement, serverUrl, connection); } @@ -61,47 +66,47 @@ public boolean wasNull() throws SQLException { @Override public String getString(final int parameterIndex) throws SQLException { - return null; + return (String) getData(parameterIndex); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return false; + return (Boolean) getData(parameterIndex); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return 0; + return (byte) getData(parameterIndex); } @Override public short getShort(final int parameterIndex) throws SQLException { - return 0; + return (short) getData(parameterIndex); } @Override public int getInt(final int parameterIndex) throws SQLException { - return 0; + return (int) getData(parameterIndex); } @Override public long getLong(final int parameterIndex) throws SQLException { - return 0; + return (long) getData(parameterIndex); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return 0; + return (float) getData(parameterIndex); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return 0; + return (double) getData(parameterIndex); } @Override public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throws SQLException { - return null; + return getBigDecimal(parameterIndex).setScale(scale, RoundingMode.HALF_UP); } @Override @@ -131,7 +136,7 @@ public Object getObject(final int parameterIndex) throws SQLException { @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return null; + return (BigDecimal) getData(parameterIndex); } @Override @@ -326,47 +331,47 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return null; + return (String) getData(parameterName); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return false; + return (boolean) getData(parameterName); } @Override public byte getByte(final String parameterName) throws SQLException { - return 0; + return (byte) getData(parameterName); } @Override public short getShort(final String parameterName) throws SQLException { - return 0; + return (short) getData(parameterName); } @Override public int getInt(final String parameterName) throws SQLException { - return 0; + return (int) getData(parameterName); } @Override public long getLong(final String parameterName) throws SQLException { - return 0; + return (long) getData(parameterName); } @Override public float getFloat(final String parameterName) throws SQLException { - return 0; + return (float) getData(parameterName); } @Override public double getDouble(final String parameterName) throws SQLException { - return 0; + return (double) getData(parameterName); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return new byte[0]; + return (byte[]) getData(parameterName); } @Override @@ -386,12 +391,12 @@ public Timestamp getTimestamp(final String parameterName) throws SQLException { @Override public Object getObject(final String parameterName) throws SQLException { - return null; + return getData(parameterName); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return null; + return (BigDecimal) getData(parameterName); } @Override @@ -646,4 +651,19 @@ private void setOutParameter(final int parameterIndex) { private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } + + private Object getData(final int parameterIndex) throws SQLException { + return getDataRow().getValue(parameterIndex - 1); + } + + private Object getData(final String parameterName) throws SQLException { + return getDataRow().getValues().get(parameterName); + } + + private Row getDataRow() throws SQLException { + if(Objects.isNull(resultRow)){ + resultRow = dataSet.getNextRow(); + } + return resultRow; + } } 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..fce8f3b 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 @@ -17,10 +17,11 @@ package com.consol.citrus.db.driver.data; import java.util.Arrays; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.SortedMap; +import java.util.TreeMap; /** * @author Christoph Deppisch @@ -28,7 +29,7 @@ public class Row { /** Row values with column name as key */ - private Map values = new LinkedHashMap<>(); + private SortedMap values = new TreeMap<>(); private Object lastValue; @@ -45,7 +46,7 @@ public List getColumns() { * @param columnName * @return */ - public Object getValue(String columnName) { + public Object getValue(final String columnName) { lastValue = values.get(columnName); return lastValue; } @@ -55,7 +56,7 @@ public Object getValue(String columnName) { * @param columnIndex * @return */ - public Object getValue(int columnIndex) { + public Object getValue(final int columnIndex) { lastValue = values.values().toArray()[columnIndex]; return lastValue; } @@ -74,15 +75,15 @@ public Map getValues() { * * @param values */ - public void setValues(Map values) { + public void setValues(final SortedMap values) { this.values = values; } @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); } 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 index 0b82d19..3c19f91 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -1,9 +1,13 @@ 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 org.apache.http.client.HttpClient; 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; @@ -12,21 +16,27 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; +import java.util.SortedMap; +import java.util.TreeMap; import static org.mockito.Mockito.mock; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; public class JdbcCallableStatementTest { private HttpClient httpClient = mock(HttpClient.class); private String serverUrl = "localhost"; private JdbcConnection jdbcConnection = mock(JdbcConnection.class); + private final int TEST_VALUE_INDEX = 2; + private final String TEST_VALUE_NAME = "col2"; + + private final JdbcCallableStatement callableStatement = generateCallableStatement(); @Test public void testRegisterOutParameter() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); final int index = 2; //WHEN @@ -40,7 +50,6 @@ public void testRegisterOutParameter() throws SQLException { public void testRegisterOutParameterWithScale() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); final int index = 2; //WHEN @@ -54,7 +63,6 @@ public void testRegisterOutParameterWithScale() throws SQLException { public void testRegisterOutParameterWithTypeName() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); final int index = 2; //WHEN @@ -110,7 +118,6 @@ public void testRegisterOutParameterByNameWithTypeName() throws SQLException { public void testRegisterOutParameterWithSqlType() { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); //WHEN callableStatement.registerOutParameter(1, JDBCType.INTEGER); @@ -123,7 +130,6 @@ public void testRegisterOutParameterWithSqlType() { public void testRegisterOutParameterWithSqlTypeAndScale() { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); //WHEN callableStatement.registerOutParameter(1, JDBCType.INTEGER, 2); @@ -136,8 +142,6 @@ public void testRegisterOutParameterWithSqlTypeAndScale() { public void testRegisterOutParameterWithSqlTypeAndTypeName() { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); - //WHEN callableStatement.registerOutParameter(1, JDBCType.INTEGER, "STRUCT"); @@ -409,13 +413,322 @@ public void testSetTimeStampWithName() throws SQLException { assertEquals(callableStatement.getParameters().get(parameterName), myTimeStamp); } + @Test + void testGetTwoValuesByIndex() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement("bar"); + + //WHEN + final String firstOutParameter = callableStatement.getString(1); + final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX); + + //THEN + assertEquals(firstOutParameter, "dummyValue"); + assertEquals(secondOutParameter, "bar"); + } + + @Test + void testGetStringByIndex() throws SQLException { + + //GIVEN + final String expectedString = "bar"; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); + + //WHEN + final String string = callableStatement.getString(TEST_VALUE_INDEX); + + //THEN + assertEquals(string, expectedString); + } + + @Test + void testGetBooleanByIndex() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(true); + + //WHEN + final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX); + + //THEN + assertTrue(aBoolean); + } + + @Test + void testGetByteByIndex() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); + + //WHEN + final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX); + + //THEN + assertEquals(aByte, expectedByte); + } + + @Test + void testGetShortByIndex() throws SQLException { + + //GIVEN + final short expectedShort = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); + + //WHEN + final short aShort = callableStatement.getShort(TEST_VALUE_INDEX); + + //THEN + assertEquals(aShort, expectedShort); + } + + @Test + void testGetIntByIndex() throws SQLException { + + //GIVEN + final int expectedInt = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); + + //WHEN + final int anInt = callableStatement.getInt(TEST_VALUE_INDEX); + + //THEN + assertEquals(anInt, expectedInt); + } + + @Test + void testGetLongByIndex() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); + + //WHEN + final long aLong = callableStatement.getLong(TEST_VALUE_INDEX); + + //THEN + assertEquals(aLong, expectedLong); + } + + @Test + void testGetFloatByIndex() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); + + //WHEN + final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX); + + //THEN + assertEquals(aFloat, expectedFloat); + } + + @Test + void testGetDoubleByIndex() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); + + //WHEN + final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX); + + //THEN + assertEquals(aDouble, expectedDouble); + } + + @Test + void testGetBigDecimalByIndexWithScale() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); + + //WHEN + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX, 2); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + } + + @Test + void testGetStringByName() throws SQLException { + + //GIVEN + final String expectedString = "bar"; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); + + //WHEN + final String string = callableStatement.getString(TEST_VALUE_NAME); + + //THEN + assertEquals(string, expectedString); + } + + @Test + void testGetBooleanByName() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(true); + + //WHEN + final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_NAME); + + //THEN + assertTrue(aBoolean); + } + + @Test + void testGetByteByName() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); + + //WHEN + final byte aByte = callableStatement.getByte(TEST_VALUE_NAME); + + //THEN + assertEquals(aByte, expectedByte); + } + + @Test + void testGetShortByName() throws SQLException { + + //GIVEN + final short expectedShort = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); + + //WHEN + final short aShort = callableStatement.getShort(TEST_VALUE_NAME); + + //THEN + assertEquals(aShort, expectedShort); + } + + @Test + void testGetIntByName() throws SQLException { + + //GIVEN + final int expectedInt = 42; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); + + //WHEN + final int anInt = callableStatement.getInt(TEST_VALUE_NAME); + + //THEN + assertEquals(anInt, expectedInt); + } + + @Test + void testGetLongByName() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); + + //WHEN + final long aLong = callableStatement.getLong(TEST_VALUE_NAME); + + //THEN + assertEquals(aLong, expectedLong); + } + + @Test + void testGetFloatByName() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); + + //WHEN + final float aFloat = callableStatement.getFloat(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedFloat); + } + + @Test + void testGetDoubleByName() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); + + //WHEN + final double aFloat = callableStatement.getDouble(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedDouble); + } + + @Test + void testGetBytesByName() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "Foobar".getBytes(); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); + + //WHEN + final byte[] bytes = callableStatement.getBytes(TEST_VALUE_NAME); + + //THEN + assertEquals(bytes, expectedBytes); + } + + @Test + void testGetObjectByName() throws SQLException { + + //GIVEN + final Object expectedObject = "Foobar".getBytes(); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); + + //WHEN + final Object object = callableStatement.getObject(TEST_VALUE_NAME); + + //THEN + assertEquals(object, expectedObject); + } + + @Test + void testGetBigDecimalByName() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); + + //WHEN + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME ); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal); + } + private JdbcCallableStatement generateCallableStatement() { final String statement = "CALL myFunction(?,?)"; return new JdbcCallableStatement(httpClient, statement, serverUrl, jdbcConnection); } + private JdbcCallableStatement generateCallableStatement(final Object testValue) throws SQLException { + final JdbcCallableStatement callableStatement = generateCallableStatement(); + callableStatement.dataSet = generateTestDataSet(testValue); + return callableStatement; + } + private JdbcCallableStatement generateCallableStatementWithParameter(final String parameterName) { final String statement = "CALL myFunction("+parameterName+",?)"; return new JdbcCallableStatement(httpClient,statement, serverUrl, jdbcConnection); } + + private DataSet generateTestDataSet(final Object testValue) throws SQLException { + final SortedMap testData = new TreeMap<>(); + testData.put("col1", "dummyValue"); + testData.put("col2", testValue); + + final Row row = new Row(); + row.setValues(testData); + return new DataSetBuilder().add(row).build(); + } } \ No newline at end of file From 5edee36fa7d9b79f28d8c70a628539fd0e47f9c3 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 5 Feb 2019 18:40:37 +0100 Subject: [PATCH 08/27] (#29) Fixed regression by changing map implementation --- .../src/main/java/com/consol/citrus/db/driver/data/Row.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 fce8f3b..c0c1e3c 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 @@ -17,11 +17,11 @@ package com.consol.citrus.db.driver.data; import java.util.Arrays; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.SortedMap; -import java.util.TreeMap; /** * @author Christoph Deppisch @@ -29,7 +29,7 @@ public class Row { /** Row values with column name as key */ - private SortedMap values = new TreeMap<>(); + private Map values = new LinkedHashMap<>(); private Object lastValue; From 5efc24d939aee054448393bf5f309926023f1dd2 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 13:42:54 +0100 Subject: [PATCH 09/27] (#29) Ensure that getNextRow throws SQLException instead of IndexOutOfBounds --- .../citrus/db/driver/dataset/DataSet.java | 6 +++++- .../citrus/db/driver/dataset/DataSetTest.java | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 driver/src/test/java/com/consol/citrus/db/driver/dataset/DataSetTest.java 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..645c527 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 @@ -46,7 +46,11 @@ public class DataSet { */ public Row getNextRow() throws SQLException { checkNotClosed(); - return rows.get(cursor.getAndIncrement()); + try{ + return rows.get(cursor.getAndIncrement()); + }catch (IndexOutOfBoundsException e){ + throw new SQLException("No further row in dataset", e); + } } /** 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..5990a8e --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/dataset/DataSetTest.java @@ -0,0 +1,21 @@ +package com.consol.citrus.db.driver.dataset; + +import org.testng.annotations.Test; + +import java.sql.SQLException; + +public class DataSetTest { + + private DataSet dataSet = new DataSet(); + + @Test(expectedExceptions = SQLException.class) + public void emptyDataSetThrowsExceptionOnGetNextRow() throws SQLException { + + //WHEN + dataSet.getNextRow(); + + //THEN + //exception + } + +} \ No newline at end of file From b0fe29d6f5a9c9dbc49c27bef292458f9d7c99e4 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 16:04:25 +0100 Subject: [PATCH 10/27] (#29) Added type conversion to get Parameters --- driver/pom.xml | 8 ++ .../db/driver/JdbcCallableStatement.java | 84 +++++++++----- .../citrus/db/driver/dataset/DataSet.java | 42 ++++--- .../db/driver/JdbcCallableStatementTest.java | 107 ++++++++++++++++-- .../citrus/db/driver/dataset/DataSetTest.java | 11 ++ pom.xml | 72 +++++++++++- 6 files changed, 275 insertions(+), 49 deletions(-) diff --git a/driver/pom.xml b/driver/pom.xml index a7c71b0..4069797 100644 --- a/driver/pom.xml +++ b/driver/pom.xml @@ -33,6 +33,10 @@ + + commons-beanutils + commons-beanutils + com.fasterxml.jackson.core @@ -81,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 49ba3b4..af6001e 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 @@ -17,6 +17,7 @@ package com.consol.citrus.db.driver; import com.consol.citrus.db.driver.data.Row; +import org.apache.commons.beanutils.ConvertUtils; import org.apache.http.client.HttpClient; import java.io.InputStream; @@ -45,7 +46,10 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements Call private Row resultRow; - public JdbcCallableStatement(final HttpClient httpClient, final String callableStatement, final String serverUrl, final JdbcConnection connection) { + public JdbcCallableStatement(final HttpClient httpClient, + final String callableStatement, + final String serverUrl, + final JdbcConnection connection) { super(httpClient, callableStatement, serverUrl, connection); } @@ -61,47 +65,47 @@ public void registerOutParameter(final int parameterIndex, final int sqlType, fi @Override public boolean wasNull() throws SQLException { - return false; + return Objects.isNull(getDataRow().getLastValue()); } @Override public String getString(final int parameterIndex) throws SQLException { - return (String) getData(parameterIndex); + return (String) getData(parameterIndex, String.class); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return (Boolean) getData(parameterIndex); + return (boolean) getData(parameterIndex, boolean.class); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return (byte) getData(parameterIndex); + return (byte) getData(parameterIndex, byte.class); } @Override public short getShort(final int parameterIndex) throws SQLException { - return (short) getData(parameterIndex); + return (short) getData(parameterIndex, short.class); } @Override public int getInt(final int parameterIndex) throws SQLException { - return (int) getData(parameterIndex); + return (int) getData(parameterIndex, int.class); } @Override public long getLong(final int parameterIndex) throws SQLException { - return (long) getData(parameterIndex); + return (long) getData(parameterIndex, long.class); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return (float) getData(parameterIndex); + return (float) getData(parameterIndex, float.class); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return (double) getData(parameterIndex); + return (double) getData(parameterIndex, double.class); } @Override @@ -136,7 +140,7 @@ public Object getObject(final int parameterIndex) throws SQLException { @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return (BigDecimal) getData(parameterIndex); + return (BigDecimal) getData(parameterIndex, BigDecimal.class); } @Override @@ -331,47 +335,47 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return (String) getData(parameterName); + return (String) getData(parameterName, String.class); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return (boolean) getData(parameterName); + return (boolean) getData(parameterName, boolean.class); } @Override public byte getByte(final String parameterName) throws SQLException { - return (byte) getData(parameterName); + return (byte) getData(parameterName, byte.class); } @Override public short getShort(final String parameterName) throws SQLException { - return (short) getData(parameterName); + return (short) getData(parameterName, short.class); } @Override public int getInt(final String parameterName) throws SQLException { - return (int) getData(parameterName); + return (int) getData(parameterName, int.class); } @Override public long getLong(final String parameterName) throws SQLException { - return (long) getData(parameterName); + return (long) getData(parameterName, long.class); } @Override public float getFloat(final String parameterName) throws SQLException { - return (float) getData(parameterName); + return (float) getData(parameterName, float.class); } @Override public double getDouble(final String parameterName) throws SQLException { - return (double) getData(parameterName); + return (double) getData(parameterName, double.class); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return (byte[]) getData(parameterName); + return (byte[]) getData(parameterName, byte[].class); } @Override @@ -391,12 +395,12 @@ public Timestamp getTimestamp(final String parameterName) throws SQLException { @Override public Object getObject(final String parameterName) throws SQLException { - return getData(parameterName); + return getData(parameterName, Object.class); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return (BigDecimal) getData(parameterName); + return (BigDecimal) getData(parameterName, BigDecimal.class); } @Override @@ -652,12 +656,18 @@ private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } - private Object getData(final int parameterIndex) throws SQLException { - return getDataRow().getValue(parameterIndex - 1); + private Object getData(final int parameterIndex, final Class clazz) throws SQLException { + final Object value = getDataRow().getValue(parameterIndex - 1); + return convertData(value, clazz); } - private Object getData(final String parameterName) throws SQLException { - return getDataRow().getValues().get(parameterName); + private Object getData(final String parameterName, final Class clazz) throws SQLException { + final Object value = getDataRow().getValue(parameterName); + return convertData(value, clazz); + } + + private Object convertData(final Object value, final Class clazz) { + return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz); } private Row getDataRow() throws SQLException { @@ -666,4 +676,26 @@ private Row getDataRow() throws SQLException { } return resultRow; } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof JdbcCallableStatement)) return false; + if (!super.equals(o)) return false; + final JdbcCallableStatement that = (JdbcCallableStatement) o; + return Objects.equals(resultRow, that.resultRow); + } + + @Override + public int hashCode() { + return Objects.hash(resultRow); + } + + @Override + public String toString() { + return "JdbcCallableStatement{" + + "resultRow=" + resultRow + + ", dataSet=" + dataSet + + '}'; + } } 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 645c527..fa956f8 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 @@ -41,21 +41,21 @@ public class DataSet { /** * Gets next row in this result set based on cursor position. - * @return - * @throws SQLException + * @return The next row of the dataset + * @throws SQLException In case the DataSet has been closed or no further rows are availabe */ public Row getNextRow() throws SQLException { checkNotClosed(); try{ return rows.get(cursor.getAndIncrement()); - }catch (IndexOutOfBoundsException e){ - throw new SQLException("No further row in dataset", e); + }catch (final IndexOutOfBoundsException cause){ + throw new SQLException("No further row in dataset", cause); } } /** * 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()); @@ -63,8 +63,8 @@ public List getColumns() { /** * Gets all rows in this dataset. - * @return - * @throws SQLException + * @return The rows of the Dataset + * @throws SQLException in case the DataSet has been closed */ public List getRows() throws SQLException { checkNotClosed(); @@ -73,7 +73,7 @@ public List getRows() throws SQLException { /** * Gets current row index. - * @return + * @return The current curser of the DataSet */ public int getCursor() { return cursor.get(); @@ -88,7 +88,7 @@ public void close() { /** * If closed throws new SQLException. - * @throws SQLException + * @throws SQLException In case the DataSet has been closed */ private void checkNotClosed() throws SQLException { if (closed) { @@ -98,19 +98,33 @@ private void checkNotClosed() throws SQLException { /** * Returns true if the data set is already closed, otherwise false. - * @return + * @return Whether the DataSet has been closed */ public boolean isClosed() { return closed; } @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; - DataSet dataSet = (DataSet) o; + if (!(o instanceof DataSet)) return false; + final DataSet dataSet = (DataSet) o; return closed == dataSet.closed && Objects.equals(rows, dataSet.rows) && - Objects.equals(cursor.get(), dataSet.cursor.get()); + Objects.equals(cursor, dataSet.cursor); + } + + @Override + public int hashCode() { + return Objects.hash(rows, closed, cursor); + } + + @Override + public String toString() { + return "DataSet{" + + "rows=" + rows + + ", closed=" + closed + + ", cursor=" + cursor + + '}'; } } 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 index 3c19f91..3b0877d 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -3,7 +3,14 @@ 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.http.client.HttpClient; +import org.powermock.api.mockito.PowerMockito; +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.math.BigDecimal; @@ -19,20 +26,29 @@ import java.util.SortedMap; import java.util.TreeMap; -import static org.mockito.Mockito.mock; +import static org.mockito.ArgumentMatchers.eq; +import static org.powermock.api.mockito.PowerMockito.mock; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; -public class JdbcCallableStatementTest { +@PrepareForTest(ConvertUtils.class) +public class JdbcCallableStatementTest extends PowerMockTestCase { - private HttpClient httpClient = mock(HttpClient.class); + private HttpClient httpClient; private String serverUrl = "localhost"; - private JdbcConnection jdbcConnection = mock(JdbcConnection.class); + private JdbcConnection jdbcConnection; private final int TEST_VALUE_INDEX = 2; private final String TEST_VALUE_NAME = "col2"; - private final JdbcCallableStatement callableStatement = generateCallableStatement(); + @BeforeMethod + public void setup(){ + httpClient = mock(HttpClient.class); + jdbcConnection = mock(JdbcConnection.class); + PowerMockito.spy(ConvertUtils.class); + } + @Test public void testRegisterOutParameter() throws SQLException { @@ -440,19 +456,22 @@ void testGetStringByIndex() throws SQLException { //THEN assertEquals(string, expectedString); + verifyConversion(expectedString, String.class); } @Test void testGetBooleanByIndex() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(true); + final boolean expectedBoolean = true; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); //WHEN final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX); //THEN assertTrue(aBoolean); + verifyConversion(expectedBoolean, boolean.class); } @Test @@ -467,6 +486,7 @@ void testGetByteByIndex() throws SQLException { //THEN assertEquals(aByte, expectedByte); + verifyConversion(expectedByte, byte.class); } @Test @@ -481,6 +501,7 @@ void testGetShortByIndex() throws SQLException { //THEN assertEquals(aShort, expectedShort); + verifyConversion(expectedShort, short.class); } @Test @@ -495,6 +516,7 @@ void testGetIntByIndex() throws SQLException { //THEN assertEquals(anInt, expectedInt); + verifyConversion(expectedInt, int.class); } @Test @@ -509,6 +531,7 @@ void testGetLongByIndex() throws SQLException { //THEN assertEquals(aLong, expectedLong); + verifyConversion(expectedLong, long.class); } @Test @@ -523,6 +546,7 @@ void testGetFloatByIndex() throws SQLException { //THEN assertEquals(aFloat, expectedFloat); + verifyConversion(expectedFloat, float.class); } @Test @@ -537,6 +561,7 @@ void testGetDoubleByIndex() throws SQLException { //THEN assertEquals(aDouble, expectedDouble); + verifyConversion(expectedDouble, double.class); } @Test @@ -551,6 +576,7 @@ void testGetBigDecimalByIndexWithScale() throws SQLException { //THEN assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + verifyConversion(expectedBigDecimal, BigDecimal.class); } @Test @@ -565,19 +591,22 @@ void testGetStringByName() throws SQLException { //THEN assertEquals(string, expectedString); + verifyConversion(expectedString, String.class); } @Test void testGetBooleanByName() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(true); + final boolean expectedBoolean = true; + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); //WHEN final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_NAME); //THEN assertTrue(aBoolean); + verifyConversion(expectedBoolean, boolean.class); } @Test @@ -592,6 +621,7 @@ void testGetByteByName() throws SQLException { //THEN assertEquals(aByte, expectedByte); + verifyConversion(expectedByte, byte.class); } @Test @@ -606,6 +636,7 @@ void testGetShortByName() throws SQLException { //THEN assertEquals(aShort, expectedShort); + verifyConversion(expectedShort, short.class); } @Test @@ -620,6 +651,7 @@ void testGetIntByName() throws SQLException { //THEN assertEquals(anInt, expectedInt); + verifyConversion(expectedInt, int.class); } @Test @@ -634,6 +666,7 @@ void testGetLongByName() throws SQLException { //THEN assertEquals(aLong, expectedLong); + verifyConversion(expectedLong, long.class); } @Test @@ -648,6 +681,7 @@ void testGetFloatByName() throws SQLException { //THEN assertEquals(aFloat, expectedFloat); + verifyConversion(expectedFloat, float.class); } @Test @@ -662,6 +696,7 @@ void testGetDoubleByName() throws SQLException { //THEN assertEquals(aFloat, expectedDouble); + verifyConversion(expectedDouble, double.class); } @Test @@ -676,6 +711,7 @@ void testGetBytesByName() throws SQLException { //THEN assertEquals(bytes, expectedBytes); + verifyConversion(expectedBytes, byte[].class); } @Test @@ -690,6 +726,7 @@ void testGetObjectByName() throws SQLException { //THEN assertEquals(object, expectedObject); + verifyConversion(expectedObject, Object.class); } @Test @@ -704,6 +741,57 @@ void testGetBigDecimalByName() throws SQLException { //THEN assertEquals(aBigDecimal, expectedBigDecimal); + verifyConversion(aBigDecimal, BigDecimal.class); + } + + @Test(expectedExceptions = SQLException.class) + void testWasNullThrowsException() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(); + + //WHEN + callableStatement.wasNull(); + + //THEN + //exception + } + + @Test + void testWasNullIsFalse() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); + + //WHEN + callableStatement.getBigDecimal(TEST_VALUE_NAME); + + //THEN + assertFalse(callableStatement.wasNull()); + } + + @Test + void testWasNullIsTrue() throws SQLException { + + //GIVEN + final JdbcCallableStatement callableStatement = generateCallableStatement(null); + + //WHEN + callableStatement.getBigDecimal(TEST_VALUE_NAME); + + //THEN + assertTrue(callableStatement.wasNull()); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcCallableStatement.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcCallableStatement.class); } private JdbcCallableStatement generateCallableStatement() { @@ -731,4 +819,9 @@ private DataSet generateTestDataSet(final Object testValue) throws SQLException row.setValues(testData); return new DataSetBuilder().add(row).build(); } + + private void verifyConversion(final Object expectedValue, final Class clazz) { + PowerMockito.verifyStatic(ConvertUtils.class); + ConvertUtils.convert(eq(expectedValue), 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 index 5990a8e..7683727 100644 --- 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 @@ -1,5 +1,7 @@ package com.consol.citrus.db.driver.dataset; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; import org.testng.annotations.Test; import java.sql.SQLException; @@ -18,4 +20,13 @@ public void emptyDataSetThrowsExceptionOnGetNextRow() throws SQLException { //exception } + @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 24681c1..73cf1a3 100644 --- a/pom.xml +++ b/pom.xml @@ -25,9 +25,20 @@ 4.5.7 4.4.11 2.7.1 - 1.7.9 + + 1.9.9 6.14.3 - 2.8.47 + + 2.24.0 + + 1.9.3 + + 1.4.5 + + 3.1.4 + + + 2.0.0 1.11.1 @@ -206,6 +217,13 @@ ${asciidoctor.version} + + + commons-beanutils + commons-beanutils + ${apache.commons.beanutils.version} + + org.testng @@ -225,6 +243,36 @@ ${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 + @@ -246,6 +294,26 @@ 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 + From f1f2b647089eccc6b5f5af5e454b85477d2588e0 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 16:07:08 +0100 Subject: [PATCH 11/27] (#29) Added hashCode, equals and toString --- .../db/driver/JdbcPreparedStatement.java | 25 +++++++++++++++++++ .../db/driver/JdbcPreparedStatementTest.java | 12 +++++++++ 2 files changed, 37 insertions(+) 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 1029059..a0df421 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 @@ -39,6 +39,7 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Map; +import java.util.Objects; import java.util.TreeMap; import java.util.stream.Collectors; @@ -344,5 +345,29 @@ private String composeStatement() { 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 + + ", dataSet=" + dataSet + + '}'; + } } 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 1e97848..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; @@ -65,4 +67,14 @@ public void testSetParameterOverwritesValue() { Assert.assertEquals(jdbcPreparedStatement.getParameters().size(), 1); 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 From 4638edf9fcd3fa69c0c1ad4a30b3b988a1c4d860 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 16:25:00 +0100 Subject: [PATCH 12/27] (#29) Added further getter implementations --- .../db/driver/JdbcCallableStatement.java | 16 +-- .../db/driver/JdbcCallableStatementTest.java | 120 ++++++++++++++++++ 2 files changed, 128 insertions(+), 8 deletions(-) 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 af6001e..b86f85d 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 @@ -115,27 +115,27 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - return new byte[0]; + return (byte[]) getData(parameterIndex, byte[].class); } @Override public Date getDate(final int parameterIndex) throws SQLException { - return null; + return (Date) getData(parameterIndex, Date.class); } @Override public Time getTime(final int parameterIndex) throws SQLException { - return null; + return (Time) getData(parameterIndex, Time.class); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - return null; + return (Timestamp) getData(parameterIndex, Timestamp.class); } @Override public Object getObject(final int parameterIndex) throws SQLException { - return null; + return getData(parameterIndex, Object.class); } @Override @@ -380,17 +380,17 @@ public byte[] getBytes(final String parameterName) throws SQLException { @Override public Date getDate(final String parameterName) throws SQLException { - return null; + return (Date) getData(parameterName, Date.class); } @Override public Time getTime(final String parameterName) throws SQLException { - return null; + return (Time) getData(parameterName, Time.class); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - return null; + return (Timestamp) getData(parameterName, Timestamp.class); } @Override 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 index 3b0877d..c1e6d3b 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -489,6 +489,21 @@ void testGetByteByIndex() throws SQLException { verifyConversion(expectedByte, byte.class); } + @Test + void testGetBytesByIndex() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); + + //WHEN + final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX); + + //THEN + assertEquals(aByte, expectedBytes); + verifyConversion(expectedBytes, byte[].class); + } + @Test void testGetShortByIndex() throws SQLException { @@ -784,6 +799,111 @@ void testWasNullIsTrue() throws SQLException { assertTrue(callableStatement.wasNull()); } + @Test + void testGetDateByIndex() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); + + //WHEN + final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX); + + //THEN + assertEquals(aDate, expectedDate); + verifyConversion(expectedDate, Date.class); + } + + @Test + void testGetTimeByIndex() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); + + //WHEN + final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX); + + //THEN + assertEquals(aTime, expectedTime); + verifyConversion(expectedTime, Time.class); + } + + @Test + void testGetTimestampByIndex() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); + + //WHEN + final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verifyConversion(expectedTimestamp, Timestamp.class); + } + + @Test + void testGetObjectStampByIndex() throws SQLException { + + //GIVEN + final Object expectedObject = new Timestamp(619912812345L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); + + //WHEN + final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX); + + //THEN + assertEquals(anObject, expectedObject); + verifyConversion(expectedObject, Object.class); + } + + @Test + void testGetDateByName() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); + + //WHEN + final Date aDate = callableStatement.getDate(TEST_VALUE_NAME); + + //THEN + assertEquals(aDate, expectedDate); + verifyConversion(expectedDate, Date.class); + } + + @Test + void testGetTimeByName() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); + + //WHEN + final Time aTime = callableStatement.getTime(TEST_VALUE_NAME); + + //THEN + assertEquals(aTime, expectedTime); + verifyConversion(expectedTime, Time.class); + } + + @Test + void testGetTimestampByName() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); + + //WHEN + final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_NAME); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verifyConversion(expectedTimestamp, Timestamp.class); + } + @Test public void testToString(){ ToStringVerifier.forClass(JdbcCallableStatement.class); From e210cf47996e1eae0e2238e230fa60636fed4730 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 17:08:28 +0100 Subject: [PATCH 13/27] (#29) Added some equals and hash code implementations. Removed atomic integer from equals in DataSet --- .../citrus/db/driver/JdbcConnection.java | 25 ++++++++++++++++++ .../citrus/db/driver/JdbcStatement.java | 26 +++++++++++++++---- .../citrus/db/driver/dataset/DataSet.java | 3 +-- .../citrus/db/driver/JdbcConnectionTest.java | 12 +++++++++ .../citrus/db/driver/JdbcStatementTest.java | 21 +++++++++++++-- 5 files changed, 78 insertions(+), 9 deletions(-) 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/JdbcStatement.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcStatement.java index fddb590..9247b3f 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,12 +28,12 @@ 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; @@ -377,10 +377,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(connection, that.connection) && Objects.equals(dataSet, that.dataSet); } + + @Override + public int hashCode() { + return Objects.hash(httpClient, serverUrl, connection, dataSet); + } + + @Override + public String toString() { + return "JdbcStatement{" + + "httpClient=" + httpClient + + ", serverUrl='" + serverUrl + '\'' + + ", connection=" + connection + + ", dataSet=" + dataSet + + '}'; + } } 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 fa956f8..269e5f2 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 @@ -110,8 +110,7 @@ public boolean equals(final Object o) { if (!(o instanceof DataSet)) return false; final DataSet dataSet = (DataSet) o; return closed == dataSet.closed && - Objects.equals(rows, dataSet.rows) && - Objects.equals(cursor, dataSet.cursor); + Objects.equals(rows, dataSet.rows); } @Override 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/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 From 580a16ba96a961b28c5b60bbdb72cc97cd345379 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Thu, 7 Feb 2019 17:53:01 +0100 Subject: [PATCH 14/27] (#29) Fixed contract clash between Dataset JdbcCallableStatement --- .../db/driver/JdbcCallableStatement.java | 6 +++- .../citrus/db/driver/JdbcResultSet.java | 31 ++++++++++++------- .../citrus/db/driver/dataset/DataSet.java | 12 ++++--- .../db/driver/JdbcCallableStatementTest.java | 8 ++--- .../citrus/db/driver/dataset/DataSetTest.java | 11 ++++--- 5 files changed, 43 insertions(+), 25 deletions(-) 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 b86f85d..5057ff2 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 @@ -65,7 +65,11 @@ public void registerOutParameter(final int parameterIndex, final int sqlType, fi @Override public boolean wasNull() throws SQLException { - return Objects.isNull(getDataRow().getLastValue()); + final Row dataRow = getDataRow(); + if(dataRow == null){ + return false; + } + return Objects.isNull(dataRow.getLastValue()); } @Override 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..b6c8bc6 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,13 +16,29 @@ 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.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; @@ -48,14 +64,7 @@ public JdbcResultSet(DataSet dataSet, JdbcStatement statement) throws SQLExcepti @Override public boolean next() throws SQLException { - try { - row = dataSet.getNextRow(); - } catch(SQLException ex) { - throw ex; - } catch(Exception ex) { - return false; - } - + row = dataSet.getNextRow(); return row != null; } 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 269e5f2..f5be771 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 @@ -42,15 +42,17 @@ public class DataSet { /** * Gets next row in this result set based on cursor position. * @return The next row of the dataset - * @throws SQLException In case the DataSet has been closed or no further rows are availabe + * @throws SQLException In case the DataSet has been closed or no further rows are available */ public Row getNextRow() throws SQLException { checkNotClosed(); - try{ - return rows.get(cursor.getAndIncrement()); - }catch (final IndexOutOfBoundsException cause){ - throw new SQLException("No further row in dataset", cause); + + final int index = cursor.getAndIncrement(); + if(rows.size() <= index){ + return null; } + + return rows.get(index); } /** 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 index c1e6d3b..5f10797 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -759,17 +759,17 @@ void testGetBigDecimalByName() throws SQLException { verifyConversion(aBigDecimal, BigDecimal.class); } - @Test(expectedExceptions = SQLException.class) - void testWasNullThrowsException() throws SQLException { + @Test + void testWasNullReturnsFalseAsDefault() throws SQLException { //GIVEN final JdbcCallableStatement callableStatement = generateCallableStatement(); //WHEN - callableStatement.wasNull(); + final boolean wasNull = callableStatement.wasNull(); //THEN - //exception + assertFalse(wasNull); } @Test 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 index 7683727..2f469c7 100644 --- 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 @@ -1,23 +1,26 @@ 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(expectedExceptions = SQLException.class) - public void emptyDataSetThrowsExceptionOnGetNextRow() throws SQLException { + @Test + public void emptyDataSetReturnsNullOnGetNextRow() throws SQLException { //WHEN - dataSet.getNextRow(); + final Row nextRow = dataSet.getNextRow(); //THEN - //exception + assertNull(nextRow); } @Test From 9033831fa34ec221711c8920122b86de1cddb2d8 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 8 Feb 2019 10:16:14 +0100 Subject: [PATCH 15/27] (#29) Fixed plugin versions --- pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pom.xml b/pom.xml index 73cf1a3..3c67b6f 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,8 @@ 1.6 2.5.3 0.12 + 3.1.1 + 3.0.0-M1 2.7 @@ -327,6 +329,7 @@ org.apache.maven.plugins maven-jar-plugin + ${maven.jar.plugin.version} true @@ -352,6 +355,11 @@ + + org.apache.maven.plugins + maven-deploy-plugin + ${maven.deploy.plugin.version} + org.apache.maven.plugins From 43317c54aa5918b0b30f84afddd8d19f20085ae3 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 8 Feb 2019 10:40:31 +0100 Subject: [PATCH 16/27] (#29) Moved responsibility for type conversions to the Row class. Index mapping stays in JdbcCallableStatement --- .../db/driver/JdbcCallableStatement.java | 71 ++++----- .../com/consol/citrus/db/driver/data/Row.java | 51 +++++-- .../db/driver/JdbcCallableStatementTest.java | 116 +++++++-------- .../consol/citrus/db/driver/data/RowTest.java | 136 ++++++++++++++++++ 4 files changed, 258 insertions(+), 116 deletions(-) create mode 100644 driver/src/test/java/com/consol/citrus/db/driver/data/RowTest.java 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 5057ff2..b110584 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 @@ -17,7 +17,6 @@ package com.consol.citrus.db.driver; import com.consol.citrus.db.driver.data.Row; -import org.apache.commons.beanutils.ConvertUtils; import org.apache.http.client.HttpClient; import java.io.InputStream; @@ -74,42 +73,42 @@ public boolean wasNull() throws SQLException { @Override public String getString(final int parameterIndex) throws SQLException { - return (String) getData(parameterIndex, String.class); + return (String) getDataRow().getValue(parameterIndex-1, String.class); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return (boolean) getData(parameterIndex, boolean.class); + return (boolean) getDataRow().getValue(parameterIndex-1, boolean.class); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return (byte) getData(parameterIndex, byte.class); + return (byte) getDataRow().getValue(parameterIndex-1, byte.class); } @Override public short getShort(final int parameterIndex) throws SQLException { - return (short) getData(parameterIndex, short.class); + return (short) getDataRow().getValue(parameterIndex-1, short.class); } @Override public int getInt(final int parameterIndex) throws SQLException { - return (int) getData(parameterIndex, int.class); + return (int) getDataRow().getValue(parameterIndex-1, int.class); } @Override public long getLong(final int parameterIndex) throws SQLException { - return (long) getData(parameterIndex, long.class); + return (long) getDataRow().getValue(parameterIndex-1, long.class); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return (float) getData(parameterIndex, float.class); + return (float) getDataRow().getValue(parameterIndex-1, float.class); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return (double) getData(parameterIndex, double.class); + return (double) getDataRow().getValue(parameterIndex-1, double.class); } @Override @@ -119,32 +118,32 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - return (byte[]) getData(parameterIndex, byte[].class); + return (byte[]) getDataRow().getValue(parameterIndex-1, byte[].class); } @Override public Date getDate(final int parameterIndex) throws SQLException { - return (Date) getData(parameterIndex, Date.class); + return (Date) getDataRow().getValue(parameterIndex-1, Date.class); } @Override public Time getTime(final int parameterIndex) throws SQLException { - return (Time) getData(parameterIndex, Time.class); + return (Time) getDataRow().getValue(parameterIndex-1, Time.class); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - return (Timestamp) getData(parameterIndex, Timestamp.class); + return (Timestamp) getDataRow().getValue(parameterIndex-1, Timestamp.class); } @Override public Object getObject(final int parameterIndex) throws SQLException { - return getData(parameterIndex, Object.class); + return getDataRow().getValue(parameterIndex-1, Object.class); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return (BigDecimal) getData(parameterIndex, BigDecimal.class); + return (BigDecimal) getDataRow().getValue(parameterIndex-1, BigDecimal.class); } @Override @@ -339,72 +338,72 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return (String) getData(parameterName, String.class); + return (String) getDataRow().getValue(parameterName, String.class); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return (boolean) getData(parameterName, boolean.class); + return (boolean) getDataRow().getValue(parameterName, boolean.class); } @Override public byte getByte(final String parameterName) throws SQLException { - return (byte) getData(parameterName, byte.class); + return (byte) getDataRow().getValue(parameterName, byte.class); } @Override public short getShort(final String parameterName) throws SQLException { - return (short) getData(parameterName, short.class); + return (short) getDataRow().getValue(parameterName, short.class); } @Override public int getInt(final String parameterName) throws SQLException { - return (int) getData(parameterName, int.class); + return (int) getDataRow().getValue(parameterName, int.class); } @Override public long getLong(final String parameterName) throws SQLException { - return (long) getData(parameterName, long.class); + return (long) getDataRow().getValue(parameterName, long.class); } @Override public float getFloat(final String parameterName) throws SQLException { - return (float) getData(parameterName, float.class); + return (float) getDataRow().getValue(parameterName, float.class); } @Override public double getDouble(final String parameterName) throws SQLException { - return (double) getData(parameterName, double.class); + return (double) getDataRow().getValue(parameterName, double.class); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return (byte[]) getData(parameterName, byte[].class); + return (byte[]) getDataRow().getValue(parameterName, byte[].class); } @Override public Date getDate(final String parameterName) throws SQLException { - return (Date) getData(parameterName, Date.class); + return (Date) getDataRow().getValue(parameterName, Date.class); } @Override public Time getTime(final String parameterName) throws SQLException { - return (Time) getData(parameterName, Time.class); + return (Time) getDataRow().getValue(parameterName, Time.class); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - return (Timestamp) getData(parameterName, Timestamp.class); + return (Timestamp) getDataRow().getValue(parameterName, Timestamp.class); } @Override public Object getObject(final String parameterName) throws SQLException { - return getData(parameterName, Object.class); + return getDataRow().getValue(parameterName, Object.class); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return (BigDecimal) getData(parameterName, BigDecimal.class); + return (BigDecimal) getDataRow().getValue(parameterName, BigDecimal.class); } @Override @@ -660,20 +659,6 @@ private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } - private Object getData(final int parameterIndex, final Class clazz) throws SQLException { - final Object value = getDataRow().getValue(parameterIndex - 1); - return convertData(value, clazz); - } - - private Object getData(final String parameterName, final Class clazz) throws SQLException { - final Object value = getDataRow().getValue(parameterName); - return convertData(value, clazz); - } - - private Object convertData(final Object value, final Class clazz) { - return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz); - } - private Row getDataRow() throws SQLException { if(Objects.isNull(resultRow)){ resultRow = dataSet.getNextRow(); 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 c0c1e3c..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,6 +16,8 @@ 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; @@ -23,9 +25,6 @@ import java.util.Objects; import java.util.SortedMap; -/** - * @author Christoph Deppisch - */ public class Row { /** Row values with column name as key */ @@ -35,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(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(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; @@ -73,12 +96,16 @@ public Map getValues() { /** * Sets the values. * - * @param values + * @param values The values to set in the Row */ 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(final Object o) { if (this == o) return true; @@ -94,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/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java index 5f10797..8417fd0 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -5,11 +5,7 @@ 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.http.client.HttpClient; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.testng.PowerMockTestCase; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; @@ -26,27 +22,30 @@ import java.util.SortedMap; import java.util.TreeMap; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; import static org.powermock.api.mockito.PowerMockito.mock; +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 JdbcCallableStatementTest extends PowerMockTestCase { +public class JdbcCallableStatementTest{ private HttpClient httpClient; private String serverUrl = "localhost"; private JdbcConnection jdbcConnection; - private final int TEST_VALUE_INDEX = 2; + 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"; private final JdbcCallableStatement callableStatement = generateCallableStatement(); + private Row rowSpy; + @BeforeMethod public void setup(){ httpClient = mock(HttpClient.class); jdbcConnection = mock(JdbcConnection.class); - PowerMockito.spy(ConvertUtils.class); } @Test @@ -437,7 +436,7 @@ void testGetTwoValuesByIndex() throws SQLException { //WHEN final String firstOutParameter = callableStatement.getString(1); - final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX); + final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(firstOutParameter, "dummyValue"); @@ -452,11 +451,11 @@ void testGetStringByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); //WHEN - final String string = callableStatement.getString(TEST_VALUE_INDEX); + final String string = callableStatement.getString(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(string, expectedString); - verifyConversion(expectedString, String.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); } @Test @@ -467,11 +466,11 @@ void testGetBooleanByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); //WHEN - final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX); + final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX_JDBC); //THEN assertTrue(aBoolean); - verifyConversion(expectedBoolean, boolean.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, boolean.class); } @Test @@ -482,11 +481,11 @@ void testGetByteByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); //WHEN - final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX); + final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aByte, expectedByte); - verifyConversion(expectedByte, byte.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte.class); } @Test @@ -497,11 +496,11 @@ void testGetBytesByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); //WHEN - final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX); + final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aByte, expectedBytes); - verifyConversion(expectedBytes, byte[].class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); } @Test @@ -512,11 +511,11 @@ void testGetShortByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); //WHEN - final short aShort = callableStatement.getShort(TEST_VALUE_INDEX); + final short aShort = callableStatement.getShort(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aShort, expectedShort); - verifyConversion(expectedShort, short.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, short.class); } @Test @@ -527,11 +526,11 @@ void testGetIntByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); //WHEN - final int anInt = callableStatement.getInt(TEST_VALUE_INDEX); + final int anInt = callableStatement.getInt(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(anInt, expectedInt); - verifyConversion(expectedInt, int.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, int.class); } @Test @@ -542,11 +541,11 @@ void testGetLongByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); //WHEN - final long aLong = callableStatement.getLong(TEST_VALUE_INDEX); + final long aLong = callableStatement.getLong(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aLong, expectedLong); - verifyConversion(expectedLong, long.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, long.class); } @Test @@ -557,11 +556,11 @@ void testGetFloatByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); //WHEN - final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX); + final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aFloat, expectedFloat); - verifyConversion(expectedFloat, float.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, float.class); } @Test @@ -572,11 +571,11 @@ void testGetDoubleByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); //WHEN - final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX); + final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aDouble, expectedDouble); - verifyConversion(expectedDouble, double.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, double.class); } @Test @@ -587,11 +586,11 @@ void testGetBigDecimalByIndexWithScale() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX, 2); + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX_JDBC, 2); //THEN assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); - verifyConversion(expectedBigDecimal, BigDecimal.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, BigDecimal.class); } @Test @@ -606,7 +605,7 @@ void testGetStringByName() throws SQLException { //THEN assertEquals(string, expectedString); - verifyConversion(expectedString, String.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); } @Test @@ -621,7 +620,7 @@ void testGetBooleanByName() throws SQLException { //THEN assertTrue(aBoolean); - verifyConversion(expectedBoolean, boolean.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, boolean.class); } @Test @@ -636,7 +635,7 @@ void testGetByteByName() throws SQLException { //THEN assertEquals(aByte, expectedByte); - verifyConversion(expectedByte, byte.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte.class); } @Test @@ -651,7 +650,7 @@ void testGetShortByName() throws SQLException { //THEN assertEquals(aShort, expectedShort); - verifyConversion(expectedShort, short.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, short.class); } @Test @@ -666,7 +665,7 @@ void testGetIntByName() throws SQLException { //THEN assertEquals(anInt, expectedInt); - verifyConversion(expectedInt, int.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, int.class); } @Test @@ -681,7 +680,7 @@ void testGetLongByName() throws SQLException { //THEN assertEquals(aLong, expectedLong); - verifyConversion(expectedLong, long.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, long.class); } @Test @@ -696,7 +695,7 @@ void testGetFloatByName() throws SQLException { //THEN assertEquals(aFloat, expectedFloat); - verifyConversion(expectedFloat, float.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, float.class); } @Test @@ -711,7 +710,7 @@ void testGetDoubleByName() throws SQLException { //THEN assertEquals(aFloat, expectedDouble); - verifyConversion(expectedDouble, double.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, double.class); } @Test @@ -726,7 +725,7 @@ void testGetBytesByName() throws SQLException { //THEN assertEquals(bytes, expectedBytes); - verifyConversion(expectedBytes, byte[].class); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); } @Test @@ -741,7 +740,7 @@ void testGetObjectByName() throws SQLException { //THEN assertEquals(object, expectedObject); - verifyConversion(expectedObject, Object.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Object.class); } @Test @@ -752,11 +751,11 @@ void testGetBigDecimalByName() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME ); + final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME); //THEN assertEquals(aBigDecimal, expectedBigDecimal); - verifyConversion(aBigDecimal, BigDecimal.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, BigDecimal.class); } @Test @@ -807,11 +806,11 @@ void testGetDateByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); //WHEN - final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX); + final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aDate, expectedDate); - verifyConversion(expectedDate, Date.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Date.class); } @Test @@ -822,11 +821,11 @@ void testGetTimeByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); //WHEN - final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX); + final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aTime, expectedTime); - verifyConversion(expectedTime, Time.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Time.class); } @Test @@ -837,11 +836,11 @@ void testGetTimestampByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); //WHEN - final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX); + final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(aTimestamp, expectedTimestamp); - verifyConversion(expectedTimestamp, Timestamp.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Timestamp.class); } @Test @@ -852,11 +851,11 @@ void testGetObjectStampByIndex() throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); //WHEN - final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX); + final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX_JDBC); //THEN assertEquals(anObject, expectedObject); - verifyConversion(expectedObject, Object.class); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Object.class); } @Test @@ -871,7 +870,7 @@ void testGetDateByName() throws SQLException { //THEN assertEquals(aDate, expectedDate); - verifyConversion(expectedDate, Date.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Date.class); } @Test @@ -886,7 +885,7 @@ void testGetTimeByName() throws SQLException { //THEN assertEquals(aTime, expectedTime); - verifyConversion(expectedTime, Time.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Time.class); } @Test @@ -901,7 +900,7 @@ void testGetTimestampByName() throws SQLException { //THEN assertEquals(aTimestamp, expectedTimestamp); - verifyConversion(expectedTimestamp, Timestamp.class); + verify(rowSpy).getValue(TEST_VALUE_NAME, Timestamp.class); } @Test @@ -935,13 +934,8 @@ private DataSet generateTestDataSet(final Object testValue) throws SQLException testData.put("col1", "dummyValue"); testData.put("col2", testValue); - final Row row = new Row(); - row.setValues(testData); - return new DataSetBuilder().add(row).build(); - } - - private void verifyConversion(final Object expectedValue, final Class clazz) { - PowerMockito.verifyStatic(ConvertUtils.class); - ConvertUtils.convert(eq(expectedValue), eq(clazz)); + 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/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 From b07231f7bf51c11f9953bef928c858fa06f9df86 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 8 Feb 2019 14:19:54 +0100 Subject: [PATCH 17/27] (#29) Simplified getObject --- .../com/consol/citrus/db/driver/JdbcCallableStatement.java | 4 ++-- .../consol/citrus/db/driver/JdbcCallableStatementTest.java | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) 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 b110584..8de4e01 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 @@ -138,7 +138,7 @@ public Timestamp getTimestamp(final int parameterIndex) throws SQLException { @Override public Object getObject(final int parameterIndex) throws SQLException { - return getDataRow().getValue(parameterIndex-1, Object.class); + return getDataRow().getValue(parameterIndex-1); } @Override @@ -398,7 +398,7 @@ public Timestamp getTimestamp(final String parameterName) throws SQLException { @Override public Object getObject(final String parameterName) throws SQLException { - return getDataRow().getValue(parameterName, Object.class); + return getDataRow().getValue(parameterName); } @Override 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 index 8417fd0..fc9565a 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -740,7 +740,6 @@ void testGetObjectByName() throws SQLException { //THEN assertEquals(object, expectedObject); - verify(rowSpy).getValue(TEST_VALUE_NAME, Object.class); } @Test @@ -844,7 +843,7 @@ void testGetTimestampByIndex() throws SQLException { } @Test - void testGetObjectStampByIndex() throws SQLException { + void testGetObjectByIndex() throws SQLException { //GIVEN final Object expectedObject = new Timestamp(619912812345L); @@ -855,7 +854,6 @@ void testGetObjectStampByIndex() throws SQLException { //THEN assertEquals(anObject, expectedObject); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Object.class); } @Test From 38593cc7e5c61e9207cad503c8d2d21421e49304 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 8 Feb 2019 14:21:08 +0100 Subject: [PATCH 18/27] (#29) Replaced type conversion with conversion by dependency --- .../citrus/db/driver/JdbcResultSet.java | 618 ++++++---------- .../citrus/db/driver/JdbcResultSetTest.java | 668 ++++++++++++++++++ pom.xml | 13 + 3 files changed, 905 insertions(+), 394 deletions(-) create mode 100644 driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java diff --git a/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java b/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java index b6c8bc6..c0e38e1 100644 --- a/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java +++ b/driver/src/main/java/com/consol/citrus/db/driver/JdbcResultSet.java @@ -24,6 +24,7 @@ import java.io.InputStreamReader; import java.io.Reader; import java.math.BigDecimal; +import java.math.RoundingMode; import java.net.URL; import java.sql.Array; import java.sql.Blob; @@ -41,10 +42,9 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Map; +import java.util.Objects; + -/** - * @author Christoph Deppisch - */ public class JdbcResultSet implements java.sql.ResultSet { /** Remote ResultSet */ @@ -57,7 +57,7 @@ public class JdbcResultSet implements java.sql.ResultSet { /** * Constructor using remote result set. */ - public JdbcResultSet(DataSet dataSet, JdbcStatement statement) throws SQLException { + public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) throws SQLException { this.dataSet = dataSet; this.statement = statement; } @@ -73,332 +73,148 @@ public void close() throws SQLException { dataSet.close(); } - private T convert(Object value, Class type) throws SQLException { - if (value == null) { - return null; - } - - if (type.isInstance(value)) { - return type.cast(value); - } - - if (String.class.isAssignableFrom(type)) { - return (T) value.toString(); - } - - if (Byte.class.isAssignableFrom(type)) { - return (T) Byte.valueOf(value.toString()); - } - - if (Boolean.class.isAssignableFrom(type)) { - return (T) Boolean.valueOf(value.toString()); - } - - if (Short.class.isAssignableFrom(type)) { - return (T) Short.valueOf(value.toString()); - } - - if (Integer.class.isAssignableFrom(type)) { - return (T) Integer.valueOf(value.toString()); - } - - if (Long.class.isAssignableFrom(type)) { - return (T) Long.valueOf(value.toString()); - } - - if (Double.class.isAssignableFrom(type)) { - return (T) Double.valueOf(value.toString()); - } - - if (Float.class.isAssignableFrom(type)) { - return (T) Float.valueOf(value.toString()); - } - - if (Timestamp.class.isAssignableFrom(type)) { - return (T) Timestamp.valueOf(value.toString()); - } - - if (Time.class.isAssignableFrom(type)) { - return (T) Time.valueOf(value.toString()); - } - - if (Date.class.isAssignableFrom(type)) { - return (T) Date.valueOf(value.toString()); - } - - throw new SQLException(String.format("Missing conversion strategy for type %s", type)); - } - - public String getString(int columnIndex) throws SQLException { - return convert(row.getValue(columnIndex-1), String.class); + public String getString(final int columnIndex) throws SQLException { + return (String) row.getValue(columnIndex-1, String.class); } - public String getString(String columnName) throws SQLException { - return convert(row.getValue(columnName), String.class); + public String getString(final String columnName) throws SQLException { + return (String) row.getValue(columnName, String.class); } - public float getFloat(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Float.class); - } + public float getFloat(final int columnIndex) throws SQLException { + return (float) row.getValue(columnIndex-1, float.class); } - public float getFloat(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Float.class); - } + public float getFloat(final String columnName) throws SQLException { + return (float) row.getValue(columnName, float.class); } - public int getInt(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Integer.class); - } + public int getInt(final int columnIndex) throws SQLException { + return (int) row.getValue(columnIndex-1, int.class); } - public int getInt(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Integer.class); - } + public int getInt(final String columnName) throws SQLException { + return (int) row.getValue(columnName, int.class); } - public boolean getBoolean(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1) == null) { - return false; - } else { - return convert(row.getValue(columnIndex-1), Boolean.class); - } + public boolean getBoolean(final int columnIndex) throws SQLException { + return (boolean) row.getValue(columnIndex-1, boolean.class); } - public byte getByte(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Byte.class); - } + public byte getByte(final int columnIndex) throws SQLException { + return (byte) row.getValue(columnIndex-1, byte.class); } - public short getShort(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Short.class); - } + public short getShort(final int columnIndex) throws SQLException { + return (short) row.getValue(columnIndex-1, short.class); } - public long getLong(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Long.class); - } + public long getLong(final int columnIndex) throws SQLException { + return (long) row.getValue(columnIndex-1, long.class); } - public double getDouble(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return 0; - } else { - return convert(row.getValue(columnIndex-1), Double.class); - } + public double getDouble(final int columnIndex) throws SQLException { + return (double) row.getValue(columnIndex-1, double.class); } - public BigDecimal getBigDecimal(int columnIndex,int scale) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getBigDecimal'"); + public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException { + return getBigDecimal(columnIndex).setScale(scale, RoundingMode.HALF_UP); } - public byte[] getBytes(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), String.class).getBytes(); + public byte[] getBytes(final int columnIndex) throws SQLException { + return (byte[]) row.getValue(columnIndex-1, byte[].class); } - public Date getDate(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Date.class); + public Date getDate(final int columnIndex) throws SQLException { + return (Date) row.getValue(columnIndex-1, Date.class); } - public Time getTime(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Time.class); + public Time getTime(final int columnIndex) throws SQLException { + return (Time) row.getValue(columnIndex-1, Time.class); } - public Timestamp getTimestamp(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - return convert(row.getValue(columnIndex-1), Timestamp.class); + public Timestamp getTimestamp(final int columnIndex) throws SQLException { + return (Timestamp) row.getValue(columnIndex-1, Timestamp.class); } - public InputStream getAsciiStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getAsciiStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getString(columnIndex).getBytes()); } - public InputStream getUnicodeStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getUnicodeStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getString(columnIndex).getBytes()); } - public InputStream getBinaryStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getBinaryStream(final int columnIndex) throws SQLException { + return new ByteArrayInputStream(getBytes(columnIndex)); } - public Object getObject(int columnIndex) throws SQLException { + public Object getObject(final int columnIndex) throws SQLException { return row.getValue(columnIndex-1); } - public BigDecimal getBigDecimal(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - Long bigdObj = convert(row.getValue(columnIndex-1), Long.class); - return BigDecimal.valueOf(bigdObj); + public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { + return (BigDecimal) row.getValue(columnIndex-1, BigDecimal.class); } - public boolean getBoolean(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return false; - } else { - return convert(row.getValue(columnName), Boolean.class); - } + public boolean getBoolean(final String columnName) throws SQLException { + return (boolean) row.getValue(columnName, boolean.class); } - public byte getByte(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Byte.class); - } + public byte getByte(final String columnName) throws SQLException { + return (byte) row.getValue(columnName, byte.class); } - public short getShort(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Short.class); - } + public short getShort(final String columnName) throws SQLException { + return (short) row.getValue(columnName, short.class); } - public long getLong(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Long.class); - } + public long getLong(final String columnName) throws SQLException { + return (long) row.getValue(columnName, long.class); } - public double getDouble(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return 0; - } else { - return convert(row.getValue(columnName), Double.class); - } + public double getDouble(final String columnName) throws SQLException { + return (double) row.getValue(columnName, double.class); } - public BigDecimal getBigDecimal(String columnName,int scale) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getBigDecimal'"); + public BigDecimal getBigDecimal(final String columnName, final int scale) throws SQLException { + return getBigDecimal(columnName).setScale(scale, RoundingMode.HALF_UP); } - public byte[] getBytes(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } else { - return convert(row.getValue(columnName), String.class).getBytes(); - } + public byte[] getBytes(final String columnName) throws SQLException { + return (byte[]) row.getValue(columnName, byte[].class); } - public Date getDate(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Date.class); + public Date getDate(final String columnName) throws SQLException { + return (Date) row.getValue(columnName, Date.class); } - public Time getTime(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Time.class); + public Time getTime(final String columnName) throws SQLException { + return (Time) row.getValue(columnName, Time.class); } - public Timestamp getTimestamp(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - return convert(row.getValue(columnName), Timestamp.class); + public Timestamp getTimestamp(final String columnName) throws SQLException { + return (Timestamp) row.getValue(columnName, Timestamp.class); } - public Object getObject(String columnName) throws SQLException { + public Object getObject(final String columnName) throws SQLException { return row.getValue(columnName); } - public BigDecimal getBigDecimal(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - Long bigdObj = convert(row.getValue(columnName), Long.class); - return BigDecimal.valueOf(bigdObj); + public BigDecimal getBigDecimal(final String columnName) throws SQLException { + return (BigDecimal) row.getValue(columnName, BigDecimal.class); } - public InputStream getAsciiStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getAsciiStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getString(columnName).getBytes()); } - public InputStream getUnicodeStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getUnicodeStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getString(columnName).getBytes()); } - public InputStream getBinaryStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new ByteArrayInputStream(byteArray); + public InputStream getBinaryStream(final String columnName) throws SQLException { + return new ByteArrayInputStream(getBytes(columnName)); } public SQLWarning getWarnings() throws SQLException { @@ -416,26 +232,16 @@ public ResultSetMetaData getMetaData() throws SQLException { return new JdbcResultSetMetaData(dataSet); } - public int findColumn(String columnName) throws SQLException { + public int findColumn(final String columnName) throws SQLException { return row.getColumns().indexOf(columnName); } - public Reader getCharacterStream(int columnIndex) throws SQLException { - if (row.getValue(columnIndex-1)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnIndex-1), String.class).getBytes(); - return new InputStreamReader(new ByteArrayInputStream(byteArray)); + public Reader getCharacterStream(final int columnIndex) throws SQLException { + return new InputStreamReader(new ByteArrayInputStream(getString(columnIndex).getBytes())); } - public Reader getCharacterStream(String columnName) throws SQLException { - if (row.getValue(columnName)==null) { - return null; - } - - byte[] byteArray = convert(row.getValue(columnName), String.class).getBytes(); - return new InputStreamReader(new ByteArrayInputStream(byteArray)); + public Reader getCharacterStream(final String columnName) throws SQLException { + return new InputStreamReader(new ByteArrayInputStream(getString(columnName).getBytes())); } public boolean isBeforeFirst() throws SQLException { @@ -472,11 +278,11 @@ public int getRow() throws SQLException { return dataSet.getCursor() + 1; } - public boolean absolute(int row) throws SQLException { + public boolean absolute(final int row) throws SQLException { throw new SQLException("Not supported JDBC result set function 'absolute'"); } - public boolean relative(int rows) throws SQLException { + public boolean relative(final int rows) throws SQLException { throw new SQLException("Not supported JDBC result set function 'relative'"); } @@ -484,14 +290,14 @@ public boolean previous() throws SQLException { throw new SQLException("Not supported JDBC result set function 'previous'"); } - public void setFetchDirection(int direction) throws SQLException { + public void setFetchDirection(final int direction) throws SQLException { } public int getFetchDirection() throws SQLException { throw new SQLException("Not supported JDBC result set function 'getFetchDirection'"); } - public void setFetchSize(int rows) throws SQLException { + public void setFetchSize(final int rows) throws SQLException { } public int getFetchSize() throws SQLException { @@ -507,123 +313,123 @@ public int getConcurrency() throws SQLException { } public boolean rowUpdated() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } public boolean rowInserted() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } public boolean rowDeleted() throws SQLException { - return dataSet.getRows().size() > 0; + return !dataSet.getRows().isEmpty(); } - public void updateNull(int columnIndex) throws SQLException { + public void updateNull(final int columnIndex) throws SQLException { } - public void updateBoolean(int columnIndex,boolean x) throws SQLException { + public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { } - public void updateByte(int columnIndex, byte x) throws SQLException { + public void updateByte(final int columnIndex, final byte x) throws SQLException { } - public void updateShort(int columnIndex,short x) throws SQLException { + public void updateShort(final int columnIndex, final short x) throws SQLException { } - public void updateInt(int columnIndex,int x) throws SQLException { + public void updateInt(final int columnIndex, final int x) throws SQLException { } - public void updateLong(int columnIndex,long x) throws SQLException { + public void updateLong(final int columnIndex, final long x) throws SQLException { } - public void updateFloat(int columnIndex,float x) throws SQLException { + public void updateFloat(final int columnIndex, final float x) throws SQLException { } - public void updateDouble(int columnIndex,double x) throws SQLException { + public void updateDouble(final int columnIndex, final double x) throws SQLException { } - public void updateBigDecimal(int columnIndex,BigDecimal x) throws SQLException { + public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { } - public void updateString(int columnIndex,String x) throws SQLException { + public void updateString(final int columnIndex, final String x) throws SQLException { } - public void updateBytes(int columnIndex,byte[] x) throws SQLException { + public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { } - public void updateDate(int columnIndex,Date x) throws SQLException { + public void updateDate(final int columnIndex, final Date x) throws SQLException { } - public void updateTime(int columnIndex,Time x) throws SQLException { + public void updateTime(final int columnIndex, final Time x) throws SQLException { } - public void updateTimestamp(int columnIndex,Timestamp x) throws SQLException { + public void updateTimestamp(final int columnIndex, final Timestamp x) throws SQLException { } - public void updateBinaryStream(int columnIndex,InputStream x,int length) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x, final int length) throws SQLException { } - public void updateCharacterStream(int columnIndex,Reader x,int length) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x, final int length) throws SQLException { } - public void updateObject(int columnIndex,Object x,int scale) throws SQLException { + public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { } - public void updateObject(int columnIndex,Object x) throws SQLException { + public void updateObject(final int columnIndex, final Object x) throws SQLException { } - public void updateNull(String columnName) throws SQLException { + public void updateNull(final String columnName) throws SQLException { } - public void updateByte(String columnName, byte x) throws SQLException { + public void updateByte(final String columnName, final byte x) throws SQLException { } - public void updateShort(String columnName, short x) throws SQLException { + public void updateShort(final String columnName, final short x) throws SQLException { } - public void updateInt(String columnName,int x) throws SQLException { + public void updateInt(final String columnName, final int x) throws SQLException { } - public void updateLong(String columnName,long x) throws SQLException { + public void updateLong(final String columnName, final long x) throws SQLException { } - public void updateFloat(String columnName, float x) throws SQLException { + public void updateFloat(final String columnName, final float x) throws SQLException { } - public void updateDouble(String columnName,double x) throws SQLException { + public void updateDouble(final String columnName, final double x) throws SQLException { } - public void updateBigDecimal(String columnName,BigDecimal x) throws SQLException { + public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { } - public void updateString(String columnName,String x) throws SQLException { + public void updateString(final String columnName, final String x) throws SQLException { } - public void updateBytes(String columnName,byte[] x) throws SQLException { + public void updateBytes(final String columnName, final byte[] x) throws SQLException { } - public void updateDate(String columnName,Date x) throws SQLException { + public void updateDate(final String columnName, final Date x) throws SQLException { } - public void updateTime(String columnName, Time x) throws SQLException { + public void updateTime(final String columnName, final Time x) throws SQLException { } - public void updateTimestamp(String columnName,Timestamp x) throws SQLException { + public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException { } - public void updateAsciiStream(String columnName,InputStream x,int length) throws SQLException { + public void updateAsciiStream(final String columnName, final InputStream x, final int length) throws SQLException { } - public void updateBinaryStream(String columnName,InputStream x,int length) throws SQLException { + public void updateBinaryStream(final String columnName, final InputStream x, final int length) throws SQLException { } - public void updateCharacterStream(String columnName,Reader reader,int length) throws SQLException { + public void updateCharacterStream(final String columnName, final Reader reader, final int length) throws SQLException { } - public void updateObject(String columnName,Object x,int scale) throws SQLException { + public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { } - public void updateObject(String columnName,Object x) throws SQLException { + public void updateObject(final String columnName, final Object x) throws SQLException { } public void insertRow() throws SQLException { @@ -655,88 +461,88 @@ public Statement getStatement() throws SQLException { } - public Date getDate(int columnIndex,Calendar cal) throws SQLException { + public Date getDate(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getDate'"); } - public Date getDate(String columnName,Calendar cal) throws SQLException { + public Date getDate(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getDate'"); } - public Time getTime(int columnIndex,Calendar cal) throws SQLException { + public Time getTime(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTime'"); } - public Time getTime(String columnName,Calendar cal) throws SQLException { + public Time getTime(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTime'"); } - public Timestamp getTimestamp(int columnIndex,Calendar cal) throws SQLException { + public Timestamp getTimestamp(final int columnIndex, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTimestamp'"); } - public Timestamp getTimestamp(String columnName,Calendar cal) throws SQLException { + public Timestamp getTimestamp(final String columnName, final Calendar cal) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getTimestamp'"); } @Override - public URL getURL(int columnIndex) throws SQLException { + public URL getURL(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getURL'"); } @Override - public URL getURL(String columnLabel) throws SQLException { + public URL getURL(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getURL'"); } @Override - public void updateRef(int columnIndex, Ref x) throws SQLException { + public void updateRef(final int columnIndex, final Ref x) throws SQLException { } @Override - public void updateRef(String columnLabel, Ref x) throws SQLException { + public void updateRef(final String columnLabel, final Ref x) throws SQLException { } @Override - public void updateBlob(int columnIndex, Blob x) throws SQLException { + public void updateBlob(final int columnIndex, final Blob x) throws SQLException { } @Override - public void updateBlob(String columnLabel, Blob x) throws SQLException { + public void updateBlob(final String columnLabel, final Blob x) throws SQLException { } @Override - public void updateClob(int columnIndex, Clob x) throws SQLException { + public void updateClob(final int columnIndex, final Clob x) throws SQLException { } @Override - public void updateClob(String columnLabel, Clob x) throws SQLException { + public void updateClob(final String columnLabel, final Clob x) throws SQLException { } @Override - public void updateArray(int columnIndex, Array x) throws SQLException { + public void updateArray(final int columnIndex, final Array x) throws SQLException { } @Override - public void updateArray(String columnLabel, Array x) throws SQLException { + public void updateArray(final String columnLabel, final Array x) throws SQLException { } @Override - public RowId getRowId(int columnIndex) throws SQLException { + public RowId getRowId(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRowId'"); } @Override - public RowId getRowId(String columnLabel) throws SQLException { + public RowId getRowId(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRowId'"); } @Override - public void updateRowId(int columnIndex, RowId x) throws SQLException { + public void updateRowId(final int columnIndex, final RowId x) throws SQLException { } @Override - public void updateRowId(String columnLabel, RowId x) throws SQLException { + public void updateRowId(final String columnLabel, final RowId x) throws SQLException { } @Override @@ -750,188 +556,188 @@ public boolean isClosed() throws SQLException { } @Override - public void updateNString(int columnIndex, String nString) throws SQLException { + public void updateNString(final int columnIndex, final String nString) throws SQLException { } @Override - public void updateNString(String columnLabel, String nString) throws SQLException { + public void updateNString(final String columnLabel, final String nString) throws SQLException { } @Override - public void updateNClob(int columnIndex, NClob nClob) throws SQLException { + public void updateNClob(final int columnIndex, final NClob nClob) throws SQLException { } @Override - public void updateNClob(String columnLabel, NClob nClob) throws SQLException { + public void updateNClob(final String columnLabel, final NClob nClob) throws SQLException { } @Override - public NClob getNClob(int columnIndex) throws SQLException { + public NClob getNClob(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNClob'"); } @Override - public NClob getNClob(String columnLabel) throws SQLException { + public NClob getNClob(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNClob'"); } @Override - public SQLXML getSQLXML(int columnIndex) throws SQLException { + public SQLXML getSQLXML(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getSQLXML'"); } @Override - public SQLXML getSQLXML(String columnLabel) throws SQLException { + public SQLXML getSQLXML(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getSQLXML'"); } @Override - public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException { + public void updateSQLXML(final int columnIndex, final SQLXML xmlObject) throws SQLException { } @Override - public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException { + public void updateSQLXML(final String columnLabel, final SQLXML xmlObject) throws SQLException { } @Override - public String getNString(int columnIndex) throws SQLException { + public String getNString(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNString'"); } @Override - public String getNString(String columnLabel) throws SQLException { + public String getNString(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNString'"); } @Override - public Reader getNCharacterStream(int columnIndex) throws SQLException { + public Reader getNCharacterStream(final int columnIndex) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNCharacterStream'"); } @Override - public Reader getNCharacterStream(String columnLabel) throws SQLException { + public Reader getNCharacterStream(final String columnLabel) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getNCharacterStream'"); } @Override - public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + public void updateNCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { } @Override - public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x, final long length) throws SQLException { } @Override - public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x, final long length) throws SQLException { } @Override - public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { } @Override - public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateAsciiStream(final String columnLabel, final InputStream x, final long length) throws SQLException { } @Override - public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException { + public void updateBinaryStream(final String columnLabel, final InputStream x, final long length) throws SQLException { } @Override - public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException { + public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException { + public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { } @Override - public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException { + public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { } @Override - public void updateClob(int columnIndex, Reader reader, long length) throws SQLException { + public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { } @Override - public void updateClob(String columnLabel, Reader reader, long length) throws SQLException { + public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException { + public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { } @Override - public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException { + public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { } @Override - public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateNCharacterStream(final int columnIndex, final Reader x) throws SQLException { } @Override - public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x) throws SQLException { } @Override - public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException { + public void updateBinaryStream(final int columnIndex, final InputStream x) throws SQLException { } @Override - public void updateCharacterStream(int columnIndex, Reader x) throws SQLException { + public void updateCharacterStream(final int columnIndex, final Reader x) throws SQLException { } @Override - public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException { + public void updateAsciiStream(final String columnLabel, final InputStream x) throws SQLException { } @Override - public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException { + public void updateBinaryStream(final String columnLabel, final InputStream x) throws SQLException { } @Override - public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException { + public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException { + public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { } @Override - public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException { + public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { } @Override - public void updateClob(int columnIndex, Reader reader) throws SQLException { + public void updateClob(final int columnIndex, final Reader reader) throws SQLException { } @Override - public void updateClob(String columnLabel, Reader reader) throws SQLException { + public void updateClob(final String columnLabel, final Reader reader) throws SQLException { } @Override - public void updateNClob(int columnIndex, Reader reader) throws SQLException { + public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { } @Override - public void updateNClob(String columnLabel, Reader reader) throws SQLException { + public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { } @Override - public T getObject(int columnIndex, Class type) throws SQLException { + public T getObject(final int columnIndex, final Class type) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } @Override - public T getObject(String columnLabel, Class type) throws SQLException { + public T getObject(final String columnLabel, final Class type) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } @@ -939,59 +745,83 @@ public boolean wasNull()throws SQLException { return row.getLastValue() == null; } - public void updateBoolean(String columnName, boolean x) throws SQLException { + public void updateBoolean(final String columnName, final boolean x) throws SQLException { } - public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException { + public void updateAsciiStream(final int columnIndex, final InputStream x, final int length) throws SQLException { } - public Object getObject(int i, Map map) throws SQLException { + public Object getObject(final int i, final Map map) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } - public Ref getRef(int i) throws SQLException { + public Ref getRef(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRef'"); } - public Blob getBlob(int i) throws SQLException { + public Blob getBlob(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getBlob'"); } - public Clob getClob(int i) throws SQLException { + public Clob getClob(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getClob'"); } - public Array getArray(int i) throws SQLException { + public Array getArray(final int i) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getArray'"); } - public Object getObject(String colName, Map map) throws SQLException { + public Object getObject(final String colName, final Map map) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getObject'"); } - public Ref getRef(String colName) throws SQLException { + public Ref getRef(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getRef'"); } - public Blob getBlob(String colName) throws SQLException { + public Blob getBlob(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getBlob'"); } - public Clob getClob(String colName) throws SQLException { + public Clob getClob(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getClob'"); } - public Array getArray(String colName) throws SQLException { + public Array getArray(final String colName) throws SQLException { throw new SQLException("Not supported JDBC result set function 'getArray'"); } @Override - public T unwrap(Class iface) throws SQLException { + public T unwrap(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'unwrap'"); } @Override - public boolean isWrapperFor(Class iface) throws SQLException { + public boolean isWrapperFor(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'isWrapperFor'"); } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof JdbcResultSet)) return false; + final JdbcResultSet that = (JdbcResultSet) o; + return Objects.equals(dataSet, that.dataSet) && + Objects.equals(statement, that.statement) && + Objects.equals(row, that.row); + } + + @Override + public int hashCode() { + return Objects.hash(dataSet, statement, row); + } + + @Override + public String toString() { + return "JdbcResultSet{" + + "dataSet=" + dataSet + + ", statement=" + statement + + ", row=" + row + + '}'; + } } \ No newline at end of file diff --git a/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java new file mode 100644 index 0000000..36d6f80 --- /dev/null +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java @@ -0,0 +1,668 @@ +package com.consol.citrus.db.driver; + +import com.consol.citrus.db.driver.data.Row; +import com.consol.citrus.db.driver.dataset.DataSet; +import com.consol.citrus.db.driver.dataset.DataSetBuilder; +import com.jparams.verifier.tostring.ToStringVerifier; +import nl.jqno.equalsverifier.EqualsVerifier; +import org.apache.commons.beanutils.ConvertUtils; +import org.apache.commons.io.IOUtils; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.sql.Date; +import java.sql.SQLException; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.SortedMap; +import java.util.TreeMap; + +import static org.mockito.Mockito.verify; +import static org.powermock.api.mockito.PowerMockito.spy; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; + +@PrepareForTest(ConvertUtils.class) +public class JdbcResultSetTest { + + private JdbcResultSet resultSet; + private Row rowSpy; + + private final int TEST_VALUE_INDEX_JDBC = 2; + //Because in JDBC, arrays start at 1 + private final int TEST_VALUE_INDEX_INTERNAL = TEST_VALUE_INDEX_JDBC-1; + private final String TEST_VALUE_NAME = "col2"; + + @Test + public void testGetStringByIndex() throws SQLException { + //GIVEN + final String expectedString = "bar"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + resultSet.next(); + + //WHEN + final String string = resultSet.getString(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(string, expectedString); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetStringByName() throws SQLException { + + //GIVEN + final String expectedString = "bar"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + resultSet.next(); + + //WHEN + final String string = resultSet.getString(TEST_VALUE_NAME); + + //THEN + assertEquals(string, expectedString); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetFloatByIndex() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + resultSet.next(); + //WHEN + final float aFloat = resultSet.getFloat(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aFloat, expectedFloat); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, float.class); + } + + @Test + void testGetFloatByName() throws SQLException { + + //GIVEN + final float expectedFloat = 4.2F; + resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + resultSet.next(); + + //WHEN + final float aFloat = resultSet.getFloat(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedFloat); + verify(rowSpy).getValue(TEST_VALUE_NAME, float.class); + } + + @Test + void testGetIntByIndex() throws SQLException { + + //GIVEN + final int expectedInt = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + resultSet.next(); + + //WHEN + final int anInt = resultSet.getInt(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(anInt, expectedInt); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, int.class); + } + + @Test + void testGetIntByName() throws SQLException { + + //GIVEN + final int expectedInt = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + resultSet.next(); + + //WHEN + final int anInt = resultSet.getInt(TEST_VALUE_NAME); + + //THEN + assertEquals(anInt, expectedInt); + verify(rowSpy).getValue(TEST_VALUE_NAME, int.class); + } + + @Test + void testGetBooleanByIndex() throws SQLException { + + //GIVEN + final boolean expectedBoolean = true; + resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + resultSet.next(); + + //WHEN + final boolean aBoolean = resultSet.getBoolean(TEST_VALUE_INDEX_JDBC); + + //THEN + assertTrue(aBoolean); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, boolean.class); + } + + @Test + void testGetByteByIndex() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + resultSet.next(); + + //WHEN + final byte aByte = resultSet.getByte(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aByte, expectedByte); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte.class); + } + + @Test + void testGetShortByIndex() throws SQLException { + + //GIVEN + final short expectedShort = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + resultSet.next(); + + //WHEN + final short aShort = resultSet.getShort(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aShort, expectedShort); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, short.class); + } + + @Test + void testGetLongByIndex() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + resultSet.next(); + + //WHEN + final long aLong = resultSet.getLong(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aLong, expectedLong); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, long.class); + } + + @Test + void testGetDoubleByIndex() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + resultSet.next(); + + //WHEN + final double aDouble = resultSet.getDouble(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aDouble, expectedDouble); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, double.class); + } + + @Test + void testGetBigDecimalByIndexWithScale() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + resultSet.next(); + + //WHEN + final BigDecimal aBigDecimal = resultSet.getBigDecimal(TEST_VALUE_INDEX_JDBC, 2); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, BigDecimal.class); + } + + @Test + void testGetBytesByIndex() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final byte[] aByte = resultSet.getBytes(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aByte, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); + } + + @Test + void testGetDateByIndex() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + resultSet.next(); + + //WHEN + final Date aDate = resultSet.getDate(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aDate, expectedDate); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Date.class); + } + + @Test + void testGetTimeByIndex() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + resultSet.next(); + + //WHEN + final Time aTime = resultSet.getTime(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aTime, expectedTime); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Time.class); + } + + @Test + void testGetTimestampByIndex() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + resultSet.next(); + + //WHEN + final Timestamp aTimestamp = resultSet.getTimestamp(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Timestamp.class); + } + + @Test + void testGetAsciiStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getAsciiStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(inputStream, "ISO-8859-1"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetUnicodeStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getUnicodeStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(inputStream, "UTF-8"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetBinaryStreamByIndex() throws SQLException, IOException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getBinaryStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final byte[] text = IOUtils.toByteArray(inputStream); + assertEquals(text, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); + } + + @Test + void testGetObjectByIndex() throws SQLException { + + //GIVEN + final Object expectedObject = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedObject), null); + resultSet.next(); + + //WHEN + final Object anObject = resultSet.getObject(TEST_VALUE_INDEX_JDBC); + + //THEN + assertEquals(anObject, expectedObject); + } + + @Test + void testGetBooleanByName() throws SQLException { + + //GIVEN + final boolean expectedBoolean = true; + resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + resultSet.next(); + + //WHEN + final boolean aBoolean = resultSet.getBoolean(TEST_VALUE_NAME); + + //THEN + assertTrue(aBoolean); + verify(rowSpy).getValue(TEST_VALUE_NAME, boolean.class); + } + + @Test + void testGetByteByName() throws SQLException { + + //GIVEN + final byte expectedByte = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + resultSet.next(); + + //WHEN + final byte aByte = resultSet.getByte(TEST_VALUE_NAME); + + //THEN + assertEquals(aByte, expectedByte); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte.class); + } + + @Test + void testGetShortByName() throws SQLException { + + //GIVEN + final short expectedShort = 42; + resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + resultSet.next(); + + //WHEN + final short aShort = resultSet.getShort(TEST_VALUE_NAME); + + //THEN + assertEquals(aShort, expectedShort); + verify(rowSpy).getValue(TEST_VALUE_NAME, short.class); + } + + @Test + void testGetLongByName() throws SQLException { + + //GIVEN + final long expectedLong = 42L; + resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + resultSet.next(); + + //WHEN + final long aLong = resultSet.getLong(TEST_VALUE_NAME); + + //THEN + assertEquals(aLong, expectedLong); + verify(rowSpy).getValue(TEST_VALUE_NAME, long.class); + } + + @Test + void testGetDoubleByName() throws SQLException { + + //GIVEN + final double expectedDouble = 4.2; + resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + resultSet.next(); + + //WHEN + final double aFloat = resultSet.getDouble(TEST_VALUE_NAME); + + //THEN + assertEquals(aFloat, expectedDouble); + verify(rowSpy).getValue(TEST_VALUE_NAME, double.class); + } + + @Test + void testGetBigDecimalByNameWithScale() throws SQLException { + + //GIVEN + final BigDecimal expectedBigDecimal = new BigDecimal(4.257); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + resultSet.next(); + + //WHEN + final BigDecimal aBigDecimal = resultSet.getBigDecimal(TEST_VALUE_NAME, 2); + + //THEN + assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); + verify(rowSpy).getValue(TEST_VALUE_NAME, BigDecimal.class); + } + + @Test + void testGetBytesByName() throws SQLException { + + //GIVEN + final byte[] expectedBytes = "Foobar".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final byte[] bytes = resultSet.getBytes(TEST_VALUE_NAME); + + //THEN + assertEquals(bytes, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); + } + + @Test + void testGetDateByName() throws SQLException { + + //GIVEN + final Date expectedDate = new Date(619912800000L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + resultSet.next(); + + //WHEN + final Date aDate = resultSet.getDate(TEST_VALUE_NAME); + + //THEN + assertEquals(aDate, expectedDate); + verify(rowSpy).getValue(TEST_VALUE_NAME, Date.class); + } + + @Test + void testGetTimeByName() throws SQLException { + + //GIVEN + final Time expectedTime = new Time(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + resultSet.next(); + + //WHEN + final Time aTime = resultSet.getTime(TEST_VALUE_NAME); + + //THEN + assertEquals(aTime, expectedTime); + verify(rowSpy).getValue(TEST_VALUE_NAME, Time.class); + } + + @Test + void testGetTimestampByName() throws SQLException { + + //GIVEN + final Timestamp expectedTimestamp = new Timestamp(619912812345L); + resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + resultSet.next(); + + //WHEN + final Timestamp aTimestamp = resultSet.getTimestamp(TEST_VALUE_NAME); + + //THEN + assertEquals(aTimestamp, expectedTimestamp); + verify(rowSpy).getValue(TEST_VALUE_NAME, Timestamp.class); + } + + @Test + void testGetAsciiStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getAsciiStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(inputStream, "ISO-8859-1"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetUnicodeStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getUnicodeStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(inputStream, "UTF-8"); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testGetBinaryStreamByName() throws SQLException, IOException { + + //GIVEN + final byte[] expectedBytes = "nuqneh".getBytes(); + resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + resultSet.next(); + + //WHEN + final InputStream inputStream = resultSet.getBinaryStream(TEST_VALUE_NAME); + + //THEN + final byte[] text = IOUtils.toByteArray(inputStream); + assertEquals(text, expectedBytes); + verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); + } + + @Test + void testGetCharacterStreamByIndex() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final Reader reader = resultSet.getCharacterStream(TEST_VALUE_INDEX_JDBC); + + //THEN + final String text = IOUtils.toString(reader); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + } + + @Test + void testGetCharacterStreamByName() throws SQLException, IOException { + + //GIVEN + final String expectedText = "nuqneh"; + resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + resultSet.next(); + + //WHEN + final Reader reader = resultSet.getCharacterStream(TEST_VALUE_NAME); + + //THEN + final String text = IOUtils.toString(reader); + assertEquals(text, expectedText); + verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + } + + @Test + void testRowUpdated() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowUpdated(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowInserted() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowInserted(); + + //THEN + assertTrue(updated); + } + + @Test + void testRowDeleted() throws SQLException { + + //GIVEN + resultSet = new JdbcResultSet(generateTestDataSet(), null); + + //WHEN + final boolean updated = resultSet.rowDeleted(); + + //THEN + assertTrue(updated); + } + + @Test + public void testToString(){ + ToStringVerifier.forClass(JdbcResultSet.class); + } + + @Test + public void equalsContract(){ + EqualsVerifier.forClass(JdbcResultSet.class); + } + + private DataSet generateTestDataSet() throws SQLException { + return generateTestDataSet(null); + } + + private DataSet generateTestDataSet(final Object testValue) throws SQLException { + final SortedMap testData = new TreeMap<>(); + testData.put("col1", "dummyValue"); + testData.put("col2", testValue); + + rowSpy = spy(new Row()); + rowSpy.setValues(testData); + return new DataSetBuilder().add(rowSpy).build(); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3c67b6f..87690fc 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,8 @@ 2.24.0 1.9.3 + + 2.6 1.4.5 @@ -275,6 +277,12 @@ ${powermock.version} test + + commons-io + commons-io + ${apache.commons.io.version} + test + @@ -316,6 +324,11 @@ powermock-api-mockito2 test + + commons-io + commons-io + test + From e7fde80404933913e7d647f7c8324f02e5b4d854 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Fri, 8 Feb 2019 14:32:22 +0100 Subject: [PATCH 19/27] (#29) Some cleanup --- .../citrus/db/driver/JdbcResultSet.java | 102 +++++++++++++++++- 1 file changed, 98 insertions(+), 4 deletions(-) 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 c0e38e1..b435dfc 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 @@ -57,7 +57,7 @@ public class JdbcResultSet implements java.sql.ResultSet { /** * Constructor using remote result set. */ - public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) throws SQLException { + public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) { this.dataSet = dataSet; this.statement = statement; } @@ -222,6 +222,7 @@ public SQLWarning getWarnings() throws SQLException { } public void clearWarnings() throws SQLException { + //currently not required } public String getCursorName() throws SQLException { @@ -261,9 +262,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 { @@ -291,6 +294,7 @@ public boolean previous() throws SQLException { } public void setFetchDirection(final int direction) throws SQLException { + //currently not required } public int getFetchDirection() throws SQLException { @@ -298,6 +302,7 @@ public int getFetchDirection() throws SQLException { } public void setFetchSize(final int rows) throws SQLException { + //currently not required } public int getFetchSize() throws SQLException { @@ -325,132 +330,175 @@ public boolean rowDeleted() throws SQLException { } public void updateNull(final int columnIndex) throws SQLException { + //currently not required } public void updateBoolean(final int columnIndex, final boolean x) throws SQLException { + //currently not required } public void updateByte(final int columnIndex, final byte x) throws SQLException { + //currently not required } public void updateShort(final int columnIndex, final short x) throws SQLException { + //currently not required } public void updateInt(final int columnIndex, final int x) throws SQLException { + //currently not required } public void updateLong(final int columnIndex, final long x) throws SQLException { + //currently not required } public void updateFloat(final int columnIndex, final float x) throws SQLException { + //currently not required } public void updateDouble(final int columnIndex, final double x) throws SQLException { + //currently not required } public void updateBigDecimal(final int columnIndex, final BigDecimal x) throws SQLException { + //currently not required } public void updateString(final int columnIndex, final String x) throws SQLException { + //currently not required } public void updateBytes(final int columnIndex, final byte[] x) throws SQLException { + //currently not required } public void updateDate(final int columnIndex, final Date x) throws SQLException { + //currently not required } public void updateTime(final int columnIndex, final Time x) throws SQLException { + //currently not required } public void updateTimestamp(final int columnIndex, final Timestamp x) throws SQLException { + //currently not required } public void updateBinaryStream(final int columnIndex, final InputStream x, final int length) throws SQLException { + //currently not required } public void updateCharacterStream(final int columnIndex, final Reader x, final int length) throws SQLException { + //currently not required } public void updateObject(final int columnIndex, final Object x, final int scale) throws SQLException { + //currently not required } public void updateObject(final int columnIndex, final Object x) throws SQLException { + //currently not required } public void updateNull(final String columnName) throws SQLException { + //currently not required } public void updateByte(final String columnName, final byte x) throws SQLException { + //currently not required } public void updateShort(final String columnName, final short x) throws SQLException { + //currently not required } public void updateInt(final String columnName, final int x) throws SQLException { + //currently not required } public void updateLong(final String columnName, final long x) throws SQLException { + //currently not required } public void updateFloat(final String columnName, final float x) throws SQLException { + //currently not required } public void updateDouble(final String columnName, final double x) throws SQLException { + //currently not required } public void updateBigDecimal(final String columnName, final BigDecimal x) throws SQLException { + //currently not required } public void updateString(final String columnName, final String x) throws SQLException { + //currently not required } public void updateBytes(final String columnName, final byte[] x) throws SQLException { + //currently not required } public void updateDate(final String columnName, final Date x) throws SQLException { + //currently not required } public void updateTime(final String columnName, final Time x) throws SQLException { + //currently not required } public void updateTimestamp(final String columnName, final Timestamp x) throws SQLException { + //currently not required } public void updateAsciiStream(final String columnName, final InputStream x, final int length) throws SQLException { + //currently not required } public void updateBinaryStream(final String columnName, final InputStream x, final int length) throws SQLException { + //currently not required } public void updateCharacterStream(final String columnName, final Reader reader, final int length) throws SQLException { + //currently not required } public void updateObject(final String columnName, final Object x, final int scale) throws SQLException { + //currently not required } 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 { @@ -497,34 +545,42 @@ public URL getURL(final String columnLabel) throws SQLException { @Override public void updateRef(final int columnIndex, final Ref x) throws SQLException { + //currently not required } @Override public void updateRef(final String columnLabel, final Ref x) throws SQLException { + //currently not required } @Override public void updateBlob(final int columnIndex, final Blob x) throws SQLException { + //currently not required } @Override public void updateBlob(final String columnLabel, final Blob x) throws SQLException { + //currently not required } @Override public void updateClob(final int columnIndex, final Clob x) throws SQLException { + //currently not required } @Override public void updateClob(final String columnLabel, final Clob x) throws SQLException { + //currently not required } @Override public void updateArray(final int columnIndex, final Array x) throws SQLException { + //currently not required } @Override public void updateArray(final String columnLabel, final Array x) throws SQLException { + //currently not required } @Override @@ -539,10 +595,12 @@ public RowId getRowId(final String columnLabel) throws SQLException { @Override public void updateRowId(final int columnIndex, final RowId x) throws SQLException { + //currently not required } @Override public void updateRowId(final String columnLabel, final RowId x) throws SQLException { + //currently not required } @Override @@ -557,18 +615,22 @@ public boolean isClosed() throws SQLException { @Override public void updateNString(final int columnIndex, final String nString) throws SQLException { + //currently not required } @Override public void updateNString(final String columnLabel, final String nString) throws SQLException { + //currently not required } @Override public void updateNClob(final int columnIndex, final NClob nClob) throws SQLException { + //currently not required } @Override public void updateNClob(final String columnLabel, final NClob nClob) throws SQLException { + //currently not required } @Override @@ -593,10 +655,12 @@ public SQLXML getSQLXML(final String columnLabel) throws SQLException { @Override public void updateSQLXML(final int columnIndex, final SQLXML xmlObject) throws SQLException { + //currently not required } @Override public void updateSQLXML(final String columnLabel, final SQLXML xmlObject) throws SQLException { + //currently not required } @Override @@ -621,124 +685,152 @@ public Reader getNCharacterStream(final String columnLabel) throws SQLException @Override public void updateNCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { + //currently not required } @Override public void updateNCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateAsciiStream(final int columnIndex, final InputStream x, final long length) throws SQLException { + //currently not required } @Override public void updateBinaryStream(final int columnIndex, final InputStream x, final long length) throws SQLException { + //currently not required } @Override public void updateCharacterStream(final int columnIndex, final Reader x, final long length) throws SQLException { + //currently not required } @Override public void updateAsciiStream(final String columnLabel, final InputStream x, final long length) throws SQLException { + //currently not required } @Override public void updateBinaryStream(final String columnLabel, final InputStream x, final long length) throws SQLException { + //currently not required } @Override public void updateCharacterStream(final String columnLabel, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateBlob(final int columnIndex, final InputStream inputStream, final long length) throws SQLException { + //currently not required } @Override public void updateBlob(final String columnLabel, final InputStream inputStream, final long length) throws SQLException { + //currently not required } @Override public void updateClob(final int columnIndex, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateClob(final String columnLabel, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateNClob(final int columnIndex, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateNClob(final String columnLabel, final Reader reader, final long length) throws SQLException { + //currently not required } @Override public void updateNCharacterStream(final int columnIndex, final Reader x) throws SQLException { + //currently not required } @Override public void updateNCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override public void updateAsciiStream(final int columnIndex, final InputStream x) throws SQLException { + //currently not required } @Override public void updateBinaryStream(final int columnIndex, final InputStream x) throws SQLException { + //currently not required } @Override public void updateCharacterStream(final int columnIndex, final Reader x) throws SQLException { + //currently not required } @Override public void updateAsciiStream(final String columnLabel, final InputStream x) throws SQLException { + //currently not required } @Override public void updateBinaryStream(final String columnLabel, final InputStream x) throws SQLException { + //currently not required } @Override public void updateCharacterStream(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override public void updateBlob(final int columnIndex, final InputStream inputStream) throws SQLException { + //currently not required } @Override public void updateBlob(final String columnLabel, final InputStream inputStream) throws SQLException { + //currently not required } @Override public void updateClob(final int columnIndex, final Reader reader) throws SQLException { + //currently not required } @Override public void updateClob(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override public void updateNClob(final int columnIndex, final Reader reader) throws SQLException { + //currently not required } @Override public void updateNClob(final String columnLabel, final Reader reader) throws SQLException { + //currently not required } @Override public T getObject(final int columnIndex, final Class type) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + throw new SQLException("Not supported JDBC result set function 'getObject(int, Class)'"); } @Override public T getObject(final String columnLabel, final Class type) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + throw new SQLException("Not supported JDBC result set function '(String, Class)'"); } public boolean wasNull()throws SQLException { @@ -746,14 +838,16 @@ public boolean wasNull()throws SQLException { } public void updateBoolean(final String columnName, final boolean x) throws SQLException { + //currently not required } public void updateAsciiStream(final int columnIndex, final InputStream x, final int length) throws SQLException { + //currently not required } public Object getObject(final int i, final Map map) throws SQLException { - throw new SQLException("Not supported JDBC result set function 'getObject'"); + throw new SQLException("Not supported JDBC result set function '(int, Map)'"); } public Ref getRef(final int i) throws SQLException { From 5654f601dcd97cb225d5b48d561cffc786df66c6 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Mon, 11 Feb 2019 17:19:57 +0100 Subject: [PATCH 20/27] corrected gpg key --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 87690fc..8fdb2e8 100644 --- a/pom.xml +++ b/pom.xml @@ -451,7 +451,7 @@ true - christoph.deppisch@consol.de + dev@citrusframework.org ${skip.gpg} --allow-weak-digest-algos From 73a7fc8480be772376c17f1e22bde200f60cabe0 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Mon, 11 Feb 2019 17:48:29 +0100 Subject: [PATCH 21/27] fixed maven assembly plugin version --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 8fdb2e8..59df82a 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,7 @@ 0.12 3.1.1 3.0.0-M1 + 3.1.1 2.7 @@ -373,6 +374,11 @@ maven-deploy-plugin ${maven.deploy.plugin.version} + + org.apache.maven.plugins + maven-assembly-plugin + ${maven.assembly.plugin.version} + org.apache.maven.plugins From ddcc1a3e70ad109660c545b012188413e6d2b72c Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 12:31:25 +0100 Subject: [PATCH 22/27] (#29) Exchanged usage of dataSet with JdbcResultSet in jdbc statements to encapsulate the dataset properly in a jdbc conform interface --- .../db/driver/JdbcCallableStatement.java | 124 +++++++++--------- .../db/driver/JdbcPreparedStatement.java | 3 +- .../citrus/db/driver/JdbcResultSet.java | 2 +- .../citrus/db/driver/JdbcStatement.java | 18 +-- .../db/driver/JdbcCallableStatementTest.java | 8 +- 5 files changed, 77 insertions(+), 78 deletions(-) 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 8de4e01..225eceb 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 @@ -16,7 +16,6 @@ package com.consol.citrus.db.driver; -import com.consol.citrus.db.driver.data.Row; import org.apache.http.client.HttpClient; import java.io.InputStream; @@ -39,12 +38,9 @@ import java.sql.Timestamp; import java.util.Calendar; import java.util.Map; -import java.util.Objects; public class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement { - private Row resultRow; - public JdbcCallableStatement(final HttpClient httpClient, final String callableStatement, final String serverUrl, @@ -64,51 +60,56 @@ public void registerOutParameter(final int parameterIndex, final int sqlType, fi @Override public boolean wasNull() throws SQLException { - final Row dataRow = getDataRow(); - if(dataRow == null){ - return false; - } - return Objects.isNull(dataRow.getLastValue()); + getRow(); + return resultSet.wasNull(); } @Override public String getString(final int parameterIndex) throws SQLException { - return (String) getDataRow().getValue(parameterIndex-1, String.class); + getRow(); + return resultSet.getString(parameterIndex); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - return (boolean) getDataRow().getValue(parameterIndex-1, boolean.class); + getRow(); + return resultSet.getBoolean(parameterIndex); } @Override public byte getByte(final int parameterIndex) throws SQLException { - return (byte) getDataRow().getValue(parameterIndex-1, byte.class); + getRow(); + return resultSet.getByte(parameterIndex); } @Override public short getShort(final int parameterIndex) throws SQLException { - return (short) getDataRow().getValue(parameterIndex-1, short.class); + getRow(); + return resultSet.getShort(parameterIndex); } @Override public int getInt(final int parameterIndex) throws SQLException { - return (int) getDataRow().getValue(parameterIndex-1, int.class); + getRow(); + return resultSet.getInt(parameterIndex); } @Override public long getLong(final int parameterIndex) throws SQLException { - return (long) getDataRow().getValue(parameterIndex-1, long.class); + getRow(); + return resultSet.getLong(parameterIndex); } @Override public float getFloat(final int parameterIndex) throws SQLException { - return (float) getDataRow().getValue(parameterIndex-1, float.class); + getRow(); + return resultSet.getFloat(parameterIndex); } @Override public double getDouble(final int parameterIndex) throws SQLException { - return (double) getDataRow().getValue(parameterIndex-1, double.class); + getRow(); + return resultSet.getDouble(parameterIndex); } @Override @@ -118,32 +119,38 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - return (byte[]) getDataRow().getValue(parameterIndex-1, byte[].class); + getRow(); + return resultSet.getBytes(parameterIndex); } @Override public Date getDate(final int parameterIndex) throws SQLException { - return (Date) getDataRow().getValue(parameterIndex-1, Date.class); + getRow(); + return resultSet.getDate(parameterIndex); } @Override public Time getTime(final int parameterIndex) throws SQLException { - return (Time) getDataRow().getValue(parameterIndex-1, Time.class); + getRow(); + return resultSet.getTime(parameterIndex); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - return (Timestamp) getDataRow().getValue(parameterIndex-1, Timestamp.class); + getRow(); + return resultSet.getTimestamp(parameterIndex); } @Override public Object getObject(final int parameterIndex) throws SQLException { - return getDataRow().getValue(parameterIndex-1); + getRow(); + return resultSet.getObject(parameterIndex); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - return (BigDecimal) getDataRow().getValue(parameterIndex-1, BigDecimal.class); + getRow(); + return resultSet.getBigDecimal(parameterIndex); } @Override @@ -338,72 +345,86 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - return (String) getDataRow().getValue(parameterName, String.class); + getRow(); + return resultSet.getString(parameterName); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - return (boolean) getDataRow().getValue(parameterName, boolean.class); + getRow(); + return resultSet.getBoolean(parameterName); } @Override public byte getByte(final String parameterName) throws SQLException { - return (byte) getDataRow().getValue(parameterName, byte.class); + getRow(); + return resultSet.getByte(parameterName); } @Override public short getShort(final String parameterName) throws SQLException { - return (short) getDataRow().getValue(parameterName, short.class); + getRow(); + return resultSet.getShort(parameterName); } @Override public int getInt(final String parameterName) throws SQLException { - return (int) getDataRow().getValue(parameterName, int.class); + getRow(); + return resultSet.getInt(parameterName); } @Override public long getLong(final String parameterName) throws SQLException { - return (long) getDataRow().getValue(parameterName, long.class); + getRow(); + return resultSet.getLong(parameterName); } @Override public float getFloat(final String parameterName) throws SQLException { - return (float) getDataRow().getValue(parameterName, float.class); + getRow(); + return resultSet.getFloat(parameterName); } @Override public double getDouble(final String parameterName) throws SQLException { - return (double) getDataRow().getValue(parameterName, double.class); + getRow(); + return resultSet.getDouble(parameterName); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - return (byte[]) getDataRow().getValue(parameterName, byte[].class); + getRow(); + return resultSet.getBytes(parameterName); } @Override public Date getDate(final String parameterName) throws SQLException { - return (Date) getDataRow().getValue(parameterName, Date.class); + getRow(); + return resultSet.getDate(parameterName); } @Override public Time getTime(final String parameterName) throws SQLException { - return (Time) getDataRow().getValue(parameterName, Time.class); + getRow(); + return resultSet.getTime(parameterName); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - return (Timestamp) getDataRow().getValue(parameterName, Timestamp.class); + getRow(); + return resultSet.getTimestamp(parameterName); } @Override public Object getObject(final String parameterName) throws SQLException { - return getDataRow().getValue(parameterName); + getRow(); + return resultSet.getObject(parameterName); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - return (BigDecimal) getDataRow().getValue(parameterName, BigDecimal.class); + getRow(); + return resultSet.getBigDecimal(parameterName); } @Override @@ -659,32 +680,9 @@ private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } - private Row getDataRow() throws SQLException { - if(Objects.isNull(resultRow)){ - resultRow = dataSet.getNextRow(); + private void getRow() throws SQLException { + if(resultSet.getRow() == 0){ + resultSet.next(); } - return resultRow; - } - - @Override - public boolean equals(final Object o) { - if (this == o) return true; - if (!(o instanceof JdbcCallableStatement)) return false; - if (!super.equals(o)) return false; - final JdbcCallableStatement that = (JdbcCallableStatement) o; - return Objects.equals(resultRow, that.resultRow); - } - - @Override - public int hashCode() { - return Objects.hash(resultRow); - } - - @Override - public String toString() { - return "JdbcCallableStatement{" + - "resultRow=" + resultRow + - ", dataSet=" + dataSet + - '}'; } } 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 a0df421..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 @@ -202,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 @@ -366,7 +366,6 @@ public String toString() { return "JdbcPreparedStatement{" + "preparedStatement='" + preparedStatement + '\'' + ", parameters=" + parameters + - ", dataSet=" + dataSet + '}'; } } 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 b435dfc..d5bcafa 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 @@ -278,7 +278,7 @@ public boolean last() throws SQLException { } public int getRow() throws SQLException { - return dataSet.getCursor() + 1; + return dataSet.getCursor(); } public boolean absolute(final int row) throws SQLException { 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 9247b3f..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 @@ -40,7 +40,7 @@ public class JdbcStatement implements Statement { 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 @@ -382,12 +384,12 @@ public boolean equals(final Object o) { return Objects.equals(httpClient, that.httpClient) && Objects.equals(serverUrl, that.serverUrl) && Objects.equals(connection, that.connection) && - Objects.equals(dataSet, that.dataSet); + Objects.equals(resultSet, that.resultSet); } @Override public int hashCode() { - return Objects.hash(httpClient, serverUrl, connection, dataSet); + return Objects.hash(httpClient, serverUrl, connection, resultSet); } @Override @@ -396,7 +398,7 @@ public String toString() { "httpClient=" + httpClient + ", serverUrl='" + serverUrl + '\'' + ", connection=" + connection + - ", dataSet=" + dataSet + + ", resultSet=" + resultSet + '}'; } } 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 index fc9565a..b61677a 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -758,16 +758,16 @@ void testGetBigDecimalByName() throws SQLException { } @Test - void testWasNullReturnsFalseAsDefault() throws SQLException { + void testWasNullReturnsTrueAsDefault() throws SQLException { //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(); + final JdbcCallableStatement callableStatement = generateCallableStatement(null); //WHEN final boolean wasNull = callableStatement.wasNull(); //THEN - assertFalse(wasNull); + assertTrue(wasNull); } @Test @@ -918,7 +918,7 @@ private JdbcCallableStatement generateCallableStatement() { private JdbcCallableStatement generateCallableStatement(final Object testValue) throws SQLException { final JdbcCallableStatement callableStatement = generateCallableStatement(); - callableStatement.dataSet = generateTestDataSet(testValue); + callableStatement.resultSet = new JdbcResultSet(generateTestDataSet(testValue), null); return callableStatement; } From 9129a333913c2dfbc1e0d55b7ccef0feb5758733 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 14:02:03 +0100 Subject: [PATCH 23/27] (#29) simplified test based on data access delegation to JdbcResultSet --- .../db/driver/JdbcCallableStatement.java | 60 +-- .../db/driver/JdbcCallableStatementTest.java | 351 +++++------------- 2 files changed, 115 insertions(+), 296 deletions(-) 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 225eceb..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 @@ -60,55 +60,55 @@ public void registerOutParameter(final int parameterIndex, final int sqlType, fi @Override public boolean wasNull() throws SQLException { - getRow(); + prepareResultSet(); return resultSet.wasNull(); } @Override public String getString(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getString(parameterIndex); } @Override public boolean getBoolean(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBoolean(parameterIndex); } @Override public byte getByte(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getByte(parameterIndex); } @Override public short getShort(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getShort(parameterIndex); } @Override public int getInt(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getInt(parameterIndex); } @Override public long getLong(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getLong(parameterIndex); } @Override public float getFloat(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getFloat(parameterIndex); } @Override public double getDouble(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getDouble(parameterIndex); } @@ -119,37 +119,37 @@ public BigDecimal getBigDecimal(final int parameterIndex, final int scale) throw @Override public byte[] getBytes(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBytes(parameterIndex); } @Override public Date getDate(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getDate(parameterIndex); } @Override public Time getTime(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getTime(parameterIndex); } @Override public Timestamp getTimestamp(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getTimestamp(parameterIndex); } @Override public Object getObject(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getObject(parameterIndex); } @Override public BigDecimal getBigDecimal(final int parameterIndex) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBigDecimal(parameterIndex); } @@ -345,85 +345,85 @@ public void setNull(final String parameterName, final int sqlType, final String @Override public String getString(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getString(parameterName); } @Override public boolean getBoolean(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBoolean(parameterName); } @Override public byte getByte(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getByte(parameterName); } @Override public short getShort(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getShort(parameterName); } @Override public int getInt(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getInt(parameterName); } @Override public long getLong(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getLong(parameterName); } @Override public float getFloat(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getFloat(parameterName); } @Override public double getDouble(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getDouble(parameterName); } @Override public byte[] getBytes(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBytes(parameterName); } @Override public Date getDate(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getDate(parameterName); } @Override public Time getTime(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getTime(parameterName); } @Override public Timestamp getTimestamp(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getTimestamp(parameterName); } @Override public Object getObject(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getObject(parameterName); } @Override public BigDecimal getBigDecimal(final String parameterName) throws SQLException { - getRow(); + prepareResultSet(); return resultSet.getBigDecimal(parameterName); } @@ -680,7 +680,7 @@ private void setOutParameter(final String parameterName) { setParameter(parameterName, "?"); } - private void getRow() throws SQLException { + private void prepareResultSet() throws SQLException { if(resultSet.getRow() == 0){ resultSet.next(); } 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 index b61677a..61f94c0 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcCallableStatementTest.java @@ -1,8 +1,5 @@ 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.http.client.HttpClient; @@ -19,33 +16,32 @@ import java.sql.Time; import java.sql.Timestamp; import java.sql.Types; -import java.util.SortedMap; -import java.util.TreeMap; +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.spy; +import static org.powermock.api.mockito.PowerMockito.when; import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertTrue; public class JdbcCallableStatementTest{ private HttpClient httpClient; private String serverUrl = "localhost"; private JdbcConnection jdbcConnection; - 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 int TEST_VALUE_INDEX = 2; private final String TEST_VALUE_NAME = "col2"; - private final JdbcCallableStatement callableStatement = generateCallableStatement(); + private JdbcCallableStatement callableStatement; - private Row rowSpy; + 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 @@ -428,477 +424,316 @@ public void testSetTimeStampWithName() throws SQLException { assertEquals(callableStatement.getParameters().get(parameterName), myTimeStamp); } - @Test - void testGetTwoValuesByIndex() throws SQLException { - - //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement("bar"); - - //WHEN - final String firstOutParameter = callableStatement.getString(1); - final String secondOutParameter = callableStatement.getString(TEST_VALUE_INDEX_JDBC); - - //THEN - assertEquals(firstOutParameter, "dummyValue"); - assertEquals(secondOutParameter, "bar"); - } - @Test void testGetStringByIndex() throws SQLException { - //GIVEN - final String expectedString = "bar"; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); - //WHEN - final String string = callableStatement.getString(TEST_VALUE_INDEX_JDBC); + callableStatement.getString(TEST_VALUE_INDEX); //THEN - assertEquals(string, expectedString); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, String.class); + verify(resultSetSpy).getString(TEST_VALUE_INDEX); } @Test void testGetBooleanByIndex() throws SQLException { - //GIVEN - final boolean expectedBoolean = true; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); - //WHEN - final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_INDEX_JDBC); + callableStatement.getBoolean(TEST_VALUE_INDEX); //THEN - assertTrue(aBoolean); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, boolean.class); + verify(resultSetSpy).getBoolean(TEST_VALUE_INDEX); } @Test void testGetByteByIndex() throws SQLException { - //GIVEN - final byte expectedByte = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); - //WHEN - final byte aByte = callableStatement.getByte(TEST_VALUE_INDEX_JDBC); + callableStatement.getByte(TEST_VALUE_INDEX); //THEN - assertEquals(aByte, expectedByte); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte.class); + verify(resultSetSpy).getByte(TEST_VALUE_INDEX); } @Test void testGetBytesByIndex() throws SQLException { - //GIVEN - final byte[] expectedBytes = "nuqneh".getBytes(); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); - //WHEN - final byte[] aByte = callableStatement.getBytes(TEST_VALUE_INDEX_JDBC); + callableStatement.getBytes(TEST_VALUE_INDEX); //THEN - assertEquals(aByte, expectedBytes); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, byte[].class); + verify(resultSetSpy).getBytes(TEST_VALUE_INDEX); } @Test void testGetShortByIndex() throws SQLException { - //GIVEN - final short expectedShort = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); - //WHEN - final short aShort = callableStatement.getShort(TEST_VALUE_INDEX_JDBC); + callableStatement.getShort(TEST_VALUE_INDEX); //THEN - assertEquals(aShort, expectedShort); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, short.class); + verify(resultSetSpy).getShort(TEST_VALUE_INDEX); } @Test void testGetIntByIndex() throws SQLException { - //GIVEN - final int expectedInt = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); - //WHEN - final int anInt = callableStatement.getInt(TEST_VALUE_INDEX_JDBC); + callableStatement.getInt(TEST_VALUE_INDEX); //THEN - assertEquals(anInt, expectedInt); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, int.class); + verify(resultSetSpy).getInt(TEST_VALUE_INDEX); } @Test void testGetLongByIndex() throws SQLException { - //GIVEN - final long expectedLong = 42L; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); - //WHEN - final long aLong = callableStatement.getLong(TEST_VALUE_INDEX_JDBC); + callableStatement.getLong(TEST_VALUE_INDEX); //THEN - assertEquals(aLong, expectedLong); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, long.class); + verify(resultSetSpy).getLong(TEST_VALUE_INDEX); } @Test void testGetFloatByIndex() throws SQLException { - //GIVEN - final float expectedFloat = 4.2F; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); - //WHEN - final float aFloat = callableStatement.getFloat(TEST_VALUE_INDEX_JDBC); + callableStatement.getFloat(TEST_VALUE_INDEX); //THEN - assertEquals(aFloat, expectedFloat); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, float.class); + verify(resultSetSpy).getFloat(TEST_VALUE_INDEX); } @Test void testGetDoubleByIndex() throws SQLException { - //GIVEN - final double expectedDouble = 4.2; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); - //WHEN - final double aDouble = callableStatement.getDouble(TEST_VALUE_INDEX_JDBC); + callableStatement.getDouble(TEST_VALUE_INDEX); //THEN - assertEquals(aDouble, expectedDouble); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, double.class); + verify(resultSetSpy).getDouble(TEST_VALUE_INDEX); } @Test - void testGetBigDecimalByIndexWithScale() throws SQLException { + void testGetBigDecimalByIndexWithScale() throws Exception { //GIVEN - final BigDecimal expectedBigDecimal = new BigDecimal(4.257); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); + final BigDecimal bigDecimalMock = mock(BigDecimal.class); + when(resultSetSpy.getBigDecimal(TEST_VALUE_INDEX)).thenReturn(bigDecimalMock); //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_INDEX_JDBC, 2); + callableStatement.getBigDecimal(TEST_VALUE_INDEX, 2); //THEN - assertEquals(aBigDecimal, expectedBigDecimal.setScale(2, RoundingMode.HALF_UP)); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, BigDecimal.class); + //noinspection ResultOfMethodCallIgnored + verify(bigDecimalMock).setScale(2, RoundingMode.HALF_UP); } @Test void testGetStringByName() throws SQLException { - //GIVEN - final String expectedString = "bar"; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedString); - //WHEN - final String string = callableStatement.getString(TEST_VALUE_NAME); + callableStatement.getString(TEST_VALUE_NAME); //THEN - assertEquals(string, expectedString); - verify(rowSpy).getValue(TEST_VALUE_NAME, String.class); + verify(resultSetSpy).getString(TEST_VALUE_NAME); } @Test void testGetBooleanByName() throws SQLException { - //GIVEN - final boolean expectedBoolean = true; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBoolean); - //WHEN - final boolean aBoolean = callableStatement.getBoolean(TEST_VALUE_NAME); + callableStatement.getBoolean(TEST_VALUE_NAME); //THEN - assertTrue(aBoolean); - verify(rowSpy).getValue(TEST_VALUE_NAME, boolean.class); + verify(resultSetSpy).getBoolean(TEST_VALUE_NAME); } @Test void testGetByteByName() throws SQLException { - //GIVEN - final byte expectedByte = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedByte); - //WHEN - final byte aByte = callableStatement.getByte(TEST_VALUE_NAME); + callableStatement.getByte(TEST_VALUE_NAME); //THEN - assertEquals(aByte, expectedByte); - verify(rowSpy).getValue(TEST_VALUE_NAME, byte.class); + verify(resultSetSpy).getByte(TEST_VALUE_NAME); } @Test void testGetShortByName() throws SQLException { - //GIVEN - final short expectedShort = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedShort); - //WHEN - final short aShort = callableStatement.getShort(TEST_VALUE_NAME); + callableStatement.getShort(TEST_VALUE_NAME); //THEN - assertEquals(aShort, expectedShort); - verify(rowSpy).getValue(TEST_VALUE_NAME, short.class); + verify(resultSetSpy).getShort(TEST_VALUE_NAME); } @Test void testGetIntByName() throws SQLException { - //GIVEN - final int expectedInt = 42; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedInt); - //WHEN - final int anInt = callableStatement.getInt(TEST_VALUE_NAME); + callableStatement.getInt(TEST_VALUE_NAME); //THEN - assertEquals(anInt, expectedInt); - verify(rowSpy).getValue(TEST_VALUE_NAME, int.class); + verify(resultSetSpy).getInt(TEST_VALUE_NAME); } @Test void testGetLongByName() throws SQLException { - //GIVEN - final long expectedLong = 42L; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedLong); - //WHEN - final long aLong = callableStatement.getLong(TEST_VALUE_NAME); + callableStatement.getLong(TEST_VALUE_NAME); //THEN - assertEquals(aLong, expectedLong); - verify(rowSpy).getValue(TEST_VALUE_NAME, long.class); + verify(resultSetSpy).getLong(TEST_VALUE_NAME); } @Test void testGetFloatByName() throws SQLException { - //GIVEN - final float expectedFloat = 4.2F; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedFloat); - //WHEN - final float aFloat = callableStatement.getFloat(TEST_VALUE_NAME); + callableStatement.getFloat(TEST_VALUE_NAME); //THEN - assertEquals(aFloat, expectedFloat); - verify(rowSpy).getValue(TEST_VALUE_NAME, float.class); + verify(resultSetSpy).getFloat(TEST_VALUE_NAME); } @Test void testGetDoubleByName() throws SQLException { - //GIVEN - final double expectedDouble = 4.2; - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDouble); - //WHEN - final double aFloat = callableStatement.getDouble(TEST_VALUE_NAME); + callableStatement.getDouble(TEST_VALUE_NAME); //THEN - assertEquals(aFloat, expectedDouble); - verify(rowSpy).getValue(TEST_VALUE_NAME, double.class); + verify(resultSetSpy).getDouble(TEST_VALUE_NAME); } @Test void testGetBytesByName() throws SQLException { - //GIVEN - final byte[] expectedBytes = "Foobar".getBytes(); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBytes); - //WHEN - final byte[] bytes = callableStatement.getBytes(TEST_VALUE_NAME); + callableStatement.getBytes(TEST_VALUE_NAME); //THEN - assertEquals(bytes, expectedBytes); - verify(rowSpy).getValue(TEST_VALUE_NAME, byte[].class); + verify(resultSetSpy).getBytes(TEST_VALUE_NAME); } @Test void testGetObjectByName() throws SQLException { - //GIVEN - final Object expectedObject = "Foobar".getBytes(); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); - //WHEN - final Object object = callableStatement.getObject(TEST_VALUE_NAME); + callableStatement.getObject(TEST_VALUE_NAME); //THEN - assertEquals(object, expectedObject); + verify(resultSetSpy).getObject(TEST_VALUE_NAME); } @Test void testGetBigDecimalByName() throws SQLException { - //GIVEN - final BigDecimal expectedBigDecimal = new BigDecimal(4.257); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); - - //WHEN - final BigDecimal aBigDecimal = callableStatement.getBigDecimal(TEST_VALUE_NAME); - - //THEN - assertEquals(aBigDecimal, expectedBigDecimal); - verify(rowSpy).getValue(TEST_VALUE_NAME, BigDecimal.class); - } - - @Test - void testWasNullReturnsTrueAsDefault() throws SQLException { - - //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(null); - - //WHEN - final boolean wasNull = callableStatement.wasNull(); - - //THEN - assertTrue(wasNull); - } - - @Test - void testWasNullIsFalse() throws SQLException { - - //GIVEN - final BigDecimal expectedBigDecimal = new BigDecimal(4.257); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedBigDecimal); - //WHEN callableStatement.getBigDecimal(TEST_VALUE_NAME); //THEN - assertFalse(callableStatement.wasNull()); + verify(resultSetSpy).getBigDecimal(TEST_VALUE_NAME); } @Test - void testWasNullIsTrue() throws SQLException { + void testDelegateWasNull() throws SQLException { - //GIVEN - final JdbcCallableStatement callableStatement = generateCallableStatement(null); //WHEN - callableStatement.getBigDecimal(TEST_VALUE_NAME); + callableStatement.wasNull(); //THEN - assertTrue(callableStatement.wasNull()); + verify(resultSetSpy).wasNull(); } @Test void testGetDateByIndex() throws SQLException { - //GIVEN - final Date expectedDate = new Date(619912800000L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); - //WHEN - final Date aDate = callableStatement.getDate(TEST_VALUE_INDEX_JDBC); + callableStatement.getDate(TEST_VALUE_INDEX); //THEN - assertEquals(aDate, expectedDate); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Date.class); + verify(resultSetSpy).getDate(TEST_VALUE_INDEX); } @Test void testGetTimeByIndex() throws SQLException { - //GIVEN - final Time expectedTime = new Time(619912812345L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); - //WHEN - final Time aTime = callableStatement.getTime(TEST_VALUE_INDEX_JDBC); + callableStatement.getTime(TEST_VALUE_INDEX); //THEN - assertEquals(aTime, expectedTime); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Time.class); + verify(resultSetSpy).getTime(TEST_VALUE_INDEX); } @Test void testGetTimestampByIndex() throws SQLException { - //GIVEN - final Timestamp expectedTimestamp = new Timestamp(619912812345L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); - //WHEN - final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_INDEX_JDBC); + callableStatement.getTimestamp(TEST_VALUE_INDEX); //THEN - assertEquals(aTimestamp, expectedTimestamp); - verify(rowSpy).getValue(TEST_VALUE_INDEX_INTERNAL, Timestamp.class); + verify(resultSetSpy).getTimestamp(TEST_VALUE_INDEX); } @Test void testGetObjectByIndex() throws SQLException { - //GIVEN - final Object expectedObject = new Timestamp(619912812345L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedObject); - //WHEN - final Object anObject = callableStatement.getObject(TEST_VALUE_INDEX_JDBC); + callableStatement.getObject(TEST_VALUE_INDEX); //THEN - assertEquals(anObject, expectedObject); + verify(resultSetSpy).getObject(TEST_VALUE_INDEX); } @Test void testGetDateByName() throws SQLException { - //GIVEN - final Date expectedDate = new Date(619912800000L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedDate); - //WHEN - final Date aDate = callableStatement.getDate(TEST_VALUE_NAME); + callableStatement.getDate(TEST_VALUE_NAME); //THEN - assertEquals(aDate, expectedDate); - verify(rowSpy).getValue(TEST_VALUE_NAME, Date.class); + verify(resultSetSpy).getDate(TEST_VALUE_NAME); } @Test void testGetTimeByName() throws SQLException { - //GIVEN - final Time expectedTime = new Time(619912812345L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTime); - //WHEN - final Time aTime = callableStatement.getTime(TEST_VALUE_NAME); + callableStatement.getTime(TEST_VALUE_NAME); //THEN - assertEquals(aTime, expectedTime); - verify(rowSpy).getValue(TEST_VALUE_NAME, Time.class); + 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 - final Timestamp expectedTimestamp = new Timestamp(619912812345L); - final JdbcCallableStatement callableStatement = generateCallableStatement(expectedTimestamp); + when(resultSetSpy.getRow()).thenReturn(0).thenReturn(1); //WHEN - final Timestamp aTimestamp = callableStatement.getTimestamp(TEST_VALUE_NAME); + callableStatement.getTimestamp(1); + callableStatement.getString(2); + //THEN - assertEquals(aTimestamp, expectedTimestamp); - verify(rowSpy).getValue(TEST_VALUE_NAME, Timestamp.class); + verify(resultSetSpy, times(2)).getRow(); + verify(resultSetSpy, times(1)).next(); } @Test @@ -915,25 +750,9 @@ private JdbcCallableStatement generateCallableStatement() { final String statement = "CALL myFunction(?,?)"; return new JdbcCallableStatement(httpClient, statement, serverUrl, jdbcConnection); } - - private JdbcCallableStatement generateCallableStatement(final Object testValue) throws SQLException { - final JdbcCallableStatement callableStatement = generateCallableStatement(); - callableStatement.resultSet = new JdbcResultSet(generateTestDataSet(testValue), null); - return callableStatement; - } - private JdbcCallableStatement generateCallableStatementWithParameter(final String parameterName) { final String statement = "CALL myFunction("+parameterName+",?)"; return new JdbcCallableStatement(httpClient,statement, serverUrl, jdbcConnection); } - 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 From e89e103fc20876858d88a13f3cc87d2344b7b150 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 19:25:37 +0100 Subject: [PATCH 24/27] (#29) Moved data state from DataSet to JdbcResultSet, added tests to match jdbc contract based on state --- .../citrus/db/driver/JdbcResultSet.java | 65 +- .../citrus/db/driver/dataset/DataSet.java | 52 +- .../db/driver/json/JsonDataSetWriter.java | 7 +- .../db/driver/xml/XmlDataSetWriter.java | 57 +- .../citrus/db/driver/JdbcResultSetTest.java | 1200 ++++++++++++++++- 5 files changed, 1250 insertions(+), 131 deletions(-) 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 d5bcafa..b15be3f 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 @@ -54,6 +54,9 @@ 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. */ @@ -64,56 +67,68 @@ public JdbcResultSet(final DataSet dataSet, final JdbcStatement statement) { @Override public boolean next() throws SQLException { + ensureNotClosed(); row = dataSet.getNextRow(); return row != null; } @Override public void close() throws SQLException { - dataSet.close(); + closed = true; } public String getString(final int columnIndex) throws SQLException { + validateAccess(); return (String) row.getValue(columnIndex-1, String.class); } public String getString(final String columnName) throws SQLException { + validateAccess(); return (String) 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(final String columnName) throws SQLException { + validateAccess(); return (float) 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(final String columnName) throws SQLException { + validateAccess(); return (int) row.getValue(columnName, int.class); } public boolean getBoolean(final int columnIndex) throws SQLException { + validateAccess(); return (boolean) row.getValue(columnIndex-1, boolean.class); } public byte getByte(final int columnIndex) throws SQLException { + validateAccess(); return (byte) row.getValue(columnIndex-1, byte.class); } public short getShort(final int columnIndex) throws SQLException { + validateAccess(); return (short) row.getValue(columnIndex-1, short.class); } public long getLong(final int columnIndex) throws SQLException { + validateAccess(); return (long) row.getValue(columnIndex-1, long.class); } public double getDouble(final int columnIndex) throws SQLException { + validateAccess(); return (double) row.getValue(columnIndex-1, double.class); } @@ -122,18 +137,22 @@ public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws S } public byte[] getBytes(final int columnIndex) throws SQLException { + validateAccess(); return (byte[]) row.getValue(columnIndex-1, byte[].class); } public Date getDate(final int columnIndex) throws SQLException { + validateAccess(); return (Date) row.getValue(columnIndex-1, Date.class); } public Time getTime(final int columnIndex) throws SQLException { + validateAccess(); return (Time) row.getValue(columnIndex-1, Time.class); } public Timestamp getTimestamp(final int columnIndex) throws SQLException { + validateAccess(); return (Timestamp) row.getValue(columnIndex-1, Timestamp.class); } @@ -150,30 +169,37 @@ public InputStream getBinaryStream(final int columnIndex) throws SQLException { } public Object getObject(final int columnIndex) throws SQLException { + validateAccess(); return row.getValue(columnIndex-1); } public BigDecimal getBigDecimal(final int columnIndex) throws SQLException { + validateAccess(); return (BigDecimal) row.getValue(columnIndex-1, BigDecimal.class); } public boolean getBoolean(final String columnName) throws SQLException { + validateAccess(); return (boolean) row.getValue(columnName, boolean.class); } public byte getByte(final String columnName) throws SQLException { + validateAccess(); return (byte) row.getValue(columnName, byte.class); } public short getShort(final String columnName) throws SQLException { + validateAccess(); return (short) row.getValue(columnName, short.class); } public long getLong(final String columnName) throws SQLException { + validateAccess(); return (long) row.getValue(columnName, long.class); } public double getDouble(final String columnName) throws SQLException { + validateAccess(); return (double) row.getValue(columnName, double.class); } @@ -182,26 +208,32 @@ public BigDecimal getBigDecimal(final String columnName, final int scale) throws } public byte[] getBytes(final String columnName) throws SQLException { + validateAccess(); return (byte[]) row.getValue(columnName, byte[].class); } public Date getDate(final String columnName) throws SQLException { + validateAccess(); return (Date) row.getValue(columnName, Date.class); } public Time getTime(final String columnName) throws SQLException { + validateAccess(); return (Time) row.getValue(columnName, Time.class); } public Timestamp getTimestamp(final String columnName) throws SQLException { + validateAccess(); return (Timestamp) row.getValue(columnName, Timestamp.class); } public Object getObject(final String columnName) throws SQLException { + validateAccess(); return row.getValue(columnName); } public BigDecimal getBigDecimal(final String columnName) throws SQLException { + validateAccess(); return (BigDecimal) row.getValue(columnName, BigDecimal.class); } @@ -234,7 +266,8 @@ public ResultSetMetaData getMetaData() throws SQLException { } public int findColumn(final String columnName) throws SQLException { - return row.getColumns().indexOf(columnName); + validateAccess(); + return row.getColumns().indexOf(columnName)+1; } public Reader getCharacterStream(final int columnIndex) throws SQLException { @@ -278,6 +311,7 @@ public boolean last() throws SQLException { } public int getRow() throws SQLException { + ensureNotClosed(); return dataSet.getCursor(); } @@ -318,14 +352,17 @@ public int getConcurrency() throws SQLException { } public boolean rowUpdated() throws SQLException { + ensureNotClosed(); return !dataSet.getRows().isEmpty(); } public boolean rowInserted() throws SQLException { + ensureNotClosed(); return !dataSet.getRows().isEmpty(); } public boolean rowDeleted() throws SQLException { + ensureNotClosed(); return !dataSet.getRows().isEmpty(); } @@ -502,9 +539,7 @@ public void moveToCurrentRow() throws SQLException { } public Statement getStatement() throws SQLException { - if (isClosed()) { - throw new SQLException("ResultSet has already been closed."); - } + ensureNotClosed(); return statement; } @@ -610,7 +645,7 @@ public int getHoldability() throws SQLException { @Override public boolean isClosed() throws SQLException { - return dataSet.isClosed(); + return closed; } @Override @@ -834,6 +869,7 @@ public T getObject(final String columnLabel, final Class type) throws SQL } public boolean wasNull()throws SQLException { + validateAccess(); return row.getLastValue() == null; } @@ -885,6 +921,23 @@ 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"); + } + } + @Override public T unwrap(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'unwrap'"); 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 f5be771..0dd01b6 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,35 +18,25 @@ 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 The next row of the dataset - * @throws SQLException In case the DataSet has been closed or no further rows are available + * 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(); - + public Row getNextRow(){ final int index = cursor.getAndIncrement(); if(rows.size() <= index){ return null; @@ -66,10 +56,8 @@ public List getColumns() { /** * Gets all rows in this dataset. * @return The rows of the Dataset - * @throws SQLException in case the DataSet has been closed */ - public List getRows() throws SQLException { - checkNotClosed(); + public List getRows() { return rows; } @@ -81,50 +69,24 @@ 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 In case the DataSet has been closed - */ - private void checkNotClosed() throws SQLException { - if (closed) { - throw new SQLException("Result set already closed"); - } - } - - /** - * Returns true if the data set is already closed, otherwise false. - * @return Whether the DataSet has been closed - */ - public boolean isClosed() { - return 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 closed == dataSet.closed && - Objects.equals(rows, dataSet.rows); + return Objects.equals(rows, dataSet.rows) && + Objects.equals(cursor, dataSet.cursor); } @Override public int hashCode() { - return Objects.hash(rows, closed, cursor); + return Objects.hash(rows, cursor); } @Override public String toString() { return "DataSet{" + "rows=" + rows + - ", closed=" + closed + ", 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/JdbcResultSetTest.java b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java index 36d6f80..81ab53c 100644 --- a/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java +++ b/driver/src/test/java/com/consol/citrus/db/driver/JdbcResultSetTest.java @@ -17,20 +17,22 @@ 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 JdbcResultSet resultSet; private Row rowSpy; private final int TEST_VALUE_INDEX_JDBC = 2; @@ -42,7 +44,7 @@ public class JdbcResultSetTest { public void testGetStringByIndex() throws SQLException { //GIVEN final String expectedString = "bar"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + final JdbcResultSet resultSet = generateResultSet(expectedString); resultSet.next(); //WHEN @@ -58,7 +60,7 @@ void testGetStringByName() throws SQLException { //GIVEN final String expectedString = "bar"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedString), null); + final JdbcResultSet resultSet = generateResultSet(expectedString); resultSet.next(); //WHEN @@ -74,7 +76,7 @@ void testGetFloatByIndex() throws SQLException { //GIVEN final float expectedFloat = 4.2F; - resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + final JdbcResultSet resultSet = generateResultSet(expectedFloat); resultSet.next(); //WHEN final float aFloat = resultSet.getFloat(TEST_VALUE_INDEX_JDBC); @@ -89,7 +91,7 @@ void testGetFloatByName() throws SQLException { //GIVEN final float expectedFloat = 4.2F; - resultSet = new JdbcResultSet(generateTestDataSet(expectedFloat), null); + final JdbcResultSet resultSet = generateResultSet(expectedFloat); resultSet.next(); //WHEN @@ -105,7 +107,7 @@ void testGetIntByIndex() throws SQLException { //GIVEN final int expectedInt = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + final JdbcResultSet resultSet = generateResultSet(expectedInt); resultSet.next(); //WHEN @@ -121,7 +123,7 @@ void testGetIntByName() throws SQLException { //GIVEN final int expectedInt = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedInt), null); + final JdbcResultSet resultSet = generateResultSet(expectedInt); resultSet.next(); //WHEN @@ -137,7 +139,7 @@ void testGetBooleanByIndex() throws SQLException { //GIVEN final boolean expectedBoolean = true; - resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + final JdbcResultSet resultSet = generateResultSet(expectedBoolean); resultSet.next(); //WHEN @@ -153,7 +155,7 @@ void testGetByteByIndex() throws SQLException { //GIVEN final byte expectedByte = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + final JdbcResultSet resultSet = generateResultSet(expectedByte); resultSet.next(); //WHEN @@ -169,7 +171,7 @@ void testGetShortByIndex() throws SQLException { //GIVEN final short expectedShort = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + final JdbcResultSet resultSet = generateResultSet(expectedShort); resultSet.next(); //WHEN @@ -185,7 +187,7 @@ void testGetLongByIndex() throws SQLException { //GIVEN final long expectedLong = 42L; - resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + final JdbcResultSet resultSet = generateResultSet(expectedLong); resultSet.next(); //WHEN @@ -201,7 +203,7 @@ void testGetDoubleByIndex() throws SQLException { //GIVEN final double expectedDouble = 4.2; - resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + final JdbcResultSet resultSet = generateResultSet(expectedDouble); resultSet.next(); //WHEN @@ -217,7 +219,7 @@ void testGetBigDecimalByIndexWithScale() throws SQLException { //GIVEN final BigDecimal expectedBigDecimal = new BigDecimal(4.257); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + final JdbcResultSet resultSet = generateResultSet(expectedBigDecimal); resultSet.next(); //WHEN @@ -233,7 +235,7 @@ void testGetBytesByIndex() throws SQLException { //GIVEN final byte[] expectedBytes = "nuqneh".getBytes(); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); resultSet.next(); //WHEN @@ -249,7 +251,7 @@ void testGetDateByIndex() throws SQLException { //GIVEN final Date expectedDate = new Date(619912800000L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + final JdbcResultSet resultSet = generateResultSet(expectedDate); resultSet.next(); //WHEN @@ -265,7 +267,7 @@ void testGetTimeByIndex() throws SQLException { //GIVEN final Time expectedTime = new Time(619912812345L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + final JdbcResultSet resultSet = generateResultSet(expectedTime); resultSet.next(); //WHEN @@ -281,7 +283,7 @@ void testGetTimestampByIndex() throws SQLException { //GIVEN final Timestamp expectedTimestamp = new Timestamp(619912812345L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + final JdbcResultSet resultSet = generateResultSet(expectedTimestamp); resultSet.next(); //WHEN @@ -297,7 +299,7 @@ void testGetAsciiStreamByIndex() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -314,7 +316,7 @@ void testGetUnicodeStreamByIndex() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -331,7 +333,7 @@ void testGetBinaryStreamByIndex() throws SQLException, IOException { //GIVEN final byte[] expectedBytes = "nuqneh".getBytes(); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); resultSet.next(); //WHEN @@ -348,7 +350,7 @@ void testGetObjectByIndex() throws SQLException { //GIVEN final Object expectedObject = new Timestamp(619912812345L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedObject), null); + final JdbcResultSet resultSet = generateResultSet(expectedObject); resultSet.next(); //WHEN @@ -358,12 +360,27 @@ void testGetObjectByIndex() throws SQLException { 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; - resultSet = new JdbcResultSet(generateTestDataSet(expectedBoolean), null); + final JdbcResultSet resultSet = generateResultSet(expectedBoolean); resultSet.next(); //WHEN @@ -379,7 +396,7 @@ void testGetByteByName() throws SQLException { //GIVEN final byte expectedByte = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedByte), null); + final JdbcResultSet resultSet = generateResultSet(expectedByte); resultSet.next(); //WHEN @@ -395,7 +412,7 @@ void testGetShortByName() throws SQLException { //GIVEN final short expectedShort = 42; - resultSet = new JdbcResultSet(generateTestDataSet(expectedShort), null); + final JdbcResultSet resultSet = generateResultSet(expectedShort); resultSet.next(); //WHEN @@ -411,7 +428,7 @@ void testGetLongByName() throws SQLException { //GIVEN final long expectedLong = 42L; - resultSet = new JdbcResultSet(generateTestDataSet(expectedLong), null); + final JdbcResultSet resultSet = generateResultSet(expectedLong); resultSet.next(); //WHEN @@ -427,7 +444,7 @@ void testGetDoubleByName() throws SQLException { //GIVEN final double expectedDouble = 4.2; - resultSet = new JdbcResultSet(generateTestDataSet(expectedDouble), null); + final JdbcResultSet resultSet = generateResultSet(expectedDouble); resultSet.next(); //WHEN @@ -443,7 +460,7 @@ void testGetBigDecimalByNameWithScale() throws SQLException { //GIVEN final BigDecimal expectedBigDecimal = new BigDecimal(4.257); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBigDecimal), null); + final JdbcResultSet resultSet = generateResultSet(expectedBigDecimal); resultSet.next(); //WHEN @@ -459,7 +476,7 @@ void testGetBytesByName() throws SQLException { //GIVEN final byte[] expectedBytes = "Foobar".getBytes(); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); resultSet.next(); //WHEN @@ -475,7 +492,7 @@ void testGetDateByName() throws SQLException { //GIVEN final Date expectedDate = new Date(619912800000L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedDate), null); + final JdbcResultSet resultSet = generateResultSet(expectedDate); resultSet.next(); //WHEN @@ -491,7 +508,7 @@ void testGetTimeByName() throws SQLException { //GIVEN final Time expectedTime = new Time(619912812345L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedTime), null); + final JdbcResultSet resultSet = generateResultSet(expectedTime); resultSet.next(); //WHEN @@ -507,7 +524,7 @@ void testGetTimestampByName() throws SQLException { //GIVEN final Timestamp expectedTimestamp = new Timestamp(619912812345L); - resultSet = new JdbcResultSet(generateTestDataSet(expectedTimestamp), null); + final JdbcResultSet resultSet = generateResultSet(expectedTimestamp); resultSet.next(); //WHEN @@ -523,7 +540,7 @@ void testGetAsciiStreamByName() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -540,7 +557,7 @@ void testGetUnicodeStreamByName() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -557,7 +574,7 @@ void testGetBinaryStreamByName() throws SQLException, IOException { //GIVEN final byte[] expectedBytes = "nuqneh".getBytes(); - resultSet = new JdbcResultSet(generateTestDataSet(expectedBytes), null); + final JdbcResultSet resultSet = generateResultSet(expectedBytes); resultSet.next(); //WHEN @@ -574,7 +591,7 @@ void testGetCharacterStreamByIndex() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -591,7 +608,7 @@ void testGetCharacterStreamByName() throws SQLException, IOException { //GIVEN final String expectedText = "nuqneh"; - resultSet = new JdbcResultSet(generateTestDataSet(expectedText), null); + final JdbcResultSet resultSet = generateResultSet(expectedText); resultSet.next(); //WHEN @@ -607,7 +624,7 @@ void testGetCharacterStreamByName() throws SQLException, IOException { void testRowUpdated() throws SQLException { //GIVEN - resultSet = new JdbcResultSet(generateTestDataSet(), null); + final JdbcResultSet resultSet = generateResultSet(); //WHEN final boolean updated = resultSet.rowUpdated(); @@ -620,7 +637,7 @@ void testRowUpdated() throws SQLException { void testRowInserted() throws SQLException { //GIVEN - resultSet = new JdbcResultSet(generateTestDataSet(), null); + final JdbcResultSet resultSet = generateResultSet(); //WHEN final boolean updated = resultSet.rowInserted(); @@ -633,7 +650,7 @@ void testRowInserted() throws SQLException { void testRowDeleted() throws SQLException { //GIVEN - resultSet = new JdbcResultSet(generateTestDataSet(), null); + final JdbcResultSet resultSet = generateResultSet(); //WHEN final boolean updated = resultSet.rowDeleted(); @@ -642,14 +659,1107 @@ void testRowDeleted() throws SQLException { assertTrue(updated); } - @Test - public void testToString(){ - ToStringVerifier.forClass(JdbcResultSet.class); + @Test(expectedExceptions = SQLException.class) + void nextThrowsExceptionIfCalledOnClosedResultSet() throws SQLException { + + //GIVEN + final JdbcResultSet resultSet = generateClosedResultSet(); + + //WHEN + resultSet.next(); + + //THEN + //exception } - @Test - public void equalsContract(){ - EqualsVerifier.forClass(JdbcResultSet.class); + @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 { From 52cf0e5cc4ebe4e2b7cbc6d9c9f299ea9c844bc7 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 19:41:44 +0100 Subject: [PATCH 25/27] (#29) Fixed issue due to removed exceptions --- .../controller/AbstractJdbcController.java | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) 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"); From 8dc69d10cdf4d4e64f7c3994de687994e8899edc Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 19:53:18 +0100 Subject: [PATCH 26/27] (#29) Added cursor to hash and equals again by accident --- .../java/com/consol/citrus/db/driver/dataset/DataSet.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) 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 0dd01b6..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 @@ -74,13 +74,12 @@ 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) && - Objects.equals(cursor, dataSet.cursor); + return Objects.equals(rows, dataSet.rows); } @Override public int hashCode() { - return Objects.hash(rows, cursor); + return Objects.hash(rows); } @Override From d5a77fe7fb566d69a826535733bd5241683e3cd9 Mon Sep 17 00:00:00 2001 From: Sven Hettwer Date: Tue, 12 Feb 2019 20:07:00 +0100 Subject: [PATCH 27/27] (#29) removed redundancy --- .../com/consol/citrus/db/driver/JdbcResultSet.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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 b15be3f..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 @@ -352,18 +352,15 @@ public int getConcurrency() throws SQLException { } public boolean rowUpdated() throws SQLException { - ensureNotClosed(); - return !dataSet.getRows().isEmpty(); + return rowModified(); } public boolean rowInserted() throws SQLException { - ensureNotClosed(); - return !dataSet.getRows().isEmpty(); + return rowModified(); } public boolean rowDeleted() throws SQLException { - ensureNotClosed(); - return !dataSet.getRows().isEmpty(); + return rowModified(); } public void updateNull(final int columnIndex) throws SQLException { @@ -938,6 +935,11 @@ private void ensureNotClosed() throws SQLException { } } + private boolean rowModified() throws SQLException { + ensureNotClosed(); + return !dataSet.getRows().isEmpty(); + } + @Override public T unwrap(final Class iface) throws SQLException { throw new SQLException("Not supported JDBC result set function 'unwrap'");