|
| 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