1   package org.apache.velocity.test.util.introspection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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   * Tests uberspectors chaining
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      // replaces getFoo by getBar
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      // replaces setFoo by setBar
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 }