1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.io.StringWriter;
24
25 import junit.framework.TestCase;
26
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.exception.MethodInvocationException;
30 import org.apache.velocity.exception.ParseErrorException;
31 import org.apache.velocity.exception.ResourceNotFoundException;
32 import org.apache.velocity.util.introspection.IntrospectionCacheData;
33
34
35
36
37
38
39
40 public class IntrospectionCacheDataTestCase extends TestCase
41 {
42
43 private static class CacheHitCountingVelocityContext extends VelocityContext
44 {
45 public int cacheHit = 0;
46
47 public IntrospectionCacheData icacheGet(Object key)
48 {
49 final IntrospectionCacheData result = super.icacheGet(key);
50 if (result != null) {
51 ++cacheHit;
52 }
53 return result;
54 }
55
56 }
57
58 public void testCache() throws ParseErrorException, MethodInvocationException,
59 ResourceNotFoundException, IOException
60 {
61 CacheHitCountingVelocityContext context = new CacheHitCountingVelocityContext();
62 context.put("this", this);
63 StringWriter w = new StringWriter();
64 Velocity.evaluate(context, w, "test", "$this.exec('a')$this.exec('b')");
65 assertEquals("[a][b]", w.toString());
66 assertTrue(context.cacheHit > 0);
67 }
68
69
70
71
72
73
74
75
76 public String exec(String value)
77 {
78 return "[" + value + "]";
79 }
80
81 }