001 // Copyright 2004, 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.services.impl; 016 017 import static org.easymock.EasyMock.expect; 018 import ognl.Node; 019 import ognl.OgnlContext; 020 021 import org.apache.hivemind.ApplicationRuntimeException; 022 import org.apache.tapestry.BaseComponentTestCase; 023 import org.apache.tapestry.services.ExpressionEvaluator; 024 import org.testng.annotations.Test; 025 026 /** 027 * Tests for {@link org.apache.tapestry.services.impl.ExpressionCacheImpl}. 028 * 029 * @author Howard M. Lewis Ship 030 * @since 4.0 031 */ 032 @Test(enabled = false) 033 public class TestExpressionCache extends BaseComponentTestCase 034 { 035 public void test_Valid_Expression() 036 { 037 ExpressionCacheImpl ec = new ExpressionCacheImpl(); 038 039 Object compiled = ec.getCompiledExpression("foo ? bar : baz"); 040 041 assertNotNull(compiled); 042 } 043 044 public void test_Caching() 045 { 046 ExpressionCacheImpl ec = new ExpressionCacheImpl(); 047 048 Object c1 = ec.getCompiledExpression("foo + bar"); 049 Object c2 = ec.getCompiledExpression("zip.zap.zoom"); 050 Object c3 = ec.getCompiledExpression("foo + bar"); 051 052 assertSame(c1, c3); 053 assertNotSame(c1, c2); 054 } 055 056 public void test_Compiled_Caching() 057 { 058 ExpressionEvaluator evaluator = newMock(ExpressionEvaluator.class); 059 ExpressionCacheImpl ec = new ExpressionCacheImpl(); 060 ec.setEvaluator(evaluator); 061 062 BasicObject target = new BasicObject(); 063 OgnlContext context = new OgnlContext(); 064 065 expect(evaluator.createContext(target)).andReturn(context); 066 067 replay(); 068 069 Node e1 = (Node)ec.getCompiledExpression(target, "value"); 070 071 assertNotNull(e1.getAccessor()); 072 assertEquals(e1.getAccessor().get(context, target), "foo"); 073 074 Node e2 = (Node)ec.getCompiledExpression(target, "value"); 075 076 assertSame(e1, e2); 077 078 verify(); 079 } 080 081 public void test_Invalid_Expression() 082 { 083 ExpressionCacheImpl ec = new ExpressionCacheImpl(); 084 085 try 086 { 087 ec.getCompiledExpression("foo and bar and"); 088 unreachable(); 089 } 090 catch (ApplicationRuntimeException ex) 091 { 092 assertExceptionSubstring(ex, "Unable to parse OGNL expression 'foo and bar and'"); 093 } 094 } 095 096 // fails only when running from command line, must be threading issue 097 @Test(enabled = false) 098 public void test_Clear_Cache() 099 { 100 ExpressionEvaluator evaluator = newMock(ExpressionEvaluator.class); 101 ExpressionCacheImpl ec = new ExpressionCacheImpl(); 102 ec.setEvaluator(evaluator); 103 104 BasicObject target = new BasicObject(); 105 OgnlContext context = new OgnlContext(); 106 107 expect(evaluator.createContext(target)).andReturn(context).anyTimes(); 108 109 replay(); 110 111 Node e1 = (Node)ec.getCompiledExpression(target, "value"); 112 113 ec.resetEventDidOccur(); 114 115 Node e2 = (Node)ec.getCompiledExpression(target, "value"); 116 117 assertNotSame(e1, e2); 118 119 verify(); 120 } 121 122 }