/*
 * Copyright 2002-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;

/**
 * Test cases for the if statement.
 * 
 * @author Dion Gillard
 */
public class IfTest extends TestCase {

    public IfTest(String testName) {
        super(testName);
    }

    /**
     * Make sure if true executes the true statement
     * 
     * @throws Exception on any error
     */
    public void testSimpleIfTrue() throws Exception {
        Expression e = ExpressionFactory.createExpression("if (true) 1");
        JexlContext jc = JexlHelper.createContext();

        Object o = e.evaluate(jc);
        assertEquals("Result is not 1", Integer.valueOf(1), o);
    }

    /**
     * Make sure if false doesn't execute the true statement
     * 
     * @throws Exception on any error
     */
    public void testSimpleIfFalse() throws Exception {
        Expression e = ExpressionFactory.createExpression("if (false) 1");
        JexlContext jc = JexlHelper.createContext();

        Object o = e.evaluate(jc);
        assertNull("Return value is not empty", o);
    }

    /**
     * Make sure if false executes the false statement
     * 
     * @throws Exception on any error
     */
    public void testSimpleElse() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if (false) 1; else 2;");
        JexlContext jc = JexlHelper.createContext();

        Object o = e.evaluate(jc);
        assertEquals("Result is not 2", Integer.valueOf(2), o);
    }

    /**
     * Test the if statement handles blocks correctly
     * 
     * @throws Exception on any error
     */
    public void testBlockIfTrue() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if (true) { 'hello'; }");
        JexlContext jc = JexlHelper.createContext();

        Object o = e.evaluate(jc);
        assertEquals("Result is wrong", "hello", o);
    }

    /**
     * Test the if statement handles blocks in the else statement correctly
     * 
     * @throws Exception on any error
     */
    public void testBlockElse() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if (false) {1;} else {2;}");
        JexlContext jc = JexlHelper.createContext();

        Object o = e.evaluate(jc);
        assertEquals("Result is wrong", Integer.valueOf(2), o);
    }

    /**
     * Test the if statement evaluates expressions correctly
     * 
     * @throws Exception on any error
     */
    public void testIfWithSimpleExpression() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if (x == 1) true;");
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("x", Integer.valueOf(1));

        Object o = e.evaluate(jc);
        assertEquals("Result is not true", Boolean.TRUE, o);
    }

    /**
     * Test the if statement evaluates arithmetic expressions correctly
     * 
     * @throws Exception on any error
     */
    public void testIfWithArithmeticExpression() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if ((x * 2) + 1 == 5) true;");
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("x", Integer.valueOf(2));

        Object o = e.evaluate(jc);
        assertEquals("Result is not true", Boolean.TRUE, o);
    }

    /**
     * Test the if statement evaluates decimal arithmetic expressions correctly
     * 
     * @throws Exception on any error
     */
    public void testIfWithDecimalArithmeticExpression() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if ((x * 2) == 5) true;");
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("x", Float.valueOf(2.5f));

        Object o = e.evaluate(jc);
        assertEquals("Result is not true", Boolean.TRUE, o);
    }

    /**
     * Test the if statement works with assignment
     * 
     * @throws Exception on any error
     */
    public void testIfWithAssignment() throws Exception {
        Expression e = ExpressionFactory
                .createExpression("if ((x * 2) == 5) {y = 1;} else {y = 2;}");
        JexlContext jc = JexlHelper.createContext();
        jc.getVars().put("x", Float.valueOf(2.5f));

        e.evaluate(jc);
        Object result = jc.getVars().get("y");
        assertEquals("y has the wrong value", Integer.valueOf(1), result);
    }
}

@@ -1238,24 +1238,28 @@
  not 1%22,
+ new
  Integer
 .valueOf
@@ -1246,32 +1246,24 @@
  new Integer
-.valueOf
 (1), o);%0A   
@@ -2065,24 +2065,28 @@
  not 2%22,
+ new
  Integer
 .valueOf
@@ -2073,32 +2073,24 @@
  new Integer
-.valueOf
 (2), o);%0A   
@@ -2935,24 +2935,28 @@
  wrong%22,
+ new
  Integer
 .valueOf
@@ -2947,24 +2947,16 @@
  Integer
-.valueOf
 (2), o);
@@ -3325,32 +3325,36 @@
 s().put(%22x%22,
+ new
  Integer
 .valueOf(1))
@@ -3341,24 +3341,16 @@
  Integer
-.valueOf
 (1));%0A%0A 
@@ -3842,24 +3842,28 @@
 put(%22x%22,
+ new
  Integer
 .valueOf
@@ -3854,24 +3854,16 @@
  Integer
-.valueOf
 (2));%0A%0A 
@@ -4362,38 +4362,34 @@
 s().put(%22x%22,
+ new
  Float
-.valueOf
 (2.5f));%0A%0A  
@@ -4866,22 +4866,18 @@
 %22x%22,
+ new
  Float
-.valueOf
 (2.5
@@ -4998,24 +4998,28 @@
  value%22,
+ new
  Integer
 .valueOf
@@ -5014,16 +5014,8 @@
 eger
-.valueOf
 (1),

Remove jdk15 valueOf methods