view src/org/RhinoTests/BindingsTest.java @ 353:6cb1ff232c39 draft

Nine new tests added into BindingsTest.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Tue, 15 Apr 2014 13:19:41 +0200
parents 7668089de997
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.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.LinkedHashMap;
import java.util.TreeMap;

import javax.script.SimpleBindings;
import javax.script.Bindings;

/**
 * Test for the class javax.script.Bingings.
 *
 * @author Pavel Tisnovsky
 */
public class BindingsTest extends BaseRhinoTest {

    @Override
    protected void setUp(String[] args) {
        // this block could be empty
        return;
    }

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

    /**
     * Test of the default constructor.
     */
    protected void testConstructor1() {
        Bindings bindings = new SimpleBindings();
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(bindings.isEmpty(), "bindings should be empty");
        assertTrue(bindings.size() == 0, "bindings should be empty");
    }

    /**
     * Test of the constructor with Map parameter..
     */
    protected void testConstructor2() {
        Bindings bindings = new SimpleBindings(new HashMap<String, Object>());
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(bindings.isEmpty(), "bindings should be empty");
        assertTrue(bindings.size() == 0, "bindings should be empty");
    }

    protected void testConstructor3() {
        Bindings bindings = new SimpleBindings(new TreeMap<String, Object>());
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(bindings.isEmpty(), "bindings should be empty");
        assertTrue(bindings.size() == 0, "bindings should be empty");
    }

    protected void testConstructor4() {
        Bindings bindings = new SimpleBindings(new Hashtable<String, Object>());
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(bindings.isEmpty(), "bindings should be empty");
        assertTrue(bindings.size() == 0, "bindings should be empty");
    }

    protected void testConstructor5() {
        Bindings bindings = new SimpleBindings(new LinkedHashMap<String, Object>());
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(bindings.isEmpty(), "bindings should be empty");
        assertTrue(bindings.size() == 0, "bindings should be empty");
    }

    protected void testConstructor6() {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("key", "value");
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNotNull(bindings.get("key"), "this object should be stored in bindings");
        assertEquals(bindings.get("key"), "value", "wrong value returned");
    }

    protected void testConstructor7() {
    	Map<String, Object> map = new TreeMap<String, Object>();
    	map.put("key", "value");
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNotNull(bindings.get("key"), "this object should be stored in bindings");
        assertEquals(bindings.get("key"), "value", "wrong value returned");
    }

    protected void testConstructor8() {
    	Map<String, Object> hashtable = new Hashtable<String, Object>();
    	hashtable.put("key", "value");
        Bindings bindings = new SimpleBindings(hashtable);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNotNull(bindings.get("key"), "this object should be stored in bindings");
        assertEquals(bindings.get("key"), "value", "wrong value returned");
    }

    protected void testConstructor9() {
    	Map<String, Object> hashtable = new LinkedHashMap<String, Object>();
    	hashtable.put("key", "value");
        Bindings bindings = new SimpleBindings(hashtable);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNotNull(bindings.get("key"), "this object should be stored in bindings");
        assertEquals(bindings.get("key"), "value", "wrong value returned");
    }

    protected void testConstructor10() {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testConstructor11() {
    	Map<String, Object> map = new TreeMap<String, Object>();
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testConstructor12() {
    	Map<String, Object> map = new LinkedHashMap<String, Object>();
    	map.put("key", "value");
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testConstructor13() {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("key", "value");
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testConstructor14() {
    	Map<String, Object> map = new TreeMap<String, Object>();
    	map.put("key", "value");
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testConstructor15() {
    	Map<String, Object> map = new LinkedHashMap<String, Object>();
    	map.put("key", null);
        Bindings bindings = new SimpleBindings(map);
        assertNotNull(bindings, "new SimpleBindings() failed");
        assertTrue(!bindings.isEmpty(), "bindings should not be empty");
        assertTrue(bindings.size() == 1, "bindings should not be empty");
        assertNull(bindings.get("key"), "this object should be stored in bindings");
    }

    protected void testContainsKey1() {
        Bindings bindings = new SimpleBindings();
        assertFalse(bindings.containsKey("key"), "Bingings.containsKey() failed");
    }

    protected void testContainsKey2() {
        Bindings bindings = new SimpleBindings();
        bindings.put("key", "value");
        assertTrue(bindings.containsKey("key"), "Bingings.containsKey() failed");
    }

    protected void testContainsKeyNegative1() throws Exception {
        Bindings bindings = new SimpleBindings();
        try {
            bindings.containsKey(null);
        }
        catch (NullPointerException e) {
            return;
        }
        throw new Exception("NPE did not thrown as expected");
    }

    protected void testContainsKeyNegative2() throws Exception {
        Bindings bindings = new SimpleBindings();
        try {
            bindings.containsKey("");
        }
        catch (IllegalArgumentException e) {
            return;
        }
        throw new Exception("IllegalArgumentException did not thrown as expected");
    }

    protected void testContainsKeyNegative3() throws Exception {
        Bindings bindings = new SimpleBindings();
        try {
            bindings.containsKey(new Integer(42));
        }
        catch (ClassCastException e) {
            return;
        }
        throw new Exception("ClassCastException did not thrown as expected");
    }

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

}

/*
 boolean	containsKey(Object key) 
           Returns true if this map contains a mapping for the specified key.
 Object	get(Object key) 
           Returns the value to which this map maps the specified key.
 Object	put(String name, Object value) 
           Set a named value.
 void	putAll(Map<? extends String,? extends Object> toMerge) 
           Adds all the mappings in a given Map to this Bindings.
 Object	remove(Object key) 
           Removes the mapping for this key from this map if it is present (optional operation).
           */