@Override public ReflectionMappers createCopy() { return new ReflectionMappers(this); } }
@Override public RowMapper<T> specialize(ResultSet rs, StatementContext ctx) throws SQLException { final List<String> columnNames = getColumnNames(rs); final List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers(); final List<String> unmatchedColumns = new ArrayList<>(columnNames); RowMapper<T> mapper = specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns) .orElseThrow(() -> new IllegalArgumentException(String.format(NO_MATCHING_COLUMNS, type))); if (ctx.getConfig(ReflectionMappers.class).isStrictMatching() && anyColumnsStartWithPrefix(unmatchedColumns, prefix, columnNameMatchers)) { throw new IllegalArgumentException( String.format(UNMATCHED_COLUMNS_STRICT, type.getSimpleName(), unmatchedColumns)); } return mapper; }
@Test public void shouldThrowOnMismatchedColumnsStrictMatch() throws Exception { ctx.getConfig(ReflectionMappers.class).setStrictMatching(true); mockColumns("longField", "misspelledField"); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers();
@Test public void shouldThrowOnMismatchedColumnsStrictMatch() throws Exception { ctx.getConfig(ReflectionMappers.class).setStrictMatching(true); mockColumns("longField", "misspelledField"); assertThatThrownBy(() -> mapper.map(resultSet, ctx)).isInstanceOf(IllegalArgumentException.class); }
@Override public RowMapper<T> specialize(ResultSet rs, StatementContext ctx) throws SQLException { final List<String> columnNames = getColumnNames(rs); final List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers(); final List<String> unmatchedColumns = new ArrayList<>(columnNames); RowMapper<T> result = specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns) .orElseThrow(() -> new IllegalArgumentException(String.format(NO_MATCHING_COLUMNS, type))); if (ctx.getConfig(ReflectionMappers.class).isStrictMatching() && anyColumnsStartWithPrefix(unmatchedColumns, prefix, columnNameMatchers)) { throw new IllegalArgumentException( String.format(UNMATCHED_COLUMNS_STRICT, type.getSimpleName(), unmatchedColumns)); } return result; }
@Test public void nestedPrefixParametersStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(ConstructorMapper.factory(NestedPrefixBean.class)); assertThat(handle .createQuery("select i nested_i, s nested_s from bean") .mapTo(NestedPrefixBean.class) .findOnly()) .extracting("nested.s", "nested.i") .containsExactly("3", 2); assertThatThrownBy(() -> handle .createQuery("select i nested_i, s nested_s, 1 as other from bean") .mapTo(NestedPrefixBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match parameters for columns: [other]"); assertThatThrownBy(() -> handle .createQuery("select i nested_i, s nested_s, 1 as nested_other from bean") .mapTo(NestedPrefixBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match parameters for columns: [nested_other]"); }
@Override public ReflectionMappers createCopy() { return new ReflectionMappers(this); } }
@Override public RowMapper<T> specialize(ResultSet rs, StatementContext ctx) throws SQLException { final List<String> columnNames = getColumnNames(rs); final List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers(); final List<String> unmatchedColumns = new ArrayList<>(columnNames); RowMapper<T> mapper = specialize0(ctx, columnNames, columnNameMatchers, unmatchedColumns) .orElseThrow(() -> new IllegalArgumentException(String.format( UNMATCHED_CONSTRUCTOR_PARAMETERS, factory))); if (ctx.getConfig(ReflectionMappers.class).isStrictMatching() && anyColumnsStartWithPrefix(unmatchedColumns, prefix, columnNameMatchers)) { throw new IllegalArgumentException( String.format(UNMATCHED_COLUMNS_STRICT, factory, unmatchedColumns)); } return mapper; }
@Test public void testNestedPrefixStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class)); handle.execute("insert into something (id, name, integerValue) values (1, 'foo', 5)"); // three, sir! assertThat(handle .createQuery("select id nested_id, name nested_name, integerValue from something") .mapTo(NestedPrefixBean.class) .findOnly()) .extracting("nested.id", "nested.name", "integerValue") .containsExactly(1, "foo", 5); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as other from something") .mapTo(NestedPrefixBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match properties for columns: [other]"); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as nested_other from something") .mapTo(NestedPrefixBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match properties for columns: [nested_other]"); }
List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers(); if ( ctx.getConfig(ReflectionMappers.class).isStrictMatching() && columnNumbers.size() != metadata.getColumnCount()) { throw new IllegalArgumentException(String.format("Mapping bean type %s " +
@Test public void testNestedStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(BeanMapper.factory(NestedBean.class)); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .createQuery("select id, name from something") .mapTo(NestedBean.class) .findOnly()) .extracting("nested.id", "nested.name") .containsExactly(1, "foo"); assertThatThrownBy(() -> handle .createQuery("select id, name, 1 as other from something") .mapTo(NestedBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match properties for columns: [other]"); }
List<ColumnNameMatcher> columnNameMatchers = ctx.getConfig(ReflectionMappers.class).getColumnNameMatchers(); if ( ctx.getConfig(ReflectionMappers.class).isStrictMatching() && columnNumbers.size() != metadata.getColumnCount()) { throw new IllegalArgumentException(String.format("Mapping fields for type %s " +
@Test public void testNestedPrefixStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(FieldMapper.factory(NestedPrefixThing.class)); handle.execute("insert into something (id, name, integerValue) values (1, 'foo', 5)"); // three, sir! assertThat(handle .createQuery("select id nested_id, name nested_name, integerValue from something") .mapTo(NestedPrefixThing.class) .findOnly()) .extracting("nested.i", "nested.s", "integerValue") .containsExactly(1, "foo", 5); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as other from something") .mapTo(NestedPrefixThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [other]"); assertThatThrownBy(() -> handle .createQuery("select id nested_id, name nested_name, 1 as nested_other from something") .mapTo(NestedPrefixThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [nested_other]"); }
@Test public void nestedParametersStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(ConstructorMapper.factory(NestedBean.class)); assertThat(dbRule.getSharedHandle() .registerRowMapper(ConstructorMapper.factory(NestedBean.class)) .select("select s, i from bean") .mapTo(NestedBean.class) .findOnly()) .extracting("nested.s", "nested.i") .containsExactly("3", 2); assertThatThrownBy(() -> handle .createQuery("select s, i, 1 as other from bean") .mapTo(NestedBean.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match parameters for columns: [other]"); }
@Test public void testNestedStrict() { Handle handle = dbRule.getSharedHandle(); handle.getConfig(ReflectionMappers.class).setStrictMatching(true); handle.registerRowMapper(FieldMapper.factory(NestedThing.class)); handle.execute("insert into something (id, name) values (1, 'foo')"); assertThat(handle .registerRowMapper(FieldMapper.factory(NestedThing.class)) .select("select id, name from something") .mapTo(NestedThing.class) .findOnly()) .extracting("nested.i", "nested.s") .containsExactly(1, "foo"); assertThatThrownBy(() -> handle .createQuery("select id, name, 1 as other from something") .mapTo(NestedThing.class) .findOnly()) .isInstanceOf(IllegalArgumentException.class) .hasMessageContaining("could not match fields for columns: [other]"); }