Skip to content

Commit

Permalink
Merge pull request spring-projects#291 from dsyer/10579
Browse files Browse the repository at this point in the history
# By Chris Beams (1) and Dave Syer (1)
* SPR-10579:
  Polish pull request spring-projects#291 per committer guidelines
  Make CommandLinePropertySource enumerable
  • Loading branch information
cbeams committed May 28, 2013
2 parents 3c73a8f + 46d47fe commit ba45f70
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -185,7 +185,7 @@
* @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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -21,6 +21,7 @@
import java.util.List;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

/**
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
Expand Down Expand Up @@ -77,6 +78,19 @@ protected boolean containsOption(String name) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -95,6 +95,14 @@ public SimpleCommandLinePropertySource(String name, String[] args) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -117,17 +117,18 @@ public void withDottedOptionName() {
@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));
assertThat(ps.containsProperty("o2"), is(true));

assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.getProperty("nonOptionArgs"), nullValue());
assertThat(ps.getPropertyNames().length, is(2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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}.
*
Expand Down Expand Up @@ -67,14 +65,15 @@ public void withOptionArgsOnly() {

@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));
assertThat(ps.containsProperty("o2"), is(true));

assertThat(ps.containsProperty("nonOptionArgs"), is(false));
assertThat(ps.getProperty("nonOptionArgs"), nullValue());
assertThat(ps.getPropertyNames().length, is(2));
}

@Test
Expand Down

0 comments on commit ba45f70

Please sign in to comment.