Merge pull request #291 from dsyer/10579
# By Chris Beams (1) and Dave Syer (1) * SPR-10579: Polish pull request #291 per committer guidelines Make CommandLinePropertySource enumerable
This commit is contained in:
@@ -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") {
|
||||
|
||||
@@ -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<T> extends PropertySource<T> {
|
||||
public abstract class CommandLinePropertySource<T> extends EnumerablePropertySource<T> {
|
||||
|
||||
/** The default name given to {@link CommandLinePropertySource} instances: {@value} */
|
||||
public static final String COMMAND_LINE_PROPERTY_SOURCE_NAME = "commandLineArgs";
|
||||
|
||||
@@ -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<Opt
|
||||
return this.source.has(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getPropertyNames() {
|
||||
List<String> names = new ArrayList<String>();
|
||||
for (OptionSpec<?> spec : source.specs()) {
|
||||
List<String> aliases = new ArrayList<String>(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<String> getOptionValues(String name) {
|
||||
List<?> argValues = this.source.valuesOf(name);
|
||||
|
||||
@@ -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<C
|
||||
super(name, new SimpleCommandLineArgsParser().parse(args));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the property names for the option arguments.
|
||||
*/
|
||||
@Override
|
||||
public String[] getPropertyNames() {
|
||||
return source.getOptionNames().toArray(new String[source.getOptionNames().size()]);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean containsOption(String name) {
|
||||
return this.source.containsOption(name);
|
||||
|
||||
@@ -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.
|
||||
@@ -117,10 +117,10 @@ public class JOptCommandLinePropertySourceTests {
|
||||
@Test
|
||||
public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() {
|
||||
OptionParser parser = new OptionParser();
|
||||
parser.accepts("o1").withRequiredArg();
|
||||
parser.acceptsAll(Arrays.asList("o1","option1")).withRequiredArg();
|
||||
parser.accepts("o2");
|
||||
OptionSet optionSet = parser.parse("--o1=v1", "--o2");
|
||||
PropertySource<?> 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user