1 package org.apache.velocity.test;
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.IOException;
23 import java.io.StringWriter;
24
25 import junit.framework.TestCase;
26
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.exception.MethodInvocationException;
30 import org.apache.velocity.exception.ParseErrorException;
31 import org.apache.velocity.exception.ResourceNotFoundException;
32 import org.apache.velocity.util.introspection.IntrospectionCacheData;
33
34 /**
35 * Checks that arrays are cached correctly in the Introspector.
36 *
37 * @author <a href="Alexey Pachenko">alex+news@olmisoft.com</a>
38 * @version $Id: IntrospectionCacheDataTestCase.java 463298 2006-10-12 16:10:32Z henning $
39 */
40 public class IntrospectionCacheDataTestCase extends TestCase
41 {
42
43 private static class CacheHitCountingVelocityContext extends VelocityContext
44 {
45 public int cacheHit = 0;
46
47 public IntrospectionCacheData icacheGet(Object key)
48 {
49 final IntrospectionCacheData result = super.icacheGet(key);
50 if (result != null) {
51 ++cacheHit;
52 }
53 return result;
54 }
55
56 }
57
58 public void testCache() throws ParseErrorException, MethodInvocationException,
59 ResourceNotFoundException, IOException
60 {
61 CacheHitCountingVelocityContext context = new CacheHitCountingVelocityContext();
62 context.put("this", this);
63 StringWriter w = new StringWriter();
64 Velocity.evaluate(context, w, "test", "$this.exec('a')$this.exec('b')");
65 assertEquals("[a][b]", w.toString());
66 assertTrue(context.cacheHit > 0);
67 }
68
69
70 /**
71 * For use when acting as a context reference.
72 *
73 * @param value
74 * @return
75 */
76 public String exec(String value)
77 {
78 return "[" + value + "]";
79 }
80
81 }