aboutsummaryrefslogtreecommitdiff
path: root/velocity-custom-parser-example/src/test/java/org/apache/velocity/runtime/parser/CustomParserTestCase.java
blob: 9a27ff89d71423c0ccea75d7d360de9e5460b6dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package org.apache.velocity.runtime.parser;

import org.apache.commons.io.IOUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;

public class CustomParserTestCase
{
    VelocityEngine engine;

    static String TEMPLATES_DIR = System.getProperty("test.templates.dir");
    static String RESULTS_DIR = System.getProperty("test.results.dir");
    static String REFERENCE_DIR = System.getProperty("test.reference.dir");

    @Before
    public void setUp()
    {
        engine = new VelocityEngine();
        engine.setProperty("resource.loaders", "file");
        engine.setProperty("resource.loader.file.path", TEMPLATES_DIR);
        engine.setProperty("parser.class", "org.apache.velocity.runtime.parser.custom.CustomParser");
        engine.init();
    }

    @Test
    public void testMarkdownTemplate() throws Exception
    {
        VelocityContext ctx = new VelocityContext();
        ctx.put("some", "value");
        Template tmpl = engine.getTemplate("test.md", "UTF-8");

        String resultFile = RESULTS_DIR + File.separator + "test.md";
        String referenceFile = REFERENCE_DIR + File.separator + "test.md";

        new File(resultFile).getParentFile().mkdirs();

        FileWriter writer = new FileWriter(resultFile);
        tmpl.merge(ctx, writer);
        writer.flush();
        writer.close();

        String result = IOUtils.toString(new FileInputStream(resultFile), StandardCharsets.UTF_8);
        String reference = IOUtils.toString(new FileInputStream(referenceFile), StandardCharsets.UTF_8);
        assertEquals(reference, result);
    }
}