Search in sources :

Example 6 with ProcessorTest

use of org.mapstruct.ap.testutil.ProcessorTest in project mapstruct by mapstruct.

the class JodaConversionTest method testSourceToTargetMappingForStrings.

@ProcessorTest
@EnabledOnJre(JRE.JAVA_8)
public // See https://bugs.openjdk.java.net/browse/JDK-8211262, there is a difference in the default formats on Java 9+
void testSourceToTargetMappingForStrings() {
    Source src = new Source();
    src.setLocalTime(new LocalTime(0, 0));
    src.setLocalDate(new LocalDate(2014, 1, 1));
    src.setLocalDateTime(new LocalDateTime(2014, 1, 1, 0, 0));
    src.setDateTime(new DateTime(2014, 1, 1, 0, 0, 0, DateTimeZone.UTC));
    // with given format
    Target target = SourceTargetMapper.INSTANCE.sourceToTarget(src);
    assertThat(target).isNotNull();
    assertThat(target.getDateTime()).isEqualTo("01.01.2014 00:00 UTC");
    assertThat(target.getLocalDateTime()).isEqualTo("01.01.2014 00:00");
    assertThat(target.getLocalDate()).isEqualTo("01.01.2014");
    assertThat(target.getLocalTime()).isEqualTo("00:00");
    // and now with default mappings
    target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping(src);
    assertThat(target).isNotNull();
    assertThat(target.getDateTime()).isEqualTo("1. Januar 2014 00:00:00 UTC");
    assertThat(target.getLocalDateTime()).isEqualTo("1. Januar 2014 00:00:00");
    assertThat(target.getLocalDate()).isEqualTo("1. Januar 2014");
    assertThat(target.getLocalTime()).isEqualTo("00:00:00");
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) LocalTime(org.joda.time.LocalTime) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) ProcessorTest(org.mapstruct.ap.testutil.ProcessorTest) EnabledOnJre(org.junit.jupiter.api.condition.EnabledOnJre)

Example 7 with ProcessorTest

use of org.mapstruct.ap.testutil.ProcessorTest in project mapstruct by mapstruct.

the class JodaConversionTest method testStringToLocalDate.

@ProcessorTest
public void testStringToLocalDate() {
    String dateTimeAsString = "01.01.2014";
    Target target = new Target();
    target.setLocalDate(dateTimeAsString);
    LocalDate sourceDate = new LocalDate(2014, 1, 1);
    Source src = SourceTargetMapper.INSTANCE.targetToSourceLocalDateMapped(target);
    assertThat(src).isNotNull();
    assertThat(src.getLocalDate()).isEqualTo(sourceDate);
}
Also used : LocalDate(org.joda.time.LocalDate) ProcessorTest(org.mapstruct.ap.testutil.ProcessorTest)

Example 8 with ProcessorTest

use of org.mapstruct.ap.testutil.ProcessorTest in project mapstruct by mapstruct.

the class IgnorePropertyTest method shouldInheritIgnoreByDefaultFromBase.

@ProcessorTest
@IssueKey("1933")
public void shouldInheritIgnoreByDefaultFromBase() {
    WorkBenchDto workBenchDto = new WorkBenchDto();
    workBenchDto.setArticleName("MyBench");
    workBenchDto.setArticleDescription("Beautiful");
    workBenchDto.setCreationDate(new Date());
    workBenchDto.setModificationDate(new Date());
    WorkBenchEntity benchTarget = ToolMapper.INSTANCE.mapBench(workBenchDto);
    assertThat(benchTarget).isNotNull();
    assertThat(benchTarget.getArticleName()).isNull();
    assertThat(benchTarget.getDescription()).isEqualTo("Beautiful");
    assertThat(benchTarget.getKey()).isNull();
    assertThat(benchTarget.getModificationDate()).isNull();
    assertThat(benchTarget.getCreationDate()).isNull();
}
Also used : Date(java.util.Date) IssueKey(org.mapstruct.ap.testutil.IssueKey) ProcessorTest(org.mapstruct.ap.testutil.ProcessorTest)

Example 9 with ProcessorTest

use of org.mapstruct.ap.testutil.ProcessorTest in project mapstruct by mapstruct.

the class DecoratorTest method shouldApplyCustomMappers.

@IssueKey("173")
@ProcessorTest
@WithClasses({ Person2Mapper.class, Person2MapperDecorator.class, Person2.class, PersonDto2.class, Employer.class, EmployerDto.class, EmployerMapper.class, SportsClub.class, SportsClubDto.class })
public void shouldApplyCustomMappers() {
    // given
    Calendar birthday = Calendar.getInstance();
    birthday.set(1928, Calendar.MAY, 23);
    Person2 person = new Person2("Gary", "Crant", birthday.getTime(), new Address("42 Ocean View Drive"));
    person.setEmployer(new Employer("ACME"));
    person.setSportsClub(new SportsClub("SC Duckburg"));
    // when
    PersonDto2 personDto = Person2Mapper.INSTANCE.personToPersonDto(person);
    // then
    assertThat(personDto).isNotNull();
    assertThat(personDto.getName()).isEqualTo("Gary Crant");
    assertThat(personDto.getAddress()).isNotNull();
    assertThat(personDto.getAddress().getAddressLine()).isEqualTo("42 Ocean View Drive");
    assertThat(personDto.getEmployer()).isNotNull();
    assertThat(personDto.getEmployer().getName()).isNotNull();
    assertThat(personDto.getEmployer().getName()).isEqualTo("ACME");
    assertThat(personDto.getSportsClub()).isNotNull();
    assertThat(personDto.getSportsClub().getName()).isNotNull();
    assertThat(personDto.getSportsClub().getName()).isEqualTo("SC Duckburg");
}
Also used : Calendar(java.util.Calendar) IssueKey(org.mapstruct.ap.testutil.IssueKey) ProcessorTest(org.mapstruct.ap.testutil.ProcessorTest) WithClasses(org.mapstruct.ap.testutil.WithClasses)

Example 10 with ProcessorTest

use of org.mapstruct.ap.testutil.ProcessorTest in project mapstruct by mapstruct.

the class DecoratorTest method shouldInvokeDecoratorMethods.

@ProcessorTest
@WithClasses({ PersonMapper.class, PersonMapperDecorator.class })
public void shouldInvokeDecoratorMethods() {
    // given
    Calendar birthday = Calendar.getInstance();
    birthday.set(1928, Calendar.MAY, 23);
    Person person = new Person("Gary", "Crant", birthday.getTime(), new Address("42 Ocean View Drive"));
    // when
    PersonDto personDto = PersonMapper.INSTANCE.personToPersonDto(person);
    // then
    assertThat(personDto).isNotNull();
    assertThat(personDto.getName()).isEqualTo("Gary Crant");
    assertThat(personDto.getAddress()).isNotNull();
    assertThat(personDto.getAddress().getAddressLine()).isEqualTo("42 Ocean View Drive");
}
Also used : Calendar(java.util.Calendar) ProcessorTest(org.mapstruct.ap.testutil.ProcessorTest) WithClasses(org.mapstruct.ap.testutil.WithClasses)

Aggregations

ProcessorTest (org.mapstruct.ap.testutil.ProcessorTest)314 WithClasses (org.mapstruct.ap.testutil.WithClasses)118 GeneratedSource (org.mapstruct.ap.testutil.runner.GeneratedSource)51 IssueKey (org.mapstruct.ap.testutil.IssueKey)39 XmlGregorianCalendarBean (org.mapstruct.ap.test.builtin.jodatime.bean.XmlGregorianCalendarBean)24 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)22 Date (java.util.Date)16 LocalDateTimeBean (org.mapstruct.ap.test.builtin.jodatime.bean.LocalDateTimeBean)16 Calendar (java.util.Calendar)13 LocalDateTime (org.joda.time.LocalDateTime)13 CarDto (org.mapstruct.ap.test.complex._target.CarDto)13 Car (org.mapstruct.ap.test.complex.source.Car)13 PersonDto (org.mapstruct.ap.test.constructor.PersonDto)13 GregorianCalendar (java.util.GregorianCalendar)12 XmlGregorianCalendarProperty (org.mapstruct.ap.test.builtin.bean.XmlGregorianCalendarProperty)12 Song (org.mapstruct.ap.test.nestedsourceproperties.source.Song)12 BigDecimal (java.math.BigDecimal)11 ChartEntry (org.mapstruct.ap.test.nestedsourceproperties._target.ChartEntry)11 DriverAndCarDto (org.mapstruct.ap.test.nullvaluemapping._target.DriverAndCarDto)11 DateTime (org.joda.time.DateTime)10