| package org.eclipse.e4.languages.javascript.test; |
| |
| import java.io.*; |
| import java.util.HashMap; |
| import java.util.Map; |
| import junit.framework.TestCase; |
| import org.eclipse.e4.internal.languages.javascript.*; |
| import org.eclipse.e4.languages.javascript.*; |
| import org.mozilla.javascript.*; |
| import org.osgi.framework.Constants; |
| |
| public class JSFrameworkTest extends TestCase { |
| public static String TEST = "test"; |
| |
| private static void writeToFile(String string, File file) throws FileNotFoundException, IOException { |
| Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))); |
| try { |
| writer.write(string); |
| } finally { |
| try { |
| writer.close(); |
| } catch (IOException e) { |
| // ignore |
| } |
| } |
| } |
| |
| public JSFrameworkTest(String name) { |
| super(name); |
| } |
| |
| public void testZeroBundles() { |
| JSFramework framework = new JSFrameworkImpl(); |
| assertEquals(0, framework.getBundles().length); |
| } |
| |
| public void testInstallResolveBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| |
| framework.installBundle("testloc", headers); //$NON-NLS-1$ |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| } |
| |
| public void testInstallResolveBundleFromLocation() throws JSBundleException, IOException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| |
| String result = JSONUtil.write(headers); |
| |
| File temp = File.createTempFile("temp", "js"); |
| try { |
| writeToFile(result, temp); |
| framework.installBundle(temp.toURI().toString(), headers); //$NON-NLS-1$ |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| } finally { |
| temp.delete(); |
| } |
| } |
| |
| public void testStartStopBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| jsBundle.start(); |
| assertEquals(JSBundle.ACTIVE, jsBundle.getState()); |
| jsBundle.stop(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| } |
| |
| public void testInstallUnresolveableBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(Constants.IMPORT_PACKAGE, "some.test"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| |
| } |
| |
| public void testSingletonInstallResolveBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test; singleton:=true"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| |
| Map headers2 = new HashMap(); |
| headers2.put(Constants.BUNDLE_SYMBOLICNAME, "test; singleton:=true"); |
| headers2.put(Constants.BUNDLE_VERSION, "1.9"); |
| headers2.put(Constants.EXPORT_PACKAGE, "test"); |
| headers2.put(JSConstants.BUNDLE_SCRIPT, "var test=\"test\";"); |
| |
| framework.installBundle("testloc2", headers2); |
| JSBundle jsBundle2 = framework.getBundles()[1]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle2.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| assertEquals(JSBundle.INSTALLED, jsBundle2.getState()); |
| } |
| |
| public void testScopedVariableBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(JSConstants.BUNDLE_SCRIPT, "var test=\"test\";"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertEquals("test", obj); |
| } |
| |
| public void testScopedVariableBundleWithExtraPath() throws JSBundleException, IOException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(JSConstants.BUNDLE_SCRIPT, "var test=\"test\";"); |
| |
| File temp1 = File.createTempFile("temp1", "js"); |
| File temp2 = File.createTempFile("temp2", "js"); |
| try { |
| headers.put(JSConstants.BUNDLE_SCRIPTPATH, "., " + temp2.getName()); |
| writeToFile(JSONUtil.write(headers), temp1); |
| writeToFile("var test2=test +'x';", temp2); |
| framework.installBundle(temp1.toURI().toString()); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertEquals("test", obj); |
| Object obj2 = scope.get("test2", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj2); |
| assertEquals("testx", obj2); |
| } finally { |
| temp1.delete(); |
| temp2.delete(); |
| } |
| } |
| |
| public void testStartStopActivatorBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| String script = ""; |
| script += "var x = 1;"; |
| script += "var Activator = function() {"; |
| script += "this.start = function() {x = x + 1;}\n"; |
| script += "this.stop = function() {x = x + 2;}\n"; |
| script += "}"; |
| headers.put(JSConstants.BUNDLE_SCRIPT, script); |
| headers.put(Constants.BUNDLE_ACTIVATOR, "Activator"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("x", scope); |
| assertEquals("1.0", obj.toString()); |
| jsBundle.start(); |
| obj = scope.get("x", scope); |
| assertEquals("2.0", obj.toString()); |
| jsBundle.stop(); |
| obj = scope.get("x", scope); |
| assertEquals("4.0", obj.toString()); |
| jsBundle.start(); |
| obj = scope.get("x", scope); |
| assertEquals("5.0", obj.toString()); |
| jsBundle.stop(); |
| obj = scope.get("x", scope); |
| assertEquals("7.0", obj.toString()); |
| } |
| |
| public void testStartStopScopedActivatorBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| String script = ""; |
| script += "var x = 1;"; |
| script += "var z = {};"; |
| script += "z.Activator = function() {"; |
| script += "this.start = function() {x = x + 1;}\n"; |
| script += "this.stop = function() {x = x + 2;}\n"; |
| script += "}"; |
| headers.put(JSConstants.BUNDLE_SCRIPT, script); |
| headers.put(Constants.BUNDLE_ACTIVATOR, "z.Activator"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("x", scope); |
| assertEquals("1.0", obj.toString()); |
| jsBundle.start(); |
| obj = scope.get("x", scope); |
| assertEquals("2.0", obj.toString()); |
| jsBundle.stop(); |
| obj = scope.get("x", scope); |
| assertEquals("4.0", obj.toString()); |
| jsBundle.start(); |
| obj = scope.get("x", scope); |
| assertEquals("5.0", obj.toString()); |
| jsBundle.stop(); |
| obj = scope.get("x", scope); |
| assertEquals("7.0", obj.toString()); |
| } |
| |
| public void testImportsBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(Constants.IMPORT_PACKAGE, "test"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| |
| Map headers2 = new HashMap(); |
| headers2.put(Constants.BUNDLE_SYMBOLICNAME, "test2"); |
| headers2.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers2.put(Constants.EXPORT_PACKAGE, "test"); |
| headers2.put(JSConstants.BUNDLE_SCRIPT, "var test=\"test\";"); |
| |
| framework.installBundle("testloc2", headers2); |
| JSBundle jsBundle2 = framework.getBundles()[1]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle2.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| assertEquals(JSBundle.RESOLVED, jsBundle2.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertEquals("test", obj); |
| } |
| |
| public void testRequiresBundle() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(Constants.REQUIRE_BUNDLE, "test2"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| |
| Map headers2 = new HashMap(); |
| headers2.put(Constants.BUNDLE_SYMBOLICNAME, "test2"); |
| headers2.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers2.put(Constants.EXPORT_PACKAGE, "a.test"); |
| headers2.put(JSConstants.BUNDLE_SCRIPT, "var a = {}; a.test=\"test\";"); |
| |
| framework.installBundle("testloc2", headers2); |
| JSBundle jsBundle2 = framework.getBundles()[1]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle2.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| assertEquals(JSBundle.RESOLVED, jsBundle2.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("a", scope); |
| assertTrue(obj instanceof Scriptable); |
| scope = (Scriptable) obj; |
| obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertEquals("test", obj); |
| } |
| |
| public void testCallFromJava() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(JSConstants.BUNDLE_SCRIPT, "var test=function() { return \"test\";}"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertTrue(obj instanceof Callable); |
| Callable test = (Callable) obj; |
| Context.enter(); |
| try { |
| assertEquals("test", test.call(Context.getCurrentContext(), scope, scope, null)); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testCallJavaFromJSCreateString() throws JSBundleException { |
| JSFramework framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| headers.put(JSConstants.BUNDLE_SCRIPT, "var test=function() { return new java.lang.String(\"test\");}"); |
| |
| framework.installBundle("testloc", headers); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertTrue(obj instanceof Callable); |
| Callable test = (Callable) obj; |
| Context.enter(); |
| try { |
| Object result = test.call(Context.getCurrentContext(), scope, scope, null); |
| Object javaResult = Context.jsToJava(result, String.class); |
| assertEquals("test", javaResult); |
| } finally { |
| Context.exit(); |
| } |
| } |
| |
| public void testCallJavaFromJSGetBundleStatic() throws JSBundleException { |
| JSFrameworkImpl framework = new JSFrameworkImpl(); |
| Map headers = new HashMap(); |
| headers.put(Constants.BUNDLE_SYMBOLICNAME, "test"); |
| headers.put(Constants.BUNDLE_VERSION, "1.8"); |
| String script = ""; |
| script += "var test=function() {"; |
| script += "var clazz = Packages.org.eclipse.e4.languages.javascript.test.JSFrameworkTest;"; |
| script += "return clazz.TEST;"; |
| script += "}"; |
| headers.put(JSConstants.BUNDLE_SCRIPT, script); |
| |
| framework.installBundle("testloc", headers, new RhinoClassLoader(Activator.getBundleContext().getBundle())); |
| assertEquals(1, framework.getBundles().length); |
| JSBundle jsBundle = framework.getBundles()[0]; |
| |
| assertEquals(JSBundle.INSTALLED, jsBundle.getState()); |
| framework.resolve(); |
| assertEquals(JSBundle.RESOLVED, jsBundle.getState()); |
| Scriptable scope = jsBundle.getScope(); |
| Object obj = scope.get("test", scope); |
| assertNotSame(Scriptable.NOT_FOUND, obj); |
| assertTrue(obj instanceof Callable); |
| Callable test = (Callable) obj; |
| Context.enter(); |
| try { |
| Object result = test.call(Context.getCurrentContext(), scope, scope, null); |
| Object javaResult = Context.jsToJava(result, String.class); |
| assertEquals("test", javaResult); |
| } finally { |
| Context.exit(); |
| } |
| } |
| } |