Repository: vackosar/gitflow-incremental-builder
Pull request #197 (2020-07-16 11:51:45)

Fix NPE when there is no plugin <configuration>


Patch:
diff --git a/.dependabot/config.yml b/.dependabot/config.yml
index 5c79400..1717f23 100644
--- a/.dependabot/config.yml
+++ b/.dependabot/config.yml
@@ -13,5 +13,3 @@ update_configs:
           dependency_name: "ch.qos.logback:logback-classic"
       - match:
           dependency_name: "javax.inject:javax.inject"
-      - match:
-          dependency_name: "net.sf.saxon:Saxon-HE"
diff --git a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
index a7ca387..81a0c6d 100644
--- a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
+++ b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
@@ -5,7 +5,6 @@ import com.vackosar.gitflowincrementalbuild.control.Property.ValueWithOriginCont
 
 import org.apache.maven.execution.MavenExecutionRequest;
 import org.apache.maven.execution.MavenSession;
-import org.apache.maven.model.Plugin;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 
 import javax.inject.Inject;
@@ -174,13 +173,13 @@ public class Configuration {
     }
 
     private static Properties getPluginProperties(MavenSession session) {
-        Properties properties = new Properties();
-        Plugin plugin = session.getTopLevelProject().getPlugin(PLUGIN_KEY);
-        if (plugin != null) {
-            Arrays.stream(((Xpp3Dom) plugin.getConfiguration()).getChildren())
-                    .forEach(child -> properties.put(child.getName(), child.getValue()));
-        }
-        return properties;
+        return Optional.ofNullable(session.getTopLevelProject().getPlugin(PLUGIN_KEY))
+                .map(plugin -> (Xpp3Dom) plugin.getConfiguration())
+                .map(Xpp3Dom::getChildren)
+                .filter(children -> children.length > 0)
+                .map(children -> Arrays.stream(children)
+                        .collect(Collectors.toMap(Xpp3Dom::getName, Xpp3Dom::getValue, (a, b) -> a, Properties::new)))
+                .orElseGet(Properties::new);
     }
 
     private static BuildUpstreamMode parseBuildUpstreamMode(MavenSession session, Properties pluginProperties, Properties projectProperties) {


Test patch:
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
index 13b2b98..75c0af1 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/boundary/ConfigurationTest.java
@@ -389,14 +389,50 @@ public class ConfigurationTest {
         assertEquals("foo", configuration.baseBranch);
     }
 
+    @Test
+    public void plugin_noConfig_projectProperties() {
+        mockPlugin(null);
+        projectProperties.put(Property.baseBranch.prefixedName(), "foo");
+
+        Configuration configuration = new Configuration.Provider(mavenSessionMock).get();
+
+        assertEquals("foo", configuration.baseBranch);
+    }
+
+    @Test
+    public void plugin_emptyConfigChildrenNull_projectProperties() {
+        mockPlugin(mock(Xpp3Dom.class));
+        projectProperties.put(Property.baseBranch.prefixedName(), "foo");
+
+        Configuration configuration = new Configuration.Provider(mavenSessionMock).get();
+
+        assertEquals("foo", configuration.baseBranch);
+    }
+
+    @Test
+    public void plugin_emptyConfigChildrenEmpty_projectProperties() {
+        Xpp3Dom configMock = mock(Xpp3Dom.class);
+        when(configMock.getChildren()).thenReturn(new Xpp3Dom[] {});
+        mockPlugin(configMock);
+        projectProperties.put(Property.baseBranch.prefixedName(), "foo");
+
+        Configuration configuration = new Configuration.Provider(mavenSessionMock).get();
+
+        assertEquals("foo", configuration.baseBranch);
+    }
+
     private void mockPluginConfig(String propertyName, String value) {
         Xpp3Dom childConfigMock = mock(Xpp3Dom.class);
         when(childConfigMock.getName()).thenReturn(propertyName);
         when(childConfigMock.getValue()).thenReturn(value);
         Xpp3Dom configMock = mock(Xpp3Dom.class);
         when(configMock.getChildren()).thenReturn(new Xpp3Dom[]{ childConfigMock });
+        mockPlugin(configMock);
+    }
+
+    private void mockPlugin(Xpp3Dom config) {
         Plugin pluginMock = mock(Plugin.class);
-        when(pluginMock.getConfiguration()).thenReturn(configMock);
+        when(pluginMock.getConfiguration()).thenReturn(config);
         when(mockTLProject.getPlugin(Configuration.PLUGIN_KEY)).thenReturn(pluginMock);
     }
 }