You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2024/11/10 18:29:48 UTC

(sling-org-apache-sling-scripting-sightly) 01/02: SLING-12473 refactor tests, add a new test

This is an automated email from the ASF dual-hosted git repository.

joerghoh pushed a commit to branch SLING-12473
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-scripting-sightly.git

commit 640e49d6f170ebe019f7d8975453a9d4b4dc5f6c
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Sun Nov 10 19:28:42 2024 +0100

    SLING-12473 refactor tests, add a new test
---
 .../impl/utils/ScriptDependencyResolverTest.java   | 63 +++++++++++-----------
 1 file changed, 33 insertions(+), 30 deletions(-)

diff --git a/src/test/java/org/apache/sling/scripting/sightly/impl/utils/ScriptDependencyResolverTest.java b/src/test/java/org/apache/sling/scripting/sightly/impl/utils/ScriptDependencyResolverTest.java
index 4c4417c..0949839 100644
--- a/src/test/java/org/apache/sling/scripting/sightly/impl/utils/ScriptDependencyResolverTest.java
+++ b/src/test/java/org/apache/sling/scripting/sightly/impl/utils/ScriptDependencyResolverTest.java
@@ -42,6 +42,7 @@ import org.junit.Rule;
 import org.junit.Test;
 
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.spy;
@@ -57,6 +58,9 @@ public class ScriptDependencyResolverTest {
     @Rule
     public SlingContext context = new SlingContext();
 
+    private ResourceResolver scriptingResolver;
+    private RenderContext renderContext;
+
     @Before
     public void before() throws PersistenceException {
         // resource hierarchy
@@ -73,15 +77,24 @@ public class ScriptDependencyResolverTest {
         testResourceProperties.put("sling:resourceType", "inherit");
         ResourceUtil.getOrCreateResource(context.resourceResolver(), "/content/test", testResourceProperties, "sling:Folder", true);
         context.request().setResource(context.resourceResolver().getResource("/content/test"));
+
+        renderContext = mock(RenderContext.class);
+        Bindings bindings = mock(Bindings.class);
+        when(renderContext.getBindings()).thenReturn(bindings);
+        when(bindings.get(SlingBindings.REQUEST)).thenReturn(context.request());
     }
 
-    @Test
-    public void testDependenciesResolvingCacheDisabled() {
+    // Perform the remaining setup work, which depends on the configuration
+    public void setupDependencies(boolean enableCaching) {
+        int cacheSize=0;
+        if (enableCaching) {
+            cacheSize=1024;
+        }
         SightlyEngineConfiguration configuration = mock(SightlyEngineConfiguration.class);
-        when(configuration.getScriptResolutionCacheSize()).thenReturn(0);
+        when(configuration.getScriptResolutionCacheSize()).thenReturn(cacheSize);
         context.registerService(configuration);
 
-        ResourceResolver scriptingResolver = spy(context.resourceResolver());
+        scriptingResolver = spy(context.resourceResolver());
 
         ScriptingResourceResolverProvider scriptingResourceResolverProvider =
                 mock(ScriptingResourceResolverProvider.class);
@@ -89,11 +102,11 @@ public class ScriptDependencyResolverTest {
         context.registerService(ScriptingResourceResolverProvider.class, scriptingResourceResolverProvider);
 
         scriptDependencyResolver = context.registerInjectActivateService(new ScriptDependencyResolver());
+    }
 
-        RenderContext renderContext = mock(RenderContext.class);
-        Bindings bindings = mock(Bindings.class);
-        when(renderContext.getBindings()).thenReturn(bindings);
-        when(bindings.get(SlingBindings.REQUEST)).thenReturn(context.request());
+    @Test
+    public void testDependenciesResolvingCacheDisabled() {
+        setupDependencies(false);
 
         Resource partial = scriptDependencyResolver.resolveScript(renderContext, "partial.html");
         assertNotNull(partial);
@@ -104,45 +117,35 @@ public class ScriptDependencyResolverTest {
 
     @Test
     public void testDependenciesResolvingCacheEnabled() {
-        SightlyEngineConfiguration configuration = mock(SightlyEngineConfiguration.class);
-        when(configuration.getScriptResolutionCacheSize()).thenReturn(1024);
-        context.registerService(configuration);
-
-        ResourceResolver scriptingResolver = spy(context.resourceResolver());
-
-        ScriptingResourceResolverProvider scriptingResourceResolverProvider =
-                mock(ScriptingResourceResolverProvider.class);
-        when(scriptingResourceResolverProvider.getRequestScopedResourceResolver()).thenReturn(scriptingResolver);
-        context.registerService(ScriptingResourceResolverProvider.class, scriptingResourceResolverProvider);
-
-        scriptDependencyResolver = context.registerInjectActivateService(new ScriptDependencyResolver());
-
-        RenderContext renderContext = mock(RenderContext.class);
-        Bindings bindings = mock(Bindings.class);
-        when(renderContext.getBindings()).thenReturn(bindings);
-        when(bindings.get(SlingBindings.REQUEST)).thenReturn(context.request());
+        setupDependencies(true);
 
         Resource partial = scriptDependencyResolver.resolveScript(renderContext, "partial.html");
         assertNotNull(partial);
         partial = scriptDependencyResolver.resolveScript(renderContext, "partial.html");
         assertNotNull(partial);
-        verify(scriptingResolver, times(5)).getResource(anyString());
+        verify(scriptingResolver, times(6)).getResource(anyString());
 
         // simulate a change in the resource tree
         List<ResourceChange> changes = new ArrayList<>();
         changes.add(mock(ResourceChange.class));
         scriptDependencyResolver.onChange(changes);
 
-
-        // cache should be empty; another retrieval should increase the invocations by 4
+        // cache should be empty; another retrieval should increase the invocations by 5
         partial = scriptDependencyResolver.resolveScript(renderContext, "partial.html");
         assertNotNull(partial);
-        verify(scriptingResolver, times(9)).getResource(anyString());
+        verify(scriptingResolver, times(11)).getResource(anyString());
 
         // cache repopulated; another retrieval should increase the invocations only by 1
         partial = scriptDependencyResolver.resolveScript(renderContext, "partial.html");
         assertNotNull(partial);
-        verify(scriptingResolver, times(10)).getResource(anyString());
+        verify(scriptingResolver, times(12)).getResource(anyString());
+    }
+
+    @Test
+    public void noExistingResourceReturnsNull() {
+        setupDependencies(true);
+        Resource nonexisting = scriptDependencyResolver.resolveScript(renderContext, "nonexisting.html");
+        assertNull(nonexisting);
     }
 
 }