Author: rahul Date: Sun Aug 6 11:36:29 2006 New Revision: 429169 URL: http://svn.apache.org/viewvc?rev=429169&view=rev Log: ExpressionFactory should rethrow javacc thrown Error as an Exception.
Reported by: Kohsuke Kawaguchi (kk AT kohsuke DOT org) Also added a couple of tests to ensure that the ParseExceptions are thrown. JEXL-17 Added: jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ParseFailuresTest.java Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ExpressionFactory.java Modified: jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ExpressionFactory.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ExpressionFactory.java?rev=429169&r1=429168&r2=429169&view=diff ============================================================================== --- jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ExpressionFactory.java (original) +++ jakarta/commons/proper/jexl/trunk/src/java/org/apache/commons/jexl/ExpressionFactory.java Sun Aug 6 11:36:29 2006 @@ -23,8 +23,10 @@ import org.apache.commons.jexl.parser.ASTReferenceExpression; import org.apache.commons.jexl.parser.ASTStatementExpression; import org.apache.commons.jexl.parser.ASTWhileStatement; +import org.apache.commons.jexl.parser.ParseException; import org.apache.commons.jexl.parser.Parser; import org.apache.commons.jexl.parser.SimpleNode; +import org.apache.commons.jexl.parser.TokenMgrError; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -117,7 +119,11 @@ SimpleNode tree; synchronized (parser) { log.debug("Parsing expression: " + expr); - tree = parser.parse(new StringReader(expr)); + try { + tree = parser.parse(new StringReader(expr)); + } catch (TokenMgrError tme) { + throw new ParseException(tme.getMessage()); + } } if (tree.jjtGetNumChildren() > 1 && log.isWarnEnabled()) { Added: jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ParseFailuresTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ParseFailuresTest.java?rev=429169&view=auto ============================================================================== --- jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ParseFailuresTest.java (added) +++ jakarta/commons/proper/jexl/trunk/src/test/org/apache/commons/jexl/ParseFailuresTest.java Sun Aug 6 11:36:29 2006 @@ -0,0 +1,63 @@ +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.jexl; + +import junit.framework.TestCase; + +import org.apache.commons.jexl.parser.ParseException; + +/** + * Tests for malformed expressions. + * ([EMAIL PROTECTED] ExpressionFactory} should throw [EMAIL PROTECTED] ParseException}s). + * + * @since 1.1 + */ +public class ParseFailuresTest extends TestCase { + + /** + * Create the test. + * + * @param testName name of the test + */ + public ParseFailuresTest(String testName) { + super(testName); + } + + public void testMalformedExpression1() throws Exception { + // this will throw a ParseException + String badExpression = "eq"; + try { + Expression e = ExpressionFactory.createExpression(badExpression); + fail("Parsing \"" + badExpression + + "\" should result in a ParseException"); + } catch (ParseException pe) { + // expected + } + } + + public void testMalformedExpression2() throws Exception { + // this will throw a TokenMgrErr, which we rethrow as a ParseException + String badExpression = "?"; + try { + Expression e = ExpressionFactory.createExpression(badExpression); + fail("Parsing \"" + badExpression + + "\" should result in a ParseException"); + } catch (ParseException pe) { + // expected + } + } + +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]