001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.el; 020 021import org.apache.commons.el.parser.ELParser; 022import org.apache.commons.el.parser.ParseException; 023 024import java.io.StringReader; 025import java.io.IOException; 026 027import javax.servlet.jsp.el.FunctionMapper; 028import javax.servlet.jsp.el.VariableResolver; 029import javax.servlet.jsp.el.ELException; 030 031import junit.framework.TestCase; 032 033public class ELTest extends TestCase { 034 035 private MockVariableResolver resolver; 036 private MockFunctionMapper mapper; 037 038 protected void setUp() { 039 this.resolver = new MockVariableResolver(); 040 this.mapper = new MockFunctionMapper(); 041 } 042 043 protected void tearDown() { 044 this.resolver = null; 045 this.mapper = null; 046 } 047 048 public void testMath() { 049 assertEquals("2", evaluate("${1 + 1}")); 050 assertEquals("5", evaluate("${10 - 5}")); 051 } 052 053 public void testLiteral() { 054 assertEquals("ac", evaluate("${'ac'}")); 055 assertEquals("\"", evaluate("${'\"'}")); 056 assertEquals("true", evaluate("${true}")); 057 } 058 059 public void testEL6() { 060 assertEquals("a''c", evaluate("${'a\\'\\'c'}")); 061 } 062 063 private String evaluate(String input) { 064 try { 065 StringReader rdr = new StringReader(input); 066 ELParser parser = new ELParser(rdr); 067 Object result = parser.ExpressionString(); 068 if(result instanceof String) { 069 return (String) result; 070 } else if(result instanceof Expression) { 071 Expression expr = (Expression) result; 072 result = expr.evaluate(this.resolver, this.mapper); 073 return result == null ? null : result.toString(); 074 } else if(result instanceof ExpressionString) { 075 Expression expr = (Expression) result; 076 result = expr.evaluate(this.resolver, this.mapper); 077 return result == null ? null : result.toString(); 078 } else { 079 throw new RuntimeException("Incorrect type returned; not String, Expression or ExpressionString"); 080 } 081 } catch(ParseException pe) { 082 throw new RuntimeException("ParseException thrown: " + pe.getMessage(), pe); 083 } catch(ELException ele) { 084 throw new RuntimeException("ELException thrown: " + ele.getMessage(), ele); 085 } 086 } 087 088}