-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Made BeanProperty repeatable. - Added BeanProperty#targets, allowing users to specify which mappings the specified BeanProperty should apply to. - Compatibility with existing usages of the BeanProperty-annotation. Closes: #188
- Loading branch information
1 parent
e326fa8
commit 74e2a58
Showing
14 changed files
with
328 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/io/beanmapper/exceptions/DuplicateBeanPropertyTargetException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.beanmapper.exceptions; | ||
|
||
public class DuplicateBeanPropertyTargetException extends BeanMappingException { | ||
|
||
private static final String MESSAGE_TEMPLATE = "Multiple BeanProperty-annotations for a single property, contain reference to the same target-class. %s"; | ||
|
||
public DuplicateBeanPropertyTargetException(String message) { | ||
super(MESSAGE_TEMPLATE.formatted(message)); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
src/test/java/io/beanmapper/annotations/BeanPropertyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package io.beanmapper.annotations; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import io.beanmapper.BeanMapper; | ||
import io.beanmapper.annotations.model.bean_property.ComplexInvalidPersonForm; | ||
import io.beanmapper.annotations.model.bean_property.ComplexPerson; | ||
import io.beanmapper.annotations.model.bean_property.ComplexPersonResult; | ||
import io.beanmapper.annotations.model.bean_property.InvalidPersonForm; | ||
import io.beanmapper.annotations.model.bean_property.Person; | ||
import io.beanmapper.annotations.model.bean_property.PersonForm; | ||
import io.beanmapper.annotations.model.bean_property.PersonResult; | ||
import io.beanmapper.config.BeanMapperBuilder; | ||
import io.beanmapper.exceptions.DuplicateBeanPropertyTargetException; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class BeanPropertyTest { | ||
|
||
private BeanMapper beanMapper; | ||
|
||
@BeforeEach | ||
void prepareBeanMapper() { | ||
beanMapper = new BeanMapperBuilder() | ||
.setApplyStrictMappingConvention(false) | ||
.addPackagePrefix(BeanMapper.class) | ||
.build(); | ||
} | ||
|
||
@Test | ||
void testMultipleBeanPropertiesFormToEntity() { | ||
var personForm = new PersonForm("Henk", "Jan"); | ||
var person = assertDoesNotThrow(() -> beanMapper.map(personForm, Person.class)); | ||
assertEquals("Henk Jan", person.getName()); | ||
} | ||
|
||
@Test | ||
void testMultipleBeanPropertiesEntityToResult() { | ||
var person = new Person("Henk Jan"); | ||
var personResult = assertDoesNotThrow(() -> beanMapper.map(person, PersonResult.class)); | ||
assertEquals("Henk Jan", personResult.fullName); | ||
} | ||
|
||
@Test | ||
void testMultipleBeanPropertiesFormToResult() { | ||
var personForm = new PersonForm("Henk", "Jan"); | ||
var personResult = assertDoesNotThrow(() -> beanMapper.map(personForm, PersonResult.class)); | ||
assertEquals("Henk Jan", personResult.fullName); | ||
} | ||
|
||
@Test | ||
void testInvalidBeanPropertiesShouldThrow() { | ||
var personForm = new InvalidPersonForm("Henk", "Jan"); | ||
assertThrows(DuplicateBeanPropertyTargetException.class, () -> beanMapper.map(personForm, Person.class)); | ||
} | ||
|
||
@Test | ||
void testComplexInvalidBeanPropertiesShouldThrow() { | ||
var personForm = new ComplexInvalidPersonForm("Henk", "Jan"); | ||
assertThrows(DuplicateBeanPropertyTargetException.class, () -> beanMapper.map(personForm, ComplexPerson.class)); | ||
assertThrows(DuplicateBeanPropertyTargetException.class, () -> beanMapper.map(personForm, ComplexPersonResult.class)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/test/java/io/beanmapper/annotations/model/bean_property/ComplexInvalidPersonForm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.beanmapper.annotations.model.bean_property; | ||
|
||
import io.beanmapper.annotations.BeanProperty; | ||
|
||
public class ComplexInvalidPersonForm { | ||
|
||
@BeanProperty("firstName") | ||
@BeanProperty(name = "firstName", targets = { ComplexPerson.class, ComplexPersonResult.class }) | ||
@BeanProperty(name = "f_name", targets = { ComplexPerson.class, ComplexPersonResult.class }) | ||
public String firstName; | ||
|
||
@BeanProperty("lastName") | ||
@BeanProperty(name = "lastName", targets = { ComplexPerson.class, ComplexPersonResult.class }) | ||
@BeanProperty(name = "l_name", targets = { ComplexPerson.class, ComplexPersonResult.class }) | ||
public String lastName; | ||
|
||
public ComplexInvalidPersonForm(String firstName, String lastName) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
} | ||
} |
Oops, something went wrong.