Mercurial > hg > rhino-tests
changeset 25:c131ac811c4f draft
Added new test case src/org/RhinoTests/ScriptExceptionClassTest with 20 tests.
author | Pavel Tisnovsky <ptisnovs@redhat.com> |
---|---|
date | Fri, 17 Aug 2012 14:52:08 +0200 |
parents | 23e45df03db4 |
children | dbc45e813082 |
files | ChangeLog Makefile src/org/RhinoTests/ScriptExceptionClassTest.java |
diffstat | 3 files changed, 432 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog Thu Aug 16 11:58:45 2012 +0200 +++ b/ChangeLog Fri Aug 17 14:52:08 2012 +0200 @@ -1,3 +1,10 @@ +2012-08-17 Pavel Tisnovsky <ptisnovs@redhat.com> + + * src/org/RhinoTests/ScriptExceptionClassTest.java: + Added new test case with 20 tests. + * Makefile: + Added new class to compile and new test to run. + 2012-08-16 Pavel Tisnovsky <ptisnovs@redhat.com> * src/org/RhinoTests/ScriptEngineTest.java:
--- a/Makefile Thu Aug 16 11:58:45 2012 +0200 +++ b/Makefile Fri Aug 17 14:52:08 2012 +0200 @@ -63,6 +63,7 @@ ScriptContextTest \ ScriptContextClassTest \ ScriptExceptionTest \ + ScriptExceptionClassTest \ SimpleBindingsTests \ SimpleScriptContextTest \ SimpleScriptContextClassTest @@ -92,6 +93,7 @@ $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptEngineManagerClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptEngineTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptExceptionTest.class \ + $(BUILD_DIR)/$(TEST_PACKAGE)/ScriptExceptionClassTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleBindingsTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleScriptContextTest.class \ $(BUILD_DIR)/$(TEST_PACKAGE)/SimpleScriptContextClassTest.class
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/RhinoTests/ScriptExceptionClassTest.java Fri Aug 17 14:52:08 2012 +0200 @@ -0,0 +1,423 @@ +/* + 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.ScriptException; + + + + +/** + * Set of tests which check the API of ScriptException class using + * Java reflection API. + * + * @author Pavel Tisnovsky + */ +public class ScriptExceptionClassTest extends BaseRhinoTest { + + /** + * Object that represents the type of ScriptException. + */ + Class scriptExceptionClass = null; + + @Override + protected void setUp(String[] args) { + // setup attribute used by tests + this.scriptExceptionClass = ScriptException.class; + } + + @Override + protected void tearDown() { + // this block could be empty + return; + } + + /** + * Test for method javax.script.ScriptException.getClass().isAssignableFrom() + */ + protected void testIsAssignableFrom() { + assertTrue(this.scriptExceptionClass.isAssignableFrom(ScriptException.class), + "Method ScriptException.getClass().isAssignableFrom() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isInstance() + */ + protected void testIsInstance() { + assertTrue(this.scriptExceptionClass.isInstance(new ScriptException("script exception")), + "Method ScriptException.getClass().isInstance() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isInterface() + */ + protected void testIsInterface() { + assertFalse(this.scriptExceptionClass.isInterface(), + "Method ScriptException.getClass().isInterface() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isLocalClass() + */ + protected void testIsLocalClass() { + assertFalse(this.scriptExceptionClass.isLocalClass(), + "Method ScriptException.getClass().isLocalClass() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isMemberClass() + */ + protected void testIsMemberClass() { + assertFalse(this.scriptExceptionClass.isMemberClass(), + "Method ScriptException.getClass().isMemberClass() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isPrimitive() + */ + protected void testIsPrimitive() { + assertFalse(this.scriptExceptionClass.isPrimitive(), + "Method ScriptException.getClass().isPrimitive() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().isSynthetic() + */ + protected void testIsSynthetic() { + assertFalse(this.scriptExceptionClass.isSynthetic(), + "Method ScriptException.getClass().isSynthetic() returns wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().getInterfaces() + */ + protected void testGetInterfaces() { + List interfaces = Arrays.asList(this.scriptExceptionClass.getInterfaces()); + assertTrue(interfaces.isEmpty(), + "list of implemented interfaces should be empty"); + } + + /** + * Test for method javax.script.ScriptException.getClass().getModifiers() + */ + protected void testGetModifiers() { + int modifiers = this.scriptExceptionClass.getModifiers(); + assertTrue(Modifier.isPublic(modifiers), + "Method ScriptException.getClass().getModifiers() - isPublic modifier is set to a wrong value"); + assertFalse(Modifier.isPrivate(modifiers), + "Method ScriptException.getClass().getModifiers() - isPrivate modifier is set to a wrong value"); + assertFalse(Modifier.isProtected(modifiers), + "Method ScriptException.getClass().getModifiers() - isProtected modifier is set to a wrong value"); + assertFalse(Modifier.isAbstract(modifiers), + "Method ScriptException.getClass().getModifiers() - isAbstract modifier is set to a wrong value"); + assertFalse(Modifier.isFinal(modifiers), + "Method ScriptException.getClass().getModifiers() - isFinal modifier is set to a wrong value"); + assertFalse(Modifier.isInterface(modifiers), + "Method ScriptException.getClass().getModifiers() - isInterface modifier is set to a wrong value"); + assertFalse(Modifier.isNative(modifiers), + "Method ScriptException.getClass().getModifiers() - isNative modifier is set to a wrong value"); + assertFalse(Modifier.isStatic(modifiers), + "Method ScriptException.getClass().getModifiers() - isStatic modifier is set to a wrong value"); + assertFalse(Modifier.isStrict(modifiers), + "Method ScriptException.getClass().getModifiers() - isStrict modifier is set to a wrong value"); + assertFalse(Modifier.isSynchronized(modifiers), + "Method ScriptException.getClass().getModifiers() - isSynchronized modifier is set to a wrong value"); + assertFalse(Modifier.isTransient(modifiers), + "Method ScriptException.getClass().getModifiers() - isTransient modifier is set to a wrong value"); + assertFalse(Modifier.isVolatile(modifiers), + "Method ScriptException.getClass().getModifiers() - isVolatile modifier is set to a wrong value"); + } + + /** + * Test for method javax.script.ScriptException.getClass().getName() + */ + protected void testGetName() { + String name = this.scriptExceptionClass.getName(); + assertEquals(name, "javax.script.ScriptException", + "Method ScriptException.getClass().getName() returns wrong value " + name); + } + + /** + * Test for method javax.script.ScriptException.getClass().getPackage() + */ + protected void testGetPackage() { + Package p = this.scriptExceptionClass.getPackage(); + String packageName = p.getName(); + assertEquals(packageName, "javax.script", + "Method ScriptException.getClass().getPackage().getName() returns wrong value " + packageName); + } + + /** + * Test for method javax.script.ScriptException.getClass().getSimpleName() + */ + protected void testGetSimpleName() { + String simpleName = this.scriptExceptionClass.getSimpleName(); + assertEquals(simpleName, "ScriptException", + "Method ScriptException.getClass().getSimpleName() returns wrong value " + simpleName); + } + + /** + * Test for method javax.script.ScriptException.getClass().getSuperclass() + */ + protected void testGetSuperclass() { + Class superClass = this.scriptExceptionClass.getSuperclass(); + String superClassName = superClass.getName(); + assertEquals(superClassName, "java.lang.Exception", + "Method ScriptException.getClass().getSuperclass() returns wrong value " + superClassName); + } + + /** + * Test for method javax.script.ScriptException.getClass().getConstructors() + */ + protected void testGetConstructors() { + Constructor[] constructors = this.scriptExceptionClass.getConstructors(); + assertEquals(constructors.length, 4, "only 4 constructors should be set"); + String constructorName; + String constructorString; + constructorName = constructors[0].getName(); + constructorString = constructors[0].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[1].getName(); + constructorString = constructors[1].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.Exception)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[2].getName(); + constructorString = constructors[2].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String,java.lang.String,int)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[3].getName(); + constructorString = constructors[3].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String,java.lang.String,int,int)", + "wrong constructor.toString() " + constructorName); + } + + /** + * Test for method javax.script.ScriptException.getClass().getDeclaredConstructors() + */ + protected void testGetDeclaredConstructors() { + Constructor[] constructors = this.scriptExceptionClass.getDeclaredConstructors(); + assertEquals(constructors.length, 4, "only 4 constructors should be set"); + String constructorName; + String constructorString; + constructorName = constructors[0].getName(); + constructorString = constructors[0].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[1].getName(); + constructorString = constructors[1].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.Exception)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[2].getName(); + constructorString = constructors[2].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String,java.lang.String,int)", + "wrong constructor.toString() " + constructorName); + constructorName = constructors[3].getName(); + constructorString = constructors[3].toString(); + assertEquals(constructorName, "javax.script.ScriptException", + "wrong constructor name " + constructorName); + assertEquals(constructorString, "public javax.script.ScriptException(java.lang.String,java.lang.String,int,int)", + "wrong constructor.toString() " + constructorName); + } + + /** + * Test for method javax.script.ScriptException.getClass().getFields() + */ + protected void testGetFields() { + // following fields should exists + final String[] fieldsThatShouldExists = { + }; + // get all fields + Field[] fields = this.scriptExceptionClass.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.ScriptException.getClass().getDeclaredFields() + */ + protected void testGetDeclaredFields() { + // following fields should be declared + final String[] fieldsThatShouldExists = { + "private java.lang.String javax.script.ScriptException.fileName", + "private int javax.script.ScriptException.lineNumber", + "private int javax.script.ScriptException.columnNumber", + }; + // get all declared fields + Field[] declaredFields = this.scriptExceptionClass.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.ScriptException.getClass().getMethods() + */ + protected void testGetMethods() { + // following methods should be inherited + final String[] methodsThatShouldExists = { + "public boolean java.lang.Object.equals(java.lang.Object)", + "public final native java.lang.Class java.lang.Object.getClass()", + "public final native void java.lang.Object.notify()", + "public final native void java.lang.Object.notifyAll()", + "public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException", + "public final void java.lang.Object.wait() throws java.lang.InterruptedException", + "public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException", + "public int javax.script.ScriptException.getColumnNumber()", + "public int javax.script.ScriptException.getLineNumber()", + "public java.lang.StackTraceElement[] java.lang.Throwable.getStackTrace()", + "public java.lang.String java.lang.Throwable.getLocalizedMessage()", + "public java.lang.String java.lang.Throwable.toString()", + "public java.lang.String javax.script.ScriptException.getFileName()", + "public java.lang.String javax.script.ScriptException.getMessage()", + "public java.lang.Throwable java.lang.Throwable.getCause()", + "public native int java.lang.Object.hashCode()", + "public synchronized java.lang.Throwable java.lang.Throwable.initCause(java.lang.Throwable)", + "public synchronized native java.lang.Throwable java.lang.Throwable.fillInStackTrace()", + "public void java.lang.Throwable.printStackTrace()", + "public void java.lang.Throwable.printStackTrace(java.io.PrintStream)", + "public void java.lang.Throwable.printStackTrace(java.io.PrintWriter)", + "public void java.lang.Throwable.setStackTrace(java.lang.StackTraceElement[])", + }; + // get all inherited methods + Method[] methods = this.scriptExceptionClass.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.ScriptException.getClass().getDeclaredMethods() + */ + protected void testGetDeclaredMethods() { + // following methods should be declared + final String[] declaredMethodsThatShouldExists = { + "public int javax.script.ScriptException.getColumnNumber()", + "public int javax.script.ScriptException.getLineNumber()", + "public java.lang.String javax.script.ScriptException.getFileName()", + "public java.lang.String javax.script.ScriptException.getMessage()", + }; + // get all declared methods + Method[] declaredMethods = this.scriptExceptionClass.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.ScriptException + */ + protected void testInstanceOf() { + // tested object + Object o = new ScriptException("script exception"); + + // basic check of instanceof operator + assertTrue(o instanceof ScriptException, "instanceof ScriptException is wrongly evaluated to false"); + + // check operator instanceof against all superclasses + assertTrue(o instanceof Exception, "instanceof Exception is wrongly evaluated to false"); + assertTrue(o instanceof Throwable, "instanceof Throwable is wrongly evaluated to false"); + assertTrue(o instanceof Object, "instanceof Object is wrongly evaluated to false"); + 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 ScriptExceptionClassTest().doTests(args); + } +} +