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 junit.framework.Test;
23 import junit.framework.TestSuite;
24 import org.apache.velocity.app.Velocity;
25 import org.apache.velocity.test.misc.TestLogChute;
26 import org.apache.velocity.util.introspection.*;
27 import org.apache.velocity.test.BaseTestCase;
28 import org.apache.velocity.VelocityContext;
29
30 import java.io.StringWriter;
31
32
33
34
35 public class ChainedUberspectorsTestCase extends BaseTestCase {
36
37 public ChainedUberspectorsTestCase(String name)
38 throws Exception
39 {
40 super(name);
41 }
42
43 public static Test suite()
44 {
45 return new TestSuite(ChainedUberspectorsTestCase.class);
46 }
47
48 public void setUp()
49 throws Exception
50 {
51 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
52 Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.util.introspection.UberspectImpl");
53 Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$ChainedUberspector");
54 Velocity.addProperty(Velocity.UBERSPECT_CLASSNAME,"org.apache.velocity.test.util.introspection.ChainedUberspectorsTestCase$LinkedUberspector");
55 Velocity.init();
56 }
57
58 public void tearDown()
59 {
60 }
61
62 public void testChaining()
63 throws Exception
64 {
65 VelocityContext context = new VelocityContext();
66 context.put("foo",new Foo());
67 StringWriter writer = new StringWriter();
68
69 Velocity.evaluate(context,writer,"test","$foo.zeMethod()");
70 assertEquals(writer.toString(),"ok");
71
72 Velocity.evaluate(context,writer,"test","#set($foo.foo = 'someValue')");
73
74 writer = new StringWriter();
75 Velocity.evaluate(context,writer,"test","$foo.bar");
76 assertEquals(writer.toString(),"someValue");
77
78 writer = new StringWriter();
79 Velocity.evaluate(context,writer,"test","$foo.foo");
80 assertEquals(writer.toString(),"someValue");
81 }
82
83
84 public static class ChainedUberspector extends AbstractChainableUberspector
85 {
86 public VelPropertySet getPropertySet(Object obj, String identifier, Object arg, Info info) throws Exception
87 {
88 identifier = identifier.replaceAll("foo","bar");
89 return inner.getPropertySet(obj,identifier,arg,info);
90 }
91 }
92
93
94 public static class LinkedUberspector extends UberspectImpl
95 {
96 public VelPropertyGet getPropertyGet(Object obj, String identifier, Info info) throws Exception
97 {
98 identifier = identifier.replaceAll("foo","bar");
99 return super.getPropertyGet(obj,identifier,info);
100 }
101 }
102
103 public static class Foo
104 {
105 private String bar;
106
107 public String zeMethod() { return "ok"; }
108 public String getBar() { return bar; }
109 public void setBar(String s) { bar = s; }
110 }
111
112 }