Skip to content

Commit

Permalink
corrected equals and to string tests
Browse files Browse the repository at this point in the history
  • Loading branch information
svettwer committed Feb 15, 2019
1 parent 80fbdf8 commit 38777f8
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.Calendar;
import java.util.Map;

public class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement {
public final class JdbcCallableStatement extends JdbcPreparedStatement implements CallableStatement {

public JdbcCallableStatement(final HttpClient httpClient,
final String callableStatement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
}

@Override
public boolean equals(final Object o) {
public final boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof JdbcConnection)) return false;
final JdbcConnection that = (JdbcConnection) o;
Expand All @@ -452,7 +452,7 @@ public boolean equals(final Object o) {
}

@Override
public int hashCode() {
public final int hashCode() {
return Objects.hash(httpClient, serverUrl, closed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class JdbcPreparedStatement extends JdbcStatement implements PreparedStatement {

private final String preparedStatement;
private Map<String, Object> parameters = new TreeMap<>();
private final Map<String, Object> parameters = new TreeMap<>();

public JdbcPreparedStatement(final HttpClient httpClient, final String preparedStatement, final String serverUrl, JdbcConnection connection) {
super(httpClient, serverUrl, connection);
Expand Down Expand Up @@ -347,26 +347,31 @@ Map<String, Object> getParameters() {
}

@Override
public boolean equals(final Object o) {
public final boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof JdbcPreparedStatement)) return false;
if (!super.equals(o)) return false;
if (o.getClass().equals(JdbcStatement.class)) return false;
final JdbcPreparedStatement that = (JdbcPreparedStatement) o;
return Objects.equals(preparedStatement, that.preparedStatement) &&
Objects.equals(parameters, that.parameters);
Objects.equals(parameters, that.parameters)&&
Objects.equals(httpClient, that.httpClient) &&
Objects.equals(serverUrl, that.serverUrl) &&
Objects.equals(connection, that.connection) &&
Objects.equals(resultSet, that.resultSet);
}

@Override
public int hashCode() {
return Objects.hash(preparedStatement, parameters);
public final int hashCode() {
return Objects.hash(super.hashCode(), preparedStatement, parameters);
}

@Override
public String toString() {
return "JdbcPreparedStatement{" +
"preparedStatement='" + preparedStatement + '\'' +
", parameters=" + parameters +
'}';
", resultSet=" + resultSet +
"} " + super.toString();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -951,18 +951,19 @@ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
}

@Override
public boolean equals(final Object o) {
public final 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);
final JdbcResultSet resultSet = (JdbcResultSet) o;
return closed == resultSet.closed &&
Objects.equals(dataSet, resultSet.dataSet) &&
Objects.equals(statement, resultSet.statement) &&
Objects.equals(row, resultSet.row);
}

@Override
public int hashCode() {
return Objects.hash(dataSet, statement, row);
public final int hashCode() {
return Objects.hash(dataSet, statement, row, closed);
}

@Override
Expand All @@ -971,6 +972,7 @@ public String toString() {
"dataSet=" + dataSet +
", statement=" + statement +
", row=" + row +
", closed=" + closed +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

public class JdbcStatement implements Statement {

private final HttpClient httpClient;
private final String serverUrl;
private final JdbcConnection connection;
final HttpClient httpClient;
final String serverUrl;
final JdbcConnection connection;

protected JdbcResultSet resultSet;

Expand Down Expand Up @@ -379,6 +379,8 @@ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (Objects.isNull(o)) return false;
if (o.getClass().equals(JdbcPreparedStatement.class)) return false;
if (!(o instanceof JdbcStatement)) return false;
final JdbcStatement that = (JdbcStatement) o;
return Objects.equals(httpClient, that.httpClient) &&
Expand Down
28 changes: 15 additions & 13 deletions driver/src/main/java/com/consol/citrus/db/driver/data/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,6 @@ private <T> Object convertData(final Object value, final Class<T> clazz) {
return Objects.isNull(value) ? null : ConvertUtils.convert(value, clazz);
}

@Override
public boolean equals(final Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Row row = (Row) o;
return Objects.equals(values, row.values);
}

@Override
public int hashCode() {
return Objects.hash(values);
}

/**
* Gets the lastValue.
* @return The last value as object
Expand All @@ -127,10 +114,25 @@ public Object getLastValue() {
return lastValue;
}

@Override
public final boolean equals(final Object o) {
if (this == o) return true;
if (!(o instanceof Row)) return false;
final Row row = (Row) o;
return Objects.equals(values, row.values) &&
Objects.equals(lastValue, row.lastValue);
}

@Override
public final int hashCode() {
return Objects.hash(values, lastValue);
}

@Override
public String toString() {
return "Row{" +
"values=" + values +
", lastValue=" + lastValue +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ public int getCursor() {
}

@Override
public boolean equals(final Object o) {
public final 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);
}

@Override
public int hashCode() {
public final int hashCode() {
return Objects.hash(rows);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import com.consol.citrus.db.driver.data.Row;

import java.sql.SQLException;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author Christoph Deppisch
Expand All @@ -45,9 +46,8 @@ public DataSetBuilder add(Row... rows) {
/**
* Build new data set instance.
* @return
* @throws SQLException
*/
public DataSet build() throws SQLException {
public DataSet build() {
DataSet dataSet = new DataSet();
dataSet.getRows().addAll(rows);
return dataSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.dataset.DataSet;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.http.client.HttpClient;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -738,12 +740,18 @@ void verifyResultSetHandling() throws SQLException {

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcCallableStatement.class);
ToStringVerifier.forClass(JdbcCallableStatement.class).verify();
}

@Test
public void equalsContract(){
EqualsVerifier.forClass(JdbcCallableStatement.class);
EqualsVerifier.forClass(JdbcCallableStatement.class)
.withPrefabValues(
JdbcResultSet.class,
new JdbcResultSet(mock(DataSet.class), mock(JdbcStatement.class)),
new JdbcResultSet(mock(DataSet.class), mock(JdbcStatement.class)))
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

private JdbcCallableStatement generateCallableStatement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
Expand Down Expand Up @@ -335,11 +336,13 @@ public void testPrepareStatementIoExceptionIsWrappedInSqlException() throws Exce

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcStatement.class);
ToStringVerifier.forClass(JdbcConnection.class).verify();
}

@Test
public void equalsContract(){
EqualsVerifier.forClass(JdbcStatement.class);
EqualsVerifier.forClass(JdbcConnection.class)
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.dataset.DataSet;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.http.client.HttpClient;
import org.powermock.api.mockito.PowerMockito;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -70,11 +73,18 @@ public void testSetParameterOverwritesValue() {

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcPreparedStatement.class);
ToStringVerifier.forClass(JdbcPreparedStatement.class).verify();
}

@Test
public void equalsContract(){
EqualsVerifier.forClass(JdbcPreparedStatement.class);
EqualsVerifier.forClass(JdbcPreparedStatement.class)
.withPrefabValues(
JdbcResultSet.class,
new JdbcResultSet(PowerMockito.mock(DataSet.class), PowerMockito.mock(JdbcStatement.class)),
new JdbcResultSet(PowerMockito.mock(DataSet.class), PowerMockito.mock(JdbcStatement.class)))
.withRedefinedSuperclass()
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.consol.citrus.db.driver.dataset.DataSetBuilder;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.HttpClient;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -1733,12 +1735,18 @@ void testFindColumn() throws SQLException {

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcResultSet.class);
ToStringVerifier.forClass(JdbcResultSet.class).verify();
}

@Test
public void equalsContract(){
EqualsVerifier.forClass(JdbcResultSet.class);
EqualsVerifier.forClass(JdbcResultSet.class)
.withPrefabValues(
JdbcStatement.class,
new JdbcStatement(mock(HttpClient.class), "asdf", mock(JdbcConnection.class)),
new JdbcStatement(mock(HttpClient.class), "asdf", mock(JdbcConnection.class)))
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

private JdbcResultSet generateClosedResultSet() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

package com.consol.citrus.db.driver;

import com.consol.citrus.db.driver.dataset.DataSet;
import com.jparams.verifier.tostring.ToStringVerifier;
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
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;
import org.powermock.api.mockito.PowerMockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -251,11 +254,18 @@ public void testCloseIoExceptionIsWrappedInSqlException() throws Exception{

@Test
public void testToString(){
ToStringVerifier.forClass(JdbcStatement.class);
ToStringVerifier.forClass(JdbcStatement.class).verify();
}

@Test
public void equalsContract(){
EqualsVerifier.forClass(JdbcStatement.class);
EqualsVerifier.forClass(JdbcStatement.class)
.withPrefabValues(
JdbcResultSet.class,
new JdbcResultSet(PowerMockito.mock(DataSet.class), PowerMockito.mock(JdbcStatement.class)),
new JdbcResultSet(PowerMockito.mock(DataSet.class), PowerMockito.mock(JdbcStatement.class)))
.withRedefinedSubclass(JdbcPreparedStatement.class)
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}
}
Loading

0 comments on commit 38777f8

Please sign in to comment.