Skip to content

Commit e65d02b

Browse files
committed
FreeMarkerTemplateSyntaxVerifier with report
1 parent a17fdb7 commit e65d02b

File tree

11 files changed

+1053
-873
lines changed

11 files changed

+1053
-873
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- [fj-doc-maven-plugin] verify plugin using FreeMarkerTemplateSyntaxVerifier
1313
- [fj-doc-maven-plugin] m2e lifecycle configuration
14-
- [fj-doc-freemarker] tool FreeMarkerTemplateSyntaxVerifier (check for FreeMarker templates syntax)
14+
- [fj-doc-freemarker] tool FreeMarkerTemplateSyntaxVerifier (check for FreeMarker templates syntax) with report
1515

1616
### Fixed
1717

docs/html/doc_meta_info.html

Lines changed: 175 additions & 175 deletions
Large diffs are not rendered by default.

fj-doc-base/src/main/docs/doc_xsd_config_ref.html

Lines changed: 536 additions & 536 deletions
Large diffs are not rendered by default.

fj-doc-freemarker/src/main/docs/fdp_xsd_config_ref.html

Lines changed: 159 additions & 159 deletions
Large diffs are not rendered by default.

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/tool/FreeMarkerTemplateSyntaxVerifier.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,24 @@
55
import freemarker.template.Template;
66
import freemarker.template.Version;
77
import lombok.extern.slf4j.Slf4j;
8+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
89
import org.fugerit.java.core.function.SafeFunction;
10+
import org.fugerit.java.core.io.FileIO;
11+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
12+
import org.fugerit.java.core.lang.helpers.StringUtils;
913
import org.fugerit.java.core.util.result.Result;
14+
import org.fugerit.java.doc.base.config.DocConfig;
15+
import org.fugerit.java.doc.base.process.DocProcessContext;
1016
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
17+
import org.fugerit.java.doc.freemarker.helper.FreeMarkerDocProcess;
1118
import org.fugerit.java.doc.freemarker.tool.verify.VerifyTemplateInfo;
1219
import org.fugerit.java.doc.freemarker.tool.verify.VerifyTemplateOutput;
1320

21+
import java.io.ByteArrayOutputStream;
1422
import java.io.File;
1523
import java.io.IOException;
24+
import java.util.Collections;
25+
import java.util.Comparator;
1626
import java.util.Properties;
1727

1828
/**
@@ -44,6 +54,16 @@ public class FreeMarkerTemplateSyntaxVerifier {
4454
*/
4555
public static final String PARAM_TEMPLATE_FILE_PATTERN = "templateFilePattern";
4656

57+
/**
58+
* If set to true a report will be generated (and property 'reportOutputFolder' will be olso required).
59+
*/
60+
public static final String PARAM_GENERATE_REPORT = "generateReport";
61+
62+
/**
63+
* Output folder for the generated report.
64+
*/
65+
public static final String PARAM_REPORT_OUTPUT_FOLDER = "reportOutputFolder";
66+
4767
public static VerifyTemplateOutput doCreateConfigurationAndVerify(File baseFolder) {
4868
return new FreeMarkerTemplateSyntaxVerifier().createConfigurationAndVerify( baseFolder, new Properties() );
4969
}
@@ -92,10 +112,38 @@ public VerifyTemplateOutput verify(File baseFolder, Configuration cfg, Propertie
92112
}
93113
// setup iteration with baseFolder as currentFolder
94114
this.iterateVerify( output, baseFolder, cfg, baseFolder );
115+
Collections.sort( output.getInfos(), Comparator.comparing(VerifyTemplateInfo::getTemplateId) );
116+
// generate report
117+
this.generateReport( output, params );
95118
return output;
96119
} );
97120
}
98121

122+
public void generateReport( VerifyTemplateOutput output, Properties params ) throws IOException {
123+
boolean generateReport = BooleanUtils.isTrue( params.getProperty( PARAM_GENERATE_REPORT ) );
124+
if ( generateReport ) {
125+
String reportOutputFolder = params.getProperty( PARAM_REPORT_OUTPUT_FOLDER );
126+
if (StringUtils.isEmpty( reportOutputFolder )) {
127+
throw new ConfigRuntimeException( String.format( "Parameter '%s' is required", PARAM_REPORT_OUTPUT_FOLDER ) );
128+
} else {
129+
File reportOutputFolderFile = new File( reportOutputFolder );
130+
if ( !reportOutputFolderFile.exists() ) {
131+
log.info( "{} does not exist, create directories : {}", reportOutputFolder, reportOutputFolderFile.mkdirs() );
132+
}
133+
String chainId = "freemarker-verify-syntax-report";
134+
String type = DocConfig.TYPE_HTML;
135+
File reportFile = new File( reportOutputFolderFile, chainId+"."+type );
136+
try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {
137+
FreeMarkerDocProcess.getInstance().fullProcess( chainId, DocProcessContext.newContext( "output", output ), type, buffer );
138+
FileIO.writeBytes( buffer.toByteArray(), reportFile );
139+
} catch ( Exception e ) {
140+
throw new ConfigRuntimeException(
141+
String.format( "Fail to generate report in folder %s, error : %s", reportOutputFolder, e.getMessage() ), e );
142+
}
143+
}
144+
}
145+
}
146+
99147
/**
100148
* It will create a new FreeMarker configuration, loading templates from given base folder.
101149
*

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/tool/verify/VerifyTemplateOutput.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.FileFilter;
99
import java.util.ArrayList;
10+
import java.util.Date;
1011
import java.util.List;
1112
import java.util.stream.Collectors;
1213

@@ -19,8 +20,12 @@ public class VerifyTemplateOutput {
1920
@Getter
2021
private List<VerifyTemplateInfo> infos;
2122

23+
@Getter
24+
private Date creationTime;
25+
2226
public VerifyTemplateOutput() {
2327
this.infos = new ArrayList<>();
28+
this.creationTime = new Date();
2429
}
2530

2631
/**

fj-doc-freemarker/src/main/resources/fj_doc_freemarker_config/fm-freemarker-doc-process-config.xml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@
22
<freemarker-doc-process-config
33
xmlns="https://freemarkerdocprocess.fugerit.org"
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" >
5+
xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" >
6+
7+
<docHandlerConfig registerById="true">
8+
9+
<!-- Type handler for markdown format -->
10+
<docHandler id="md-ext" info="md" type="org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandler" />
11+
<!-- Type henalder for xml format, generates the source xml:doc -->
12+
<docHandler id="xml-doc" info="xml" type="org.fugerit.java.doc.base.config.DocTypeHandlerXMLUTF8" />
13+
14+
<!-- Type handlers for html using freemarker -->
15+
<docHandler id="html-fm" info="html" type="org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerEscapeUTF8" />
16+
<!-- Type handlers for html using freemarker (fragment version, only generates body content no html or head part -->
17+
<docHandler id="html-fragment-fm" info="fhtml" type="org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandlerEscapeUTF8" />
18+
19+
</docHandlerConfig>
620

721
<docChain id="base-freemarker">
822
<chainStep stepType="config">
@@ -42,4 +56,8 @@
4256
<chainStep stepType="complex" template-path="freemarker-doc-process-config-stub.ftl"/>
4357
</docChain>
4458

59+
<docChain id="freemarker-verify-syntax-report" parent="base-freemarker">
60+
<chainStep stepType="complex" map-all="1" template-path="${chainId}.ftl"/>
61+
</docChain>
62+
4563
</freemarker-doc-process-config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<doc
3+
xmlns="http://javacoredoc.fugerit.org"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd" >
6+
7+
<#--
8+
This is a Venus Fugerit Doc (https://github.com/fugerit-org/fj-doc) FreeMarker Template XML (ftl[x]).
9+
For consideration of Venus Fugerit Doc and Apache FreeMarker integration see :
10+
https://venusguides.fugerit.org/src/docs/common/doc_format_freemarker.html
11+
The result will be a :
12+
-->
13+
<!--
14+
This is a Venus Fugerit Doc (https://github.com/fugerit-org/fj-doc) XML Source Document.
15+
For documentation on how to write a valid Venus Doc XML Meta Model refer to :
16+
https://venusguides.fugerit.org/src/docs/common/doc_format_summary.html
17+
-->
18+
19+
<#assign defaultTitle="FreeMarker syntax verifier report">
20+
21+
<metadata>
22+
<!-- Margin for document : left;right;top;bottom -->
23+
<info name="margins">10;10;10;30</info>
24+
<!-- documenta meta information -->
25+
<info name="doc-title">${docTitle!defaultTitle}</info>
26+
<info name="doc-subject">Output of the tool FreeMarker syntax verifier</info>
27+
<info name="doc-author">fugerit79</info>
28+
<info name="doc-language">en</info>
29+
<!-- property specific for xls/xlsx -->
30+
<info name="excel-table-id">data-table=print</info>
31+
<!-- property specific for csv -->
32+
<info name="csv-table-id">data-table</info>
33+
<footer-ext>
34+
<para align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
35+
</footer-ext>
36+
</metadata>
37+
<body>
38+
<para head-level="1">${docTitle!defaultTitle} - ${output.creationTime?datetime?iso_utc}</para>
39+
<#if output?? && output.infos?? && output.infos?size != 0 >
40+
<para>Total number of templates verified : ${output.infos?size}</para>
41+
<para>Total number of templates with syntax errors : ${output.errors?size}</para>
42+
<#if output.errors?size != 0 ><para>Template with errors :<#list output.errorsTemplateIds as current> <phrase link="#${current}">${current}</phrase></#list></para></#if>
43+
<table columns="3" colwidths="30;20;50" width="100" id="data-table" padding="2">
44+
<row header="true">
45+
<cell align="center"><para>Template</para></cell>
46+
<cell align="center"><para>Verify result</para></cell>
47+
<cell align="center"><para>Note</para></cell>
48+
</row>
49+
<#list output.infos as current>
50+
<#if current.resultCode == 0 ><#assign backcolor="#99ff99"/><#else><#assign backcolor="#ff9999"/></#if>
51+
<row>
52+
<cell back-color="${backcolor}"><phrase anchor="${current.templateId}"></phrase><para>${current.templateId}</para></cell>
53+
<cell back-color="${backcolor}"><para><#if current.resultCode == 0 >OK<#else>Fail</#if> (${current.resultCode})</para></cell>
54+
<cell back-color="${backcolor}"><para><#if current.exception??><![CDATA[${current.exception.message}]]></#if></para></cell>
55+
</row>
56+
</#list>
57+
</table>
58+
<#else>
59+
<para>No data found</para>
60+
</#if>
61+
</body>
62+
63+
</doc>

fj-doc-freemarker/src/test/java/test/org/fugerit/java/doc/freemarker/tool/TestFreeMarkerTemplateSyntaxyVerifier.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package test.org.fugerit.java.doc.freemarker.tool;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
45
import org.fugerit.java.core.function.SafeFunction;
56
import org.fugerit.java.core.util.result.Result;
67
import org.fugerit.java.doc.freemarker.tool.FreeMarkerTemplateSyntaxVerifier;
@@ -12,9 +13,9 @@
1213

1314
import java.io.File;
1415
import java.util.Arrays;
16+
import java.util.List;
1517
import java.util.Properties;
1618
import java.util.function.Function;
17-
import java.util.stream.Collectors;
1819

1920
@Slf4j
2021
public class TestFreeMarkerTemplateSyntaxyVerifier extends BasicTest {
@@ -62,6 +63,8 @@ public void verifyTestKo() {
6263
public void verifyTemplateFilePattern() {
6364
Properties params = new Properties();
6465
params.setProperty( FreeMarkerTemplateSyntaxVerifier.PARAM_TEMPLATE_FILE_PATTERN, ".{0,}[.]ftl" );
66+
params.setProperty( FreeMarkerTemplateSyntaxVerifier.PARAM_GENERATE_REPORT, "1" );
67+
params.setProperty( FreeMarkerTemplateSyntaxVerifier.PARAM_REPORT_OUTPUT_FOLDER, "target/report-1" );
6568
FreeMarkerTemplateSyntaxVerifier verifier = new FreeMarkerTemplateSyntaxVerifier();
6669
Arrays.asList( "src/test/resources/fj_doc_test/template-fail",
6770
"src/test/resources/fj_doc_test/template-macro" ).forEach(
@@ -71,6 +74,14 @@ public void verifyTemplateFilePattern() {
7174
Assert.assertEquals(Result.RESULT_CODE_OK, output.getResultCode());
7275
}
7376
);
77+
// check missing report folder
78+
params.remove( FreeMarkerTemplateSyntaxVerifier.PARAM_REPORT_OUTPUT_FOLDER );
79+
Arrays.asList( "src/test/resources/fj_doc_test/template-fail" ).forEach(
80+
basePath -> {
81+
Assert.assertThrows( ConfigRuntimeException.class, () -> this.verifyWorker( basePath,
82+
f -> verifier.createConfigurationAndVerify( f, params ) ) );
83+
}
84+
);
7485
}
7586

7687
@Test
@@ -83,4 +94,22 @@ public void checkPojo() {
8394
Assert.assertThrows( NullPointerException.class, () -> new VerifyTemplateInfo( null, null, null ) );
8495
}
8596

97+
@Test
98+
public void failReport() {
99+
VerifyTemplateOutput output = new VerifyTemplateOutput() {
100+
@Override
101+
public List<VerifyTemplateInfo> getInfos() {
102+
if ( Boolean.TRUE ) {
103+
throw new ConfigRuntimeException( "Scenario error" );
104+
}
105+
return super.getInfos();
106+
}
107+
};
108+
FreeMarkerTemplateSyntaxVerifier verifier = new FreeMarkerTemplateSyntaxVerifier();
109+
Properties params = new Properties();
110+
params.setProperty( FreeMarkerTemplateSyntaxVerifier.PARAM_GENERATE_REPORT, "1" );
111+
params.setProperty( FreeMarkerTemplateSyntaxVerifier.PARAM_REPORT_OUTPUT_FOLDER, "target/report-fail" );
112+
Assert.assertThrows( ConfigRuntimeException.class, () -> verifier.generateReport( output, params ) );
113+
}
114+
86115
}

fj-doc-maven-plugin/src/main/java/org/fugerit/java/doc/maven/MojoVerify.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public class MojoVerify extends AbstractMojo {
2727
@Parameter(property = "failOnErrors", defaultValue = "true", required = true)
2828
protected boolean failOnErrors;
2929

30+
@Parameter(property = "generateReport", defaultValue = "false", required = false)
31+
protected boolean generateReport;
32+
33+
@Parameter(property = "reportOutputFolder", required = false)
34+
protected String reportOutputFolder;
35+
3036
private void addIfPresent( Properties params, String key, String value ) {
3137
if ( value != null ) {
3238
this.getLog().info( String.format( "setting verifier parameter : %s -> %s", key, value ) );
@@ -46,6 +52,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
4652
Properties params = new Properties();
4753
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_VERSION, this.freemarkerVersion );
4854
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_TEMPLATE_FILE_PATTERN, this.templateFilePattern );
55+
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_GENERATE_REPORT, String.valueOf( this.generateReport ) );
56+
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_REPORT_OUTPUT_FOLDER, this.reportOutputFolder );
4957
FreeMarkerTemplateSyntaxVerifier verifier = new FreeMarkerTemplateSyntaxVerifier();
5058
VerifyTemplateOutput output = verifier.createConfigurationAndVerify( baseFolder, params );
5159
this.getLog().info( String.format( "verify output resultCode %s, checked templates : %s", output.getResultCode(), output.getInfos().size() ) );

fj-doc-maven-plugin/src/test/java/test/org/fugerit/java/doc/project/facade/TestMojoVerify.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ public class TestMojoVerify {
1313

1414
private static final String PATH_KO = "src/test/resources/fj_doc_test/template-fail";
1515

16+
private static final String OUTPUT_1 = "target/report-1";
17+
18+
private static final String OUTPUT_2 = "target/report-2";
19+
20+
1621
@Test
1722
public void testMojoVerifyOk() throws MojoExecutionException, MojoFailureException {
1823
SimpleValue<Boolean> res = new SimpleValue<>( Boolean.FALSE );
@@ -22,6 +27,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
2227
this.failOnErrors=true;
2328
this.templateFilePattern = ".{0,}[.]ftl";
2429
this.templateBasePath = PATH_OK;
30+
this.generateReport = true;
31+
this.reportOutputFolder = OUTPUT_1;
2532
super.execute();
2633
res.setValue( Boolean.TRUE );
2734
}
@@ -54,6 +61,8 @@ public void testMojoVerifyKoFail() {
5461
public void execute() throws MojoExecutionException, MojoFailureException {
5562
this.failOnErrors=true;
5663
this.templateBasePath = PATH_KO;
64+
this.generateReport = true;
65+
this.reportOutputFolder = OUTPUT_2;
5766
super.execute();
5867
res.setValue( Boolean.TRUE );
5968
}

0 commit comments

Comments
 (0)