[CLI] #5147: make -D work like system properties (#5191)

* Issue #5147: allow empty values for properties.

* Issue #5147: Allow multiple `-D` options.

* Issue #5147: take advantage (and demonstrate) the new usage of -D.

* Issue #5147: also update windows script and security ones.
This commit is contained in:
Paŭlo Ebermann
2017-06-16 11:29:44 +02:00
committed by wing328
parent 1f1e92d964
commit ec448a6167
18 changed files with 127 additions and 73 deletions

View File

@@ -12,6 +12,9 @@ import org.slf4j.LoggerFactory;
import static io.swagger.codegen.config.CodegenConfiguratorUtils.*;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import java.util.ArrayList;
import java.util.List;
/**
* User: lanwen
* Date: 24.03.15
@@ -48,8 +51,8 @@ public class Generate implements Runnable {
private String auth;
@Option(name = {"-D"}, title = "system properties", description = "sets specified system properties in " +
"the format of name=value,name=value")
private String systemProperties;
"the format of name=value,name=value (or multiple options, each with name=value)")
private List<String> systemProperties = new ArrayList<>();
@Option(name = {"-c", "--config"}, title = "configuration file", description = "Path to json configuration file. " +
"File content should be in a json format {\"optionKey\":\"optionValue\", \"optionKey1\":\"optionValue1\"...} " +
@@ -229,7 +232,7 @@ public class Generate implements Runnable {
configurator.setRemoveOperationIdPrefix(removeOperationIdPrefix);
}
applySystemPropertiesKvp(systemProperties, configurator);
applySystemPropertiesKvpList(systemProperties, configurator);
applyInstantiationTypesKvp(instantiationTypes, configurator);
applyImportMappingsKvp(importMappings, configurator);
applyTypeMappingsKvp(typeMappings, configurator);

View File

@@ -114,6 +114,15 @@ public class GenerateTest {
times = 1;
}};
setupAndRunGenericTest("-Dhello=world,foo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
}};
setupAndRunGenericTest("-D", "hello=world,key=,foo=bar");
new FullVerifications() {{
@@ -121,8 +130,41 @@ public class GenerateTest {
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
configurator.addSystemProperty("key", anyString);
times = 0;
configurator.addSystemProperty("key", "");
times = 1;
}};
setupAndRunGenericTest("-D", "hello=world,key,foo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
configurator.addSystemProperty("key", "");
times = 1;
}};
setupAndRunGenericTest("-D", "hello=world", "-D", "key", "-D", "foo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
configurator.addSystemProperty("key", "");
times = 1;
}};
setupAndRunGenericTest("-Dhello=world", "-Dkey", "-Dfoo=bar");
new FullVerifications() {{
configurator.addSystemProperty("hello", "world");
times = 1;
configurator.addSystemProperty("foo", "bar");
times = 1;
configurator.addSystemProperty("key", "");
times = 1;
}};
}
@@ -183,8 +225,8 @@ public class GenerateTest {
times = 1;
configurator.addInstantiationType("foo", "bar");
times = 1;
configurator.addInstantiationType("key", anyString);
times = 0;
configurator.addInstantiationType("key", "");
times = 1;
}};
}
@@ -197,8 +239,8 @@ public class GenerateTest {
times = 1;
configurator.addTypeMapping("foo", "bar");
times = 1;
configurator.addTypeMapping("key", anyString);
times = 0;
configurator.addTypeMapping("key", "");
times = 1;
}};
}
@@ -211,8 +253,8 @@ public class GenerateTest {
times = 1;
configurator.addAdditionalProperty("foo", "bar");
times = 1;
configurator.addAdditionalProperty("key", anyString);
times = 0;
configurator.addAdditionalProperty("key", "");
times = 1;
}};
}
@@ -241,8 +283,8 @@ public class GenerateTest {
times = 1;
configurator.addImportMapping("foo", "bar");
times = 1;
configurator.addImportMapping("key", anyString);
times = 0;
configurator.addImportMapping("key", "");
times = 1;
}};
}

View File

@@ -2,12 +2,12 @@ package io.swagger.codegen.cmd.utils;
import io.swagger.codegen.utils.OptionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.Test;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.Arrays.asList;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
@@ -16,39 +16,40 @@ public class OptionUtilsTest {
@Test
public void splitCommaSeparatedList() throws Exception {
doCommaSeparatedListTest("a,b,c", Arrays.asList("a", "b", "c"));
doCommaSeparatedListTest("a,,c", Arrays.asList("a", "c"));
doCommaSeparatedListTest("", new ArrayList<String>());
doCommaSeparatedListTest(null, new ArrayList<String>());
doCommaSeparatedListTest("a,b,c", asList("a", "b", "c"));
doCommaSeparatedListTest("a,,c", asList("a", "c"));
doCommaSeparatedListTest("", emptyList());
doCommaSeparatedListTest(null, emptyList());
}
@Test
public void testParseCommaSeparatedTuples() throws Exception {
doTupleListTest("a=1,b=2,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("b", "2"), Pair.of("c", "3")));
doTupleListTest("a=1,,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3")));
doTupleListTest("a=1,xyz,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3")));
doTupleListTest("a=1,=,c=3", Arrays.asList(Pair.of("a", "1"), Pair.of("c", "3")));
doTupleListTest("", new ArrayList<Pair<String, String>>());
doTupleListTest(null, new ArrayList<Pair<String, String>>());
doTupleListTest("a=1,b=2,c=3", asList(Pair.of("a", "1"), Pair.of("b", "2"), Pair.of("c", "3")));
doTupleListTest("xyz", asList(Pair.of("xyz", "")));
doTupleListTest("a=1,,c=3", asList(Pair.of("a", "1"), Pair.of("c", "3")));
doTupleListTest("a=1,xyz=,c=3", asList(Pair.of("a", "1"), Pair.of("xyz", ""), Pair.of("c", "3")));
doTupleListTest("a=1,xyz,c=3", asList(Pair.of("a", "1"), Pair.of("xyz", ""), Pair.of("c", "3")));
doTupleListTest("a=1,=,c=3", asList(Pair.of("a", "1"), Pair.of("c", "3")));
doTupleListTest("", emptyPairList());
doTupleListTest(null, emptyPairList());
}
private static void doTupleListTest(String input, List<Pair<String, String>> expectedResults) {
final List<Pair<String, String>> result = OptionUtils.parseCommaSeparatedTuples(input);
assertNotNull(result);
assertEquals(result.size(), expectedResults.size());
for (int i = 0; i < expectedResults.size(); i++) {
final Pair<String, String> actualPair = result.get(i);
final Pair<String, String> expected = expectedResults.get(i);
assertEquals(actualPair, expected);
}
assertEquals(result, expectedResults);
}
private static void doCommaSeparatedListTest(String csvStr, List<String> expectedResults) {
final List<String> result = OptionUtils.splitCommaSeparatedList(csvStr);
assertNotNull(result);
assertEquals(result.size(), expectedResults.size());
for (int i = 0; i < expectedResults.size(); i++) {
assertEquals(result.get(i), expectedResults.get(i));
}
assertEquals(result, expectedResults);
}
private static List<Pair<String,String>> emptyPairList() {
return Collections.emptyList();
}
private static List<String> emptyList() {
return Collections.emptyList();
}
}