From 7860af8624131300e0009428d471a571f0c34ef9 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 24 May 2013 10:54:39 +0100 Subject: [PATCH 1/2] Make CommandLinePropertySource enumerable JOpt 4.4 has enumerable options, so this change can be made if we upgrade. The only awkward thing is that JOpt allows aliases for options, so we have to pick one to avoid double counting. This implementation picks the last one in the list which is the alphebtically last of the long options, if there are any (e.g. "o1", "option1" returns "option1"). Most of the time there will only be one or two aliases for each option so it won't matter. Issue: SPR-10579 --- build.gradle | 2 +- .../core/env/CommandLinePropertySource.java | 4 ++-- .../core/env/JOptCommandLinePropertySource.java | 16 +++++++++++++++- .../env/SimpleCommandLinePropertySource.java | 8 ++++++++ .../env/JOptCommandLinePropertySourceTests.java | 5 +++-- .../SimpleCommandLinePropertySourceTests.java | 11 +++++------ 6 files changed, 34 insertions(+), 12 deletions(-) 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..04d7c536f4f 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 @@ -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"; @@ -218,7 +218,7 @@ public abstract class CommandLinePropertySource extends PropertySource { public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) { this.nonOptionArgsPropertyName = nonOptionArgsPropertyName; } - + /** * Return whether this {@code PropertySource} contains a property with the given name. *

This implementation first checks to see if the name specified is the special 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..1767ad0e817 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 @@ -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}. @@ -76,7 +77,20 @@ 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..bc00692f813 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 @@ -94,6 +94,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..e08183d7223 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 @@ -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 From 46d47fef9a74a277779dd3523235a57e57715677 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Tue, 28 May 2013 12:48:57 +0200 Subject: [PATCH 2/2] Polish pull request #291 per committer guidelines - Update Apache license headers - Remove trailing whitespace - Edit original commit comment to use 'Issue:' syntax - Revert use of diamond operator (<>) in main sources See committer guidelines at https://github.com/SpringSource/spring-framework/blob/master/CONTRIBUTING.md Issue: SPR-10579 --- .../core/env/CommandLinePropertySource.java | 4 ++-- .../core/env/JOptCommandLinePropertySource.java | 12 ++++++------ .../core/env/SimpleCommandLinePropertySource.java | 4 ++-- .../core/env/JOptCommandLinePropertySourceTests.java | 2 +- .../env/SimpleCommandLinePropertySourceTests.java | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) 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 04d7c536f4f..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. @@ -218,7 +218,7 @@ public abstract class CommandLinePropertySource extends EnumerablePropertySou public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) { this.nonOptionArgsPropertyName = nonOptionArgsPropertyName; } - + /** * Return whether this {@code PropertySource} contains a property with the given name. *

This implementation first checks to see if the name specified is the special 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 1767ad0e817..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. @@ -77,20 +77,20 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource names = new ArrayList<>(); + List names = new ArrayList(); for (OptionSpec spec : source.specs()) { - List aliases = new ArrayList<>(spec.options()); + List aliases = new ArrayList(spec.options()); if (!aliases.isEmpty()) { // Only the longest name is used for enumerating - names.add(aliases.get(aliases.size()-1)); + 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 bc00692f813..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. @@ -94,7 +94,7 @@ public class SimpleCommandLinePropertySource extends CommandLinePropertySource