diff --git a/build.gradle b/build.gradle index 7002f17e8ea..30ca20b94ae 100644 --- a/build.gradle +++ b/build.gradle @@ -205,7 +205,7 @@ project("spring-core") { compile(files(cglibRepackJar)) compile("commons-logging:commons-logging:1.1.1") optional("org.aspectj:aspectjweaver:${aspectjVersion}") - optional("net.sf.jopt-simple:jopt-simple:3.0") + optional("net.sf.jopt-simple:jopt-simple:4.4") optional("log4j:log4j:1.2.17") testCompile("xmlunit:xmlunit:1.3") testCompile("org.codehaus.woodstox:wstx-asl:3.2.7") { diff --git a/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java index ba7d8c70741..e8e0fda893d 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -185,7 +185,7 @@ import org.springframework.util.StringUtils; * @see SimpleCommandLinePropertySource * @see JOptCommandLinePropertySource */ -public abstract class CommandLinePropertySource extends PropertySource { +public abstract class CommandLinePropertySource extends EnumerablePropertySource { /** The default name given to {@link CommandLinePropertySource} instances: {@value} */ public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs"; diff --git a/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java index 154c34d623d..63590556b78 100644 --- a/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.List; import joptsimple.OptionSet; +import joptsimple.OptionSpec; /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. @@ -77,6 +78,19 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource names = new ArrayList(); + for (OptionSpec spec : source.specs()) { + List aliases = new ArrayList(spec.options()); + if (!aliases.isEmpty()) { + // Only the longest name is used for enumerating + names.add(aliases.get(aliases.size()-1)); + } + } + return names.toArray(new String[names.size()]); + } + @Override public List getOptionValues(String name) { List argValues = this.source.valuesOf(name); diff --git a/spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java index d2022317664..f1b1f8d7b5f 100644 --- a/spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -95,6 +95,14 @@ public class SimpleCommandLinePropertySource extends CommandLinePropertySource ps = new JOptCommandLinePropertySource(optionSet); + EnumerablePropertySource ps = new JOptCommandLinePropertySource(optionSet); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.containsProperty("o1"), is(true)); @@ -128,6 +128,7 @@ public class JOptCommandLinePropertySourceTests { assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.getProperty("nonOptionArgs"), nullValue()); + assertThat(ps.getPropertyNames().length, is(2)); } @Test diff --git a/spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java b/spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java index 2f7c0fcca8c..6034d4c0916 100644 --- a/spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2013 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. @@ -16,15 +16,13 @@ package org.springframework.core.env; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.junit.Assert.assertThat; - import java.util.List; import org.junit.Test; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + /** * Unit tests for {@link SimpleCommandLinePropertySource}. * @@ -67,7 +65,7 @@ public class SimpleCommandLinePropertySourceTests { @Test public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() { - PropertySource ps = new SimpleCommandLinePropertySource("--o1=v1", "--o2"); + EnumerablePropertySource ps = new SimpleCommandLinePropertySource("--o1=v1", "--o2"); assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.containsProperty("o1"), is(true)); @@ -75,6 +73,7 @@ public class SimpleCommandLinePropertySourceTests { assertThat(ps.containsProperty("nonOptionArgs"), is(false)); assertThat(ps.getProperty("nonOptionArgs"), nullValue()); + assertThat(ps.getPropertyNames().length, is(2)); } @Test