changeset 18:77d7df3f99f8 draft

Added check for JVM version to the BaseRhinoTest class. Fixed some tests which were based on older Rhino API used in JDK6.
author Pavel Tisnovsky <ptisnovs@redhat.com>
date Wed, 01 Aug 2012 11:29:33 +0200
parents 50619c42d67c
children 8faf0aad2428
files ChangeLog src/org/RhinoTests/BaseRhinoTest.java src/org/RhinoTests/CompiledScriptTest.java src/org/RhinoTests/JavaScriptsTest.java
diffstat 4 files changed, 57 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Jul 30 15:33:41 2012 +0200
+++ b/ChangeLog	Wed Aug 01 11:29:33 2012 +0200
@@ -1,3 +1,12 @@
+2012-08-01  Pavel Tisnovsky  <ptisnovs@redhat.com>
+
+	* src/org/RhinoTests/BaseRhinoTest.java:
+	* src/org/RhinoTests/CompiledScriptTest.java:
+	* src/org/RhinoTests/JavaScriptsTest.java:
+	Added check for JVM version to the BaseRhinoTest class.
+	Fixed some tests which were based on older Rhino API used
+	in JDK6.
+
 2012-07-30  Pavel Tisnovsky  <ptisnovs@redhat.com>
 
 	* BUGS:
--- a/src/org/RhinoTests/BaseRhinoTest.java	Mon Jul 30 15:33:41 2012 +0200
+++ b/src/org/RhinoTests/BaseRhinoTest.java	Wed Aug 01 11:29:33 2012 +0200
@@ -472,4 +472,16 @@
     protected static void assertType(Object object, Class<?> clazz, String message) throws AssertionError {
         assertTrue(object.getClass().equals(clazz), message);
     }
+
+    /**
+     * Returns version of Java. The input could have the following form: "1.7.0_06"
+     * and we are interested only in "7" in this case.
+     * 
+     * @return Java version
+     */
+    protected int getJavaVersion() {
+    	String javaVersionStr = System.getProperty("java.version");
+    	String[] parts = javaVersionStr.split("\\.");
+    	return Integer.parseInt(parts[1]);
+    }
 }
--- a/src/org/RhinoTests/CompiledScriptTest.java	Mon Jul 30 15:33:41 2012 +0200
+++ b/src/org/RhinoTests/CompiledScriptTest.java	Wed Aug 01 11:29:33 2012 +0200
@@ -146,14 +146,21 @@
      * @throws ScriptException
      *             this exception is thrown when this test case failed.
      */
-    protected void testEvalIntegerExpression() throws ScriptException {
+    protected void testEvalNumericExpression() throws ScriptException {
     	CompiledScript script = getCompiledScript("1+2*3");
     	Object result = script.eval();
     	assertNotNull(result, "result should not be null");
     	assertTrue(result instanceof Number, "result is not an instance of Number");
-    	assertTrue(result instanceof Integer, "result is not an instance of Integer");
-		int integerResult = ((Integer) result).intValue();
-    	assertEquals(integerResult, 7, "wrong result " + integerResult);
+		if (getJavaVersion() >= 7) {
+			assertTrue(result instanceof Double, "result is not an instance of Double");
+			double doubleResult = ((Double) result).doubleValue();
+			assertEquals(doubleResult, 7, "wrong result " + doubleResult);
+		}
+		else {
+			assertTrue(result instanceof Integer, "result is not an instance of Integer");
+			int integerResult = ((Integer) result).intValue();
+			assertEquals(integerResult, 7, "wrong result " + integerResult);
+		}
     }
 
     /**
--- a/src/org/RhinoTests/JavaScriptsTest.java	Mon Jul 30 15:33:41 2012 +0200
+++ b/src/org/RhinoTests/JavaScriptsTest.java	Wed Aug 01 11:29:33 2012 +0200
@@ -494,7 +494,9 @@
      */
     protected void testRunSimpleScriptReturnTypeInteger1() throws Exception {
         Object result = this.scriptEngine.eval("42;");
-        assertType(result, java.lang.Integer.class, result.getClass().getName() + " is not expected class");
+        // expected class could be either Integer or Double due to changes in Rhino itself
+        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
+        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
     }
 
     /**
@@ -504,7 +506,9 @@
      */
     protected void testRunSimpleScriptReturnTypeInteger2() throws Exception {
         Object result = this.scriptEngine.eval("((42));");
-        assertType(result, java.lang.Integer.class, result.getClass().getName() + " is not expected class");
+        // expected class could be either Integer or Double due to changes in Rhino itself
+        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
+        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
     }
 
     /**
@@ -514,7 +518,9 @@
      */
     protected void testRunSimpleScriptReturnTypeInteger3() throws Exception {
         Object result = this.scriptEngine.eval("1+2;");
-        assertType(result, java.lang.Integer.class, result.getClass().getName() + " is not expected class");
+        // expected class could be either Integer or Double due to changes in Rhino itself
+        Class<?> expectedClass = getJavaVersion() >= 7 ? java.lang.Double.class : java.lang.Integer.class;
+        assertType(result, expectedClass, result.getClass().getName() + " is not expected class");
     }
 
     /**
@@ -602,9 +608,17 @@
      *
      * @throws Exception this exception is thrown when this test case failed.
      */
-    protected void testRunSimpleScriptWhichReturnsInteger() throws Exception {
-        Integer result = (Integer)this.scriptEngine.eval("1+2;");
-        assertTrue(result == 3, "script returns incorrect value " + result);
+    @SuppressWarnings("boxing")
+	protected void testRunSimpleScriptWhichReturnsNumber() throws Exception {
+        Object result = this.scriptEngine.eval("1+2;");
+        if (getJavaVersion() >= 7) {
+        	Double doubleResult = (Double)result;
+            assertTrue(doubleResult == 3, "script returns incorrect value " + result);
+        }
+        else {
+        	Integer intResult = (Integer)result;
+            assertTrue(intResult == 3, "script returns incorrect value " + result);
+        }
     }
 
     /**
@@ -612,7 +626,8 @@
      *
      * @throws Exception this exception is thrown when this test case failed.
      */
-    protected void testRunSimpleScriptWhichReturnsDouble() throws Exception {
+    @SuppressWarnings("boxing")
+	protected void testRunSimpleScriptWhichReturnsDouble() throws Exception {
         Double result = (Double)this.scriptEngine.eval("1.2+3.4;");
         assertTrue(result == 4.6, "script returns incorrect value " + result);
     }
@@ -682,9 +697,9 @@
             "valuesKey.add(42);" +
             "println('\tNew array size:' + valuesKey.size());";
         List<Double> values = new ArrayList<Double>();//Arrays.asList(new String[]{"1", "2", "3"});;
-        values.add(1.);
-        values.add(2.);
-        values.add(3.);
+        values.add(Double.valueOf(1.));
+        values.add(Double.valueOf(2.));
+        values.add(Double.valueOf(3.));
         this.scriptEngine.put("valuesKey", values);
         this.scriptEngine.eval(script);
         for (Double value : values) {