Mercurial > hg > rhino-tests
changeset 46:1469d167df45 draft
Added new test case src/org/RhinoTests/BindingsClassTest.java with 25 tests.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Thu, 04 Oct 2012 11:12:33 +0200 |
parents | 66320ce2a04f |
children | f7012c8427c1 |
files | ChangeLog Makefile src/org/RhinoTests/BindingsClassTest.java |
diffstat | 3 files changed, 460 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Wed Oct 03 11:02:13 2012 +0200 +++ b/ChangeLog Thu Oct 04 11:12:33 2012 +0200 @@ -1,3 +1,10 @@ +2012-10-04 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/BindingsClassTest.java: + Added new test case with 25 tests. + * Makefile: + Added new class to compile and new test to run. + 2012-10-03 Pavel Tisnovsky <ptisnovs@redhat.com> * src/org/RhinoTests/SimpleScriptContextClassTest.java:
--- a/Makefile Wed Oct 03 11:02:13 2012 +0200 +++ b/Makefile Thu Oct 04 11:12:33 2012 +0200 @@ -59,6 +59,7 @@ ScriptEngineFactoryClassTest \ JavaScriptsTest \ BindingsTest \ + BindingsClassTest \ CompilableTest \ CompilableClassTest \ CompiledScriptTest \ @@ -89,6 +90,7 @@ $(BUILD_DIR)/$(TEST_PACKAGE)/Constants.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/BaseRhinoTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/BindingsTest.class \ + $(BUILD_DIR)/$(TEST_PACKAGE)/BindingsClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompilableTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompilableClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompiledScriptTest.class \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/RhinoTests/BindingsClassTest.java Thu Oct 04 11:12:33 2012 +0200 @@ -0,0 +1,451 @@ +/* + Rhino test framework + + Copyright (C) 2011, 2012 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.Arrays; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.HashMap; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import javax.script.Bindings; +import javax.script.Invocable;import javax.script.SimpleBindings; + + + +/** + * Set of tests which check the API of Bindings interface using + * Java reflection API. + * + * @author Pavel Tisnovsky + */ +public class BindingsClassTest extends BaseRhinoTest { + + /** + * Object that represents the type of Bindings. + */ + Class<?> bindingsClass = null; + + @Override + protected void setUp(String[] args) { + // setup attribute used by tests + this.bindingsClass = Bindings.class; + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Test for method javax.script.Bindings.getClass().isAssignableFrom() + */ + protected void testIsAssignableFrom() { + assertTrue(this.bindingsClass.isAssignableFrom(Bindings.class), + "Method Bindings.getClass().isAssignableFrom() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isInstance() + */ + protected void testIsInstance() { + assertTrue(this.bindingsClass.isInstance((Bindings)(new SimpleBindings())), + "Method Bindings.getClass().isInstance() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isInterface() + */ + protected void testIsInterface() { + assertTrue(this.bindingsClass.isInterface(), + "Method Bindings.getClass().isInterface() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isLocalClass() + */ + protected void testIsLocalClass() { + assertFalse(this.bindingsClass.isLocalClass(), + "Method Bindings.getClass().isLocalClass() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isMemberClass() + */ + protected void testIsMemberClass() { + assertFalse(this.bindingsClass.isMemberClass(), + "Method Bindings.getClass().isMemberClass() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isPrimitive() + */ + protected void testIsPrimitive() { + assertFalse(this.bindingsClass.isPrimitive(), + "Method Bindings.getClass().isPrimitive() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isSynthetic() + */ + protected void testIsSynthetic() { + assertFalse(this.bindingsClass.isSynthetic(), + "Method Bindings.getClass().isSynthetic() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isAnnotation() + */ + protected void testIsAnnotation() { + assertFalse(this.bindingsClass.isAnnotation(), + "Method Bindings.getClass().isAnnotation() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isAnnotationPresent() + */ + protected void testIsAnnotationPresent() { + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.annotation.Annotation.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.annotation.Annotation.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.annotation.Documented.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.annotation.Documented.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.annotation.Inherited.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.annotation.Inherited.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.annotation.Retention.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.annotation.Retention.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.annotation.Target.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.annotation.Target.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.Deprecated.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.Deprecated.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.Override.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.Override.class) returns wrong value"); + assertFalse(this.bindingsClass.isAnnotationPresent(java.lang.SuppressWarnings.class), + "Method Bindings.getClass().isAnnotationPresent(java.lang.SuppressWarnings.class) returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isAnonymousClass() + */ + protected void testIsAnonymousClass() { + assertFalse(this.bindingsClass.isAnonymousClass(), + "Method Bindings.getClass().isAnonymousClass() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isArray() + */ + protected void testIsArray() { + assertFalse(this.bindingsClass.isArray(), + "Method Bindings.getClass().isArray() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().isEnum() + */ + protected void testIsEnum() { + assertFalse(this.bindingsClass.isEnum(), + "Method Bindings.getClass().isEnum() returns wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().getInterfaces() + */ + protected void testGetInterfaces() { + List<Class<?>> interfaces = Arrays.asList(this.bindingsClass.getInterfaces()); + assertTrue(interfaces.contains(Map.class), + "list of implemented interfaces does not contain Map"); + } + + /** + * Test for method javax.script.Bindings.getClass().getModifiers() + */ + protected void testGetModifiers() { + int modifiers = this.bindingsClass.getModifiers(); + assertTrue(Modifier.isPublic(modifiers), + "Method Bindings.getClass().getModifiers() - isPublic modifier is set to a wrong value"); + assertFalse(Modifier.isPrivate(modifiers), + "Method Bindings.getClass().getModifiers() - isPrivate modifier is set to a wrong value"); + assertFalse(Modifier.isProtected(modifiers), + "Method Bindings.getClass().getModifiers() - isProtected modifier is set to a wrong value"); + assertTrue(Modifier.isAbstract(modifiers), + "Method Bindings.getClass().getModifiers() - isAbstract modifier is set to a wrong value"); + assertFalse(Modifier.isFinal(modifiers), + "Method Bindings.getClass().getModifiers() - isFinal modifier is set to a wrong value"); + assertTrue(Modifier.isInterface(modifiers), + "Method Bindings.getClass().getModifiers() - isInterface modifier is set to a wrong value"); + assertFalse(Modifier.isNative(modifiers), + "Method Bindings.getClass().getModifiers() - isNative modifier is set to a wrong value"); + assertFalse(Modifier.isStatic(modifiers), + "Method Bindings.getClass().getModifiers() - isStatic modifier is set to a wrong value"); + assertFalse(Modifier.isStrict(modifiers), + "Method Bindings.getClass().getModifiers() - isStrict modifier is set to a wrong value"); + assertFalse(Modifier.isSynchronized(modifiers), + "Method Bindings.getClass().getModifiers() - isSynchronized modifier is set to a wrong value"); + assertFalse(Modifier.isTransient(modifiers), + "Method Bindings.getClass().getModifiers() - isTransient modifier is set to a wrong value"); + assertFalse(Modifier.isVolatile(modifiers), + "Method Bindings.getClass().getModifiers() - isVolatile modifier is set to a wrong value"); + } + + /** + * Test for method javax.script.Bindings.getClass().getName() + */ + protected void testGetName() { + String name = this.bindingsClass.getName(); + assertEquals(name, "javax.script.Bindings", + "Method Bindings.getClass().getName() returns wrong value " + name); + } + + /** + * Test for method javax.script.Bindings.getClass().getPackage() + */ + protected void testGetPackage() { + Package p = this.bindingsClass.getPackage(); + String packageName = p.getName(); + assertEquals(packageName, "javax.script", + "Method Bindings.getClass().getPackage().getName() returns wrong value " + packageName); + } + + /** + * Test for method javax.script.Bindings.getClass().getSimpleName() + */ + protected void testGetSimpleName() { + String simpleName = this.bindingsClass.getSimpleName(); + assertEquals(simpleName, "Bindings", + "Method Bindings.getClass().getSimpleName() returns wrong value " + simpleName); + } + + /** + * Test for method javax.script.Bindings.getClass().getSuperclass() + */ + protected void testGetSuperclass() { + Class<?> superClass = this.bindingsClass.getSuperclass(); + assertNull(superClass, + "Method Bindings.getClass().getSuperclass() does not return null"); + } + + /** + * Test for method javax.script.Bindings.getClass().getConstructors() + */ + protected void testGetConstructors() { + // map of constructors which should exists + Map<String, String> testedConstructors = null; + Map<String, String> testedConstructors_jdk6 = new HashMap<String, String>(); + Map<String, String> testedConstructors_jdk7 = new HashMap<String, String>(); + + + // get the right map containing constructor signatures + testedConstructors = getJavaVersion() < 7 ? testedConstructors_jdk6 : testedConstructors_jdk7; + + // get all constructors for this class + Constructor<?>[] constructors = this.bindingsClass.getConstructors(); + + // basic check for a number of constructors + assertEquals(constructors.length, 0, "no constructors should be set"); + + } + + /** + * Test for method javax.script.Bindings.getClass().getDeclaredConstructors() + */ + protected void testGetDeclaredConstructors() { + // map of constructors which should exists + Map<String, String> testedConstructors = null; + Map<String, String> testedConstructors_jdk6 = new HashMap<String, String>(); + Map<String, String> testedConstructors_jdk7 = new HashMap<String, String>(); + + + // get the right map containing constructor signatures + testedConstructors = getJavaVersion() < 7 ? testedConstructors_jdk6 : testedConstructors_jdk7; + + // get all declared constructors for this class + Constructor<?>[] declaredConstructors = this.bindingsClass.getDeclaredConstructors(); + + // basic check for a number of declared constructors + assertEquals(declaredConstructors.length, 0, "no constructors should be set"); + + } + + /** + * Test for method javax.script.Bindings.getClass().getFields() + */ + protected void testGetFields() { + // following fields should exists + final String[] fieldsThatShouldExists = { + }; + // get all fields + Field[] fields = this.bindingsClass.getFields(); + // and transform the array into a list of field names + List<String> fieldsAsString = new ArrayList<String>(); + for (Field field : fields) { + fieldsAsString.add(field.toString()); + } + // check if all required fields really exists + for (String fieldThatShouldExists : fieldsThatShouldExists) { + assertTrue(fieldsAsString.contains(fieldThatShouldExists), + "field " + fieldThatShouldExists + " not found"); + } + } + + /** + * Test for method javax.script.Bindings.getClass().getDeclaredFields() + */ + protected void testGetDeclaredFields() { + // following fields should be declared + final String[] fieldsThatShouldExists = { + }; + // get all declared fields + Field[] declaredFields = this.bindingsClass.getDeclaredFields(); + // and transform the array into a list of field names + List<String> declaredFieldsAsString = new ArrayList<String>(); + for (Field field : declaredFields) { + declaredFieldsAsString.add(field.toString()); + } + // check if all required fields really exists + for (String fieldThatShouldExists : fieldsThatShouldExists) { + assertTrue(declaredFieldsAsString.contains(fieldThatShouldExists), + "field " + fieldThatShouldExists + " not found"); + } + } + + /** + * Test for method javax.script.Bindings.getClass().getMethods() + */ + protected void testGetMethods() { + // following methods should be inherited + final String[] methodsThatShouldExists_jdk6 = { + }; + + final String[] methodsThatShouldExists_jdk7 = { + "public abstract boolean java.util.Map.containsValue(java.lang.Object)", + "public abstract boolean java.util.Map.equals(java.lang.Object)", + "public abstract boolean java.util.Map.isEmpty()", + "public abstract boolean javax.script.Bindings.containsKey(java.lang.Object)", + "public abstract int java.util.Map.hashCode()", + "public abstract int java.util.Map.size()", + "public abstract java.lang.Object java.util.Map.put(java.lang.Object,java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.get(java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.put(java.lang.String,java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.remove(java.lang.Object)", + "public abstract java.util.Collection java.util.Map.values()", + "public abstract java.util.Set java.util.Map.entrySet()", + "public abstract java.util.Set java.util.Map.keySet()", + "public abstract void java.util.Map.clear()", + "public abstract void javax.script.Bindings.putAll(java.util.Map)", + }; + + // get all inherited methods + Method[] methods = this.bindingsClass.getMethods(); + // and transform the array into a list of method names + List<String> methodsAsString = new ArrayList<String>(); + for (Method method : methods) { + methodsAsString.add(method.toString()); + } + String[] methodsThatShouldExists = getJavaVersion() < 7 ? methodsThatShouldExists_jdk6 : methodsThatShouldExists_jdk7; + // check if all required methods really exists + for (String methodThatShouldExists : methodsThatShouldExists) { + assertTrue(methodsAsString.contains(methodThatShouldExists), + "method " + methodThatShouldExists + " not found"); + } + } + + /** + * Test for method javax.script.Bindings.getClass().getDeclaredMethods() + */ + protected void testGetDeclaredMethods() { + // following methods should be declared + final String[] declaredMethodsThatShouldExists_jdk6 = { + }; + + final String[] declaredMethodsThatShouldExists_jdk7 = { + "public abstract boolean javax.script.Bindings.containsKey(java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.get(java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.put(java.lang.String,java.lang.Object)", + "public abstract java.lang.Object javax.script.Bindings.remove(java.lang.Object)", + "public abstract void javax.script.Bindings.putAll(java.util.Map)", + }; + + // get all declared methods + Method[] declaredMethods = this.bindingsClass.getDeclaredMethods(); + // and transform the array into a list of method names + List<String> methodsAsString = new ArrayList<String>(); + for (Method method : declaredMethods) { + methodsAsString.add(method.toString()); + } + String[] declaredMethodsThatShouldExists = getJavaVersion() < 7 ? declaredMethodsThatShouldExists_jdk6 : declaredMethodsThatShouldExists_jdk7; + // check if all required methods really exists + for (String methodThatShouldExists : declaredMethodsThatShouldExists) { + assertTrue(methodsAsString.contains(methodThatShouldExists), + "declared method " + methodThatShouldExists + " not found"); + } + } + + /** + * Test for instanceof operator applied to a class javax.script.Bindings + */ + @SuppressWarnings("cast") + protected void testInstanceOf() { + // tested object + Object o = (Bindings)(new SimpleBindings()); + + // basic check of instanceof operator + assertTrue(o instanceof Bindings, "instanceof Bindings is wrongly evaluated to false"); + + // check operator instanceof against all superclasses + assertTrue(o instanceof Object, "instanceof Object is wrongly evaluated to false"); + } + + /** + * Entry point to this test case. + * + * @param args parameters passed from command line + */ + public static void main(String[] args) { + new BindingsClassTest().doTests(args); + } +} +