Author: henrib Date: Tue May 1 13:26:36 2012 New Revision: 1332642 URL: http://svn.apache.org/viewvc?rev=1332642&view=rev Log: Cleaned ASTNumberLiteral conversion code; Added specific exception tests; Various Javadoc
Added: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java (with props) Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/ASTNumberLiteral.java commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java?rev=1332642&r1=1332641&r2=1332642&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Interpreter.java Tue May 1 13:26:36 2012 @@ -131,9 +131,9 @@ public class Interpreter extends ParserV /** * Creates an interpreter. - * @param engine the engine creating this interpreter + * @param engine the engine creating this interpreter * @param aContext the context to evaluate expression - * @param eFrame the interpreter evaluation frame + * @param eFrame the interpreter evaluation frame */ protected Interpreter(Engine engine, JexlContext aContext, Scope.Frame eFrame) { this.jexl = engine; @@ -190,9 +190,9 @@ public class Interpreter extends ParserV /** * Finds the node causing a NPE for diadic operators. - * @param xrt the RuntimeException - * @param node the parent node - * @param left the left argument + * @param xrt the RuntimeException + * @param node the parent node + * @param left the left argument * @param right the right argument * @return the left, right or parent node */ @@ -254,7 +254,7 @@ public class Interpreter extends ParserV * Resolves a namespace, eventually allocating an instance using context as constructor argument. <p>The lifetime of * such instances span the current expression or script evaluation.</p> * @param prefix the prefix name (may be null for global namespace) - * @param node the AST node + * @param node the AST node * @return the namespace instance */ protected Object resolveNamespace(String prefix, JexlNode node) { @@ -868,7 +868,7 @@ public class Interpreter extends ParserV * Check for emptyness of various types: Collection, Array, Map, String, and anything that has a boolean isEmpty() * method. * - * @param node the node holding the object + * @param node the node holding the object * @param object the object to check the rmptyness of. * @return the boolean */ @@ -913,7 +913,7 @@ public class Interpreter extends ParserV * Calculate the * <code>size</code> of various types: Collection, Array, Map, String, and anything that has a int size() method. * - * @param node the node that gave the value to size + * @param node the node that gave the value to size * @param object the object to get the size of. * @return the size of val */ @@ -1037,7 +1037,7 @@ public class Interpreter extends ParserV /** * Checks whether a reference child node holds a local variable reference. - * @param node the reference node + * @param node the reference node * @param which the child we are checking * @return true if child is local variable, false otherwise */ @@ -1091,7 +1091,7 @@ public class Interpreter extends ParserV isVariable &= object == null; } if (object == null && isVariable && variableName != null - && !isTernaryProtected(node) && !(context.has(variableName.toString()) || isLocalVariable(node, 0))) { + && !isTernaryProtected(node) && !(context.has(variableName.toString()) || isLocalVariable(node, 0))) { JexlException xjexl = new JexlException.Variable(node, variableName.toString()); // variable unknown in context and not a local return unknownVariable(xjexl); @@ -1272,8 +1272,8 @@ public class Interpreter extends ParserV * 2 - if this fails, narrow the arguments and try again * </p> * - * @param node the method node - * @param bean the bean this method should be invoked upon + * @param node the method node + * @param bean the bean this method should be invoked upon * @param functor the object carrying the method or function * @return the result of the method invocation */ @@ -1446,7 +1446,7 @@ public class Interpreter extends ParserV /** * Gets an attribute of an object. * - * @param object to retrieve value from + * @param object to retrieve value from * @param attribute the attribute of the object, e.g. an index (1, 0, 2) or key for a map * @return the attribute value */ @@ -1457,9 +1457,9 @@ public class Interpreter extends ParserV /** * Gets an attribute of an object. * - * @param object to retrieve value from + * @param object to retrieve value from * @param attribute the attribute of the object, e.g. an index (1, 0, 2) or key for a map - * @param node the node that evaluated as the object + * @param node the node that evaluated as the object * @return the attribute value */ protected Object getAttribute(Object object, Object attribute, JexlNode node) { @@ -1517,9 +1517,9 @@ public class Interpreter extends ParserV /** * Sets an attribute of an object. * - * @param object to set the value to + * @param object to set the value to * @param attribute the attribute of the object, e.g. an index (1, 0, 2) or key for a map - * @param value the value to assign to the object's attribute + * @param value the value to assign to the object's attribute */ public void setAttribute(Object object, Object attribute, Object value) { setAttribute(object, attribute, value, null); @@ -1528,10 +1528,10 @@ public class Interpreter extends ParserV /** * Sets an attribute of an object. * - * @param object to set the value to + * @param object to set the value to * @param attribute the attribute of the object, e.g. an index (1, 0, 2) or key for a map - * @param value the value to assign to the object's attribute - * @param node the node that evaluated as the object + * @param value the value to assign to the object's attribute + * @param node the node that evaluated as the object */ protected void setAttribute(Object object, Object attribute, Object value, JexlNode node) { if (isCancelled()) { Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/ASTNumberLiteral.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/ASTNumberLiteral.java?rev=1332642&r1=1332641&r2=1332642&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/ASTNumberLiteral.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/ASTNumberLiteral.java Tue May 1 13:26:36 2012 @@ -37,7 +37,7 @@ public class ASTNumberLiteral extends Je public Number getLiteral() { return literal; } - + /** {@inheritDoc} */ @Override protected boolean isConstant(boolean literal) { @@ -121,28 +121,27 @@ public class ASTNumberLiteral extends Je switch (s.charAt(last)) { case 'b': case 'B': { - result = new BigDecimal(s.substring(0, last)); rclass = BigDecimal.class; + result = new BigDecimal(s.substring(0, last)); + break; + } + case 'f': + case 'F': { + rclass = Float.class; + result = Float.valueOf(s.substring(0, last)); break; } case 'd': case 'D': - default : { rclass = Double.class; - result = Double.valueOf(s); + result = Double.valueOf(s.substring(0, last)); break; - } - case 'f': - case 'F': { - rclass = Float.class; + default: { + rclass = Double.class; try { - result = Float.valueOf(s); - } catch (NumberFormatException take2) { - try { - result = Double.valueOf(s); - } catch (NumberFormatException take3) { - result = new BigDecimal(s); - } + result = Double.valueOf(s); + } catch (NumberFormatException take3) { + result = new BigDecimal(s); } break; } Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java?rev=1332642&r1=1332641&r2=1332642&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java Tue May 1 13:26:36 2012 @@ -28,14 +28,16 @@ import java.util.Stack; */ public abstract class JexlParser extends StringParser { /** - * The map of named registers aka script parameters. Each parameter is associated to a register and is materialized - * as an offset in the registers array used during evaluation. + * The map of named registers aka script parameters. + * <p>Each parameter is associated to a register and is materialized + * as an offset in the registers array used during evaluation.</p> */ protected Scope frame = null; protected Stack<Scope> frames = new Stack<Scope>(); /** - * Sets the frame to use bythis parser. <p> This is used to allow parameters to be declared before parsing. </p> + * Sets the frame to use by this parser. + * <p> This is used to allow parameters to be declared before parsing. </p> * @param theFrame the register map */ public void setFrame(Scope theFrame) { @@ -43,7 +45,8 @@ public abstract class JexlParser extends } /** - * Gets the frame used by this parser. <p> Since local variables create new named registers, it is important to + * Gets the frame used by this parser. + * <p> Since local variables create new symbols, it is important to * regain access after parsing to known which / how-many registers are needed. </p> * @return the named register map */ @@ -73,7 +76,7 @@ public abstract class JexlParser extends } /** - * Checks whether an identifier is a local variable or argument, ie stored in a register. + * Checks whether an identifier is a local variable or argument, ie a symbol, stored in a register. * @param identifier the identifier * @param image the identifier image * @return the image @@ -89,7 +92,8 @@ public abstract class JexlParser extends } /** - * Declares a local variable. <p> This method creates an new entry in the named register map. </p> + * Declares a local variable. + * <p> This method creates an new entry in the symbol map. </p> * @param identifier the identifier used to declare * @param image the variable name */ @@ -103,7 +107,8 @@ public abstract class JexlParser extends } /** - * Declares a local parameter. <p> This method creates an new entry in the named register map. </p> + * Declares a local parameter. + * <p> This method creates an new entry in the symbol map. </p> * @param identifier the identifier used to declare * @param image the variable name */ Added: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java?rev=1332642&view=auto ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java (added) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java Tue May 1 13:26:36 2012 @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jexl3; + +import org.apache.commons.jexl3.internal.Engine; + +/** + * Checks various exception handling cases. + */ +public class ExceptionTest extends JexlTestCase { + /** create a named test */ + public ExceptionTest(String name) { + super(name); + } + + public static class ThrowNPE { + public String method() { + throw new NullPointerException("ThrowNPE"); + } + } + + public void testWrappedEx() throws Exception { + JexlEngine jexl = new Engine(); + JexlExpression e = jexl.createExpression("method()"); + JexlContext jc = new ObjectContext<ThrowNPE>(jexl, new ThrowNPE()); + try { + e.evaluate(jc); + fail("Should have thrown NPE"); + } catch (JexlException xany) { + Throwable xth = xany.getCause(); + assertEquals(NullPointerException.class, xth.getClass()); + } + } + + // Unknown vars and properties versus null operands + public void testEx() throws Exception { + JexlEngine jexl = createEngine(false); + JexlExpression e = jexl.createExpression("c.e * 6"); + JexlEvalContext ctxt = new JexlEvalContext(); + // ensure errors will throw + ctxt.setSilent(false); + // make unknown vars throw + ctxt.setStrict(true); + // empty cotext + try { + /* Object o = */ e.evaluate(ctxt); + fail("c.e not declared as variable should throw"); + } catch (JexlException.Variable xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("c.e") > 0); + } + + // disallow null operands + ctxt.setStrictArithmetic(true); + ctxt.set("c.e", null); + try { + /* Object o = */ e.evaluate(ctxt); + fail("c.e as null operand should throw"); + } catch (JexlException xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("null operand") > 0); + } + + // allow null operands + ctxt.setStrictArithmetic(false); + try { + /* Object o = */ e.evaluate(ctxt); + + } catch (JexlException xjexl) { + fail("c.e in expr should not throw"); + } + + // ensure c.e is not a defined property + ctxt.set("c", "{ 'a' : 3, 'b' : 5}"); + ctxt.set("e", Integer.valueOf(2)); + try { + /* Object o = */ e.evaluate(ctxt); + fail("c.e not accessible as property should throw"); + } catch (JexlException.Property xjexl) { + String msg = xjexl.getMessage(); + assertTrue(msg.indexOf("e") > 0); + } + } + +} Propchange: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ExceptionTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java?rev=1332642&r1=1332641&r2=1332642&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java Tue May 1 13:26:36 2012 @@ -738,25 +738,6 @@ public class IssuesTest extends JexlTest assertEquals("OK", e.evaluate(jc)); } - public static class ThrowNPE { - public String method() { - throw new NullPointerException("ThrowNPE"); - } - } - - public void test126() throws Exception { - JexlEngine jexl = new Engine(); - JexlExpression e = jexl.createExpression("method()"); - JexlContext jc = new ObjectContext<ThrowNPE>(jexl, new ThrowNPE()); - try { - e.evaluate(jc); - fail("Should have thrown NPE"); - } catch (JexlException xany) { - Throwable xth = xany.getCause(); - assertEquals(NullPointerException.class, xth.getClass()); - } - } - public void test130a() throws Exception { String myName = "Test.Name"; Object myValue = "Test.Value"; @@ -786,7 +767,7 @@ public class IssuesTest extends JexlTest assertEquals(myValue, myObjectWithTernaryConditional); } - public void test131() throws Exception { + public void testBigdOp() throws Exception { BigDecimal sevendot475 = new BigDecimal("7.475"); BigDecimal SO = new BigDecimal("325"); JexlContext jc = new MapContext();