From efa797cc636a6b0a21aeee45ee0c4eb8156245bb Mon Sep 17 00:00:00 2001
From: Guy Zana <guy@cloudius-systems.com>
Date: Wed, 8 May 2013 13:18:04 +0300
Subject: [PATCH] java: use the new TestRunner class in the cli

All Java tests need to implement the Test interface and be registered with
TestRunner in order to be available to the test cli command.

TCPEchoServerTest is no longer accessible from javascript, instead, TestRunner is
now the interface of JS for running tests.
---
 console/tests.js                              | 25 +++++++++----------
 java/cli/com/cloudius/cli/main/RhinoCLI.java  |  4 +--
 .../cloudius/cli/tests/TCPEchoServerTest.java | 23 ++---------------
 3 files changed, 16 insertions(+), 36 deletions(-)

diff --git a/console/tests.js b/console/tests.js
index 990e1456a..9868d5a23 100644
--- a/console/tests.js
+++ b/console/tests.js
@@ -1,29 +1,28 @@
 importPackage(Packages.com.cloudius.tests);
 
-var unit_tests = new Array();
-unit_tests["TCPEchoServerTest"] = TCPEchoServerTest;
-
-
 var test_cmd = {
         
     init: function() {
-        this._test_names = new Array();
-        for (var test in unit_tests) {
-            this._test_names.push(test);
-        }
+        this._test_runner = new TestRunner();
+        this._test_runner.registerAllTests();
+        this._test_names = this._test_runner.getTestNames();
     },
-        
+    
     invoke: function(inp) {
         if (inp.length != 2) {
             print("test: no test specified");
             return;
         }
         
-        var classname = unit_tests[inp[1]];
-        var t = new classname();
+        var testname = inp[1];
+        
+        if (this._test_names.indexOf(testname) == -1) {
+            print("test: not a valid test");
+            return;
+        }
         
-        print(">>> Running test " + inp[1] + "...");
-        var rc = t.run();
+        print(">>> Running test " + testname + "...");
+        var rc = this._test_runner.run(testname);
         print(">>> Test completed " + (rc ? "successfully!" : "unsuccessfully..."));
     },
     
diff --git a/java/cli/com/cloudius/cli/main/RhinoCLI.java b/java/cli/com/cloudius/cli/main/RhinoCLI.java
index 62b3d03ee..77f41ea85 100644
--- a/java/cli/com/cloudius/cli/main/RhinoCLI.java
+++ b/java/cli/com/cloudius/cli/main/RhinoCLI.java
@@ -2,7 +2,7 @@ package com.cloudius.cli.main;
 
 import java.io.*;
 
-import com.cloudius.cli.tests.TCPEchoServerTest;
+import com.cloudius.cli.tests.TestRunner;
 import com.cloudius.cli.util.ELFLoader;
 
 import sun.org.mozilla.javascript.*;
@@ -25,7 +25,7 @@ public class RhinoCLI {
             
             global.init(_cx);
             _scope = ScriptableObject.getTopLevelScope(global);
-            ScriptableObject.defineClass(_scope, TCPEchoServerTest.class);
+            ScriptableObject.defineClass(_scope, TestRunner.class);
             ScriptableObject.defineClass(_scope, ELFLoader.class);
             
             FileReader cli_js = new FileReader("/console/cli.js");
diff --git a/java/cli/com/cloudius/cli/tests/TCPEchoServerTest.java b/java/cli/com/cloudius/cli/tests/TCPEchoServerTest.java
index 172728853..e22c6caea 100644
--- a/java/cli/com/cloudius/cli/tests/TCPEchoServerTest.java
+++ b/java/cli/com/cloudius/cli/tests/TCPEchoServerTest.java
@@ -1,12 +1,9 @@
 package com.cloudius.cli.tests;
+
 import java.io.*;
 import java.net.*;
 
-import sun.org.mozilla.javascript.*;
-import sun.org.mozilla.javascript.annotations.JSConstructor;
-import sun.org.mozilla.javascript.annotations.JSFunction;
-
-public class TCPEchoServerTest extends ScriptableObject {
+public class TCPEchoServerTest implements Test {
     
     private TCPEchoServer _server;
     private TCPEchoClient _client;
@@ -105,22 +102,6 @@ public class TCPEchoServerTest extends ScriptableObject {
         }
     }
     
-    private static final long serialVersionUID = 55505548787335642L;
-
-    @Override
-    public String getClassName() {
-        return "TCPEchoServerTest";
-    }
-    
-    @Override
-    public Object getDefaultValue(Class<?> typeHint) {
-        return toString();
-    }
-
-    @JSConstructor
-    public TCPEchoServerTest() { }
-
-    @JSFunction
     public boolean run() {
         
         try {
-- 
GitLab