This is an automated email from the ASF dual-hosted git repository. sgoeschl pushed a commit to branch FREEMARKER-141 in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 0dee113a03e2792dbac6583208d0de90b23bfe66 Author: Siegfried Goeschl <[email protected]> AuthorDate: Sat Apr 11 23:42:05 2020 +0200 FREEMARKER-141 freemarker-cli: Expose user-supplied parameters in the data model --- .../base/parameter/ParameterDataModelSupplier.java | 89 ++++++++++++++++++++++ .../parameter/ParameterDataModelSupplierTest.java | 80 +++++++++++++++++++ .../generator/uri/NamedUriStringParserTest.java | 11 +++ .../freemarker/generator/cli/PicocliTest.java | 15 ++++ 4 files changed, 195 insertions(+) diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/parameter/ParameterDataModelSupplier.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/parameter/ParameterDataModelSupplier.java new file mode 100644 index 0000000..78da855 --- /dev/null +++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/parameter/ParameterDataModelSupplier.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.freemarker.generator.base.parameter; + +import org.apache.freemarker.generator.base.uri.NamedUri; +import org.apache.freemarker.generator.base.uri.NamedUriStringParser; +import org.apache.freemarker.generator.base.util.StringUtils; + +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +import static java.util.Objects.requireNonNull; +import static java.util.stream.Collectors.toList; + +/** + * Converts a list of parameters (as <code>Named Uris</code>) + * to a map. The map contains either (key->String) or + * (key->Map<String, Object>). + */ +public class ParameterDataModelSupplier implements Supplier<Map<String, Object>> { + + private final Collection<String> parameters; + + public ParameterDataModelSupplier(Collection<String> parameters) { + this.parameters = requireNonNull(parameters); + } + + @Override + public Map<String, Object> get() { + final List<NamedUri> namedUris = toNamedUris(parameters); + return toMap(namedUris); + } + + private static Map<String, Object> toMap(List<NamedUri> namedUris) { + final Map<String, Object> map = new HashMap<>(); + + for (NamedUri namedUri : namedUris) { + final String key = namedUri.getName(); + final String value = namedUri.getUri().getPath().substring(1); + + if (namedUri.hasGroup()) { + final String group = namedUri.getGroup(); + final Map<String, Object> groupMap = getOrCreateGroupMap(map, group); + groupMap.put(key, value); + } else { + map.put(key, value); + } + } + + return map; + } + + private static List<NamedUri> toNamedUris(Collection<String> parameters) { + return parameters.stream() + .filter(StringUtils::isNotEmpty) + .distinct() + .map(NamedUriStringParser::parse) + .collect(toList()); + } + + @SuppressWarnings("unchecked") + private static Map<String, Object> getOrCreateGroupMap(Map<String, Object> map, String group) { + if (map.containsKey(group)) { + return (Map<String, Object>) map.get(group); + } else { + final Map<String, Object> groupMap = new HashMap<>(); + map.put(group, groupMap); + return groupMap; + } + } + +} diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/parameter/ParameterDataModelSupplierTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/parameter/ParameterDataModelSupplierTest.java new file mode 100644 index 0000000..8eeedf8 --- /dev/null +++ b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/parameter/ParameterDataModelSupplierTest.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.freemarker.generator.parameter; + +import org.apache.freemarker.generator.base.parameter.ParameterDataModelSupplier; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class ParameterDataModelSupplierTest { + + @Test + public void shouldConvertMissingParametersToMap() { + assertEquals(0, supplier(new String[0]).get().size()); + assertEquals(0, supplier("").get().size()); + } + + @Test + public void shouldConverNameValueToMap() { + final Map<String, Object> map = supplier("name=value").get(); + + assertEquals(1, map.size()); + assertEquals("value", map.get("name")); + } + + @Test + public void shouldConvertGroupedNameValueToMap() { + final Map<String, Object> map = supplier("name1:group=value1").get(); + + assertEquals(1, map.size()); + assertEquals("value1", toMap(map, "group").get("name1")); + } + + @Test + public void shouldConvertGroupedNameValuesToMap() { + final Map<String, Object> map = supplier("name1:group=value1", "name2:group=value2").get(); + + assertEquals(1, map.size()); + assertEquals("value1", toMap(map, "group").get("name1")); + assertEquals("value2", toMap(map, "group").get("name2")); + } + + @Test + public void shouldConvertMultipleGroupedNameValuesToMap() { + final Map<String, Object> map = supplier("name1:group1=value1", "name1:group2=value1", "name2:group2=value2").get(); + + assertEquals(2, map.size()); + assertEquals("value1", toMap(map, "group1").get("name1")); + assertEquals("value1", toMap(map, "group2").get("name1")); + assertEquals("value2", toMap(map, "group2").get("name2")); + } + + private static ParameterDataModelSupplier supplier(String... values) { + final ArrayList<String> parameters = new ArrayList<>(Arrays.asList(values)); + return new ParameterDataModelSupplier(parameters); + } + + @SuppressWarnings("unchecked") + private static Map<String, Object> toMap(Map<String, Object> model, String key) { + return (Map<String, Object>) model.get(key); + } +} diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/uri/NamedUriStringParserTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/uri/NamedUriStringParserTest.java index f007064..5de2289 100644 --- a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/uri/NamedUriStringParserTest.java +++ b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/uri/NamedUriStringParserTest.java @@ -85,6 +85,17 @@ public class NamedUriStringParserTest { } @Test + public void shouldParseNamedGroupFileName() { + final NamedUri namedURI = parse("name:group=users.csv"); + + assertEquals("name", namedURI.getName()); + assertEquals("group", namedURI.getGroup()); + assertEquals("file:///users.csv", namedURI.getUri().toString()); + assertEquals("users.csv", namedURI.getFile().getName()); + assertEquals(0, namedURI.getParameters().size()); + } + + @Test public void shouldParseNamedFileUri() { final NamedUri namedURI = parse("users=file:///users.csv"); diff --git a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/PicocliTest.java b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/PicocliTest.java index d5e15d6..d465fd0 100644 --- a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/PicocliTest.java +++ b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/PicocliTest.java @@ -81,6 +81,21 @@ public class PicocliTest { assertNull(main.sources); } + @Test + public void testSingleParameter() { + final Main main = parse("-t", TEMPLATE, "-P", "name:group=value"); + + assertEquals("value", main.parameters.get("name:group")); + } + + @Test + public void testMultipleParameters() { + final Main main = parse("-t", TEMPLATE, "-P", "name1:group=value1", "-P", "name2:group=value2"); + + assertEquals("value1", main.parameters.get("name1:group")); + assertEquals("value2", main.parameters.get("name2:group")); + } + private static Main parse(String... args) { final Main main = new Main(); new CommandLine(main).parseArgs(args);
