1 package org.apache.velocity.test.util.introspection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringWriter;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.runtime.RuntimeConstants;
30 import org.apache.velocity.test.BaseTestCase;
31 import org.apache.velocity.test.misc.TestLogChute;
32 import org.apache.velocity.util.introspection.AbstractChainableUberspector;
33 import org.apache.velocity.util.introspection.Info;
34 import org.apache.velocity.util.introspection.UberspectImpl;
35 import org.apache.velocity.util.introspection.VelPropertyGet;
36 import org.apache.velocity.util.introspection.VelPropertySet;
37
38
39
40
41 public class ChainedUberspectorsTestCase extends BaseTestCase
42 {
43
44 VelocityEngine engine;
45
46 public ChainedUberspectorsTestCase(String name)
47 throws Exception
48 {
49 super(name);
50 }
51
52 public static Test suite()
53 {
54 return new TestSuite(ChainedUberspectorsTestCase.class);
55 }
56
57 public void setUp()
58 throws Exception
59 {
60 engine = new VelocityEngine();
61
62 engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
63 engine.addProperty(RuntimeConstants.UBERSPECT_CLASSNAME,"org.apache.velocity.util.introspection.UberspectImpl");
64 engine.addProperty(RuntimeConstants.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$ChainedUberspector");
65 engine.addProperty(RuntimeConstants.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$LinkedUberspector");
66 engine.init();
67 }
68
69 public void tearDown()
70 {
71 }
72
73 public void testChaining()
74 throws Exception
75 {
76 VelocityContext context = new VelocityContext();
77 context.put("foo",new Foo());
78 StringWriter writer = new StringWriter();
79
80 engine.evaluate(context,writer,"test","$foo.zeMethod()");
81 assertEquals(writer.toString(),"ok");
82
83 engine.evaluate(context,writer,"test","#set($foo.foo = 'someValue')");
84
85 writer = new StringWriter();
86 engine.evaluate(context,writer,"test","$foo.bar");
87 assertEquals(writer.toString(),"someValue");
88
89 writer = new StringWriter();
90 engine.evaluate(context,writer,"test","$foo.foo");
91 assertEquals(writer.toString(),"someValue");
92 }
93
94
95 public static class ChainedUberspector extends AbstractChainableUberspector
96 {
97 public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info info)
98 throws Exception
99 {
100 identifier = identifier.replaceAll("foo","bar");
101 return inner.getPropertySet(obj,identifier,arg,info);
102 }
103 }
104
105
106 public static class LinkedUberspector extends UberspectImpl
107 {
108 public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info)
109 throws Exception
110 {
111 identifier = identifier.replaceAll("foo","bar");
112 return super.getPropertyGet(obj,identifier,info);
113 }
114 }
115
116 public static class Foo
117 {
118 private String bar;
119
120 public String zeMethod() { return "ok"; }
121 public String getBar() { return bar; }
122 public void setBar(String s) { bar = s; }
123 }
124
125 }