Skip to content

Commit

Permalink
Fix #9 Add double and float to standard types
Browse files Browse the repository at this point in the history
  • Loading branch information
ljacqu committed Mar 24, 2018
1 parent a1a2a70 commit cf708f8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/main/java/ch/jalu/datasourcecolumns/StandardTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public final class StandardTypes<T> implements ColumnType<T> {
/** Boolean type. */
public static final ColumnType<Boolean> BOOLEAN = new StandardTypes<>(Boolean.class);

/** Double type. */
public static final ColumnType<Double> DOUBLE = new StandardTypes<>(Double.class);

/** Float type. */
public static final ColumnType<Float> FLOAT = new StandardTypes<>(Float.class);


private final Class<T> clazz;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ protected <T> ResultSetGetter<T> createResultSetGetter(ColumnType<T> type) {
resultSetGetter = getTypeNullable(ResultSet::getInt, 0);
} else if (type == StandardTypes.BOOLEAN) {
resultSetGetter = getTypeNullable(ResultSet::getBoolean, false);
} else if (type == StandardTypes.DOUBLE) {
resultSetGetter = getTypeNullable(ResultSet::getDouble, 0.0);
} else if (type == StandardTypes.FLOAT) {
resultSetGetter = getTypeNullable(ResultSet::getFloat, 0.0f);
} else {
throw new IllegalArgumentException("Unhandled type '" + type + "'");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public abstract class AbstractSqlColumnsHandlerTest {
private static final ColumnImpl<String> COL_EMAIL = new ColumnImpl<>("email", StandardTypes.STRING);
private static final ColumnImpl<Long> COL_LAST_LOGIN = new ColumnImpl<>("last_login", StandardTypes.LONG);
private static final ColumnImpl<Integer> COL_IS_LOCKED = new ColumnImpl<>("is_locked", StandardTypes.INTEGER);
private static final ColumnImpl<Float> COL_RATIO_FLOAT = new ColumnImpl<>("ratio", StandardTypes.FLOAT);
private static final ColumnImpl<Double> COL_RATIO_DOUBLE = new ColumnImpl<>("ratio", StandardTypes.DOUBLE);

private Connection connection;
private SqlColumnsHandler<SampleContext, Integer> handler;
Expand Down Expand Up @@ -153,15 +155,15 @@ public void shouldRetrieveMultipleValues() throws SQLException {
assertThat(hansValues.get(SampleColumns.NAME), equalTo("Hans"));
assertThat(hansValues.get(SampleColumns.IS_LOCKED), equalTo(1));
assertThat(hansValues.get(SampleColumns.LAST_LOGIN), equalTo(77665544L));
verifyThrowsException(() -> hansValues.get(SampleColumns.ID));
verifyThrowsNoValueAvailableException(() -> hansValues.get(SampleColumns.ID));

assertThat(finnValues.get(SampleColumns.NAME), equalTo("Finn"));
assertThat(finnValues.get(SampleColumns.IS_LOCKED), equalTo(0));
assertThat(finnValues.get(SampleColumns.LAST_LOGIN), nullValue());
verifyThrowsException(() -> finnValues.get(SampleColumns.IS_ACTIVE));
verifyThrowsNoValueAvailableException(() -> finnValues.get(SampleColumns.IS_ACTIVE));

assertThat(nonExistent.rowExists(), equalTo(false));
verifyThrowsException(() -> nonExistent.get(SampleColumns.NAME));
verifyThrowsNoValueAvailableException(() -> nonExistent.get(SampleColumns.NAME));
}

@Test
Expand All @@ -179,15 +181,15 @@ public void shouldHandleRetrievalOfMultipleValuesIncludingEmpty() throws SQLExce
assertThat(hansValues.get(SampleColumns.NAME), equalTo("Hans"));
assertThat(hansValues.get(SampleColumns.IS_LOCKED), equalTo(1));
assertThat(hansValues.get(SampleColumns.LAST_LOGIN), nullValue());
verifyThrowsException(() -> hansValues.get(SampleColumns.ID));
verifyThrowsNoValueAvailableException(() -> hansValues.get(SampleColumns.ID));

assertThat(finnValues.get(SampleColumns.NAME), equalTo("Finn"));
assertThat(finnValues.get(SampleColumns.IS_LOCKED), equalTo(0));
assertThat(finnValues.get(SampleColumns.LAST_LOGIN), nullValue());
verifyThrowsException(() -> finnValues.get(SampleColumns.IS_ACTIVE));
verifyThrowsNoValueAvailableException(() -> finnValues.get(SampleColumns.IS_ACTIVE));

assertThat(nonExistent.rowExists(), equalTo(false));
verifyThrowsException(() -> nonExistent.get(SampleColumns.LAST_LOGIN));
verifyThrowsNoValueAvailableException(() -> nonExistent.get(SampleColumns.LAST_LOGIN));
}

@Test
Expand All @@ -204,10 +206,10 @@ public void shouldRetrieveMultipleAllEmptyColumnsSuccessfully() throws SQLExcept
assertThat(hansValues.rowExists(), equalTo(true));
assertThat(hansValues.get(SampleColumns.EMAIL), nullValue());
assertThat(hansValues.get(SampleColumns.LAST_LOGIN), nullValue());
verifyThrowsException(() -> hansValues.get(SampleColumns.ID));
verifyThrowsNoValueAvailableException(() -> hansValues.get(SampleColumns.ID));

assertThat(nonExistent.rowExists(), equalTo(false));
verifyThrowsException(() -> nonExistent.get(SampleColumns.LAST_LOGIN));
verifyThrowsNoValueAvailableException(() -> nonExistent.get(SampleColumns.LAST_LOGIN));
}

@Test
Expand Down Expand Up @@ -511,7 +513,7 @@ public void shouldHandleInsertWithEmptyColumns() throws SQLException {
}

@Test
public void shouldThrowExceptionForInsertWithNoNonEmptyColumns() throws SQLException {
public void shouldThrowExceptionForInsertWithNoNonEmptyColumns() {
// given
context.setEmptyOptions(true, true, false);
UpdateValues<SampleContext> values =
Expand Down Expand Up @@ -631,7 +633,22 @@ public void shouldRetrieveValuesAfterCaseInsensitiveCheck() throws SQLException
assertThat(names, containsInAnyOrder("Igor", "Louis", "Finn"));
}

private static void verifyThrowsException(Runnable runnable) {
@Test
public void shouldRetrieveFloatsAndDoubles() throws SQLException {
// given / when
DataSourceValue<Double> ratioDouble = handler.retrieve(4, COL_RATIO_DOUBLE);
DataSourceValue<Float> ratioFloat = handler.retrieve(4, COL_RATIO_FLOAT);
DataSourceValue<Double> ratioDoubleEmpty = handler.retrieve(6, COL_RATIO_DOUBLE);
DataSourceValue<Float> ratioFloatEmpty = handler.retrieve(6, COL_RATIO_FLOAT);

// then
assertThat(ratioDouble.getValue(), equalTo(-4.04));
assertThat(ratioFloat.getValue(), equalTo(-4.04f));
assertThat(ratioDoubleEmpty.getValue(), nullValue());
assertThat(ratioFloatEmpty.getValue(), nullValue());
}

private static void verifyThrowsNoValueAvailableException(Runnable runnable) {
IllegalArgumentException ex = expectException(IllegalArgumentException.class, runnable::run);
assertThat(ex.getMessage(), containsString("No value available for column"));
}
Expand Down
27 changes: 14 additions & 13 deletions src/test/resources/sample-database.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@ create table testingdata (
is_locked tinyint not null,
is_active tinyint not null default 3,
last_login bigint default -123,
ratio float,
primary key(id)
);

insert into testingdata (id, username, ip, email, is_locked, is_active, last_login)
insert into testingdata (id, username, ip, email, is_locked, is_active, last_login, ratio)
VALUES
( 1, 'Alex', '111.111.111.111', NULL, 1, 0, 123456),
( 2, 'Brett', '111.111.111.111', 'test@example.com', 0, 1, 123456),
( 3, 'Cody', '22.22.22.22', 'test@example.com', 0, 0, 888888),
( 4, 'Dan', '22.22.22.22', NULL, 1, 1, NULL),
( 5, 'Emily', NULL, NULL, 0, 1, 888888),
( 6, 'Finn', '111.111.111.111', 'finn@example.org', 0, 0, NULL),
( 7, 'Gary', '44.144.41.144', 'test@example.com', 0, 1, 123456),
( 8, 'Hans', NULL, 'other@test.tld', 1, 0, 77665544),
( 9, 'Igor', '22.22.22.22', 'other@test.tld', 0, 1, 725124),
(10, 'Jake', '44.144.41.144', NULL, 0, 0, 123456),
(11, 'Keane', '22.22.22.22', 'test@example.com', 0, 1, 888888),
(12, 'Louis', NULL, 'other@test.tld', 0, 1, 732452);
( 1, 'Alex', '111.111.111.111', NULL, 1, 0, 123456, NULL),
( 2, 'Brett', '111.111.111.111', 'test@example.com', 0, 1, 123456, 3.0),
( 3, 'Cody', '22.22.22.22', 'test@example.com', 0, 0, 888888, 2.21),
( 4, 'Dan', '22.22.22.22', NULL, 1, 1, NULL, -4.04),
( 5, 'Emily', NULL, NULL, 0, 1, 888888, NULL),
( 6, 'Finn', '111.111.111.111', 'finn@example.org', 0, 0, NULL, NULL),
( 7, 'Gary', '44.144.41.144', 'test@example.com', 0, 1, 123456, 32.59),
( 8, 'Hans', NULL, 'other@test.tld', 1, 0, 77665544, 6.18),
( 9, 'Igor', '22.22.22.22', 'other@test.tld', 0, 1, 725124, -7.41),
(10, 'Jake', '44.144.41.144', NULL, 0, 0, 123456, NULL),
(11, 'Keane', '22.22.22.22', 'test@example.com', 0, 1, 888888, NULL),
(12, 'Louis', NULL, 'other@test.tld', 0, 1, 732452, 147.532);

0 comments on commit cf708f8

Please sign in to comment.