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 junit.framework.TestCase;
23
24 import org.apache.velocity.VelocityContext;
25 import org.apache.velocity.context.EvaluateContext;
26 import org.apache.velocity.context.InternalContextAdapterImpl;
27 import org.apache.velocity.runtime.RuntimeConstants;
28 import org.apache.velocity.runtime.RuntimeInstance;
29 import org.apache.velocity.test.misc.TestContext;
30
31
32
33
34
35
36
37 public class EvaluateContextTestCase extends TestCase
38 {
39 public void testLocalscopePutDoesntLeakButGetDoes()
40 throws Exception
41 {
42 RuntimeInstance instance;
43
44 instance = new RuntimeInstance();
45 instance.setProperty(RuntimeConstants.VM_CONTEXT_LOCALSCOPE, Boolean.TRUE);
46 instance.init();
47
48 VelocityContext base = new VelocityContext();
49 base.put("outsideVar", "value1");
50
51 EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
52 evc.put("newLocalVar", "value2");
53
54
55 assertNull(base.get("newLocalVar"));
56 assertEquals("value2", evc.get("newLocalVar"));
57
58
59 assertEquals("value1", evc.get("outsideVar"));
60
61
62 evc.put("outsideVar", "value3");
63 assertEquals("value3", evc.get("outsideVar"));
64 assertEquals("value1", base.get("outsideVar"));
65
66 assertEquals(2, evc.getKeys().length);
67 }
68
69
70
71
72
73 public void testSetLocalContext()
74 throws Exception
75 {
76 RuntimeInstance instance = new RuntimeInstance();
77 instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, TestContext.class.getName());
78 instance.init();
79
80 VelocityContext base = new VelocityContext();
81 base.put("outsideVar", "value1");
82 EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
83
84
85 assertEquals(1,evc.getKeys().length);
86
87
88 evc.put("test","result");
89 assertEquals(2,evc.getKeys().length);
90
91
92 evc.put("TEST","result");
93 assertEquals(2,evc.getKeys().length);
94
95 assertEquals("result",evc.get("test"));
96 assertEquals("result",evc.get("TEst"));
97
98 assertNull(evc.get("OUTSIDEVAR"));
99 }
100
101 public void testSetLocalContextWithErrors()
102 throws Exception
103 {
104 VelocityContext base = new VelocityContext();
105
106 try
107 {
108
109 RuntimeInstance instance = new RuntimeInstance();
110 instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, "org.apache");
111 instance.init();
112 EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
113 fail ("Expected an exception");
114 }
115 catch (Exception e) {}
116
117 try
118 {
119
120 RuntimeInstance instance = new RuntimeInstance();
121 instance.setProperty(RuntimeConstants.EVALUATE_CONTEXT_CLASS, org.apache.velocity.test.EvaluateContextTestCase.class.getName());
122 instance.init();
123 EvaluateContext evc = new EvaluateContext(new InternalContextAdapterImpl(base), instance);
124 fail ("Expected an exception");
125 }
126 catch (Exception e) {}
127 }
128 }