Skip to content

Commit

Permalink
prefer setter annotations over getter annotations
Browse files Browse the repository at this point in the history
As all of the mappers deal with mapping data onto beans (which they use the setters for),
they should also prefer the annotations found on the setter over the annotations on the getter.

This PR also splits the very unwieldy and confusing BeanMapperTest into three separate tests,
one focusing on general bean mapper properties, one for the PropagateNullable tests and one for
Nested tests.
  • Loading branch information
hgschmie committed Sep 12, 2022
1 parent 581a51e commit 2af7db5
Show file tree
Hide file tree
Showing 4 changed files with 382 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ private QualifiedType<?> determineQualifiedType() {
@Override
public <A extends Annotation> Optional<A> getAnnotation(Class<A> anno) {
return annoCache.computeIfAbsent(anno, x ->
Stream.of(descriptor.getReadMethod(), descriptor.getWriteMethod())
Stream.of(descriptor.getWriteMethod(), descriptor.getReadMethod())
.filter(Objects::nonNull)
.map(m -> m.getAnnotation(anno))
.filter(Objects::nonNull)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jdbi.v3.core.mapper.reflect;

import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Something;
import org.jdbi.v3.core.junit5.H2DatabaseExtension;
import org.jdbi.v3.core.mapper.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.jdbi.v3.core.junit5.H2DatabaseExtension.SOMETHING_INITIALIZER;

public class BeanMapperNestedTest {

@RegisterExtension
public H2DatabaseExtension h2Extension = H2DatabaseExtension.instance().withInitializer(SOMETHING_INITIALIZER);

@Test
public void testNested() {
Handle handle = h2Extension.getSharedHandle();

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)
.one())
.extracting("nested.id", "nested.name")
.containsExactly(1, "foo");
}

@Test
public void testNestedStrict() {
Handle handle = h2Extension.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)
.one())
.extracting("nested.id", "nested.name")
.containsExactly(1, "foo");

assertThatThrownBy(() -> handle
.createQuery("select id, name, 1 as other from something")
.mapTo(NestedBean.class)
.one())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("could not match properties for columns: [other]");
}

@Test
public void testNestedNotReturned() {
Handle handle = h2Extension.getSharedHandle();

handle.registerRowMapper(BeanMapper.factory(NestedBean.class));
assertThat(handle
.createQuery("select 42 as testValue")
.mapTo(NestedBean.class)
.one())
.extracting("testValue", "nested")
.containsExactly(42, null);
}

@Test
public void testNestedPrefix() {
Handle handle = h2Extension.getSharedHandle();

handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class));

handle.execute("insert into something (id, name) values (1, 'foo')");

assertThat(handle
.createQuery("select id nested_id, name nested_name from something")
.mapTo(NestedPrefixBean.class)
.one())
.extracting("nested.id", "nested.name")
.containsExactly(1, "foo");
}

@Test
public void testNestedPrefixStrict() {
Handle handle = h2Extension.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)
.one())
.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)
.one())
.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)
.one())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("could not match properties for columns: [nested_other]");
}

@Test
public void testNestedPrefixNotReturned() {
Handle handle = h2Extension.getSharedHandle();

handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class));
assertThat(handle
.createQuery("select 42 as integerValue")
.mapTo(NestedPrefixBean.class)
.one())
.extracting("integerValue", "nested")
.containsExactly(42, null);
}

public static class NestedBean {

private Integer testValue;
private Something nested;

public Integer getTestValue() {
return testValue;
}

public void setTestValue(Integer testValue) {
this.testValue = testValue;
}

public Something getNested() {
return nested;
}

@Nested
public void setNested(Something nested) {
this.nested = nested;
}
}

public static class NestedPrefixBean {

private Integer integerValue;
private Something nested;

public Integer getIntegerValue() {
return integerValue;
}

public void setIntegerValue(Integer integerValue) {
this.integerValue = integerValue;
}

public Something getNested() {
return nested;
}

@Nested("nested")
public void setNested(Something nested) {
this.nested = nested;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jdbi.v3.core.mapper.reflect;

import javax.annotation.Nullable;

import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.junit5.H2DatabaseExtension;
import org.jdbi.v3.core.mapper.Nested;
import org.jdbi.v3.core.mapper.PropagateNull;
import org.jdbi.v3.core.mapper.reflect.ConstructorMapperTest.ClassPropagateNullThing;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.jdbi.v3.core.junit5.H2DatabaseExtension.SOMETHING_INITIALIZER;

public class BeanMapperPropagateNullableTest {

@RegisterExtension
public H2DatabaseExtension h2Extension = H2DatabaseExtension.instance().withInitializer(SOMETHING_INITIALIZER);

@Test
public void propagateNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle
.registerRowMapper(BeanMapper.factory(PropagateNullThing.class))
.select("SELECT null as testValue, 'foo' as s")
.mapTo(PropagateNullThing.class)
.one())
.isNull();
}

@Test
public void propagateNotNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle
.registerRowMapper(BeanMapper.factory(PropagateNullThing.class))
.select("SELECT 42 as testValue, 'foo' as s")
.mapTo(PropagateNullThing.class)
.one())
.extracting("testValue", "s")
.containsExactly(42, "foo");
}

@Test
public void nestedPropagateNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle
.registerRowMapper(BeanMapper.factory(NestedPropagateNullThing.class))
.select("SELECT 42 as integerValue, null as testValue, 'foo' as s")
.mapTo(NestedPropagateNullThing.class)
.one())
.extracting("integerValue", "nested")
.containsExactly(42, null);
}

@Test
public void nestedPropagateNotNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle
.registerRowMapper(BeanMapper.factory(NestedPropagateNullThing.class))
.select("SELECT 42 as integerValue, 60 as testValue, 'foo' as s")
.mapTo(NestedPropagateNullThing.class)
.one())
.extracting("integerValue", "nested.testValue", "nested.s")
.containsExactly(42, 60, "foo");
}

@Test
public void classPropagateNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle.select("select 42 as \"value\", null as fk")
.map(BeanMapper.of(ClassPropagateNullThing.class))
.one())
.isNull();
}

@Test
public void classPropagateNotNull() {
Handle handle = h2Extension.getSharedHandle();

assertThat(handle.select("select 42 as \"value\", 'a' as fk")
.map(BeanMapper.of(ClassPropagateNullThing.class))
.one())
.extracting(cpnt -> cpnt.value)
.isEqualTo(42);
}

public static class NestedPropagateNullThing {

private Integer integerValue;
private PropagateNullThing nested;

public Integer getIntegerValue() {
return integerValue;
}

public void setIntegerValue(@Nullable Integer integerValue) {
this.integerValue = integerValue;
}

public PropagateNullThing getNested() {
return nested;
}

@Nested
public void setNested(PropagateNullThing nested) {
this.nested = nested;
}
}

public static class PropagateNullThing {

private int testValue;
private String s;

public int getTestValue() {
return testValue;
}

@PropagateNull
public void setTestValue(int testValue) {
this.testValue = testValue;
}

public String getS() {
return s;
}

public void setS(String s) {
this.s = s;
}
}
}
Loading

0 comments on commit 2af7db5

Please sign in to comment.