Skip to content

Commit f999fab

Browse files
committed
verify plugin using FreeMarkerTemplateSyntaxVerifier
1 parent 4f9fbea commit f999fab

File tree

10 files changed

+455
-0
lines changed

10 files changed

+455
-0
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- [fj-doc-maven-plugin] verify plugin using FreeMarkerTemplateSyntaxVerifier
1213
- [fj-doc-maven-plugin] m2e lifecycle configuration
1314
- [fj-doc-freemarker] tool FreeMarkerTemplateSyntaxVerifier (check for FreeMarker templates syntax)
1415

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
import lombok.Getter;
44
import lombok.Setter;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.fugerit.java.core.util.result.Result;
67

78
import java.io.FileFilter;
89
import java.util.ArrayList;
910
import java.util.List;
11+
import java.util.stream.Collectors;
1012

13+
@Slf4j
1114
public class VerifyTemplateOutput {
1215

1316
@Getter @Setter
@@ -34,4 +37,12 @@ public int getResultCode() {
3437
return Result.RESULT_CODE_OK;
3538
}
3639

40+
public List<VerifyTemplateInfo> getErrors() {
41+
return this.infos.stream().filter( info -> info.getResultCode() != Result.RESULT_CODE_OK ).collect(Collectors.toList());
42+
}
43+
44+
public List<String> getErrorsTemplateIds() {
45+
return this.getErrors().stream().map( VerifyTemplateInfo::getTemplateId ).collect(Collectors.toList());
46+
}
47+
3748
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Arrays;
1515
import java.util.Properties;
1616
import java.util.function.Function;
17+
import java.util.stream.Collectors;
1718

1819
@Slf4j
1920
public class TestFreeMarkerTemplateSyntaxyVerifier extends BasicTest {
@@ -31,6 +32,7 @@ private VerifyTemplateOutput verifyWorker(String basePath, Function<File, Verify
3132
log.info( "check info : {}", info );
3233
}
3334
log.info( "total number of checked template : {}, resultCode : {}", output.getInfos().size(), output.getResultCode() );
35+
log.info( "Templates with errors : {}", output.getErrorsTemplateIds() );
3436
return output;
3537
} );
3638
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.fugerit.java.doc.maven;
2+
3+
import org.apache.maven.plugin.AbstractMojo;
4+
import org.apache.maven.plugin.MojoExecutionException;
5+
import org.apache.maven.plugin.MojoFailureException;
6+
import org.apache.maven.plugins.annotations.Mojo;
7+
import org.apache.maven.plugins.annotations.Parameter;
8+
import org.fugerit.java.core.util.result.Result;
9+
import org.fugerit.java.doc.freemarker.tool.FreeMarkerTemplateSyntaxVerifier;
10+
import org.fugerit.java.doc.freemarker.tool.verify.VerifyTemplateOutput;
11+
12+
import java.io.File;
13+
import java.util.Properties;
14+
15+
@Mojo( name = "verify" )
16+
public class MojoVerify extends AbstractMojo {
17+
18+
@Parameter(property = "templateBasePath", required = true)
19+
protected String templateBasePath;
20+
21+
@Parameter(property = "freemarkerVersion", required = false)
22+
protected String freemarkerVersion;
23+
24+
@Parameter(property = "templateFilePattern", required = false)
25+
protected String templateFilePattern;
26+
27+
@Parameter(property = "failOnErrors", defaultValue = "true", required = true)
28+
protected boolean failOnErrors;
29+
30+
private void addIfPresent( Properties params, String key, String value ) {
31+
if ( value != null ) {
32+
this.getLog().info( String.format( "setting verifier parameter : %s -> %s", key, value ) );
33+
params.setProperty( key, value );
34+
}
35+
}
36+
37+
@Override
38+
public void execute() throws MojoExecutionException, MojoFailureException {
39+
File baseFolder = new File( this.templateBasePath );
40+
this.getLog().info( String.format( "templateBasePath: %s", baseFolder.getAbsolutePath() ) );
41+
if ( !baseFolder.isDirectory() ) {
42+
String message = String.format( "Fatal error, templateBasePath is not a directory: '%s'", templateBasePath );
43+
this.getLog().error( message );
44+
throw new MojoFailureException( message );
45+
}
46+
Properties params = new Properties();
47+
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_VERSION, this.freemarkerVersion );
48+
this.addIfPresent( params, FreeMarkerTemplateSyntaxVerifier.PARAM_TEMPLATE_FILE_PATTERN, this.templateFilePattern );
49+
FreeMarkerTemplateSyntaxVerifier verifier = new FreeMarkerTemplateSyntaxVerifier();
50+
VerifyTemplateOutput output = verifier.createConfigurationAndVerify( baseFolder, params );
51+
this.getLog().info( String.format( "verify output resultCode %s, checked templates : %s", output.getResultCode(), output.getInfos().size() ) );
52+
if ( output.getResultCode() == Result.RESULT_CODE_OK ) {
53+
this.getLog().info( "Verified OK" );
54+
} else {
55+
this.getLog().warn( String.format( "Verified KO : %s", output.getResultCode() ) );
56+
if ( this.failOnErrors ) {
57+
throw new MojoFailureException( String.format(
58+
"Build failed! FreeMarker template syntax verify failed, resultCode: %s, templates with error: %s",
59+
output.getResultCode(), output.getErrorsTemplateIds() ) );
60+
} else {
61+
this.getLog().warn( String.format(
62+
"WARNING! (failOnErrors=false) FreeMarker template syntax verify failed, resultCode: %s, templates with error: %s",
63+
output.getResultCode(), output.getErrorsTemplateIds() ) );
64+
}
65+
}
66+
}
67+
68+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package test.org.fugerit.java.doc.project.facade;
2+
3+
import org.apache.maven.plugin.MojoExecutionException;
4+
import org.apache.maven.plugin.MojoFailureException;
5+
import org.fugerit.java.core.function.SimpleValue;
6+
import org.fugerit.java.doc.maven.MojoVerify;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
12+
public class TestMojoVerify {
13+
14+
private static final String PATH_OK = "src/test/resources/fj_doc_test/template";
15+
16+
private static final String PATH_KO = "src/test/resources/fj_doc_test/template-fail";
17+
18+
@Test
19+
public void testMojoVerifyOk() throws MojoExecutionException, MojoFailureException {
20+
SimpleValue<Boolean> res = new SimpleValue<>( Boolean.FALSE );
21+
MojoVerify mojoVerify = new MojoVerify() {
22+
@Override
23+
public void execute() throws MojoExecutionException, MojoFailureException {
24+
this.failOnErrors=true;
25+
this.templateFilePattern = ".{0,}[.]ftl";
26+
this.templateBasePath = PATH_OK;
27+
super.execute();
28+
res.setValue( Boolean.TRUE );
29+
}
30+
};
31+
mojoVerify.execute();
32+
Assert.assertTrue( res.getValue() );
33+
}
34+
35+
@Test
36+
public void testMojoVerifyKo() throws MojoExecutionException, MojoFailureException {
37+
SimpleValue<Boolean> res = new SimpleValue<>( Boolean.FALSE );
38+
MojoVerify mojoVerify = new MojoVerify() {
39+
@Override
40+
public void execute() throws MojoExecutionException, MojoFailureException {
41+
this.failOnErrors=false;
42+
this.templateFilePattern = ".{0,}[.]ftl";
43+
this.templateBasePath = PATH_OK;
44+
super.execute();
45+
res.setValue( Boolean.TRUE );
46+
}
47+
};
48+
mojoVerify.execute();
49+
Assert.assertTrue( res.getValue() );
50+
}
51+
52+
@Test
53+
public void testMojoVerifyKoFail() {
54+
SimpleValue<Boolean> res = new SimpleValue<>( Boolean.FALSE );
55+
MojoVerify mojoVerify = new MojoVerify() {
56+
@Override
57+
public void execute() throws MojoExecutionException, MojoFailureException {
58+
this.failOnErrors=true;
59+
this.templateBasePath = PATH_KO;
60+
super.execute();
61+
res.setValue( Boolean.TRUE );
62+
}
63+
};
64+
Assert.assertThrows( MojoFailureException.class, () -> mojoVerify.execute() );
65+
}
66+
67+
@Test
68+
public void testMojoVerifyKoPathNoFolder() {
69+
SimpleValue<Boolean> res = new SimpleValue<>( Boolean.FALSE );
70+
MojoVerify mojoVerify = new MojoVerify() {
71+
@Override
72+
public void execute() throws MojoExecutionException, MojoFailureException {
73+
this.failOnErrors=true;
74+
this.templateBasePath = "pom.xml";
75+
super.execute();
76+
res.setValue( Boolean.TRUE );
77+
}
78+
};
79+
Assert.assertThrows( MojoFailureException.class, () -> mojoVerify.execute() );
80+
}
81+
82+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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-0.xsd" >
6+
7+
<!--
8+
Sample Apache FreeMarker template for Fugerit Doc.
9+
Note : this example has no intention of being a guid to FreeMarker
10+
(In case check FreeMarker documentation https://freemarker.apache.org/docs/index.html)
11+
-->
12+
13+
<meta>
14+
15+
<!-- Margin for document : left;right;top;bottom -->
16+
<info name="margins">10;10;10;30</info>
17+
18+
<!-- id table to be used for xlsx output -->
19+
<info name="excel-table-id">excel-table=print</info>
20+
<info name="excel-width-multiplier">450</info>
21+
<!-- id table to be used for xsv output -->
22+
<info name="csv-table-id">excel-table</info>
23+
24+
<!-- you need to escape free marker expression for currentPage -->
25+
<footer-ext>
26+
<para align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
27+
</footer-ext>
28+
29+
</meta>
30+
31+
<body>
32+
33+
<h head-level="1">Heading test level 1 default font</h>
34+
35+
<h head-level="1">Heading test level 1 TitilliumWeb</h>
36+
37+
<h head-level="2">Heading test level 2</h>
38+
39+
<h head-level="3" align="right">Heading test level 3</h>
40+
41+
<para align="right">Test right</para>
42+
43+
<para align="right">${messageFormat('test format -> {0} {1}', 'param1', 'param2')}</para>
44+
45+
<para align="right">${textWrap('test text wrap')}</para>
46+
47+
48+
<br/>
49+
<br/>
50+
<br/>
51+
52+
<phrase>Test template page params free marker</phrase>
53+
54+
<image url="cl://test/img_test_green.png" scaling="100"/>
55+
56+
<image url="cl://test/img_test_red.png" scaling="50"/>
57+
58+
<image url="png" base64="${imageBase64CLFun('test/img_test_red.png')}" scaling="25"/>
59+
60+
<image url="png" base64="${imageBase64CLFun('test/not_exists.png')}" scaling="25"/>
61+
62+
<para style="bold">italic ${sumLong(10, 20)}</para>
63+
64+
<image url="png" base64="${imageBase64CLFun()}" scaling="25"/>
65+
66+
<para style="bold">italic ${messageFormat()}</para>
67+
68+
<para style="italic">bold</para>
69+
70+
<para style="bolditalic">bold italic</para>
71+
72+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
73+
<row>
74+
<cell align="center" border-color="#ee0000" border-width="1"><para style="bold">Name</para></cell>
75+
<cell align="center"><para style="bold">Surname</para></cell>
76+
<cell align="center"><para style="bold">Title</para></cell>
77+
</row>
78+
</table>
79+
</body>
80+
81+
</doc>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<#ftl output_format="XML">
2+
<?xml version="1.0" encoding="utf-8"?>
3+
<doc
4+
xmlns="http://javacoredoc.fugerit.org"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-0.xsd" >
7+
8+
<!--
9+
Sample Apache FreeMarker template for Fugerit Doc.
10+
Note : this example has no intention of being a guid to FreeMarker
11+
(In case check FreeMarker documentation https://freemarker.apache.org/docs/index.html)
12+
-->
13+
14+
<meta>
15+
16+
<!-- Margin for document : left;right;top;bottom -->
17+
<info name="margins">10;10;10;30</info>
18+
19+
<!-- id table to be used for xlsx output -->
20+
<info name="excel-table-id">excel-table=print</info>
21+
<info name="excel-width-multiplier">450</info>
22+
<!-- id table to be used for xsv output -->
23+
<info name="csv-table-id">excel-table</info>
24+
25+
<!-- you need to escape free marker expression for currentPage -->
26+
<footer-ext>
27+
<para align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
28+
</footer-ext>
29+
30+
</meta>
31+
32+
<body>
33+
<para>${.output_format} - ${testKey!'not present'}</para>
34+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
35+
<row>
36+
<cell align="center" border-color="#ee0000" border-width="1"><para style="bold">Name</para></cell>
37+
<cell align="center"><para style="bold">Surname</para></cell>
38+
<cell align="center"><para style="bold">Title</para></cell>
39+
</row>
40+
</table>
41+
</body>
42+
43+
</doc>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<#ftl output_format="XML">
2+
<?xml version="1.0" encoding="utf-8"?>
3+
<doc
4+
xmlns="http://javacoredoc.fugerit.org"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-0.xsd" >
7+
8+
<!--
9+
Sample Apache FreeMarker template for Fugerit Doc.
10+
Note : this example has no intention of being a guid to FreeMarker
11+
(In case check FreeMarker documentation https://freemarker.apache.org/docs/index.html)
12+
-->
13+
14+
<meta>
15+
16+
<!-- Margin for document : left;right;top;bottom -->
17+
<info name="margins">10;10;10;30</info>
18+
19+
<!-- id table to be used for xlsx output -->
20+
<info name="excel-table-id">excel-table=print</info>
21+
<info name="excel-width-multiplier">450</info>
22+
<!-- id table to be used for xsv output -->
23+
<info name="csv-table-id">excel-table</info>
24+
25+
<!-- you need to escape free marker expression for currentPage -->
26+
<footer-ext>
27+
<para align="right">${r"${currentPage}"} / ${r"${pageCount}"}</para>
28+
</footer-ext>
29+
30+
</meta>
31+
32+
<body>
33+
<para>${.output_format} - ${testKey!'not present''}</para>
34+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
35+
<row>
36+
<cell align="center" border-color="#ee0000" border-width="1"><para style="bold">Name</para></cell>
37+
<cell align="center"><para style="bold">Surname</para></cell>
38+
<cell align="center"><para style="bold">Title</para></cell>
39+
</row>
40+
</table>
41+
</body>
42+
43+
</doc>

0 commit comments

Comments
 (0)