From 0867ac57772b6ea8f1a9baa43a3c1af9785b9054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn?= Date: Mon, 31 Jul 2017 15:31:29 +0200 Subject: [PATCH 1/2] Fix handling of empty/null arguments See gh-9916 --- .../springframework/boot/maven/RunArguments.java | 8 ++++++-- .../boot/maven/RunArgumentsTests.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java index 6888043c6a..ed2d40d4fc 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java @@ -21,6 +21,8 @@ import java.util.LinkedList; import org.codehaus.plexus.util.cli.CommandLineUtils; +import org.springframework.util.StringUtils; + /** * Parse and expose arguments specified in a single string. * @@ -31,14 +33,16 @@ class RunArguments { private static final String[] NO_ARGS = {}; - private final LinkedList args; + private final LinkedList args = new LinkedList<>(); RunArguments(String arguments) { this(parseArgs(arguments)); } RunArguments(String[] args) { - this.args = new LinkedList<>(Arrays.asList(args)); + if (args != null) { + Arrays.stream(args).filter(StringUtils::hasLength).forEach(this.args::add); + } } public LinkedList getArgs() { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java index 486270258c..e2184f0a9c 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java @@ -34,6 +34,20 @@ public class RunArgumentsTests { assertThat(args.length).isEqualTo(0); } + @Test + public void parseNullArray() { + String[] args = new RunArguments((String[]) null).asArray(); + assertThat(args).isNotNull(); + assertThat(args.length).isEqualTo(0); + } + + @Test + public void parseArrayContainingNullValue() { + String[] args = new RunArguments(new String[]{null}).asArray(); + assertThat(args).isNotNull(); + assertThat(args.length).isEqualTo(0); + } + @Test public void parseEmpty() { String[] args = parseArgs(" "); From 83510424690e973713018fc26d5f0cf9dedc534e Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 19 Sep 2017 15:02:08 +0200 Subject: [PATCH 2/2] Polish "Fix handling of empty/null arguments" Closes gh-9916 --- .../springframework/boot/maven/RunArguments.java | 5 ++--- .../boot/maven/RunArgumentsTests.java | 13 ++++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java index ed2d40d4fc..e08b6881af 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/RunArguments.java @@ -18,11 +18,10 @@ package org.springframework.boot.maven; import java.util.Arrays; import java.util.LinkedList; +import java.util.Objects; import org.codehaus.plexus.util.cli.CommandLineUtils; -import org.springframework.util.StringUtils; - /** * Parse and expose arguments specified in a single string. * @@ -41,7 +40,7 @@ class RunArguments { RunArguments(String[] args) { if (args != null) { - Arrays.stream(args).filter(StringUtils::hasLength).forEach(this.args::add); + Arrays.stream(args).filter(Objects::nonNull).forEach(this.args::add); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java index e2184f0a9c..00f797baef 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -43,9 +43,16 @@ public class RunArgumentsTests { @Test public void parseArrayContainingNullValue() { - String[] args = new RunArguments(new String[]{null}).asArray(); + String[] args = new RunArguments(new String[]{"foo", null, "bar"}).asArray(); assertThat(args).isNotNull(); - assertThat(args.length).isEqualTo(0); + assertThat(args).containsOnly("foo", "bar"); + } + + @Test + public void parseArrayContainingEmptyValue() { + String[] args = new RunArguments(new String[]{"foo", "", "bar"}).asArray(); + assertThat(args).isNotNull(); + assertThat(args).containsOnly("foo", "", "bar"); } @Test