Introduce CommandLinePropertySource and impls

Users may now work with command line arguments as a source of
properties for use with the PropertySource and Environment APIs.
An implementation based on the jopt library and a "simple"
implementation requiring no external libraries are are provided
out-of-the box.

See Javadoc for CommandLinePropertySource, JOptCommandLinePropertySource
and SimpleCommandLinePropertySource for details.

Issue: SPR-8482
This commit is contained in:
Chris Beams
2011-06-30 22:33:56 +00:00
parent e0d2e20fc4
commit 1eb5811347
11 changed files with 1100 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
/*
* Copyright 2002-2011 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.
* 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.
*/
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.assertEquals;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import org.junit.Test;
/**
* Unit tests for {@link JOptCommandLinePropertySource}.
*
* @author Chris Beams
* @since 3.1
*/
public class JOptCommandLinePropertySourceTests {
@Test
public void withRequiredArg_andArgIsPresent() {
OptionParser parser = new OptionParser();
parser.accepts("foo").withRequiredArg();
OptionSet options = parser.parse("--foo=bar");
PropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertThat((String)ps.getProperty("foo"), equalTo("bar"));
}
@Test
public void withOptionalArg_andArgIsMissing() {
OptionParser parser = new OptionParser();
parser.accepts("foo").withOptionalArg();
OptionSet options = parser.parse("--foo");
PropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertThat(ps.containsProperty("foo"), is(true));
assertThat((String)ps.getProperty("foo"), equalTo(""));
}
@Test
public void withNoArg() {
OptionParser parser = new OptionParser();
parser.accepts("o1");
parser.accepts("o2");
OptionSet options = parser.parse("--o1");
PropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(false));
assertThat((String)ps.getProperty("o1"), equalTo(""));
assertThat(ps.getProperty("o2"), nullValue());
}
@Test
public void withRequiredArg_andMultipleArgsPresent_usingDelimiter() {
OptionParser parser = new OptionParser();
parser.accepts("foo").withRequiredArg().withValuesSeparatedBy(',');
OptionSet options = parser.parse("--foo=bar,baz,biz");
CommandLinePropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertEquals(Arrays.asList("bar","baz","biz"), ps.getOptionValues("foo"));
assertThat(ps.getProperty("foo"), equalTo("bar,baz,biz"));
}
@Test
public void withRequiredArg_andMultipleArgsPresent_usingRepeatedOption() {
OptionParser parser = new OptionParser();
parser.accepts("foo").withRequiredArg().withValuesSeparatedBy(',');
OptionSet options = parser.parse("--foo=bar", "--foo=baz", "--foo=biz");
CommandLinePropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertEquals(Arrays.asList("bar","baz","biz"), ps.getOptionValues("foo"));
assertThat(ps.getProperty("foo"), equalTo("bar,baz,biz"));
}
@Test
public void withMissingOption() {
OptionParser parser = new OptionParser();
parser.accepts("foo").withRequiredArg().withValuesSeparatedBy(',');
OptionSet options = parser.parse(); // <-- no options whatsoever
PropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertThat(ps.getProperty("foo"), nullValue());
}
@Test
public void withDottedOptionName() {
OptionParser parser = new OptionParser();
parser.accepts("spring.profiles.active").withRequiredArg();
OptionSet options = parser.parse("--spring.profiles.active=p1");
CommandLinePropertySource<?> ps = new JOptCommandLinePropertySource(options);
assertThat(ps.getProperty("spring.profiles.active"), equalTo("p1"));
}
@Test
public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() {
OptionParser parser = new OptionParser();
parser.accepts("o1").withRequiredArg();
parser.accepts("o2");
OptionSet optionSet = parser.parse("--o1=v1", "--o2");
PropertySource<?> ps = new JOptCommandLinePropertySource(optionSet);
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.getProperty("nonOptionArgs"), nullValue());
}
@Test
public void withDefaultNonOptionArgsNameAndNonOptionArgsPresent() {
OptionParser parser = new OptionParser();
parser.accepts("o1").withRequiredArg();
parser.accepts("o2");
OptionSet optionSet = parser.parse("--o1=v1", "noa1", "--o2", "noa2");
PropertySource<?> ps = new JOptCommandLinePropertySource(optionSet);
assertThat(ps.containsProperty("nonOptionArgs"), is(true));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
String nonOptionArgs = (String)ps.getProperty("nonOptionArgs");
assertThat(nonOptionArgs, equalTo("noa1,noa2"));
}
@Test
public void withCustomNonOptionArgsNameAndNoNonOptionArgsPresent() {
OptionParser parser = new OptionParser();
parser.accepts("o1").withRequiredArg();
parser.accepts("o2");
OptionSet optionSet = parser.parse("--o1=v1", "noa1", "--o2", "noa2");
CommandLinePropertySource<?> ps = new JOptCommandLinePropertySource(optionSet);
ps.setNonOptionArgsPropertyName("NOA");
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.containsProperty("NOA"), is(true));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
String nonOptionArgs = ps.getProperty("NOA");
assertThat(nonOptionArgs, equalTo("noa1,noa2"));
}
}

View File

@@ -0,0 +1,114 @@
/*
* Copyright 2002-2011 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.
* 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.
*/
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.Collections;
import java.util.List;
import org.junit.Test;
public class SimpleCommandLineParserTests {
@Test
public void withNoOptions() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
assertThat(parser.parse().getOptionValues("foo"), nullValue());
}
@Test
public void withSingleOptionAndNoValue() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
CommandLineArgs args = parser.parse("--o1");
assertThat(args.containsOption("o1"), is(true));
assertThat(args.getOptionValues("o1"), equalTo(Collections.EMPTY_LIST));
}
@Test
public void withSingleOptionAndValue() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
CommandLineArgs args = parser.parse("--o1=v1");
assertThat(args.containsOption("o1"), is(true));
assertThat(args.getOptionValues("o1").get(0), equalTo("v1"));
}
@Test
public void withMixOfOptionsHavingValueAndOptionsHavingNoValue() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
CommandLineArgs args = parser.parse("--o1=v1", "--o2");
assertThat(args.containsOption("o1"), is(true));
assertThat(args.containsOption("o2"), is(true));
assertThat(args.containsOption("o3"), is(false));
assertThat(args.getOptionValues("o1").get(0), equalTo("v1"));
assertThat(args.getOptionValues("o2"), equalTo(Collections.EMPTY_LIST));
assertThat(args.getOptionValues("o3"), nullValue());
}
@Test(expected=IllegalArgumentException.class)
public void withEmptyOptionText() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
parser.parse("--");
}
@Test(expected=IllegalArgumentException.class)
public void withEmptyOptionName() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
parser.parse("--=v1");
}
@Test(expected=IllegalArgumentException.class)
public void withEmptyOptionValue() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
parser.parse("--o1=");
}
@Test(expected=IllegalArgumentException.class)
public void withEmptyOptionNameAndEmptyOptionValue() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
parser.parse("--=");
}
@Test
public void withNonOptionArguments() {
SimpleCommandLineArgsParser parser = new SimpleCommandLineArgsParser();
CommandLineArgs args = parser.parse("--o1=v1", "noa1", "--o2=v2", "noa2");
assertThat(args.getOptionValues("o1").get(0), equalTo("v1"));
assertThat(args.getOptionValues("o2").get(0), equalTo("v2"));
List<String> nonOptions = args.getNonOptionArgs();
assertThat(nonOptions.get(0), equalTo("noa1"));
assertThat(nonOptions.get(1), equalTo("noa2"));
assertThat(nonOptions.size(), equalTo(2));
}
@Test(expected=UnsupportedOperationException.class)
public void assertOptionNamesIsUnmodifiable() {
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
args.getOptionNames().add("bogus");
}
@Test(expected=UnsupportedOperationException.class)
public void assertNonOptionArgsIsUnmodifiable() {
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
args.getNonOptionArgs().add("foo");
}
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright 2002-2011 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.
* 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.
*/
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;
/**
* Unit tests for {@link SimpleCommandLinePropertySource}.
*
* @author Chris Beams
* @since 3.1
*/
public class SimpleCommandLinePropertySourceTests {
@Test
public void withDefaultName() {
PropertySource<?> ps = new SimpleCommandLinePropertySource();
assertThat(ps.getName(),
equalTo(CommandLinePropertySource.DEFAULT_COMMAND_LINE_PROPERTY_SOURCE_NAME));
}
@Test
public void withCustomName() {
PropertySource<?> ps = new SimpleCommandLinePropertySource("ps1", new String[0]);
assertThat(ps.getName(), equalTo("ps1"));
}
@Test
public void withNoArgs() {
PropertySource<?> ps = new SimpleCommandLinePropertySource();
assertThat(ps.containsProperty("foo"), is(false));
assertThat(ps.getProperty("foo"), nullValue());
}
@Test
public void withOptionArgsOnly() {
CommandLinePropertySource<?> ps =
new SimpleCommandLinePropertySource("--o1=v1", "--o2");
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
assertThat(ps.containsProperty("o3"), is(false));
assertThat(ps.getProperty("o1"), equalTo("v1"));
assertThat(ps.getProperty("o2"), equalTo(""));
assertThat(ps.getProperty("o3"), nullValue());
}
@Test
public void withDefaultNonOptionArgsNameAndNoNonOptionArgsPresent() {
PropertySource<?> ps = new SimpleCommandLinePropertySource("--o1=v1", "--o2");
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.getProperty("nonOptionArgs"), nullValue());
}
@Test
public void withDefaultNonOptionArgsNameAndNonOptionArgsPresent() {
CommandLinePropertySource<?> ps =
new SimpleCommandLinePropertySource("--o1=v1", "noa1", "--o2", "noa2");
assertThat(ps.containsProperty("nonOptionArgs"), is(true));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
String nonOptionArgs = ps.getProperty("nonOptionArgs");
assertThat(nonOptionArgs, equalTo("noa1,noa2"));
}
@Test
public void withCustomNonOptionArgsNameAndNoNonOptionArgsPresent() {
CommandLinePropertySource<?> ps =
new SimpleCommandLinePropertySource("--o1=v1", "noa1", "--o2", "noa2");
ps.setNonOptionArgsPropertyName("NOA");
assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.containsProperty("NOA"), is(true));
assertThat(ps.containsProperty("o1"), is(true));
assertThat(ps.containsProperty("o2"), is(true));
String nonOptionArgs = ps.getProperty("NOA");
assertThat(nonOptionArgs, equalTo("noa1,noa2"));
}
@Test
public void covertNonOptionArgsToStringArrayAndList() {
CommandLinePropertySource<?> ps =
new SimpleCommandLinePropertySource("--o1=v1", "noa1", "--o2", "noa2");
StandardEnvironment env = new StandardEnvironment();
env.getPropertySources().addFirst(ps);
String nonOptionArgs = env.getProperty("nonOptionArgs");
assertThat(nonOptionArgs, equalTo("noa1,noa2"));
String[] nonOptionArgsArray = env.getProperty("nonOptionArgs", String[].class);
assertThat(nonOptionArgsArray[0], equalTo("noa1"));
assertThat(nonOptionArgsArray[1], equalTo("noa2"));
@SuppressWarnings("unchecked")
List<String> nonOptionArgsList = env.getProperty("nonOptionArgs", List.class);
assertThat(nonOptionArgsList.get(0), equalTo("noa1"));
assertThat(nonOptionArgsList.get(1), equalTo("noa2"));
}
}