@Test public void shouldThrowOnPropertyTypeWithoutRegisteredMapper() throws Exception { mockColumns("longField", "valueTypeField"); when(resultSet.getLong(1)).thenReturn(123L); when(resultSet.getObject(2)).thenReturn(new Object()); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldHandleEmptyResult() throws Exception { mockColumns(); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean).isNotNull(); }
@Test public void shouldNotThrowOnMismatchedColumns() throws Exception { mockColumns("longField", "extraColumn"); Long expected = 666L; when(resultSet.getLong(1)).thenReturn(expected); when(resultSet.getString(2)).thenReturn("foo"); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(expected); }
@Test public void shouldSetValueOnPrivateField() throws Exception { mockColumns("longField"); Long aLongVal = 100L; when(resultSet.getLong(1)).thenReturn(aLongVal); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@Test public void shouldBeCaseInSensitiveOfColumnAndFieldNames() throws Exception { mockColumns("LoNgfielD"); Long aLongVal = 100L; when(resultSet.getLong(1)).thenReturn(aLongVal); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@Test public void shouldThrowOnTotalMismatch() throws Exception { mockColumns("somethingElseEntirely"); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldSetValuesInSuperClassFields() throws Exception { mockColumns("longField", "blongField"); Long aLongVal = 100L; Long bLongVal = 200L; when(resultSet.getLong(1)).thenReturn(aLongVal); when(resultSet.getLong(2)).thenReturn(bLongVal); when(resultSet.wasNull()).thenReturn(false); RowMapper<DerivedBean> mapper = FieldMapper.of(DerivedBean.class); DerivedBean derivedBean = mapper.map(resultSet, ctx); assertThat(derivedBean.getLongField()).isEqualTo(aLongVal); assertThat(derivedBean.getBlongField()).isEqualTo(bLongVal); }
@Test public void shouldHandleNullValue() throws Exception { mockColumns("LoNgfielD"); when(resultSet.getLong(1)).thenReturn(0L); when(resultSet.wasNull()).thenReturn(true); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isNull(); }
@Test public void shouldUseRegisteredMapperForUnknownPropertyType() throws Exception { handle.registerColumnMapper(new ValueTypeMapper()); mockColumns("longField", "valueTypeField"); when(resultSet.getLong(1)).thenReturn(123L); when(resultSet.getString(2)).thenReturn("foo"); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); Long expected = 123L; assertThat(sampleBean.getLongField()).isEqualTo(expected); assertThat(sampleBean.getValueTypeField()).isEqualTo(ValueType.valueOf("foo")); }
@Test public void shouldSetValuesOnAllFieldAccessTypes() throws Exception { mockColumns("longField", "protectedStringField", "packagePrivateIntField", "privateBigDecimalField"); Long aLongVal = 100L; String aStringVal = "something"; int aIntVal = 1; BigDecimal aBigDecimal = BigDecimal.TEN; when(resultSet.getLong(1)).thenReturn(aLongVal); when(resultSet.getString(2)).thenReturn(aStringVal); when(resultSet.getInt(3)).thenReturn(aIntVal); when(resultSet.getBigDecimal(4)).thenReturn(aBigDecimal); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); assertThat(sampleBean.getPrivateBigDecimalField()).isEqualTo(aBigDecimal); assertThat(sampleBean.getPackagePrivateIntField()).isEqualTo(aIntVal); assertThat(sampleBean.getProtectedStringField()).isEqualTo(aStringVal); }
@Test public void shouldThrowOnMismatchedColumnsStrictMatch() throws Exception { ctx.getConfig(ReflectionMappers.class).setStrictMatching(true); mockColumns("longField", "misspelledField"); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }