SLING-7305 - The org.apache.sling.scripting.javascript bundle should add more specifi...
authorRadu Cotescu <radu@apache.org>
Wed, 13 Dec 2017 16:02:44 +0000 (17:02 +0100)
committerRadu Cotescu <radu@apache.org>
Wed, 13 Dec 2017 16:03:27 +0000 (17:03 +0100)
* added tests
* enhanced reading info from the bundle's manifest file

pom.xml
src/main/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactory.java
src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 4666d1d..600cd13 100644 (file)
--- a/pom.xml
+++ b/pom.xml
             <scope>test</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.osgi-mock</artifactId>
+            <version>2.3.4</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-all</artifactId>
             <version>1.10.19</version>
index c870df7..70711a1 100644 (file)
@@ -20,7 +20,9 @@ package org.apache.sling.scripting.javascript.internal;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.Dictionary;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.jar.Attributes;
@@ -236,21 +238,32 @@ public class RhinoJavaScriptEngineFactory extends AbstractScriptEngineFactory im
         String rhinoVersion = null;
         InputStream ins = null;
         try {
-            ins = getClass().getResourceAsStream("/META-INF/MANIFEST.MF");
-            if (ins != null) {
-                Manifest manifest = new Manifest(ins);
-                Attributes attrs = manifest.getMainAttributes();
-                rhinoVersion = attrs.getValue("Rhino-Version");
-            }
-        } catch (IOException ioe) {
-            log.warn("Unable to read Rhino version.", ioe);
-        } finally {
-            if (ins != null) {
+            Enumeration<URL> resources = RhinoJavaScriptEngineFactory.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
+            boolean foundEntries = false;
+            while (resources.hasMoreElements() && !foundEntries) {
                 try {
-                    ins.close();
-                } catch (IOException ignore) {
+                    URL url = resources.nextElement();
+                    ins = url.openStream();
+                    if (ins != null) {
+                        Manifest manifest = new Manifest(ins);
+                        Attributes attrs = manifest.getMainAttributes();
+                        String bundleName = attrs.getValue("Bundle-Name");
+                        if (bundleName != null && "Apache Sling Scripting JavaScript Support".equals(bundleName)) {
+                            rhinoVersion = attrs.getValue("Rhino-Version");
+                            foundEntries = true;
+                        }
+                    }
+                } finally {
+                    if (ins != null) {
+                        try {
+                            ins.close();
+                        } catch (IOException ignore) {
+                        }
+                    }
                 }
             }
+        } catch (IOException ioe) {
+            log.warn("Unable to read Rhino version.", ioe);
         }
 
         optimizationLevel = readOptimizationLevel(configuration);
diff --git a/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java b/src/test/java/org/apache/sling/scripting/javascript/internal/RhinoJavaScriptEngineFactoryTest.java
new file mode 100644 (file)
index 0000000..b0a8d53
--- /dev/null
@@ -0,0 +1,55 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ Licensed to the Apache Software Foundation (ASF) under one
+ ~ or more contributor license agreements.  See the NOTICE file
+ ~ distributed with this work for additional information
+ ~ regarding copyright ownership.  The ASF licenses this file
+ ~ to you under the Apache License, Version 2.0 (the
+ ~ "License"); you may not use this file except in compliance
+ ~ with the License.  You may obtain a copy of the License at
+ ~
+ ~   http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing,
+ ~ software distributed under the License is distributed on an
+ ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ ~ KIND, either express or implied.  See the License for the
+ ~ specific language governing permissions and limitations
+ ~ under the License.
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+package org.apache.sling.scripting.javascript.internal;
+
+import java.util.Arrays;
+
+import javax.script.ScriptEngineFactory;
+
+import org.apache.sling.commons.classloader.DynamicClassLoaderManager;
+import org.apache.sling.scripting.api.ScriptCache;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Rule;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class RhinoJavaScriptEngineFactoryTest {
+
+    @Rule
+    public OsgiContext context = new OsgiContext();
+
+    @Test
+    public void testRegistrationProperties() {
+        DynamicClassLoaderManager dynamicClassLoaderManager = mock(DynamicClassLoaderManager.class);
+        when(dynamicClassLoaderManager.getDynamicClassLoader()).thenReturn(RhinoJavaScriptEngineFactoryTest.class.getClassLoader());
+        context.registerService(DynamicClassLoaderManager.class, dynamicClassLoaderManager);
+        context.registerService(ScriptCache.class, mock(ScriptCache.class));
+        context.registerInjectActivateService(new RhinoJavaScriptEngineFactory());
+        RhinoJavaScriptEngineFactory instance = (RhinoJavaScriptEngineFactory) context.getService(ScriptEngineFactory.class);
+        assertEquals(Arrays.asList("rhino", "Rhino", "javascript", "JavaScript", "ecmascript", "ECMAScript"), instance.getNames());
+        assertEquals("ECMAScript", instance.getLanguageName());
+        assertEquals("partial ECMAScript 2015 support", instance.getLanguageVersion());
+        assertTrue("Unexpected engine name", instance.getEngineName() != null && instance.getEngineName().contains("Rhino 1.7.7.1_1"));
+    }
+
+}