[MNG-7834] Fix NullPointerException in flatten-maven-plugin

This commit is contained in:
Guillaume Nodet 2023-07-04 07:29:31 +02:00
parent 6b3989ce5b
commit 7563949f71
3 changed files with 51 additions and 9 deletions

View File

@ -20,6 +20,7 @@ package org.apache.maven.model;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -53,4 +54,21 @@ class DependencyManagementTest {
void testToStringNullSafe() { void testToStringNullSafe() {
assertNotNull(new DependencyManagement().toString()); assertNotNull(new DependencyManagement().toString());
} }
@Test
void testDependencies() {
DependencyManagement dm = new DependencyManagement();
Dependency d1 = new Dependency();
d1.setGroupId("myGroupId");
assertNotNull(dm.getDependencies());
assertEquals(0, dm.getDependencies().size());
dm.addDependency(d1);
assertNotNull(dm.getDependencies());
assertEquals(1, dm.getDependencies().size());
dm.getDependencies().get(0).setArtifactId("myArtifactId");
assertEquals("myArtifactId", dm.getDependencies().get(0).getArtifactId());
dm.setDependencies(null);
assertNotNull(dm.getDependencies());
assertEquals(0, dm.getDependencies().size());
}
} }

View File

@ -20,8 +20,10 @@ package org.apache.maven.model;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
@ -36,6 +38,19 @@ class ModelTest {
new Model().hashCode(); new Model().hashCode();
} }
@Test
void testBuild() {
Model model = new Model();
Build build = new Build();
build.setOutputDirectory("myOutputDirectory");
model.setBuild(build);
Build build2 = model.getBuild();
assertNotNull(build2);
assertEquals("myOutputDirectory", build2.getOutputDirectory());
model.setBuild(null);
assertNull(model.getBuild());
}
@Test @Test
void testEqualsNullSafe() { void testEqualsNullSafe() {
assertFalse(new Model().equals(null)); assertFalse(new Model().equals(null));

View File

@ -201,18 +201,27 @@ public class ${class.name}
if (!Objects.equals(map, getDelegate().get${cap}())) { if (!Objects.equals(map, getDelegate().get${cap}())) {
update(getDelegate().with${cap}(map)); update(getDelegate().with${cap}(map));
} }
#elseif ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
if (${field.name} == null) {
${field.name} = Collections.emptyList();
}
if (!Objects.equals(${field.name}, ${pfx}${cap}())) {
update(getDelegate().with${cap}(
${field.name}.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
${field.name}.forEach(e -> e.childrenTracking = this::replace);
}
#elseif ( $field.to && $field.to != "String" )
if (!Objects.equals(${field.name}, ${pfx}${cap}())){
if (${field.name} != null) {
update(getDelegate().with${cap}(${field.name}.getDelegate()));
${field.name}.childrenTracking = this::replace;
} else {
update(getDelegate().with${cap}(null));
}
}
#else #else
if (!Objects.equals(${field.name}, ${pfx}${cap}())) { if (!Objects.equals(${field.name}, ${pfx}${cap}())) {
#if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
update(getDelegate().with${cap}(
${field.name}.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
${field.name}.forEach(e -> e.childrenTracking = this::replace);
#elseif ( $field.to && $field.to != "String" )
update(getDelegate().with${cap}(${field.name}.getDelegate()));
${field.name}.childrenTracking = this::replace;
#else
update(getDelegate().with${cap}(${field.name})); update(getDelegate().with${cap}(${field.name}));
#end
} }
#end #end
} }