changeset 26:dbc45e813082 draft

Added new test case src/org/RhinoTests/CompiledScriptClassTest.java with 20 tests.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Mon, 20 Aug 2012 12:09:37 +0200
parents c131ac811c4f
children c3cf3478f465
files ChangeLog Makefile src/org/RhinoTests/CompiledScriptClassTest.java
diffstat 3 files changed, 436 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Aug 17 14:52:08 2012 +0200
+++ b/ChangeLog	Mon Aug 20 12:09:37 2012 +0200
@@ -1,3 +1,10 @@
+2012-08-20  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/RhinoTests/CompiledScriptClassTest.java:
+	Added new test case with 20 tests.
+	* Makefile:
+	Added new class to compile and new test to run.
+
 2012-08-17  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* src/org/RhinoTests/ScriptExceptionClassTest.java:
--- a/Makefile	Fri Aug 17 14:52:08 2012 +0200
+++ b/Makefile	Mon Aug 20 12:09:37 2012 +0200
@@ -59,6 +59,7 @@
 	BindingsTest \
 	CompilableTest \
 	CompiledScriptTest \
+	CompiledScriptClassTest \
 	InvocableTest \
 	ScriptContextTest \
 	ScriptContextClassTest \
@@ -84,6 +85,7 @@
 	$(BUILD_DIR)/$(TEST_PACKAGE)/BindingsTest.class \
 	$(BUILD_DIR)/$(TEST_PACKAGE)/CompilableTest.class \
 	$(BUILD_DIR)/$(TEST_PACKAGE)/CompiledScriptTest.class \
+	$(BUILD_DIR)/$(TEST_PACKAGE)/CompiledScriptClassTest.class \
 	$(BUILD_DIR)/$(TEST_PACKAGE)/InvocableTest.class \
 	$(BUILD_DIR)/$(TEST_PACKAGE)/JavaScriptsTest.class \
 	$(BUILD_DIR)/$(TEST_PACKAGE)/ScriptContextTest.class \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/RhinoTests/CompiledScriptClassTest.java	Mon Aug 20 12:09:37 2012 +0200
@@ -0,0 +1,427 @@
+/*
+  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.CompiledScript;
+import javax.script.ScriptException;
+import javax.script.ScriptEngine;
+import javax.script.ScriptEngineManager;
+
+
+
+/**
+ * Set of tests which check the API of CompiledScript class using
+ * Java reflection API.
+ *
+ * @author Pavel Tisnovsky
+ */
+public class CompiledScriptClassTest extends BaseRhinoTest {
+
+	/**
+	 * Instance of ScriptEngineManager which is used by all tests in this test
+	 * case.
+	 */
+    ScriptEngineManager engineManager;
+
+	/**
+	 * Instance of ScriptEngine which is used by all tests in this test case.
+	 */
+    ScriptEngine scriptEngine;
+
+    /**
+     * Object that represents the type of CompiledScript.
+     */
+    Class compiledScriptClass = null;
+
+    @Override
+    protected void setUp(String[] args) {
+        // setup attribute used by tests
+        this.compiledScriptClass = CompiledScript.class;
+        this.engineManager = new ScriptEngineManager();
+        this.scriptEngine = this.engineManager.getEngineByName("JavaScript");
+    }
+
+    @Override
+    protected void tearDown() {
+        // this block could be empty
+        return;
+    }
+
+	/**
+	 * Helper method which tries to compile given JavaScript.
+	 *
+	 * @param scriptText script source code
+	 * @param compilingEngine instance of class which implements Compilable interface
+	 * @return compiled script
+	 * @throws ScriptException
+	 * @throws AssertionError
+	 */
+	private CompiledScript compileScript(String scriptText, Compilable compilingEngine) throws ScriptException, AssertionError {
+		CompiledScript script = compilingEngine.compile(scriptText);
+		assertNotNull(script, "cannot compile script");
+		return script;
+	}
+
+	/**
+	 * Helper method which tries to retrieve an instance of class which
+	 * implements CompiledScript interface for a given script.
+	 * 
+	 * @param scriptText
+	 *            script source code
+	 * @return instance of CompiledScript class
+	 * @throws AssertionError
+	 *             when CompilingEngine cannot be retrieved
+	 * @throws ScriptException
+	 *             thrown when script cannot be compiled
+	 */
+	private CompiledScript getCompiledScript(String scriptText) throws AssertionError, ScriptException {
+		// check if retyping could be done
+		assertTrue(this.scriptEngine instanceof Compilable, "ScriptEngine does not implement Compilable");
+		// scriptEngine should be also retyped to Compilable, at least in case of JavaScript.
+		Compilable compilingEngine = (Compilable) this.scriptEngine;
+		// should not happen, but...
+		assertNotNull(compilingEngine, "cannot get compiling engine");
+		// try to compile given script
+		return compileScript(scriptText, compilingEngine);
+	}
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isAssignableFrom()
+     */
+    protected void testIsAssignableFrom() {
+        assertTrue(this.compiledScriptClass.isAssignableFrom(CompiledScript.class),
+                "Method CompiledScript.getClass().isAssignableFrom() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isInstance()
+     */
+    protected void testIsInstance() throws ScriptException {
+        assertTrue(this.compiledScriptClass.isInstance(getCompiledScript("6*7")),
+                "Method CompiledScript.getClass().isInstance() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isInterface()
+     */
+    protected void testIsInterface() {
+        assertFalse(this.compiledScriptClass.isInterface(),
+                "Method CompiledScript.getClass().isInterface() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isLocalClass()
+     */
+    protected void testIsLocalClass() {
+        assertFalse(this.compiledScriptClass.isLocalClass(),
+                "Method CompiledScript.getClass().isLocalClass() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isMemberClass()
+     */
+    protected void testIsMemberClass() {
+        assertFalse(this.compiledScriptClass.isMemberClass(),
+                "Method CompiledScript.getClass().isMemberClass() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isPrimitive()
+     */
+    protected void testIsPrimitive() {
+        assertFalse(this.compiledScriptClass.isPrimitive(),
+                "Method CompiledScript.getClass().isPrimitive() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().isSynthetic()
+     */
+    protected void testIsSynthetic() {
+        assertFalse(this.compiledScriptClass.isSynthetic(),
+                "Method CompiledScript.getClass().isSynthetic() returns wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getInterfaces()
+     */
+    protected void testGetInterfaces() {
+        List interfaces = Arrays.asList(this.compiledScriptClass.getInterfaces());
+        assertTrue(interfaces.isEmpty(),
+                "list of implemented interfaces should be empty");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getModifiers()
+     */
+    protected void testGetModifiers() {
+        int modifiers = this.compiledScriptClass.getModifiers();
+        assertTrue(Modifier.isPublic(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isPublic modifier is set to a wrong value");
+        assertFalse(Modifier.isPrivate(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isPrivate modifier is set to a wrong value");
+        assertFalse(Modifier.isProtected(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isProtected modifier is set to a wrong value");
+        assertTrue(Modifier.isAbstract(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isAbstract modifier is set to a wrong value");
+        assertFalse(Modifier.isFinal(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isFinal modifier is set to a wrong value");
+        assertFalse(Modifier.isInterface(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isInterface modifier is set to a wrong value");
+        assertFalse(Modifier.isNative(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isNative modifier is set to a wrong value");
+        assertFalse(Modifier.isStatic(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isStatic modifier is set to a wrong value");
+        assertFalse(Modifier.isStrict(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isStrict modifier is set to a wrong value");
+        assertFalse(Modifier.isSynchronized(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isSynchronized modifier is set to a wrong value");
+        assertFalse(Modifier.isTransient(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isTransient modifier is set to a wrong value");
+        assertFalse(Modifier.isVolatile(modifiers),
+                "Method CompiledScript.getClass().getModifiers() - isVolatile modifier is set to a wrong value");
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getName()
+     */
+    protected void testGetName() {
+        String name = this.compiledScriptClass.getName();
+        assertEquals(name, "javax.script.CompiledScript",
+                "Method CompiledScript.getClass().getName() returns wrong value " + name);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getPackage()
+     */
+    protected void testGetPackage() {
+        Package p = this.compiledScriptClass.getPackage();
+        String packageName = p.getName();
+        assertEquals(packageName, "javax.script",
+                "Method CompiledScript.getClass().getPackage().getName() returns wrong value " + packageName);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getSimpleName()
+     */
+    protected void testGetSimpleName() {
+        String simpleName = this.compiledScriptClass.getSimpleName();
+        assertEquals(simpleName, "CompiledScript",
+                "Method CompiledScript.getClass().getSimpleName() returns wrong value " + simpleName);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getSuperclass()
+     */
+    protected void testGetSuperclass() {
+        Class superClass = this.compiledScriptClass.getSuperclass();
+        String superClassName = superClass.getName();
+        assertEquals(superClassName, "java.lang.Object",
+                "Method CompiledScript.getClass().getSuperclass() returns wrong value " + superClassName);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getConstructors()
+     */
+    protected void testGetConstructors() {
+        Constructor[] constructors = this.compiledScriptClass.getConstructors();
+        assertEquals(constructors.length, 1, "only one constructor should be set");
+        String constructorName;
+        String constructorString;
+        constructorName = constructors[0].getName();
+        constructorString = constructors[0].toString();
+        assertEquals(constructorName, "javax.script.CompiledScript",
+                "wrong constructor name " + constructorName);
+        assertEquals(constructorString, "public javax.script.CompiledScript()",
+                "wrong constructor.toString() " + constructorName);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getDeclaredConstructors()
+     */
+    protected void testGetDeclaredConstructors() {
+        Constructor[] constructors = this.compiledScriptClass.getDeclaredConstructors();
+        assertEquals(constructors.length, 1, "only one constructor should be set");
+        String constructorName;
+        String constructorString;
+        constructorName = constructors[0].getName();
+        constructorString = constructors[0].toString();
+        assertEquals(constructorName, "javax.script.CompiledScript",
+                "wrong constructor name " + constructorName);
+        assertEquals(constructorString, "public javax.script.CompiledScript()",
+                "wrong constructor.toString() " + constructorName);
+    }
+
+    /**
+     * Test for method javax.script.CompiledScript.getClass().getFields()
+     */
+    protected void testGetFields() {
+        // following fields should exists
+        final String[] fieldsThatShouldExists = {
+        };
+        // get all fields
+        Field[] fields = this.compiledScriptClass.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.CompiledScript.getClass().getDeclaredFields()
+     */
+    protected void testGetDeclaredFields() {
+        // following fields should be declared
+        final String[] fieldsThatShouldExists = {
+        };
+        // get all declared fields
+        Field[] declaredFields = this.compiledScriptClass.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.CompiledScript.getClass().getMethods()
+     */
+    protected void testGetMethods() {
+        // following methods should be inherited
+        final String[] methodsThatShouldExists = {
+            "public abstract java.lang.Object javax.script.CompiledScript.eval(javax.script.ScriptContext) throws javax.script.ScriptException",
+            "public abstract javax.script.ScriptEngine javax.script.CompiledScript.getEngine()",
+            "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 java.lang.Object javax.script.CompiledScript.eval() throws javax.script.ScriptException",
+            "public java.lang.Object javax.script.CompiledScript.eval(javax.script.Bindings) throws javax.script.ScriptException",
+            "public java.lang.String java.lang.Object.toString()",
+            "public native int java.lang.Object.hashCode()",
+        };
+        // get all inherited methods
+        Method[] methods = this.compiledScriptClass.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.CompiledScript.getClass().getDeclaredMethods()
+     */
+    protected void testGetDeclaredMethods() {
+        // following methods should be declared
+        final String[] declaredMethodsThatShouldExists = {
+            "public abstract java.lang.Object javax.script.CompiledScript.eval(javax.script.ScriptContext) throws javax.script.ScriptException",
+            "public abstract javax.script.ScriptEngine javax.script.CompiledScript.getEngine()",
+            "public java.lang.Object javax.script.CompiledScript.eval() throws javax.script.ScriptException",
+            "public java.lang.Object javax.script.CompiledScript.eval(javax.script.Bindings) throws javax.script.ScriptException",
+        };
+        // get all declared methods
+        Method[] declaredMethods = this.compiledScriptClass.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.CompiledScript
+     */
+    protected void testInstanceOf() throws ScriptException {
+        // tested object
+        Object o = getCompiledScript("6*7");
+
+        // basic check of instanceof operator
+        assertTrue(o instanceof CompiledScript, "instanceof CompiledScript is wrongly evaluated to false");
+
+        // check operator instanceof against all superclasses
+        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 CompiledScriptClassTest().doTests(args);
+    }
+}
+