Skip to content

Commit 08fa4db

Browse files
committed
FreeMarker syntax verify example
1 parent 1052add commit 08fa4db

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-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+
- FreeMarker syntax verify example
1213
- module fj-doc
1314
- Hour check
1415
- Period check test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<#macro failMacro param1>${messageFormat(params['prop1''], param1}</#macro>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<#macro simpleMacro param1>Print param1 : ${param1}</#macro>

code-samples-fj-doc/src/main/resources/code-samples-fj-doc/template/message-format.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2+
<#import "lib/macro.ftl" as m>
23
<doc
34
xmlns="http://javacoredoc.fugerit.org"
45
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
@@ -30,6 +31,7 @@
3031
<body>
3132
<para>${docTitle!defaultTitle}</para>
3233
<para>${messageFormat(params['prop1'], 'Galadriel')}</para>
34+
<para><@m.simpleMacro param1='print text'/></para>
3335
</body>
3436

3537
</doc>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package test.testorg.fugerit.java.codesamplesfjdoc;
2+
3+
import freemarker.core.ParseException;
4+
import freemarker.template.Configuration;
5+
import freemarker.template.Template;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import java.io.File;
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.List;
15+
16+
/**
17+
* Freemarker template syntax verify example
18+
*
19+
* It will check all templates in folder :
20+
* 'src/main/resources/code-samples-fj-doc/template'
21+
* for syntax errors.
22+
*
23+
* Expected templates with syntac errors :
24+
* macro-fail.ftl
25+
* template-fail.ftl
26+
*
27+
*/
28+
@Slf4j
29+
class VerifyTemplateTest {
30+
31+
/*
32+
* This method will :
33+
* - creates a FreeMarker configuration pointing at the template folders
34+
* - iterates over templates
35+
* - return a list of templates with syntac error
36+
*/
37+
private Collection<String> verifyTemplateSyntaxErrors() throws IOException {
38+
List<String> errors = new ArrayList<>();
39+
File baseFolder = new File( "src/main/resources/code-samples-fj-doc/template/" );
40+
Configuration cfg = new Configuration();
41+
cfg.setDirectoryForTemplateLoading( baseFolder );
42+
this.verifyTemplateSyntaxErrorsIterate( baseFolder, errors, cfg, baseFolder );
43+
return errors;
44+
}
45+
46+
private void verifyTemplateSyntaxErrorsIterate(File baseFolder, List<String> errors, Configuration cfg, File currentFolder ) throws IOException {
47+
log.info( "base folder : {}, current folder: {}", baseFolder, currentFolder );
48+
for ( String template : currentFolder.list() ) {
49+
File templateFile = new File( currentFolder, template );
50+
if ( templateFile.isDirectory() ) {
51+
this.verifyTemplateSyntaxErrorsIterate( currentFolder, errors, cfg, templateFile );
52+
} else {
53+
String templatePath = templateFile.getCanonicalPath().substring( baseFolder.getCanonicalPath().length()+1 );
54+
try {
55+
Template t = cfg.getTemplate( templatePath );
56+
log.info( "Template syntax OK: {} - template : {}", templatePath, t.getName() );
57+
} catch (ParseException e) {
58+
log.error( String.format( "Template syntax KO: %s", templatePath ), e );
59+
errors.add( templatePath );
60+
}
61+
}
62+
}
63+
}
64+
65+
@Test
66+
void test() throws IOException {
67+
Collection<String> errors = verifyTemplateSyntaxErrors();
68+
log.info( "templates with errors: {}", errors );
69+
Assertions.assertEquals( 2, errors.size() );
70+
}
71+
72+
}

0 commit comments

Comments
 (0)