Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[#3133] change test to assert against expected Files
  • Loading branch information
zyberzebra committed Sep 6, 2024
commit ed75a59626d09bb12069e64aaeee8bcfb36e6643
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
*/
package org.mapstruct.ap.test.nullcheck.redundant;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.GeneratedSource;

import static java.lang.System.lineSeparator;

@WithClasses({
FooMapper.class,
FooMapperConfigured.class,
Expand All @@ -22,159 +21,25 @@
})
@IssueKey("3133")
public class RedundantNullCheckTest {
@RegisterExtension
final GeneratedSource generatedSource = new GeneratedSource();
private final String ls = lineSeparator();

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheckWithAdditionalPrimitiveParameters() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
" if ( input == null ) {" + ls +
" return;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( input.getBar() );" + ls +
" }" );
}

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheckWithMultipleAdditionalPrimitiveParameters() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) {" + ls +
" if ( input == null ) {" + ls +
" return;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( input.getBar() );" + ls +
" }" );
}

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheckWithReturnValue() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate) {" + ls +
" if ( input == null ) {" + ls +
" return toUpdate;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( input.getBar() );" + ls +
ls +
" return toUpdate;" + ls +
" }" );
}
private static final String EXPECTED_MAPPER_IMPL = "src/test/resources/fixtures/org/mapstruct/ap/test/nullcheck/ExpectedFooMapper.java";
private static final String EXPECTED_MAPPER_CONFIGURED_IMPL = "src/test/resources/fixtures/org/mapstruct/ap/test/nullcheck/ExpectedFooMapperConfigured.java";

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheckWithReturnValueWithAdditionalPrimitiveParameters() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
" if ( input == null ) {" + ls +
" return toUpdate;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( input.getBar() );" + ls +
ls +
" return toUpdate;" + ls +
" }" );
}

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheckForNestedSourceWithReturnValueWithAdditionalPrimitiveParameters() {
generatedSource.forMapper( FooMapper.class )
.content()
.containsIgnoringWhitespaces( "@Override" + ls +
" public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) {" + ls +
" if ( input == null ) {" + ls +
" return toUpdate;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( inputNestedBar( input ) );" + ls +
ls +
" return toUpdate;" + ls +
" }" + ls +
" private String inputNestedBar(FooSourceNested fooSourceNested) {" + ls +
" FooSource nested = fooSourceNested.getNested();" + ls +
" if ( nested == null ) {" + ls +
" return null;" + ls +
" }" + ls +
" return nested.getBar();" + ls +
" }" );
}
@RegisterExtension
final GeneratedSource generatedSource = new GeneratedSource();

@ProcessorTest
@IssueKey("3133")
void shouldNotCreateRedundantNullCheck() {
void generatedMapperShouldNotContainAnyRedundantNullChecks() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public void updateFoo(FooSource input, FooTarget toUpdate) {" + ls +
" if ( input == null ) {" + ls +
" return;" + ls +
" }" + ls +
ls +
" toUpdate.setBar( input.getBar() );" + ls +
" }" );
.hasSameMapperContent( FileUtils.getFile( EXPECTED_MAPPER_IMPL ) );
}


@ProcessorTest
@IssueKey("3133")
void shouldCreateNullCheckIfNoGuardClauseIsPresentWithAdditionalPrimitiveParameters() {
void generatedMapperConfiguredShouldNotContainAnyRedundantNullChecks() {
generatedSource.forMapper( FooMapperConfigured.class )
.content()
.contains( "@Override" + ls +
" public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {" + ls +
ls +
" if ( input != null ) {" + ls +
" toUpdate.setBar( input.getBar() );" + ls +
" }" + ls +
" }" );
}

@ProcessorTest
@IssueKey("3133")
void shouldCreateNullCheckIfNoGuardClauseIsPresent() {
generatedSource.forMapper( FooMapperConfigured.class )
.content()
.contains( "@Override" + ls +
" public void updateFoo(FooSource input, FooTarget toUpdate) {" + ls +
ls +
" if ( input != null ) {" + ls +
" toUpdate.setBar( input.getBar() );" + ls +
" }" + ls +
" }" );
}

@ProcessorTest
@IssueKey("3133")
void shouldCreateNullCheckIfNoGuardClauseIsPresentForNestedTargetClass() {
generatedSource.forMapper( FooMapper.class )
.content()
.contains( "@Override" + ls +
" public FooTarget map(FooSourceNested source) {" + ls +
" if ( source == null ) {" + ls +
" return null;" + ls +
" }" + ls +
ls +
" FooTarget fooTarget = new FooTarget();" + ls +
ls +
" fooTarget.setBar( source.getBar() );" + ls +
ls +
" return fooTarget;" + ls +
" }" );
.hasSameMapperContent( FileUtils.getFile( EXPECTED_MAPPER_CONFIGURED_IMPL ) );
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package org.mapstruct.ap.test.nullcheck.redundant;

import javax.annotation.processing.Generated;

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-05-07T18:50:27+0200",
comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)"
)
public class FooMapperImpl implements FooMapper {

@Override
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return;
}

toUpdate.setBar( input.getBar() );
}

@Override
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz, int bay) {
if ( input == null ) {
return;
}

toUpdate.setBar( input.getBar() );
}

@Override
public void updateFoo(FooSource input, FooTarget toUpdate) {
if ( input == null ) {
return;
}

toUpdate.setBar( input.getBar() );
}

@Override
public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate) {
if ( input == null ) {
return toUpdate;
}

toUpdate.setBar( input.getBar() );

return toUpdate;
}

@Override
public FooTarget getUpdatedFooTarget(FooSource input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return toUpdate;
}

toUpdate.setBar( input.getBar() );

return toUpdate;
}

@Override
public FooTarget map(FooSource source) {
if ( source == null ) {
return null;
}

FooTarget fooTarget = new FooTarget();

fooTarget.setBar( source.getBar() );

return fooTarget;
}

@Override
public FooTarget map(FooSourceNested source) {
if ( source == null ) {
return null;
}

FooTarget fooTarget = new FooTarget();

fooTarget.setBar( source.getBar() );

return fooTarget;
}

@Override
public FooTarget map(FooSourceNested input, FooTarget toUpdate, boolean baz) {
if ( input == null ) {
return toUpdate;
}

toUpdate.setBar( inputNestedBar( input ) );

return toUpdate;
}

private String inputNestedBar(FooSourceNested fooSourceNested) {
FooSource nested = fooSourceNested.getNested();
if ( nested == null ) {
return null;
}
return nested.getBar();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.mapstruct.ap.test.nullcheck.redundant;

import javax.annotation.processing.Generated;

@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2024-05-07T18:50:27+0200",
comments = "version: , compiler: javac, environment: Java 21.0.2 (Azul Systems, Inc.)"
)
public class FooMapperConfiguredImpl implements FooMapperConfigured {

@Override
public void updateFoo(FooSource input, FooTarget toUpdate, boolean baz) {

if ( input != null ) {
toUpdate.setBar( input.getBar() );
}
}

@Override
public void updateFoo(FooSource input, FooTarget toUpdate) {

if ( input != null ) {
toUpdate.setBar( input.getBar() );
}
}
}