MNG-5805: Fix NPE in LifecyclePhase#toString() Signed-off-by: Jason van Zyl <ja...@tesla.io>
closes #62 Project: http://git-wip-us.apache.org/repos/asf/maven/repo Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/9f7971da Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/9f7971da Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/9f7971da Branch: refs/heads/master Commit: 9f7971dadbec8882b4c119345494b620d3a1f897 Parents: 31e70db Author: Anton Tanasenko <atg.sleepl...@gmail.com> Authored: Sat Aug 1 17:02:52 2015 +0300 Committer: Jason van Zyl <ja...@tesla.io> Committed: Fri Sep 11 18:36:02 2015 -0400 ---------------------------------------------------------------------- .../maven/lifecycle/mapping/LifecyclePhase.java | 35 ++++++---- .../lifecycle/mapping/LifecyclePhaseTest.java | 73 ++++++++++++++++++++ 2 files changed, 94 insertions(+), 14 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/maven/blob/9f7971da/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecyclePhase.java ---------------------------------------------------------------------- diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecyclePhase.java b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecyclePhase.java index d9afcf5..059f234 100644 --- a/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecyclePhase.java +++ b/maven-core/src/main/java/org/apache/maven/lifecycle/mapping/LifecyclePhase.java @@ -55,13 +55,16 @@ public class LifecyclePhase { mojos = new ArrayList<>(); - String[] mojoGoals = StringUtils.split( goals, "," ); - - for ( String mojoGoal: mojoGoals ) + if ( StringUtils.isNotEmpty( goals ) ) { - LifecycleMojo lifecycleMojo = new LifecycleMojo(); - lifecycleMojo.setGoal( mojoGoal.trim() ); - mojos.add( lifecycleMojo ); + String[] mojoGoals = StringUtils.split( goals, "," ); + + for ( String mojoGoal: mojoGoals ) + { + LifecycleMojo lifecycleMojo = new LifecycleMojo(); + lifecycleMojo.setGoal( mojoGoal.trim() ); + mojos.add( lifecycleMojo ); + } } } @@ -70,17 +73,21 @@ public class LifecyclePhase { StringBuilder sb = new StringBuilder(); boolean first = true; - for ( LifecycleMojo mojo: getMojos() ) + List<LifecycleMojo> mojos = getMojos(); + if ( mojos != null ) { - if ( first ) - { - first = false; - } - else + for ( LifecycleMojo mojo: mojos ) { - sb.append( "," ); + if ( first ) + { + first = false; + } + else + { + sb.append( "," ); + } + sb.append( mojo.getGoal() ); } - sb.append( mojo.getGoal() ); } return sb.toString(); } http://git-wip-us.apache.org/repos/asf/maven/blob/9f7971da/maven-core/src/test/java/org/apache/maven/lifecycle/mapping/LifecyclePhaseTest.java ---------------------------------------------------------------------- diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/mapping/LifecyclePhaseTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/mapping/LifecyclePhaseTest.java new file mode 100644 index 0000000..0d8cbdc --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/mapping/LifecyclePhaseTest.java @@ -0,0 +1,73 @@ +package org.apache.maven.lifecycle.mapping; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +/** + * @author atanasenko + */ +public class LifecyclePhaseTest +{ + @Test + public void testToString() + { + LifecyclePhase phase = new LifecyclePhase(); + assertEquals( "", phase.toString() ); + + LifecycleMojo mojo1 = new LifecycleMojo(); + mojo1.setGoal( "jar:jar" ); + phase.setMojos( Arrays.asList( mojo1 ) ); + assertEquals( "jar:jar", phase.toString() ); + + LifecycleMojo mojo2 = new LifecycleMojo(); + mojo2.setGoal( "war:war" ); + phase.setMojos( Arrays.asList( mojo1, mojo2 ) ); + assertEquals( "jar:jar,war:war", phase.toString() ); + } + + @Test + public void testSet() + { + LifecyclePhase phase = new LifecyclePhase(); + assertNull( phase.getMojos() ); + + phase.set( "" ); + assertNotNull( phase.getMojos() ); + assertEquals( 0, phase.getMojos().size() ); + + phase.set( "jar:jar, war:war" ); + + List<LifecycleMojo> mojos = phase.getMojos(); + assertNotNull( mojos ); + assertEquals( 2, mojos.size() ); + + LifecycleMojo mojo1 = mojos.get(0); + assertNotNull( mojo1 ); + assertEquals( "jar:jar", mojo1.getGoal() ); + + LifecycleMojo mojo2 = mojos.get(1); + assertNotNull( mojo2 ); + assertEquals( "war:war", mojo2.getGoal() ); + } +} +