Mercurial > hg > rhino-tests
changeset 35:658afb0e403e draft
Added new test case src/org/RhinoTests/CompilableClassTest.java with 20 tests.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Thu, 13 Sep 2012 10:03:49 +0200 |
parents | 1909ce4c5896 |
children | df52afae62a2 |
files | ChangeLog Makefile src/org/RhinoTests/CompilableClassTest.java |
diffstat | 3 files changed, 351 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Tue Sep 11 17:02:20 2012 +0200 +++ b/ChangeLog Thu Sep 13 10:03:49 2012 +0200 @@ -1,3 +1,8 @@ +2012-09-13 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/CompilableClassTest.java: + Added new test case with 20 tests. + 2012-09-11 Pavel Tisnovsky <ptisnovs@redhat.com> * src/org/RhinoTests/InvocableClassTest.java:
--- a/Makefile Tue Sep 11 17:02:20 2012 +0200 +++ b/Makefile Thu Sep 13 10:03:49 2012 +0200 @@ -59,6 +59,7 @@ JavaScriptsTest \ BindingsTest \ CompilableTest \ + CompilableClassTest \ CompiledScriptTest \ CompiledScriptClassTest \ InvocableTest \ @@ -88,6 +89,7 @@ $(BUILD_DIR)/$(TEST_PACKAGE)/BaseRhinoTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/BindingsTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompilableTest.class \ + $(BUILD_DIR)/$(TEST_PACKAGE)/CompilableClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompiledScriptTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/CompiledScriptClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/InvocableTest.class \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/RhinoTests/CompilableClassTest.java Thu Sep 13 10:03:49 2012 +0200 @@ -0,0 +1,344 @@ +/* + 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.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; + +import javax.script.Compilable; +import javax.script.ScriptEngineManager; +import javax.script.ScriptEngine; + + + +/** + * Set of tests which check the API of Compilable interface using + * Java reflection API. + * + * @author Pavel Tisnovsky + */ +public class CompilableClassTest extends BaseRhinoTest { + + /** + * Object that represents the type of Compilable. + */ + Class compilableClass = null; + + @Override + protected void setUp(String[] args) { + // setup attribute used by tests + this.compilableClass = Compilable.class; + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Test for method javax.script.Compilable.getClass().isAssignableFrom() + */ + protected void testIsAssignableFrom() { + assertTrue(this.compilableClass.isAssignableFrom(Compilable.class), + "Method Compilable.getClass().isAssignableFrom() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isInstance() + */ + protected void testIsInstance() { + assertTrue(this.compilableClass.isInstance((Compilable)(new ScriptEngineManager().getEngineByName(Constants.EngineNames.ENGINE_NAME_JavaScript))), + "Method Compilable.getClass().isInstance() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isInterface() + */ + protected void testIsInterface() { + assertTrue(this.compilableClass.isInterface(), + "Method Compilable.getClass().isInterface() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isLocalClass() + */ + protected void testIsLocalClass() { + assertFalse(this.compilableClass.isLocalClass(), + "Method Compilable.getClass().isLocalClass() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isMemberClass() + */ + protected void testIsMemberClass() { + assertFalse(this.compilableClass.isMemberClass(), + "Method Compilable.getClass().isMemberClass() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isPrimitive() + */ + protected void testIsPrimitive() { + assertFalse(this.compilableClass.isPrimitive(), + "Method Compilable.getClass().isPrimitive() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().isSynthetic() + */ + protected void testIsSynthetic() { + assertFalse(this.compilableClass.isSynthetic(), + "Method Compilable.getClass().isSynthetic() returns wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().getInterfaces() + */ + protected void testGetInterfaces() { + List interfaces = Arrays.asList(this.compilableClass.getInterfaces()); + assertTrue(interfaces.isEmpty(), + "list of implemented interfaces should be empty"); + } + + /** + * Test for method javax.script.Compilable.getClass().getModifiers() + */ + protected void testGetModifiers() { + int modifiers = this.compilableClass.getModifiers(); + assertTrue(Modifier.isPublic(modifiers), + "Method Compilable.getClass().getModifiers() - isPublic modifier is set to a wrong value"); + assertFalse(Modifier.isPrivate(modifiers), + "Method Compilable.getClass().getModifiers() - isPrivate modifier is set to a wrong value"); + assertFalse(Modifier.isProtected(modifiers), + "Method Compilable.getClass().getModifiers() - isProtected modifier is set to a wrong value"); + assertTrue(Modifier.isAbstract(modifiers), + "Method Compilable.getClass().getModifiers() - isAbstract modifier is set to a wrong value"); + assertFalse(Modifier.isFinal(modifiers), + "Method Compilable.getClass().getModifiers() - isFinal modifier is set to a wrong value"); + assertTrue(Modifier.isInterface(modifiers), + "Method Compilable.getClass().getModifiers() - isInterface modifier is set to a wrong value"); + assertFalse(Modifier.isNative(modifiers), + "Method Compilable.getClass().getModifiers() - isNative modifier is set to a wrong value"); + assertFalse(Modifier.isStatic(modifiers), + "Method Compilable.getClass().getModifiers() - isStatic modifier is set to a wrong value"); + assertFalse(Modifier.isStrict(modifiers), + "Method Compilable.getClass().getModifiers() - isStrict modifier is set to a wrong value"); + assertFalse(Modifier.isSynchronized(modifiers), + "Method Compilable.getClass().getModifiers() - isSynchronized modifier is set to a wrong value"); + assertFalse(Modifier.isTransient(modifiers), + "Method Compilable.getClass().getModifiers() - isTransient modifier is set to a wrong value"); + assertFalse(Modifier.isVolatile(modifiers), + "Method Compilable.getClass().getModifiers() - isVolatile modifier is set to a wrong value"); + } + + /** + * Test for method javax.script.Compilable.getClass().getName() + */ + protected void testGetName() { + String name = this.compilableClass.getName(); + assertEquals(name, "javax.script.Compilable", + "Method Compilable.getClass().getName() returns wrong value " + name); + } + + /** + * Test for method javax.script.Compilable.getClass().getPackage() + */ + protected void testGetPackage() { + Package p = this.compilableClass.getPackage(); + String packageName = p.getName(); + assertEquals(packageName, "javax.script", + "Method Compilable.getClass().getPackage().getName() returns wrong value " + packageName); + } + + /** + * Test for method javax.script.Compilable.getClass().getSimpleName() + */ + protected void testGetSimpleName() { + String simpleName = this.compilableClass.getSimpleName(); + assertEquals(simpleName, "Compilable", + "Method Compilable.getClass().getSimpleName() returns wrong value " + simpleName); + } + + /** + * Test for method javax.script.Compilable.getClass().getSuperclass() + */ + protected void testGetSuperclass() { + Class superClass = this.compilableClass.getSuperclass(); + assertNull(superClass, + "Method Compilable.getClass().getSuperclass() does not return null"); + } + + /** + * Test for method javax.script.Compilable.getClass().getConstructors() + */ + protected void testGetConstructors() { + Constructor[] constructors = this.compilableClass.getConstructors(); + assertEquals(constructors.length, 0, "no constructors should be set"); + } + + /** + * Test for method javax.script.Compilable.getClass().getDeclaredConstructors() + */ + protected void testGetDeclaredConstructors() { + Constructor[] constructors = this.compilableClass.getDeclaredConstructors(); + assertEquals(constructors.length, 0, "no constructors should be set"); + } + + /** + * Test for method javax.script.Compilable.getClass().getFields() + */ + protected void testGetFields() { + // following fields should exists + final String[] fieldsThatShouldExists = { + }; + // get all fields + Field[] fields = this.compilableClass.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.Compilable.getClass().getDeclaredFields() + */ + protected void testGetDeclaredFields() { + // following fields should be declared + final String[] fieldsThatShouldExists = { + }; + // get all declared fields + Field[] declaredFields = this.compilableClass.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.Compilable.getClass().getMethods() + */ + protected void testGetMethods() { + // following methods should be inherited + final String[] methodsThatShouldExists = { + "public abstract javax.script.CompiledScript javax.script.Compilable.compile(java.io.Reader) throws javax.script.ScriptException", + "public abstract javax.script.CompiledScript javax.script.Compilable.compile(java.lang.String) throws javax.script.ScriptException", + }; + // get all inherited methods + Method[] methods = this.compilableClass.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()); + } + // check if all required methods really exists + for (String methodThatShouldExists : methodsThatShouldExists) { + assertTrue(methodsAsString.contains(methodThatShouldExists), + "method " + methodThatShouldExists + " not found"); + } + } + + /** + * Test for method javax.script.Compilable.getClass().getDeclaredMethods() + */ + protected void testGetDeclaredMethods() { + // following methods should be declared + final String[] declaredMethodsThatShouldExists = { + "public abstract javax.script.CompiledScript javax.script.Compilable.compile(java.io.Reader) throws javax.script.ScriptException", + "public abstract javax.script.CompiledScript javax.script.Compilable.compile(java.lang.String) throws javax.script.ScriptException", + }; + // get all declared methods + Method[] declaredMethods = this.compilableClass.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()); + } + // 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.Compilable + */ + protected void testInstanceOf() { + // tested object + Object o = (Compilable)(new ScriptEngineManager().getEngineByName(Constants.EngineNames.ENGINE_NAME_JavaScript)); + + // basic check of instanceof operator + assertTrue(o instanceof Compilable, "instanceof Compilable is wrongly evaluated to false"); + + // check operator instanceof against all superclasses + // (but only one superclass is used in case of Compilable interface) + 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 CompilableClassTest().doTests(args); + } +} +