This is an automated email from the ASF dual-hosted git repository. sgoeschl pushed a commit to branch FREEMARKER-163 in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 75e9e2dcd474f3f9c8186c68e0ed80bce69038be Author: Siegfried Goeschl <[email protected]> AuthorDate: Mon Nov 9 13:14:38 2020 +0100 FREEMARKER-163 Integrate Java Faker library for test data generation --- .../src/app/config/freemarker-generator.properties | 3 +- .../examples/templates/javafaker/csv/testdata.ftl | 47 ++++++++++++++++ .../src/app/scripts/run-examples.bat | 6 ++ .../src/app/scripts/run-examples.sh | 7 +++ .../markdown/cli/introduction/getting-started.md | 61 ++++++++------------- .../site/markdown/cli/usage/generating-testdata.md | 60 ++++++++++++++++++++ .../src/site/markdown/index.md | 1 + .../freemarker/generator/cli/ExamplesTest.java | 5 ++ freemarker-generator-tools/pom.xml | 6 ++ .../generator/tools/javafaker/JavaFakerTool.java | 64 ++++++++++++++++++++++ .../tools/javafaker/JavaFakerToolTest.java | 43 +++++++++++++++ 11 files changed, 264 insertions(+), 39 deletions(-) diff --git a/freemarker-generator-cli/src/app/config/freemarker-generator.properties b/freemarker-generator-cli/src/app/config/freemarker-generator.properties index fd4a8f9..25b7029 100644 --- a/freemarker-generator-cli/src/app/config/freemarker-generator.properties +++ b/freemarker-generator-cli/src/app/config/freemarker-generator.properties @@ -14,13 +14,11 @@ ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- - ############################################################################# # General FreeMarker Configuration # See https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String- ############################################################################# # freemarker.configuration.setting.locale=JVM default - ############################################################################# # Configure FreeMarker Tools (name -> implementation class) ############################################################################# @@ -31,6 +29,7 @@ freemarker.tools.exec=org.apache.freemarker.generator.tools.commonsexec.CommonsE freemarker.tools.freemarker=org.apache.freemarker.generator.tools.freemarker.FreeMarkerTool freemarker.tools.grok=org.apache.freemarker.generator.tools.grok.GrokTool freemarker.tools.gson=org.apache.freemarker.generator.tools.gson.GsonTool +freemarker.tools.javafaker=org.apache.freemarker.generator.tools.javafaker.JavaFakerTool freemarker.tools.jsonpath=org.apache.freemarker.generator.tools.jsonpath.JsonPathTool freemarker.tools.jsoup=org.apache.freemarker.generator.tools.jsoup.JsoupTool freemarker.tools.properties=org.apache.freemarker.generator.tools.properties.PropertiesTool diff --git a/freemarker-generator-cli/src/app/examples/templates/javafaker/csv/testdata.ftl b/freemarker-generator-cli/src/app/examples/templates/javafaker/csv/testdata.ftl new file mode 100644 index 0000000..86bc223 --- /dev/null +++ b/freemarker-generator-cli/src/app/examples/templates/javafaker/csv/testdata.ftl @@ -0,0 +1,47 @@ +<#ftl output_format="plainText" strip_whitespace=true> +<#-- + 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. +--> +<#import "/freemarker-generator/lib/commons-csv.ftl" as csv /> +<#assign timeUnits = tools.javafaker.timeUnits> +<#assign faker = tools.javafaker.faker> +<#assign csvTargetFormat = csv.targetFormat()> +<#assign csvPrinter = tools.csv.printer(csvTargetFormat)> +<#assign csvHeaders = ['ID','CUSTOMER_ID','FIRST_NAME','LAST_NAME','EMAIL','IBAN','CREATED_AT']> +<#compress> + <#if csvTargetFormat.getSkipHeaderRecord()> + ${csvPrinter.printRecord(csvHeaders)}<#t> + </#if> + <#list 1..10 as i> + <#assign id = tools.uuid.randomUUID()> + <#assign customerId = faker.bothify("?#######")> + <#assign firstName = faker.name().firstName()> + <#assign lastName = faker.name().lastName()> + <#assign email = firstName + "." + lastName + "@gmail.com"> + <#assign iban = faker.finance().iban("AT")> + <#assign createAt = faker.date().past(3650, timeUnits["DAYS"])> + + ${csvPrinter.printRecord( + id, + customerId, + firstName, + lastName, + email?lower_case + iban, + createAt?datetime?iso_local + )}<#t> + </#list> +</#compress> \ No newline at end of file diff --git a/freemarker-generator-cli/src/app/scripts/run-examples.bat b/freemarker-generator-cli/src/app/scripts/run-examples.bat index c472333..4bb5e5e 100644 --- a/freemarker-generator-cli/src/app/scripts/run-examples.bat +++ b/freemarker-generator-cli/src/app/scripts/run-examples.bat @@ -127,6 +127,12 @@ echo "examples\templates\html\txt\licence.ftl" %FREEMARKER_CMD% -t examples\templates\html\txt\licence.ftl examples\data\html\dependencies.html > target\out\licence.txt REM ========================================================================= +REM Java Faker +REM ========================================================================= +echo "examples/templates/javafaker/csv/testdata.ftl" +%FREEMARKER_CMD% -t examples/templates/javafaker/csv/testdata.ftl > target/out/testdata.csv + +REM ========================================================================= REM JSON REM ========================================================================= diff --git a/freemarker-generator-cli/src/app/scripts/run-examples.sh b/freemarker-generator-cli/src/app/scripts/run-examples.sh index 89f86e7..316a098 100755 --- a/freemarker-generator-cli/src/app/scripts/run-examples.sh +++ b/freemarker-generator-cli/src/app/scripts/run-examples.sh @@ -147,6 +147,13 @@ echo "examples/templates/html/txt/licence.ftl" $FREEMARKER_CMD -t examples/templates/html/txt/licence.ftl examples/data/html/dependencies.html > target/out/licence.txt || { echo >&2 "Test failed. Aborting."; exit 1; } ############################################################################# +# Java Faker +############################################################################# + +echo "examples/templates/javafaker/csv/testdata.ftl" +$FREEMARKER_CMD -t examples/templates/javafaker/csv/testdata.ftl > target/out/testdata.csv || { echo >&2 "Test failed. Aborting."; exit 1; } + +############################################################################# # JSON ############################################################################# diff --git a/freemarker-generator-cli/src/site/markdown/cli/introduction/getting-started.md b/freemarker-generator-cli/src/site/markdown/cli/introduction/getting-started.md index 74deace..f1bc312 100644 --- a/freemarker-generator-cli/src/site/markdown/cli/introduction/getting-started.md +++ b/freemarker-generator-cli/src/site/markdown/cli/introduction/getting-started.md @@ -91,53 +91,40 @@ to better understand `Apache FreeMarker Generator` FreeMarker Generator Information ------------------------------------------------------------------------------ FreeMarker version : 2.3.30 -Template name : templates/info.ftl +Template name : info.ftl Language : en Locale : en_US -Timestamp : Jun 26, 2020 10:44:15 AM +Timestamp : Nov 9, 2020 1:13:21 PM Output encoding : UTF-8 Output format : plainText FreeMarker Generator Template Loader Directories ------------------------------------------------------------------------------ -[#1] /Users/sgoeschl/work/github/apache/freemarker-generator -[#2] /Users/sgoeschl/.freemarker-generator -[#3] /Applications/Java/freemarker-generator-2.0.0 - -FreeMarker Generator Tools ------------------------------------------------------------------------------- -- CSVTool : Process CSV files using Apache Commons CSV (see https://commons.apache.org/proper/commons-csv/) -- DataFrameTool : Bridge to [nRo/DataFrame](https://github.com/nRo/DataFrame) -- ExcelTool : Process Excels files (XLS, XLSX) using Apache POI (see https://poi.apache.org) -- ExecTool : Execute command line tools using Apache Commons Exec (see https://commons.apache.org/proper/commons-exec/) -- FreeMarkerTool : Expose advanced Apache FreeMarker classes -- GrokTool : Process text files using Grok expressions (see https://github.com/thekrakken/java-grok) -- GsonTool : Process JSON files using GSON (see https://github.com/google/gson) -- JsonPathTool : Process JSON files using Java JSON Path (see https://github.com/json-path/JsonPath) -- JsoupTool : Process HTML files using Jsoup (see https://jsoup.org) -- PropertiesTool : Process JDK properties files -- SystemTool : Expose System-related utility methods -- UUIDTool : Create UUIDs -- XmlTool : Process XML files using Apache FreeMarker (see https://freemarker.apache.org/docs/xgui.html) -- YamlTool : Process YAML files using SnakeYAML(see https://bitbucket.org/asomov/snakeyaml/wiki/Home) +[#1] /Users/sgoeschl/.freemarker-generator/templates +[#2] /Users/sgoeschl/work/github/apache/freemarker-generator/freemarker-generator-cli/target/appassembler/templates FreeMarker Generator Data Model --------------------------------------------------------------------------- -- CSVTool -- DataFrameTool -- DataSources -- ExcelTool -- ExecTool -- FreeMarkerTool -- GrokTool -- GsonTool -- JsonPathTool -- JsoupTool -- PropertiesTool -- SystemTool -- UUIDTool -- XmlTool -- YamlTool +- dataSources +- tools + +FreeMarker Generator Tools +------------------------------------------------------------------------------ +- csv : Process CSV files using Apache Commons CSV (see https://commons.apache.org/proper/commons-csv/) +- dataframe : Bridge to [nRo/DataFrame](https://github.com/nRo/DataFrame) +- excel : Process Excels files (XLS, XLSX) using Apache POI (see https://poi.apache.org) +- exec : Execute command line tools using Apache Commons Exec (see https://commons.apache.org/proper/commons-exec/) +- freemarker : Expose advanced Apache FreeMarker classes +- grok : Process text files using Grok expressions (see https://github.com/thekrakken/java-grok) +- gson : Process JSON files using GSON (see https://github.com/google/gson) +- javafaker : Generate test data using Java Faker (see https://github.com/DiUS/java-faker) +- jsonpath : Process JSON files using Java JSON Path (see https://github.com/json-path/JsonPath) +- jsoup : Process HTML files using Jsoup (see https://jsoup.org) +- properties : Process JDK properties files +- system : Expose System-related utility methods +- uuid : Create UUIDs +- xml : Process XML files using Apache FreeMarker (see https://freemarker.apache.org/docs/xgui.html) +- yaml : Process YAML files using SnakeYAML(see https://bitbucket.org/asomov/snakeyaml/wiki/Home) ``` * The "FreeMarker Generator Information" section provides insights into configuration and currently processed template. diff --git a/freemarker-generator-cli/src/site/markdown/cli/usage/generating-testdata.md b/freemarker-generator-cli/src/site/markdown/cli/usage/generating-testdata.md new file mode 100644 index 0000000..eedd1b9 --- /dev/null +++ b/freemarker-generator-cli/src/site/markdown/cli/usage/generating-testdata.md @@ -0,0 +1,60 @@ +## Generating Test Data + +The generation of test data is supported by [Java Faker](https://github.com/DiUS/java-faker). + +Let's assume that you need to populate a user table based on a CSV file + +``` +freemarker-generator -t examples/templates/javafaker/csv/testdata.ftl +``` + +will generate + +``` +ID,CUSTOMER_ID,FIRST_NAME,LAST_NAME,EMAIL,IBAN,CREATED_AT +7e522b5c-1c7f-4195-8585-2268cc7f152a,l1671977,Eddy,Schuster,[email protected],AT101071425404684529,2019-05-18T17:28:30+02:00 +701db208-923b-4100-a02b-4d574b0b703d,e5229102,Mark,Rogahn,[email protected],AT491398501778297253,2014-06-16T21:48:24+02:00 +d5e99c0f-0117-42f9-83a2-33052656e9d6,g0941131,Randall,Cronin,[email protected],AT788431029877229377,2016-01-08T05:12:26+01:00 +e2b44ea6-4033-45c9-b9c3-bc9c8f6bc3fe,o4440179,Arthur,Reichel,[email protected],AT767530622367679157,2013-04-07T12:56:13+02:00 +d0dd2806-fe11-487c-9d9b-22dd3ec1abc4,i5706274,Elroy,Sawayn,[email protected],AT459308883343277427,2017-02-06T01:53:34+01:00 +d36d6851-e961-4f07-9fcc-f7a0702fa6b6,s5645599,Jerald,Nitzsche,[email protected],AT366350952237029663,2015-06-14T21:43:32+02:00 +6b9999cf-5aed-4e12-bc6e-e88a7973154d,n4220945,Grant,Bayer,[email protected],AT275651850312588466,2020-07-18T15:07:07+02:00 +5fd8286e-cc2f-4fe9-bf56-2245d1bf0c34,t0311536,Edison,Hoppe,[email protected],AT847505633045057210,2017-04-23T23:26:42+02:00 +e0638c76-f130-4daa-9c62-40304101c8ab,b7209087,Nicky,Cole,[email protected],AT127017906823575933,2014-08-30T20:38:11+02:00 +c35e2b24-9b9b-4fb0-b25b-72af93301da5,a0087873,Raleigh,Leffler,[email protected],AT140117255798582963,2016-04-04T12:12:29+02:00 +``` + +using the following FreeMarker template + +``` +<#import "/freemarker-generator/lib/commons-csv.ftl" as csv /> +<#assign timeUnits = tools.javafaker.timeUnits> +<#assign faker = tools.javafaker.faker> +<#assign csvTargetFormat = csv.targetFormat()> +<#assign csvPrinter = tools.csv.printer(csvTargetFormat)> +<#assign csvHeaders = ['ID','CUSTOMER_ID','FIRST_NAME','LAST_NAME','EMAIL','IBAN','CREATED_AT']> +<#compress> + <#if csvTargetFormat.getSkipHeaderRecord()> + ${csvPrinter.printRecord(csvHeaders)}<#t> + </#if> + <#list 1..10 as i> + <#assign id = tools.uuid.randomUUID()> + <#assign customerId = faker.bothify("?#######")> + <#assign firstName = faker.name().firstName()> + <#assign lastName = faker.name().lastName()> + <#assign email = firstName + "." + lastName + "@gmail.com"> + <#assign iban = faker.finance().iban("AT")> + <#assign createAt = faker.date().past(3650, timeUnits["DAYS"])> + + ${csvPrinter.printRecord( + id, + customerId, + firstName, + lastName, + email?lower_case + iban, + createAt?datetime?iso_local + )}<#t> + </#list> +</#compress> +``` diff --git a/freemarker-generator-cli/src/site/markdown/index.md b/freemarker-generator-cli/src/site/markdown/index.md index 1b33d5c..9a6ecc4 100644 --- a/freemarker-generator-cli/src/site/markdown/index.md +++ b/freemarker-generator-cli/src/site/markdown/index.md @@ -30,6 +30,7 @@ * [Transforming Directories](cli/usage/transforming-directories.html) * [Using DataFrames](cli/usage/using-dataframes.html) * [Transforming CSV](cli/usage/transforming-csv.html) +* [Generating Test Data](cli/usage/generating-testdata.html) ### Advanced Topics diff --git a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java index a2e7c79..b44d94e 100644 --- a/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java +++ b/freemarker-generator-cli/src/test/java/org/apache/freemarker/generator/cli/ExamplesTest.java @@ -113,6 +113,11 @@ public class ExamplesTest extends AbstractMainTest { } @Test + public void shouldRunJavaFakerExamples() throws IOException { + assertValid(execute("-PCSV_TARGET_DELIMITER=SEMICOLON -t src/app/examples/templates/javafaker/csv/testdata.ftl")); + } + + @Test public void shouldRunInteractiveTemplateExamples() throws IOException { assertValid(execute("-i ${tools.jsonpath.parse(dataSources?values[0]).read(\"$.info.title\")} src/app/examples/data/json/swagger-spec.json")); assertValid(execute("-i ${tools.xml.parse(dataSources?values[0])[\"recipients/person[1]/name\"]} src/app/examples/data/xml/recipients.xml")); diff --git a/freemarker-generator-tools/pom.xml b/freemarker-generator-tools/pom.xml index 2d590a2..c533a6d 100644 --- a/freemarker-generator-tools/pom.xml +++ b/freemarker-generator-tools/pom.xml @@ -93,6 +93,12 @@ <artifactId>gson</artifactId> <version>2.8.6</version> </dependency> + <!-- Java Faker --> + <dependency> + <groupId>com.github.javafaker</groupId> + <artifactId>javafaker</artifactId> + <version>1.0.2</version> + </dependency> <!-- JsonPath Tool --> <dependency> <groupId>com.jayway.jsonpath</groupId> diff --git a/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerTool.java b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerTool.java new file mode 100644 index 0000000..0dd2b46 --- /dev/null +++ b/freemarker-generator-tools/src/main/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerTool.java @@ -0,0 +1,64 @@ +/* + * 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.tools.javafaker; + +import com.github.javafaker.Faker; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +public class JavaFakerTool { + + private Faker faker; + private Map<String, TimeUnit> timeUnitMap; + + public synchronized Faker getFaker() { + if (faker == null) { + faker = new Faker(Locale.getDefault()); + timeUnitMap = createTimeUnitMap(); + } + + return faker; + } + + public synchronized Map<String, TimeUnit> getTimeUnits() { + if (timeUnitMap == null) { + timeUnitMap = createTimeUnitMap(); + } + + return timeUnitMap; + } + + @Override + public String toString() { + return "Generate test data using Java Faker (see https://github.com/DiUS/java-faker)"; + } + + private static Map<String, TimeUnit> createTimeUnitMap() { + final Map<String, TimeUnit> result = new HashMap<>(); + result.put("MICROSECONDS", TimeUnit.MICROSECONDS); + result.put("MILLISECONDS", TimeUnit.MILLISECONDS); + result.put("SECONDS", TimeUnit.SECONDS); + result.put("MINUTES", TimeUnit.MINUTES); + result.put("HOURS", TimeUnit.HOURS); + result.put("DAYS", TimeUnit.DAYS); + return result; + } + +} diff --git a/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerToolTest.java b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerToolTest.java new file mode 100644 index 0000000..e378cd6 --- /dev/null +++ b/freemarker-generator-tools/src/test/java/org/apache/freemarker/generator/tools/javafaker/JavaFakerToolTest.java @@ -0,0 +1,43 @@ +/* + * 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.tools.javafaker; + +import com.github.javafaker.Faker; +import org.junit.Test; + +import java.util.concurrent.TimeUnit; + +import static junit.framework.TestCase.assertTrue; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +public class JavaFakerToolTest { + + private final Faker faker = javaFakerTool().getFaker(); + + @Test + public void shouldCreateFakeData() { + assertFalse(faker.name().fullName().isEmpty()); + assertFalse(faker.internet().emailAddress().isEmpty()); + assertNotNull(faker.date().past(12, TimeUnit.DAYS)); + assertTrue(faker.finance().iban("AT").startsWith("AT")); + } + + private static final JavaFakerTool javaFakerTool() { + return new JavaFakerTool(); + } +}
