You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2025/01/24 10:58:38 UTC
(sling-org-apache-sling-scripting-sightly-js-provider) 02/03: SLING-12635 - JS Use Scripts should only be read on-demand
This is an automated email from the ASF dual-hosted git repository.
radu pushed a commit to branch issue/SLING-12635
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly-js-provider.git
commit 6f5796f70b589e54b38c86b4053eed259c5afbda
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Fri Jan 24 11:25:21 2025 +0100
SLING-12635 - JS Use Scripts should only be read on-demand
* added a test to show that the stream is already read when the
SlyBindingsValuesProvider initialises the ScriptNameAwareReader;
the reader needs to be read only when the JsEnvironment needs to
compile/evaluate a script
---
.../js/impl/jsapi/SlyBindingsValuesProvider.java | 5 +-
.../impl/jsapi/SlyBindingsValuesProviderTest.java | 93 ++++++++++++++++++++++
2 files changed, 95 insertions(+), 3 deletions(-)
diff --git a/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java b/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
index ba1314d..31351e9 100644
--- a/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
+++ b/src/main/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProvider.java
@@ -90,13 +90,13 @@ public class SlyBindingsValuesProvider {
" dependency chain."
)
- String[] org_apache_sling_scripting_sightly_js_bindings() default "sightly:" + SlyBindingsValuesProvider.SLING_NS_PATH;
+ String[] org_apache_sling_scripting_sightly_js_bindings() default SlyBindingsValuesProvider.SLING_NS_PATH;
}
public static final String SCR_PROP_JS_BINDING_IMPLEMENTATIONS = "org.apache.sling.scripting.sightly.js.bindings";
- public static final String SLING_NS_PATH = "/libs/sling/sightly/js/internal/sly.js";
+ public static final String SLING_NS_PATH = "sightly:/libs/sling/sightly/js/internal/sly.js";
public static final String Q_PATH = "/libs/sling/sightly/js/3rd-party/q.js";
private static final String REQ_NS = SlyBindingsValuesProvider.class.getCanonicalName();
@@ -285,7 +285,6 @@ public class SlyBindingsValuesProvider {
LOGGER.error("Unable to compile the Q library at path " + Q_PATH + ".", e);
} finally {
Context.exit();
- IOUtils.closeQuietly(reader);
}
return null;
}
diff --git a/src/test/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProviderTest.java b/src/test/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProviderTest.java
new file mode 100644
index 0000000..209a9d3
--- /dev/null
+++ b/src/test/java/org/apache/sling/scripting/sightly/js/impl/jsapi/SlyBindingsValuesProviderTest.java
@@ -0,0 +1,93 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.sightly.js.impl.jsapi;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Objects;
+
+import javax.script.Bindings;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.apache.sling.scripting.core.ScriptNameAwareReader;
+import org.apache.sling.scripting.sightly.js.impl.JsEnvironment;
+import org.apache.sling.scripting.sightly.js.impl.async.AsyncContainer;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.mozilla.javascript.Function;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+class SlyBindingsValuesProviderTest {
+
+ @Mock
+ private ResourceResolver resolver;
+
+ @Mock
+ private Resource scriptResource;
+
+ @Mock
+ private SlyBindingsValuesProvider.Configuration configuration;
+
+ @Mock
+ private JsEnvironment jsEnvironment;
+
+ @Mock
+ private AsyncContainer asyncContainer;
+
+ @Mock
+ private Function function;
+
+ private InputStream inputStream;
+
+ @BeforeEach
+ void setUp() {
+ String scriptPath = SlyBindingsValuesProvider.SLING_NS_PATH.split(":")[1];
+ inputStream = spy(Objects.requireNonNull(getClass().getResourceAsStream("/SLING-INF" + scriptPath)));
+ when(scriptResource.getPath()).thenReturn(scriptPath);
+ when(resolver.getResource(scriptPath)).thenReturn(scriptResource);
+ when(scriptResource.adaptTo(InputStream.class)).thenReturn(inputStream);
+ when(asyncContainer.getResult()).thenReturn(function);
+ when(jsEnvironment.runScript(any(ScriptNameAwareReader.class), any(Bindings.class), any(Bindings.class))).thenReturn(asyncContainer);
+ }
+
+ @Test
+ void testResourceLoading() throws IOException {
+ assertNotNull(inputStream);
+ SlyBindingsValuesProvider provider = new SlyBindingsValuesProvider();
+ provider.activate(configuration);
+ provider.initialise(resolver, jsEnvironment, new SlingBindings());
+ verify(inputStream, never()).read();
+ verify(inputStream, never()).read(any(byte[].class));
+ verify(inputStream, never()).read(any(byte[].class), anyInt(), anyInt());
+ }
+
+}