diff --git a/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java b/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java index 813730c..3be676b 100644 --- a/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java +++ b/src/main/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatement.java @@ -5,10 +5,20 @@ import org.itsallcode.jdbc.ParameterMapper; +/** + * A {@link PreparedStatement} that delegates all methods and converts parameter + * value of {@link #convert(Object)} using a {@link ParameterMapper}. + */ public class ConvertingPreparedStatement extends DelegatingPreparedStatement { private final ParameterMapper parameterMapper; + /** + * Create a new instance. + * + * @param delegate delegate + * @param parameterMapper parameter mapper + */ public ConvertingPreparedStatement(final PreparedStatement delegate, final ParameterMapper parameterMapper) { super(delegate); this.parameterMapper = parameterMapper; diff --git a/src/test/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatementTest.java b/src/test/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatementTest.java new file mode 100644 index 0000000..3380f5a --- /dev/null +++ b/src/test/java/org/itsallcode/jdbc/statement/ConvertingPreparedStatementTest.java @@ -0,0 +1,33 @@ +package org.itsallcode.jdbc.statement; + +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +import org.itsallcode.jdbc.ParameterMapper; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ConvertingPreparedStatementTest { + @Mock + PreparedStatement delegateMock; + @Mock + ParameterMapper parameterMapperMock; + + @SuppressWarnings("resource") + @Test + void setObject() throws SQLException { + final Object o1 = new Object(); + final Object o2 = new Object(); + when(parameterMapperMock.map(same(o1))).thenReturn(o2); + new ConvertingPreparedStatement(delegateMock, parameterMapperMock).setObject(1, o1); + verify(delegateMock).setObject(eq(1), same(o2)); + } +}