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

sseifert pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-sling-mock.git


The following commit(s) were added to refs/heads/master by this push:
     new 0ce99a0  SLING-7867 sling-mock: Dynamically resolve SlingBindings 
properties
0ce99a0 is described below

commit 0ce99a0ae836d1143ca1a07545649fc615854499
Author: sseifert <[email protected]>
AuthorDate: Wed Aug 29 21:31:22 2018 +0200

    SLING-7867 sling-mock: Dynamically resolve SlingBindings properties
---
 .../apache/sling/testing/mock/sling/MockSling.java |  1 -
 .../mock/sling/context/MockSlingBindings.java      | 98 ++++++++++++++++++++++
 .../mock/sling/context/SlingContextImpl.java       | 14 +++-
 .../testing/mock/sling/SlingBindingsTest.java      | 58 +++++++++++++
 .../sling/context/models/SlingBindingsModel.java   | 55 ++++++++++++
 5 files changed, 221 insertions(+), 5 deletions(-)

diff --git 
a/core/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java 
b/core/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
index f042873..35489ab 100644
--- a/core/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
+++ b/core/src/main/java/org/apache/sling/testing/mock/sling/MockSling.java
@@ -81,7 +81,6 @@ public final class MockSling {
         return factory;
     }
 
-    @SuppressWarnings("null")
     private static ResourceResolverTypeAdapter 
getResourceResolverTypeAdapter(final ResourceResolverType type) {
         try {
             Class clazz = 
Class.forName(type.getResourceResolverTypeAdapterClass());
diff --git 
a/core/src/main/java/org/apache/sling/testing/mock/sling/context/MockSlingBindings.java
 
b/core/src/main/java/org/apache/sling/testing/mock/sling/context/MockSlingBindings.java
new file mode 100644
index 0000000..95cabd2
--- /dev/null
+++ 
b/core/src/main/java/org/apache/sling/testing/mock/sling/context/MockSlingBindings.java
@@ -0,0 +1,98 @@
+/*
+ * 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.testing.mock.sling.context;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.scripting.SlingBindings;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Mock extension of {@link SlingBindings} that dynamically evaluates 
properties read from SlingBindings from the current mock context.
+ * Normally the SlingBingings are set statically for each script execution, 
but in mocks where no script is really executed
+ * it's easier to evaluate them from current context. 
+ */
+class MockSlingBindings extends SlingBindings {
+    private static final long serialVersionUID = 1L;
+
+    private static final String PROP_CURRENT_NODE = "currentNode";
+    private static final String PROP_CURRENT_SESSION = "currentSession";
+    
+    private final SlingContextImpl context;
+
+    public MockSlingBindings(SlingContextImpl context) {
+        this.context = context;
+    }
+
+    @Override
+    public Object get(Object key) {
+        if (key instanceof String) {
+            Object result = context.resolveSlingBindingProperty((String)key);
+            if (result != null) {
+                return result;
+            }
+        }
+        return super.get(key);
+    }
+    
+    static @Nullable Object resolveSlingBindingProperty(@NotNull 
SlingContextImpl context, @NotNull String property) {
+
+        // -- Sling --
+        if (StringUtils.equals(property, RESOLVER)) {
+            return context.resourceResolver();
+        }
+        if (StringUtils.equals(property, RESOURCE)) {
+            return context.currentResource();
+        }
+        if (StringUtils.equals(property, REQUEST)) {
+            return context.request();
+        }
+        if (StringUtils.equals(property, RESPONSE)) {
+            return context.response();
+        }
+        if (StringUtils.equals(property, SLING)) {
+            return context.slingScriptHelper();
+        }
+        if (StringUtils.equals(property, READER)) {
+            return context.request().getReader();
+        }
+        if (StringUtils.equals(property, OUT)) {
+            return context.response().getWriter();
+        }
+        
+        // -- JCR --
+        // this emulates behavior of 
org.apache.sling.jcr.resource.internal.scripting.JcrObjectsBindingsValuesProvider
+        if (StringUtils.equals(property, PROP_CURRENT_NODE)) {
+            Resource resource = context.currentResource();
+            if (resource != null) {
+                return resource.adaptTo(Node.class);
+            }
+        }
+        if (StringUtils.equals(property, PROP_CURRENT_SESSION)) {
+            return context.resourceResolver().adaptTo(Session.class);
+        }
+        
+        return null;
+    }
+
+}
diff --git 
a/core/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
 
b/core/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
index 682f371..2eb8575 100644
--- 
a/core/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
+++ 
b/core/src/main/java/org/apache/sling/testing/mock/sling/context/SlingContextImpl.java
@@ -274,14 +274,20 @@ public class SlingContextImpl extends OsgiContextImpl {
             this.request = new 
MockSlingHttpServletRequest(this.resourceResolver(), this.bundleContext());
 
             // initialize sling bindings
-            SlingBindings bindings = new SlingBindings();
-            bindings.put(SlingBindings.REQUEST, this.request);
-            bindings.put(SlingBindings.RESPONSE, response());
-            bindings.put(SlingBindings.SLING, slingScriptHelper());
+            SlingBindings bindings = new MockSlingBindings(this);
             this.request.setAttribute(SlingBindings.class.getName(), bindings);
         }
         return this.request;
     }
+    
+    /**
+     * Dynamically resolve property request for current request {@link 
SlingBindings}.
+     * @param property Property key
+     * @return Resolved object or null if no result found
+     */
+    protected @Nullable Object resolveSlingBindingProperty(@NotNull String 
property) {
+        return MockSlingBindings.resolveSlingBindingProperty(this, property);
+    }
 
     /**
      * @return Request path info
diff --git 
a/core/src/test/java/org/apache/sling/testing/mock/sling/SlingBindingsTest.java 
b/core/src/test/java/org/apache/sling/testing/mock/sling/SlingBindingsTest.java
new file mode 100644
index 0000000..9f1f823
--- /dev/null
+++ 
b/core/src/test/java/org/apache/sling/testing/mock/sling/SlingBindingsTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.testing.mock.sling;
+
+import static org.junit.Assert.assertNotNull;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.testing.mock.sling.context.models.SlingBindingsModel;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+@SuppressWarnings("null")
+public class SlingBindingsTest {
+
+    @Rule
+    public SlingContext context = new 
SlingContext(ResourceResolverType.JCR_MOCK);
+
+    private Resource currentResource;
+
+    @Before
+    public void setUp() throws Exception {
+        context.addModelsForClasses(SlingBindingsModel.class);
+        currentResource = 
context.create().resource("/content/testPage/testResource");
+        context.currentResource(currentResource);
+    }
+
+    @Test
+    public void testBindings() {
+        SlingBindingsModel model = 
context.request().adaptTo(SlingBindingsModel.class);
+
+        assertNotNull(model);
+        assertNotNull(model.getResolver());
+        assertNotNull(model.getResource());
+        assertNotNull(model.getRequest());
+        assertNotNull(model.getResponse());
+        assertNotNull(model.getCurrentNode());
+        assertNotNull(model.getcurrentSession());
+    }
+
+}
diff --git 
a/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/SlingBindingsModel.java
 
b/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/SlingBindingsModel.java
new file mode 100644
index 0000000..0d26bea
--- /dev/null
+++ 
b/core/src/test/java/org/apache/sling/testing/mock/sling/context/models/SlingBindingsModel.java
@@ -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.testing.mock.sling.context.models;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.injectorspecific.InjectionStrategy;
+import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
+
+@Model(adaptables = SlingHttpServletRequest.class)
+public interface SlingBindingsModel {
+
+    // -- Sling --
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    ResourceResolver getResolver();
+
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    Resource getResource();
+
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    SlingHttpServletRequest getRequest();
+
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    SlingHttpServletResponse getResponse();
+
+    // -- JCR --
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    Node getCurrentNode();
+
+    @ScriptVariable(injectionStrategy = InjectionStrategy.OPTIONAL)
+    Session getcurrentSession();
+
+}

Reply via email to