view src/org/RhinoTests/JavaScriptsTest.java @ 351:48afda73ee6e draft

Two new tests added into JavaScriptsTest.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 09 Apr 2014 13:30:07 +0200
parents 19f1168ca1c0
children
line wrap: on
line source

/*
  Rhino test framework

   Copyright (C) 2011, 2012, 2013, 2014  Red Hat

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

IcedTea is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
*/

package org.RhinoTests;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;



/**
 * Basic ScriptEngine test suite.
 *
 * @author Pavel Tisnovsky
 */
public class JavaScriptsTest extends BaseRhinoTest {
	/**
	 * Encoding used in JavaScript files.
	 */
    private static final String SCRIPT_DEFAULT_ENCODING = "UTF-8";

    ScriptEngineManager engineManager;
    ScriptEngine scriptEngine;

    @Override
    protected void setUp(String[] args) {
        this.engineManager = new ScriptEngineManager();
        this.scriptEngine = this.engineManager.getEngineByName("JavaScript");
    }

    @Override
    protected void tearDown() {
        // this block could be empty
        return;
    }

    /**
     * Test if it is possible to run script from a string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptStoredInString1() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.HELLO_WORLD_1);
    }

    /**
     * Test if it is possible to run script from a string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptStoredInString2() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.HELLO_WORLD_2);
    }

    /**
     * Test if it is possible to run script from a string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptStoredInString3() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.HELLO_WORLD_3);
    }

    /**
     * Test if it is possible to run script from a string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptStoredInString4() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.HELLO_WORLD_4);
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * function.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingUnknownFunctionStoredInString() throws Exception {
        try {
            this.scriptEngine.eval(JavaScriptSnippets.UNKNOWN_FUNCTION);
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * function.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingUnknownFunction2StoredInString() throws Exception {
        try {
            this.scriptEngine.eval(JavaScriptSnippets.UNKNOWN_FUNCTION_2);
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with bad
     * parameters.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError1StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("println(bflmpsvz)");
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError2StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("xyzzy");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError3StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("do");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError4StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("while");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError5StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("if");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError6StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("class");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when string contains script with unknown
     * command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError7StoredInString() throws Exception {
        try {
            this.scriptEngine.eval("function");
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test empty script.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunEmptyScriptStoredInString() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_1);
    }

    /**
     * Test script consisting only of spaces.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString1() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_2);
    }

    /**
     * Test script consisting only of tab.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString2() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_3);
    }

    /**
     * Test script consisting only of tab and spaces.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString3() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_4);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString4() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_5);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString5() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_6);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString6() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_7);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString7() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_8);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString8() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_9);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString9() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_10);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString10() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_11);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString11() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_12);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString12() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_13);
    }

    /**
     * Test script consisting only of tabs and space.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptStoredInString13() throws ScriptException {
        this.scriptEngine.eval(JavaScriptSnippets.EMPTY_SCRIPT_14);
    }

    /**
     * Test proper handling of null-string.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunNullScriptStoredInString() throws Exception {
        try {
            this.scriptEngine.eval((String) null);
        }
        catch (NullPointerException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("NPE not thrown as expected");
    }

    /**
     * Test if script can be load from Reader.
     * 
     * @throws ScriptException this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptFromReader() throws ScriptException {
        StringReader stringReader = new StringReader("println('\tHello world!')");
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test script consisting only of empty string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunEmptyScriptFromReader() throws ScriptException {
        StringReader stringReader = new StringReader(JavaScriptSnippets.EMPTY_SCRIPT_1);
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test script consisting only of string with spaces.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptFromReader1() throws ScriptException {
        StringReader stringReader = new StringReader(JavaScriptSnippets.EMPTY_SCRIPT_2);
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test script consisting only of string with tab.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptFromReader2() throws ScriptException {
        StringReader stringReader = new StringReader(JavaScriptSnippets.EMPTY_SCRIPT_3);
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test script consisting only of string with spaces and tab.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptFromReader3() throws ScriptException {
        StringReader stringReader = new StringReader(JavaScriptSnippets.EMPTY_SCRIPT_4);
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test script consisting only of string with space and tabs.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunAlmostEmptyScriptFromReader4() throws ScriptException {
        StringReader stringReader = new StringReader(JavaScriptSnippets.EMPTY_SCRIPT_5);
        this.scriptEngine.eval(stringReader);
    }

    /**
     * Test of proper handling of null string.
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     */
    protected void testRunNullScriptFromReader() throws Exception {
        try {
            StringReader stringReader = new StringReader(null);
            this.scriptEngine.eval(stringReader);
        }
        catch (NullPointerException e) {
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("NPE not thrown as expected");
    }

    /**
     * Script containing unknown function.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingUnknownFunctionFromReader() throws Exception {
        try {
            StringReader stringReader = new StringReader("_unknown_function_('\tHello world!')");
            this.scriptEngine.eval(stringReader);
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return;
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Script containing bad function parameter.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError1FromReader() throws Exception {
        try {
            StringReader stringReader = new StringReader("println(bflmpsvz)");
            this.scriptEngine.eval(stringReader);
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Script containing improper command.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptContainingError2FromReader() throws Exception {
        try {
            StringReader stringReader = new StringReader("xyzzy");
            this.scriptEngine.eval(stringReader);
        } catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if script can be run from external file.
     * @see scripts/test_hello_world.js
     * 
     * @throws ScriptException
     *             this exception is thrown when this test case failed.
     * @throws UnsupportedEncodingException 
     */
    protected void testRunSimpleScriptStoredInFile() throws ScriptException, UnsupportedEncodingException {
        InputStream inputStream = this.getClass().getResourceAsStream("scripts/test_hello_world.js");
        this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
    }

    /**
     * Test if empty script can be run from external file.
     * @see scripts/test_empty_script_1.js
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunEmptyScript1StoredInFile() throws Exception {
        InputStream inputStream = this.getClass().getResourceAsStream("scripts/test_empty_script_1.js");
        this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
    }

    /**
     * Test if exception is thrown when external script contains error.
     * @see scripts/test_unknown_function.js
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingUnknownFunctionFromFile() throws Exception {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("scripts/test_unknown_function.js");
            this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when external script contains error.
     * @see scripts/test_invalid_parameter.js
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError1FromFile() throws Exception {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("scripts/test_invalid_parameter.js");
            this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
        }
        catch (ScriptException e) {
        	System.out.println("Exception thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when external script contains error.
     * @see scripts/test_invalid_command_1.js
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunScriptContainingError2FromFile() throws Exception {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("scripts/test_invalid_command_1.js");
            this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
        }
        catch (ScriptException e) {
        	System.out.println("\tException thrown as expected " + e.getMessage());
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Test if exception is thrown when external script does not exists.
     * 
     * @throws Exception
     *             this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptFromStoredInNonexistent() throws Exception {
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("scripts/file_not_exists.js");
            this.scriptEngine.eval(new InputStreamReader(inputStream, SCRIPT_DEFAULT_ENCODING));
        }
        catch (NullPointerException e) {
            return; // ok, it's correct if this exception is thrown
        }
        throw new Exception("ScriptException not thrown as expected");
    }

    /**
     * Basic test of script evaluation - null value.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnNull() throws Exception {
        Object result = this.scriptEngine.eval("null;");
        assertNull(result, "null not returned as expected");
    }

    /**
     * Basic test of script evaluation - undefined 'value'.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnUndefined() throws Exception {
        Object result = this.scriptEngine.eval("undefined;");
        assertNull(result, "null not returned as expected");
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeBoolean1() throws Exception {
        Object result = this.scriptEngine.eval("!false;");
        assertType(result, java.lang.Boolean.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeBoolean2() throws Exception {
        Object result = this.scriptEngine.eval("!true;");
        assertType(result, java.lang.Boolean.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeBoolean3() throws Exception {
        Object result = this.scriptEngine.eval("true && false;");
        assertType(result, java.lang.Boolean.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeBoolean4() throws Exception {
        Object result = this.scriptEngine.eval("true || false;");
        assertType(result, java.lang.Boolean.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - integer expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeInteger1() throws Exception {
        Object result = this.scriptEngine.eval("42;");
        // expected class could be either Integer or Double due to changes in Rhino itself
        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - integer expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeInteger2() throws Exception {
        Object result = this.scriptEngine.eval("((42));");
        // expected class could be either Integer or Double due to changes in Rhino itself
        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - integer expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeInteger3() throws Exception {
        Object result = this.scriptEngine.eval("1+2;");
        // expected class could be either Integer or Double due to changes in Rhino itself
        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - double expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeDouble1() throws Exception {
        Object result = this.scriptEngine.eval("1.2+3.4;");
        assertType(result, java.lang.Double.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - double expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeDouble2() throws Exception {
        Object result = this.scriptEngine.eval("1/2;");
        assertType(result, java.lang.Double.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - string expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeString1() throws Exception {
        Object result = this.scriptEngine.eval("'hello' + ' ' + 'world';");
        assertType(result, java.lang.String.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - string expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptReturnTypeString2() throws Exception {
        Object result = this.scriptEngine.eval("'1' + '2';");
        assertType(result, java.lang.String.class, result.getClass().getName() + " is not expected class");
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptWhichReturnsBoolean1() throws Exception {
        Boolean result = (Boolean)this.scriptEngine.eval("true;");
        assertTrue(result, "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptWhichReturnsBoolean2() throws Exception {
        Boolean result = (Boolean)this.scriptEngine.eval("false;");
        assertFalse(result, "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptWhichReturnsBoolean3() throws Exception {
        Boolean result = (Boolean)this.scriptEngine.eval("true || false;");
        assertTrue(result, "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - boolean expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptWhichReturnsBoolean4() throws Exception {
        Boolean result = (Boolean)this.scriptEngine.eval("true && false;");
        assertFalse(result, "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - integer expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    @SuppressWarnings("boxing")
	protected void testRunSimpleScriptWhichReturnsNumber() throws Exception {
        Object result = this.scriptEngine.eval("1+2;");
        if (getJavaVersion() >= 7) {
        	Double doubleResult = (Double)result;
            assertTrue(doubleResult == 3, "script returns incorrect value " + result);
        }
        else {
        	Integer intResult = (Integer)result;
            assertTrue(intResult == 3, "script returns incorrect value " + result);
        }
    }

    /**
     * Basic test of script evaluation - double expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    @SuppressWarnings("boxing")
	protected void testRunSimpleScriptWhichReturnsDouble() throws Exception {
        Double result = (Double)this.scriptEngine.eval("1.2+3.4;");
        assertTrue(result == 4.6, "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - string expression.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testRunSimpleScriptWhichReturnsString() throws Exception {
        String result = (String)this.scriptEngine.eval("'Hello' + ' ' + 'world'");
        assertTrue("Hello world".equals(result), "script returns incorrect value " + result);
    }

    /**
     * Basic test of script evaluation - expression returning Java object.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testCreateJavaObject1() throws ScriptException {
        final String script =
            "new java.util.Date()";
        Object date = this.scriptEngine.eval(script);
        assertType(date, java.util.Date.class, "improper type returned");
        System.out.println("\t" + date.toString());
    }

    /**
     * Basic test of script evaluation - expression returning Java object.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testCreateJavaObject2() throws ScriptException {
        final String script =
            "new java.util.Date()";
        Date date = (Date)this.scriptEngine.eval(script);
        assertType(date, java.util.Date.class, "improper type returned");
        System.out.println("\t" + date.toString());
    }

    /**
     * Basic test of script evaluation - expression returning JavaScript object.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testImport() throws ScriptException {
        final String script =
            "importPackage(java.util);" +
            "new Date()";
        Object date = this.scriptEngine.eval(script);
        System.out.println("\t" + date.toString());
    }

    /**
     * Arrays tests.
     *
     * @throws Exception this exception is thrown when this test case failed.
     */
    protected void testAccessJavaObject() throws ScriptException {
        final String script =
            "var i;" +
            "var values = valuesKey.toArray();" +
            "for(i in values) {" +
            "    println('\t' + values[i]);" +
            "}" +
            "println('\tOld array size:' + valuesKey.size());" +
            "valuesKey.add(42);" +
            "println('\tNew array size:' + valuesKey.size());";
        List<Double> values = new ArrayList<Double>();//Arrays.asList(new String[]{"1", "2", "3"});;
        values.add(Double.valueOf(1.));
        values.add(Double.valueOf(2.));
        values.add(Double.valueOf(3.));
        this.scriptEngine.put("valuesKey", values);
        this.scriptEngine.eval(script);
        for (Double value : values) {
            System.out.println("\t" + value);
        }  
    }

    /**
     * Entry point to this test case.
     *
     * @param args parameters passed from command line
     */
    public static void main(String[] args) {
        new JavaScriptsTest().doTests(args);
    }
}