Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklakariada committed Jan 20, 2024
1 parent 1da33cd commit a716233
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.ArrayList;
import java.util.List;

import org.itsallcode.jdbc.UncheckedSQLException;

/**
* Represents the metadata of a single column.
*
Expand All @@ -14,11 +16,15 @@
*/
public record ColumnMetaData(int columnIndex, String name, String label, ColumnType type) {

static List<ColumnMetaData> create(final ResultSet resultSet) throws SQLException {
return create(resultSet.getMetaData());
static List<ColumnMetaData> create(final ResultSet resultSet) {
try {
return create(resultSet.getMetaData());
} catch (final SQLException e) {
throw new UncheckedSQLException("Error extracting meta data", e);
}
}

static List<ColumnMetaData> create(final ResultSetMetaData metaData)
private static List<ColumnMetaData> create(final ResultSetMetaData metaData)
throws SQLException {
final int columnCount = metaData.getColumnCount();
final List<ColumnMetaData> columns = new ArrayList<>(columnCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

import org.itsallcode.jdbc.UncheckedSQLException;
import org.itsallcode.jdbc.dialect.DbDialect;
Expand All @@ -27,8 +26,8 @@ public class GenericRowMapper<T> implements RowMapper<T> {
* type.
*/
public GenericRowMapper(final DbDialect dialect, final ColumnValuesConverter<T> converter) {
this.dialect = dialect;
this.converter = converter;
this.dialect = Objects.requireNonNull(dialect, "dialect");
this.converter = Objects.requireNonNull(converter, "converter");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.itsallcode.jdbc.resultset.generic;

import java.sql.*;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.List;

import org.itsallcode.jdbc.UncheckedSQLException;

/**
* A wrapper for {@link ResultSetMetaData} to simplify usage.
*
Expand All @@ -19,11 +18,7 @@ public record SimpleMetaData(List<ColumnMetaData> columns) {
* @return simple metadata
*/
public static SimpleMetaData create(final ResultSet resultSet) {
try {
return new SimpleMetaData(ColumnMetaData.create(resultSet));
} catch (final SQLException e) {
throw new UncheckedSQLException("Error extracting meta data", e);
}
return new SimpleMetaData(ColumnMetaData.create(resultSet));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.itsallcode.jdbc.resultset.generic;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.when;

import java.sql.*;

import org.itsallcode.jdbc.UncheckedSQLException;
import org.itsallcode.jdbc.dialect.ColumnValueExtractor;
import org.itsallcode.jdbc.dialect.DbDialect;
import org.itsallcode.jdbc.resultset.generic.GenericRowMapper.ColumnValuesConverter;
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 GenericRowMapperTest {

private static final int ROW_NUM = 42;
@Mock
DbDialect dbDialectMock;
@Mock
ColumnValuesConverter<RowType> converterMock;
@Mock
ResultSet resultSetMock;
@Mock
ResultSetMetaData resultSetMetadataMock;
@Mock
ColumnValueExtractor columnValueExtractorMock;
final RowType expectedRow = new RowType();

@Test
void mapRowNoColumn() throws SQLException {
final RowType row = mapRow(0);
assertThat(row).isSameAs(expectedRow);
}

@Test
void mapRowSuccess() throws SQLException {
final RowType row = mapRow(2);
assertThat(row).isSameAs(expectedRow);
}

@Test
void mapRowGetObjectFails() throws SQLException {
when(columnValueExtractorMock.getObject(same(resultSetMock), eq(1))).thenThrow(new SQLException("expected"));
assertThatThrownBy(() -> mapRow(2)).isInstanceOf(UncheckedSQLException.class).hasMessage(
"Error extracting value for row 42 / column ColumnMetaData[columnIndex=1, name=null, label=null, type=ColumnType[jdbcType=NULL, typeName=null, className=null, precision=0, scale=0, displaySize=0]]: expected");
}

RowType mapRow(final int columnCount) throws SQLException {
when(resultSetMock.getMetaData()).thenReturn(resultSetMetadataMock);
when(resultSetMetadataMock.getColumnCount()).thenReturn(columnCount);
lenient().when(dbDialectMock.createExtractor(any(ColumnMetaData.class))).thenReturn(columnValueExtractorMock);
lenient().when(converterMock.mapRow(any(Row.class))).thenReturn(expectedRow);
return new GenericRowMapper<>(dbDialectMock, converterMock).mapRow(resultSetMock, ROW_NUM);
}

record RowType() {
}
}

0 comments on commit a716233

Please sign in to comment.