/*
 * 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);
        assertNotNull("Return value is empty", o);
        assertTrue("Result is not an integer", o instanceof Integer);
        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);
        assertNotNull("Return value is empty", o);
        assertTrue("Result is not an integer", o instanceof Integer);
        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);
        assertNull("Return value is not empty", 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);
        assertNull("Return value is not empty", o);
    }
}

@@ -920,32 +920,40 @@
  true statement%0A
+     * %0A
      * @throws E
@@ -1025,36 +1025,32 @@
 throws Exception
-%0A   
  %7B%0A        Expre
@@ -1452,32 +1452,40 @@
  true statement%0A
+     * %0A
      * @throws E
@@ -1558,36 +1558,32 @@
 throws Exception
-%0A   
  %7B%0A        Expre
@@ -1851,24 +1851,32 @@
 e statement%0A
+     * %0A
      * @thro
@@ -1950,36 +1950,32 @@
 throws Exception
-%0A   
  %7B%0A        Expre
@@ -1993,32 +1993,49 @@
 xpressionFactory
+%0A                
 .createExpressio
@@ -2397,32 +2397,40 @@
 locks correctly%0A
+     * %0A
      * @throws E
@@ -2501,36 +2501,32 @@
 throws Exception
-%0A   
  %7B%0A        Expre
@@ -2544,32 +2544,49 @@
 xpressionFactory
+%0A                
 .createExpressio
@@ -2842,24 +2842,32 @@
 t correctly%0A
+     * %0A
      * @thro
@@ -2948,20 +2948,16 @@
 xception
-%0A   
  %7B%0A     
@@ -2983,32 +2983,49 @@
 xpressionFactory
+%0A                
 .createExpressio
@@ -3202,11 +3202,1260 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Test the if statement evaluates expressions correctly%0A     * %0A     * @throws Exception on any error%0A     */%0A    public void testIfWithSimpleExpression() throws Exception %7B%0A        Expression e = ExpressionFactory%0A                .createExpression(%22if (x == 1) true;%22);%0A        JexlContext jc = JexlHelper.createContext();%0A        jc.getVars().put(%22x%22, Integer.valueOf(1));%0A%0A        Object o = e.evaluate(jc);%0A        assertNotNull(%22Return value is empty%22, o);%0A        assertTrue(%22Result is not a boolean%22, o instanceof Boolean);%0A        assertEquals(%22Result is not true%22, Boolean.TRUE, o);%0A    %7D%0A%0A    /**%0A     * Test the if statement evaluates arithmetic expressions correctly%0A     * %0A     * @throws Exception on any error%0A     */%0A    public void testIfWithArithmeticExpression() throws Exception %7B%0A        Expression e = ExpressionFactory%0A                .createExpression(%22if ((x * 2) + 1 == 5) true;%22);%0A        JexlContext jc = JexlHelper.createContext();%0A        jc.getVars().put(%22x%22, Integer.valueOf(2));%0A%0A        Object o = e.evaluate(jc);%0A        assertNotNull(%22Return value is empty%22, o);%0A        assertTrue(%22Result is not a boolean%22, o instanceof Boolean);%0A        assertEquals(%22Result is not true%22, Boolean.TRUE, o);%0A    %7D
 %0A%7D%0A

Add tests with more complex expressions.