@Test public void shouldHandleColumNameWithUnderscores() throws Exception { mockColumns("LONG_FIELD"); Long aLongVal = 100L; mockLongResult(aLongVal); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@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 shouldHandleNullValue() throws Exception { mockColumns("LoNgfielD"); mockAllNullsResult(); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isNull(); }
@Test public void shouldThrowOnProtectedSetter() throws Exception { mockColumns("protectedStringField"); String expected = "string"; when(resultSet.getString(1)).thenReturn(expected); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldBeCaseInSensitiveOfColumnWithUnderscoresAndPropertyNames() throws Exception { mockColumns("LoNg_FiElD"); Long aLongVal = 100L; mockLongResult(aLongVal); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@Test public void shouldThrowOnPackagePrivateSetter() throws Exception { mockColumns("packagePrivateIntField"); when(resultSet.getInt(1)).thenReturn(200); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldSetValueOnPublicSetter() throws Exception { mockColumns("longField"); Long aLongVal = 100L; mockLongResult(aLongVal); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@Test public void shouldThrowOnPrivateSetter() throws Exception { mockColumns("privateBigDecimalField"); when(resultSet.getBigDecimal(1)).thenReturn(BigDecimal.ONE); when(resultSet.wasNull()).thenReturn(false); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Test public void shouldBeCaseInSensitiveOfColumnAndPropertyNames() throws Exception { mockColumns("LoNgfielD"); Long aLongVal = 100L; mockLongResult(aLongVal); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(aLongVal); }
@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 shouldSetValuesOnPublicSetter() throws Exception { mockColumns("longField"); Long expected = 1L; mockLongResult(expected); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(expected); }
@Test public void shouldThrowOnTotalMismatch() throws Exception { mockColumns("somethingElseEntirely"); 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 shouldSetValuesInSuperClassProperties() 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 = BeanMapper.of(DerivedBean.class); DerivedBean derivedBean = mapper.map(resultSet, ctx); assertThat(derivedBean.getLongField()).isEqualTo(aLongVal); assertThat(derivedBean.getBlongField()).isEqualTo(bLongVal); }
@Test public void shouldUseRegisteredMapperForUnknownPropertyType() throws Exception { handle.registerColumnMapper(new ValueTypeMapper()); mockColumns("longField", "valueTypeField"); Long expected = 123L; when(resultSet.getLong(1)).thenReturn(expected); when(resultSet.getString(2)).thenReturn("foo"); when(resultSet.wasNull()).thenReturn(false); SampleBean sampleBean = mapper.map(resultSet, ctx); assertThat(sampleBean.getLongField()).isEqualTo(expected); assertThat(sampleBean.getValueTypeField()).isEqualTo(ValueType.valueOf("foo")); }
@Test public void shouldThrowOnMismatchedColumnsStrictMatch() throws Exception { ctx.getConfig(ReflectionMappers.class).setStrictMatching(true); mockColumns("longField", "misspelledField"); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }