01: package org.apache.velocity.test;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: private static class CacheHitCountingVelocityContext extends
43: VelocityContext {
44: public int cacheHit = 0;
45:
46: public IntrospectionCacheData icacheGet(Object key) {
47: final IntrospectionCacheData result = super .icacheGet(key);
48: if (result != null) {
49: ++cacheHit;
50: }
51: return result;
52: }
53:
54: }
55:
56: public void testCache() throws ParseErrorException,
57: MethodInvocationException, ResourceNotFoundException,
58: IOException {
59: CacheHitCountingVelocityContext context = new CacheHitCountingVelocityContext();
60: context.put("this", this );
61: StringWriter w = new StringWriter();
62: Velocity.evaluate(context, w, "test",
63: "$this.exec('a')$this.exec('b')");
64: assertEquals("[a][b]", w.toString());
65: assertTrue(context.cacheHit > 0);
66: }
67:
68: /**
69: * For use when acting as a context reference.
70: *
71: * @param value
72: * @return
73: */
74: public String exec(String value) {
75: return "[" + value + "]";
76: }
77:
78: }
|