aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/test/RenderVelocityTemplateTest.java
blob: 7428839346ebc6e66727404db129983bd7f12065 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.apache.velocity.test;

import junit.framework.Assert;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class RenderVelocityTemplateTest
{

    static class RenderVelocityTemplate
    {
        static
        {
            try
            {
                Properties p = new Properties();
                p.put("velocimacro.permissions.allow.inline.local.scope", "true");
                Velocity.init(p);
            }
            catch (Exception e)
            {
                throw new AssertionError("Failed to init Velocity");
            }
        }

        private final VelocityContext velocityContext = new VelocityContext();

        private final String template;

        public RenderVelocityTemplate(String template)
        {
            this.template = template;
        }

        public String getContent()
            throws Exception
        {
            StringWriter stringWriter = new StringWriter();
            Velocity.evaluate(velocityContext, stringWriter, "", template);
            return stringWriter.toString();
        }
    }


    private static final String templateString = "" + //
        "#macro (showhelloworld $foo)\n" + //
        "Hello, World\n" + //
        "#end\n" + //
        "\n" + //
        "<html>\n" + //
        "<head>\n" + //
        "<title>page title</title>\n" + //
        "</head>\n" + //
        "<body>\n" + //
        "<p>This is a test</p>\n" + //
        "<p>#showhelloworld ($foo)</p>\n" + //
        "</body>\n" + //
        "</html>";

    public void testMultipleEvals()
        throws Exception
    {
        RenderVelocityTemplate template = new RenderVelocityTemplate(templateString);

        String result = null;
        for (int i = 0; i < 1000; ++i)
        {
            result = template.getContent();

            // Verify that the original macro invocation has been replaced with its result.
            int index = result.indexOf("#showhelloworld");
            if (index != -1)
            {
                throw new AssertionError("Failed to substitute macro:\n" + result);
            }

            // Verify that the macro did indeed expand.
            int indexHW = result.indexOf("<p>Hello, World");
            Assert.assertTrue(indexHW >= 0);

            // Assert.assertEquals("", result); // enable to show what we really get
        }
    }

    /** Helper class for testMultiThreadMultipleEvals(). */
    static class ExceptionHandler
        implements Thread.UncaughtExceptionHandler
    {
        List<Throwable> errors = new ArrayList<>();

        @Override
        public void uncaughtException(Thread t, Throwable e)
        {
            errors.add(e);
        }
    }

    /** Helper class for testMultiThreadMultipleEvals(). */
    class RunMultipleEvals
        extends Thread
    {
        @Override
        public void run()
        {
            try
                {
                    testMultipleEvals();
                }
            catch (Exception e)
                {
                    throw new RuntimeException(e);
                }
        }
    }

    /**
     * Spawn multiple threads that concurrently call testMultipleEvals.
     */
    public void testMultiThreadMultipleEvals()
        throws Throwable
    {
        int nthreads = 4;
        ExceptionHandler eh = new ExceptionHandler();

        List<Thread> threads = new ArrayList<>(nthreads);
        for (int i = 0; i < nthreads; ++i)
        {
            Thread t = new RunMultipleEvals();
            t.setUncaughtExceptionHandler(eh);
            threads.add(t);
        }

        for (Thread t : threads)
        {
            t.start();
        }

        for (Thread t : threads)
        {
            t.join();
        }

        if (eh.errors.size() > 0)
        {
            // Rethrow the first failing exception.
            System.out.println("Failed " + eh.errors.size() + " out of " + nthreads + " template evaluations");
            throw eh.errors.get(0);
        }
    }
}