Mercurial > hg > rhino-tests
changeset 14:25d5ea98c051 draft
Added new test case for a class SimpleScriptContext.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Thu, 19 Jul 2012 12:31:34 +0200 |
parents | 223d536c00b4 |
children | 6c5e550a5579 |
files | ChangeLog Makefile src/org/RhinoTests/SimpleScriptContextClassTest.java |
diffstat | 3 files changed, 400 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Thu Jul 19 11:44:01 2012 +0200 +++ b/ChangeLog Thu Jul 19 12:31:34 2012 +0200 @@ -1,3 +1,9 @@ +2012-07-19 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/SimpleScriptContextClassTest.java: + Added new test case for a class SimpleScriptContext. + * Makefile: Added new tests to compile & run. + 2012-07-18 Pavel Tisnovsky <ptisnovs@redhat.com> * src/org/RhinoTests/ScriptContextClassTest.java:
--- a/Makefile Thu Jul 19 11:44:01 2012 +0200 +++ b/Makefile Thu Jul 19 12:31:34 2012 +0200 @@ -62,7 +62,9 @@ ScriptContextTest \ ScriptContextClassTest \ ScriptExceptionTest \ - SimpleScriptContextTest + SimpleBindingsTests \ + SimpleScriptContextTest \ + SimpleScriptContextClassTest ALL_CLASSES = \ $(BUILD_DIR)/$(TEST_CLASSES_PACKAGE)/TestClass.class \ @@ -89,7 +91,8 @@ $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptEngineTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptExceptionTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleBindingsTest.class \ - $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleScriptContextTest.class + $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleScriptContextTest.class \ + $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleScriptContextClassTest.class ALL_SCRIPTS = \ $(BUILD_DIR)/$(TEST_PACKAGE)/$(SCRIPTS)/test1.js \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/RhinoTests/SimpleScriptContextClassTest.java Thu Jul 19 12:31:34 2012 +0200 @@ -0,0 +1,389 @@ +/* + 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.ScriptContext; +import javax.script.SimpleScriptContext; + + + +/** + * Set of tests which check the API of SimpleScriptContext class using + * Java reflection API. + * + * @author Pavel Tisnovsky + */ +public class SimpleScriptContextClassTest extends BaseRhinoTest { + + /** + * Instance of SimpleScriptContext class + */ + Object simpleScriptContextInstance = null; + + /** + * Object that represents the type of one SimpleScriptContext instance. + */ + Class simpleScriptContextClass = null; + + @Override + protected void setUp(String[] args) { + // setup both attributes used by tests + this.simpleScriptContextInstance = new SimpleScriptContext(); + this.simpleScriptContextClass = this.simpleScriptContextInstance.getClass(); + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isAssignableFrom() + */ + protected void testIsAssignableFrom() { + assertTrue(this.simpleScriptContextClass.isAssignableFrom(SimpleScriptContext.class), + "Method SimpleScriptContext.getClass().isAssignableFrom() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isInstance() + */ + protected void testIsInstance() { + assertTrue(this.simpleScriptContextClass.isInstance(new SimpleScriptContext()), + "Method SimpleScriptContext.getClass().isInstance() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isInterface() + */ + protected void testIsInterface() { + assertFalse(this.simpleScriptContextClass.isInterface(), + "Method SimpleScriptContext.getClass().isInterface() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isLocalClass() + */ + protected void testIsLocalClass() { + assertFalse(this.simpleScriptContextClass.isLocalClass(), + "Method SimpleScriptContext.getClass().isLocalClass() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isMemberClass() + */ + protected void testIsMemberClass() { + assertFalse(this.simpleScriptContextClass.isMemberClass(), + "Method SimpleScriptContext.getClass().isMemberClass() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isPrimitive() + */ + protected void testIsIsPrimitive() { + assertFalse(this.simpleScriptContextClass.isPrimitive(), + "Method SimpleScriptContext.getClass().isPrimitive() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().isSynthetic() + */ + protected void testIsIsSynthetic() { + assertFalse(this.simpleScriptContextClass.isSynthetic(), + "Method SimpleScriptContext.getClass().isSynthetic() returns wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getInterfaces() + */ + protected void testGetInterfaces() { + List interfaces = Arrays.asList(this.simpleScriptContextClass.getInterfaces()); + assertTrue(interfaces.contains(ScriptContext.class), + "list of implemented interfaces does not contains ScriptContex"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getModifiers() + */ + protected void testGetModifiers() { + int modifiers = this.simpleScriptContextClass.getModifiers(); + assertTrue(Modifier.isPublic(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isPublic modifier is set to a wrong value"); + assertFalse(Modifier.isPrivate(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isPrivate modifier is set to a wrong value"); + assertFalse(Modifier.isProtected(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isProtected modifier is set to a wrong value"); + assertFalse(Modifier.isAbstract(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isAbstract modifier is set to a wrong value"); + assertFalse(Modifier.isFinal(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isFinal modifier is set to a wrong value"); + assertFalse(Modifier.isInterface(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isInterface modifier is set to a wrong value"); + assertFalse(Modifier.isNative(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isNative modifier is set to a wrong value"); + assertFalse(Modifier.isStatic(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isStatic modifier is set to a wrong value"); + assertFalse(Modifier.isStrict(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isStrict modifier is set to a wrong value"); + assertFalse(Modifier.isSynchronized(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isSynchronized modifier is set to a wrong value"); + assertFalse(Modifier.isTransient(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isTransient modifier is set to a wrong value"); + assertFalse(Modifier.isVolatile(modifiers), + "Method SimpleScriptContext.getClass().getModifiers() - isVolatile modifier is set to a wrong value"); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getName() + */ + protected void testGetName() { + String name = this.simpleScriptContextClass.getName(); + assertEquals(name, "javax.script.SimpleScriptContext", + "Method SimpleScriptContext.getClass().getName() returns wrong value " + name); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getPackage() + */ + protected void testGetPackage() { + Package p = this.simpleScriptContextClass.getPackage(); + String packageName = p.getName(); + assertEquals(packageName, "javax.script", + "Method SimpleScriptContext.getClass().getPackage().getName() returns wrong value " + packageName); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getSimpleName() + */ + protected void testGetSimpleName() { + String simpleName = this.simpleScriptContextClass.getSimpleName(); + assertEquals(simpleName, "SimpleScriptContext", + "Method SimpleScriptContext.getClass().getSimpleName() returns wrong value " + simpleName); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getSuperclass() + */ + protected void testGetSuperclass() { + Class superClass = this.simpleScriptContextClass.getSuperclass(); + String superClassName = superClass.getName(); + assertEquals(superClassName, "java.lang.Object", + "Method SimpleScriptContext.getClass().getSuperclass() returns wrong value " + superClassName); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getGetConstructors() + */ + protected void testGetConstructors() { + Constructor[] constructors = this.simpleScriptContextClass.getConstructors(); + assertEquals(constructors.length, 1, "only one constructor should be set"); + String constructorName = constructors[0].getName(); + String constructorString = constructors[0].toString(); + assertEquals(constructorName, "javax.script.SimpleScriptContext", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.SimpleScriptContext()", + "wrong constructor.toString() " + constructorName); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getGetDeclaredConstructors() + */ + protected void testGetDeclaredConstructors() { + Constructor[] constructors = this.simpleScriptContextClass.getDeclaredConstructors(); + assertEquals(constructors.length, 1, "only one constructor should be set"); + String constructorName = constructors[0].getName(); + String constructorString = constructors[0].toString(); + assertEquals(constructorName, "javax.script.SimpleScriptContext", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.SimpleScriptContext()", + "wrong constructor.toString() " + constructorName); + } + + /** + * Test for method javax.script.SimpleScriptContext.getClass().getGetFields() + */ + protected void testGetFields() { + // following fields should be inherited + final String[] fieldsThatShouldExists = { + "public static final int javax.script.ScriptContext.ENGINE_SCOPE", + "public static final int javax.script.ScriptContext.GLOBAL_SCOPE", + }; + // get all inherited fields + Field[] declaredFields = this.simpleScriptContextClass.getFields(); + // and transform the array into a list of field names + List<String> fieldsAsString = new ArrayList<String>(); + for (Field field : declaredFields) { + 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.SimpleScriptContext.getClass().getGetDeclaredFields() + */ + protected void testGetDeclaredFields() { + // following fields should be declared + final String[] fieldsThatShouldExists = { + "protected java.io.Writer javax.script.SimpleScriptContext.writer", + "protected java.io.Writer javax.script.SimpleScriptContext.errorWriter", + "protected java.io.Reader javax.script.SimpleScriptContext.reader", + "protected javax.script.Bindings javax.script.SimpleScriptContext.engineScope", + "protected javax.script.Bindings javax.script.SimpleScriptContext.globalScope", + }; + // get all declared fields + Field[] declaredFields = this.simpleScriptContextClass.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.SimpleScriptContext.getClass().getGetMethods() + */ + protected void testGetMethods() { + // following methods should be inherited + final String[] methodsThatShouldExists = { + "public java.lang.Object javax.script.SimpleScriptContext.getAttribute(java.lang.String)", + "public java.lang.Object javax.script.SimpleScriptContext.getAttribute(java.lang.String,int)", + "public int javax.script.SimpleScriptContext.getAttributesScope(java.lang.String)", + "public javax.script.Bindings javax.script.SimpleScriptContext.getBindings(int)", + "public java.io.Writer javax.script.SimpleScriptContext.getErrorWriter()", + "public java.io.Reader javax.script.SimpleScriptContext.getReader()", + "public java.util.List javax.script.SimpleScriptContext.getScopes()", + "public java.io.Writer javax.script.SimpleScriptContext.getWriter()", + "public java.lang.Object javax.script.SimpleScriptContext.removeAttribute(java.lang.String,int)", + "public void javax.script.SimpleScriptContext.setAttribute(java.lang.String,java.lang.Object,int)", + "public void javax.script.SimpleScriptContext.setBindings(javax.script.Bindings,int)", + "public void javax.script.SimpleScriptContext.setErrorWriter(java.io.Writer)", + "public void javax.script.SimpleScriptContext.setReader(java.io.Reader)", + "public void javax.script.SimpleScriptContext.setWriter(java.io.Writer)", + }; + // get all inherited methods + Method[] methods = this.simpleScriptContextClass.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.SimpleScriptContext.getClass().getGetDeclaredMethods() + */ + protected void testGetDeclaredMethods() { + // following methods should be declared + final String[] declaredMethodsThatShouldExists = { + "public void javax.script.SimpleScriptContext.setBindings(javax.script.Bindings,int)", + "public java.io.Writer javax.script.SimpleScriptContext.getWriter()", + "public java.io.Reader javax.script.SimpleScriptContext.getReader()", + "public void javax.script.SimpleScriptContext.setReader(java.io.Reader)", + "public void javax.script.SimpleScriptContext.setWriter(java.io.Writer)", + "public java.io.Writer javax.script.SimpleScriptContext.getErrorWriter()", + "public void javax.script.SimpleScriptContext.setErrorWriter(java.io.Writer)", + "public int javax.script.SimpleScriptContext.getAttributesScope(java.lang.String)", + "public javax.script.Bindings javax.script.SimpleScriptContext.getBindings(int)", + "public java.util.List javax.script.SimpleScriptContext.getScopes()", + "public java.lang.Object javax.script.SimpleScriptContext.getAttribute(java.lang.String)", + "public java.lang.Object javax.script.SimpleScriptContext.getAttribute(java.lang.String,int)", + "public java.lang.Object javax.script.SimpleScriptContext.removeAttribute(java.lang.String,int)", + "public void javax.script.SimpleScriptContext.setAttribute(java.lang.String,java.lang.Object,int)", + }; + // get all declared methods + Method[] declaredMethods = this.simpleScriptContextClass.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.SimpleScriptContext + */ + protected void testInstanceOf() { + // tested object + Object o = this.simpleScriptContextInstance; + // basic check of instanceof operator + assertTrue(o instanceof SimpleScriptContext, "instanceof SimpleScriptContext 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 SimpleScriptContextClassTest().doTests(args); + } +} +