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 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   * Tests uberspectors chaining
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      // replaces getFoo by getBar
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     // replaces setFoo by setBar
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 }