#1608 Make sure that property names for fluent setters starting with is are handled properly (#1620)

This commit is contained in:
Filip Hrisafov 2018-10-13 08:54:09 +02:00 committed by Sjaak Derksen
parent fa1ab4b781
commit f17ddcfb18
5 changed files with 154 additions and 2 deletions

View File

@ -156,8 +156,8 @@ public class DefaultAccessorNamingStrategy implements AccessorNamingStrategy {
@Override @Override
public String getPropertyName(ExecutableElement getterOrSetterMethod) { public String getPropertyName(ExecutableElement getterOrSetterMethod) {
String methodName = getterOrSetterMethod.getSimpleName().toString(); String methodName = getterOrSetterMethod.getSimpleName().toString();
if ( methodName.startsWith( "is" ) || methodName.startsWith( "get" ) || methodName.startsWith( "set" ) ) { if ( methodName.startsWith( "get" ) || methodName.startsWith( "set" ) ) {
return IntrospectorUtils.decapitalize( methodName.substring( methodName.startsWith( "is" ) ? 2 : 3 ) ); return IntrospectorUtils.decapitalize( methodName.substring( 3 ) );
} }
else if ( isFluentSetter( getterOrSetterMethod ) ) { else if ( isFluentSetter( getterOrSetterMethod ) ) {
return methodName; return methodName;

View File

@ -0,0 +1,32 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1608;
/**
* @author Filip Hrisafov
*/
public class Book {
private String isbn;
private int issueYear;
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public int getIssueYear() {
return issueYear;
}
public void setIssueYear(int issueYear) {
this.issueYear = issueYear;
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1608;
/**
* @author Filip Hrisafov
*/
public class BookDto {
private final String isbn;
private final int issueYear;
public BookDto(String isbn, int issueYear) {
this.isbn = isbn;
this.issueYear = issueYear;
}
public String getIsbn() {
return isbn;
}
public int getIssueYear() {
return issueYear;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
private String isbn;
private int issueYear;
public String getIsbn() {
return isbn;
}
public Builder isbn(String isbn) {
this.isbn = isbn;
return this;
}
public int getIssueYear() {
return issueYear;
}
public Builder setIssueYear(int issueYear) {
this.issueYear = issueYear;
return this;
}
public BookDto build() {
return new BookDto( isbn, issueYear );
}
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1608;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
/**
* @author Filip Hrisafov
*/
@Mapper
public interface Issue1608Mapper {
Issue1608Mapper INSTANCE = Mappers.getMapper( Issue1608Mapper.class );
BookDto map(Book source);
}

View File

@ -0,0 +1,39 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.test.bugs._1608;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Filip Hrisafov
*/
@RunWith(AnnotationProcessorTestRunner.class)
@IssueKey("1608")
@WithClasses({
Issue1608Mapper.class,
Book.class,
BookDto.class
})
public class Issue1608Test {
@Test
public void shouldCorrectlyUseFluentSettersStartingWithIs() {
Book book = new Book();
book.setIsbn( "978-3-16-148410-0" );
book.setIssueYear( 2018 );
BookDto bookDto = Issue1608Mapper.INSTANCE.map( book );
assertThat( bookDto.getIsbn() ).isEqualTo( "978-3-16-148410-0" );
assertThat( bookDto.getIssueYear() ).isEqualTo( 2018 );
}
}