Author: sseifert
Date: Tue Oct 21 16:12:24 2014
New Revision: 1633409

URL: http://svn.apache.org/r1633409
Log:
SLING-4083 Enable SlingObject injector to inject all context objects when a 
request is attached to the current thread

Added:
    
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
   (with props)
    
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
   (with props)
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
   (with props)
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
   (with props)
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
   (with props)
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
   (with props)
Modified:
    
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/injectorspecific/SlingObject.java
    sling/trunk/bundles/extensions/models/impl/pom.xml
    
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjector.java
    
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestTest.java

Modified: 
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/injectorspecific/SlingObject.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/injectorspecific/SlingObject.java?rev=1633409&r1=1633408&r2=1633409&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/injectorspecific/SlingObject.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/api/src/main/java/org/apache/sling/models/annotations/injectorspecific/SlingObject.java
 Tue Oct 21 16:12:24 2014
@@ -52,31 +52,35 @@ import org.apache.sling.models.spi.injec
  * <td>{@link Resource}</td>
  * <td>Resource</td>
  * <td style="text-align:center">X</td>
- * <td></td>
+ * <td style="text-align:center">X*</td>
  * <td style="text-align:center">X</td>
  * </tr>
  * <tr style="background-color:#eee">
  * <td>{@link SlingHttpServletRequest}</td>
  * <td>Sling request</td>
  * <td style="text-align:center">X</td>
- * <td></td>
- * <td></td>
+ * <td style="text-align:center">X*</td>
+ * <td style="text-align:center">X*</td>
  * </tr>
  * <tr>
  * <td>{@link SlingHttpServletResponse}</td>
  * <td>Sling response</td>
  * <td style="text-align:center">X</td>
- * <td></td>
- * <td></td>
+ * <td style="text-align:center">X*</td>
+ * <td style="text-align:center">X*</td>
  * </tr>
  * <tr style="background-color:#eee">
  * <td>{@link SlingScriptHelper}</td>
  * <td>Sling script helper</td>
  * <td style="text-align:center">X</td>
- * <td></td>
- * <td></td>
+ * <td style="text-align:center">X*</td>
+ * <td style="text-align:center">X*</td>
  * </tr>
  * </table>
+ * <p>
+ * In case of X* the class cannot be derived from the adaptable, but is 
derived from the request of the current thread
+ * set by a servlet filter. If the current thread is not associated with a 
request nothing is injected.
+ * </p>
  */
 @Target({ METHOD, FIELD, PARAMETER })
 @Retention(RUNTIME)

Modified: sling/trunk/bundles/extensions/models/impl/pom.xml
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/pom.xml?rev=1633409&r1=1633408&r2=1633409&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/pom.xml (original)
+++ sling/trunk/bundles/extensions/models/impl/pom.xml Tue Oct 21 16:12:24 2014
@@ -138,5 +138,10 @@
             <version>1.8.3</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.sling-mock</artifactId>
+            <version>1.0.1-SNAPSHOT</version>
+        </dependency>
     </dependencies>
 </project>

Modified: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjector.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjector.java?rev=1633409&r1=1633408&r2=1633409&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjector.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjector.java
 Tue Oct 21 16:12:24 2014
@@ -24,6 +24,8 @@ import javax.servlet.http.HttpServletRes
 
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
@@ -54,6 +56,9 @@ public final class SlingObjectInjector i
      * Injector name
      */
     public static final String NAME = "sling-object";
+    
+    @Reference(cardinality=ReferenceCardinality.OPTIONAL_UNARY)
+    private SlingObjectInjectorRequestContext requestContext;
 
     @Override
     public String getName() {
@@ -71,8 +76,23 @@ public final class SlingObjectInjector i
         Class<?> requestedClass = (Class<?>) type;
 
         // validate input
-        if (adaptable instanceof SlingHttpServletRequest) {
-            SlingHttpServletRequest request = (SlingHttpServletRequest) 
adaptable;
+        if (adaptable instanceof ResourceResolver) {
+            ResourceResolver resourceResolver = (ResourceResolver)adaptable;
+            if (requestedClass.equals(ResourceResolver.class)) {
+                return resourceResolver;
+            }
+        }
+        else if (adaptable instanceof Resource) {
+            Resource resource = (Resource)adaptable;
+            if (requestedClass.equals(ResourceResolver.class)) {
+                return resource.getResourceResolver();
+            }
+            if (requestedClass.equals(Resource.class) && 
element.isAnnotationPresent(SlingObject.class)) {
+                return resource;
+            }
+        }
+        SlingHttpServletRequest request = getRequest(adaptable);
+        if (request != null) {
             if (requestedClass.equals(ResourceResolver.class)) {
                 return request.getResourceResolver();
             }
@@ -82,31 +102,29 @@ public final class SlingObjectInjector i
             if (requestedClass.equals(SlingHttpServletRequest.class) || 
requestedClass.equals(HttpServletRequest.class)) {
                 return request;
             }
-            if (requestedClass.equals(SlingHttpServletResponse.class)
-                    || requestedClass.equals(HttpServletResponse.class)) {
+            if (requestedClass.equals(SlingHttpServletResponse.class) || 
requestedClass.equals(HttpServletResponse.class)) {
                 return getSlingHttpServletResponse(request);
             }
             if (requestedClass.equals(SlingScriptHelper.class)) {
                 return getSlingScriptHelper(request);
             }
-        } else if (adaptable instanceof ResourceResolver) {
-            ResourceResolver resourceResolver = (ResourceResolver) adaptable;
-            if (requestedClass.equals(ResourceResolver.class)) {
-                return resourceResolver;
-            }
-        } else if (adaptable instanceof Resource) {
-            Resource resource = (Resource) adaptable;
-            if (requestedClass.equals(ResourceResolver.class)) {
-                return resource.getResourceResolver();
-            }
-            if (requestedClass.equals(Resource.class) && 
element.isAnnotationPresent(SlingObject.class)) {
-                return resource;
-            }
         }
 
         return null;
     }
 
+    private SlingHttpServletRequest getRequest(final Object adaptable) {
+        if (adaptable instanceof SlingHttpServletRequest) {
+            return (SlingHttpServletRequest) adaptable;
+        }
+        else if (requestContext != null) {
+            return requestContext.getThreadRequest();
+        }
+        else {
+            return null;
+        }
+    }
+
     private SlingScriptHelper getSlingScriptHelper(final 
SlingHttpServletRequest request) {
         SlingBindings bindings = (SlingBindings) 
request.getAttribute(SlingBindings.class.getName());
         if (bindings != null) {

Added: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,35 @@
+/*
+ * 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.models.impl.injectors;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+
+/**
+ * OSGi service interface for getting a sling request for the current thread.
+ * 
+ * TODO: it would be nice to move this to some central part e.g. in the Sling 
API.
+ */
+public interface SlingObjectInjectorRequestContext {
+
+    /**
+     * Returns sling request associated with the current thread.
+     * The request is stored in a threadlocal set by a servlet filter.
+     * @return Sling request or null if none is associated.
+     */
+    SlingHttpServletRequest getThreadRequest();
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContext.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,73 @@
+/*
+ * 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.models.impl.injectors;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+
+/**
+ * Servlet filter that sets the current sling request during processing to make
+ * it available for the {@sling SlingObjectInjector}.
+ * 
+ * TODO: it would be nice to move this to some central part e.g. in the Sling 
API.
+ */
+@Component(immediate = true)
+@Service({ SlingObjectInjectorRequestContext.class, Filter.class })
+public final class SlingObjectInjectorRequestContextFilter implements 
SlingObjectInjectorRequestContext, Filter {
+
+    private static final ThreadLocal<SlingHttpServletRequest> 
REQUEST_THREADLOCAL = new ThreadLocal<SlingHttpServletRequest>();
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException,
+            ServletException {
+        if (request instanceof HttpServletRequest) {
+            REQUEST_THREADLOCAL.set((SlingHttpServletRequest) request);
+        }
+        try {
+            chain.doFilter(request, response);
+        } finally {
+            REQUEST_THREADLOCAL.remove();
+        }
+    }
+
+    @Override
+    public SlingHttpServletRequest getThreadRequest() {
+        return REQUEST_THREADLOCAL.get();
+    }
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        // nothing to do
+    }
+
+    @Override
+    public void destroy() {
+        // nothing to do
+    }
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,85 @@
+/*
+ * 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.models.impl.injectors;
+
+import static org.junit.Assert.*;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SlingObjectInjectorRequestContextFilterTest {
+
+    @Mock
+    private SlingHttpServletRequest request;
+    @Mock
+    private SlingHttpServletResponse response;
+
+    private SlingObjectInjectorRequestContextFilter underTest;
+
+    @Before
+    public void setUp() {
+      underTest = new SlingObjectInjectorRequestContextFilter();
+    }
+
+    @Test
+    public void testFilter() throws Exception {
+      assertNull(underTest.getThreadRequest());
+
+      underTest.doFilter(request, response, new FilterChain() {
+        @Override
+        public void doFilter(ServletRequest req, ServletResponse resp) {
+          assertSame(req, underTest.getThreadRequest());
+        }
+      });
+
+      assertNull(underTest.getThreadRequest());
+    }
+
+    @Test
+    public void testWithExeption() throws Exception {
+      assertNull(underTest.getThreadRequest());
+
+      try {
+        underTest.doFilter(request, response, new FilterChain() {
+          @Override
+          public void doFilter(ServletRequest req, ServletResponse resp) 
throws ServletException {
+            throw new ServletException("simulated exception.");
+          }
+        });
+        fail("Excpection expected");
+      }
+      catch (ServletException ex) {
+        // expected
+      }
+
+      assertNull(underTest.getThreadRequest());
+    }
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestContextFilterTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestTest.java?rev=1633409&r1=1633408&r2=1633409&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestTest.java
 (original)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequestTest.java
 Tue Oct 21 16:12:24 2014
@@ -33,7 +33,10 @@ import org.apache.sling.api.resource.Res
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.api.scripting.SlingScriptHelper;
 import org.apache.sling.models.annotations.injectorspecific.SlingObject;
+import org.apache.sling.testing.mock.sling.ResourceResolverType;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
@@ -42,77 +45,88 @@ import org.mockito.runners.MockitoJUnitR
 @RunWith(MockitoJUnitRunner.class)
 public class SlingObjectInjectorRequestTest {
 
-    private final SlingObjectInjector injector = new SlingObjectInjector();
-
+    @Rule
+    public SlingContext context = new 
SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
+    
+    @Mock
+    protected AnnotatedElement annotatedElement;
     @Mock
-    private AnnotatedElement annotatedElement;
+    protected SlingHttpServletRequest request;
     @Mock
-    private SlingHttpServletRequest request;
+    protected SlingHttpServletResponse response;
     @Mock
-    private SlingHttpServletResponse response;
+    protected SlingScriptHelper scriptHelper;
     @Mock
-    private SlingScriptHelper scriptHelper;
+    protected ResourceResolver resourceResolver;
     @Mock
-    private ResourceResolver resourceResolver;
+    protected Resource resource;
     @Mock
-    private Resource resource;
+    protected SlingObjectInjectorRequestContext requestContext;
 
+    protected SlingObjectInjector injector;
+    
     @Before
     public void setUp() {
+        context.registerService(SlingObjectInjectorRequestContext.class, 
this.requestContext);
+        injector = context.registerInjectActivateService(new 
SlingObjectInjector());
+        
         SlingBindings bindings = new SlingBindings();
         bindings.put(SlingBindings.SLING, this.scriptHelper);
+        
when(this.resource.getResourceResolver()).thenReturn(this.resourceResolver);
         
when(this.request.getResourceResolver()).thenReturn(this.resourceResolver);
         when(this.request.getResource()).thenReturn(this.resource);
         
when(this.request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
         when(this.scriptHelper.getResponse()).thenReturn(this.response);
     }
+    
+    protected Object adaptable() {
+        return this.request;
+    }
 
     @Test
     public void testResourceResolver() {
-        Object result = this.injector.getValue(this.request, null, 
ResourceResolver.class, this.annotatedElement, null);
+        Object result = this.injector.getValue(adaptable(), null, 
ResourceResolver.class, this.annotatedElement, null);
         assertSame(this.resourceResolver, result);
     }
 
     @Test
     public void testResource() {
-        Object result = this.injector.getValue(this.request, null, 
Resource.class, this.annotatedElement, null);
+        Object result = this.injector.getValue(adaptable(), null, 
Resource.class, this.annotatedElement, null);
         assertNull(result);
 
         
when(annotatedElement.isAnnotationPresent(SlingObject.class)).thenReturn(true);
-        result = this.injector.getValue(this.request, null, Resource.class, 
this.annotatedElement, null);
+        result = this.injector.getValue(adaptable(), null, Resource.class, 
this.annotatedElement, null);
         assertSame(resource, result);
     }
 
     @Test
     public void testRequest() {
-        Object result = this.injector.getValue(this.request, null, 
SlingHttpServletRequest.class,
+        Object result = this.injector.getValue(adaptable(), null, 
SlingHttpServletRequest.class,
                 this.annotatedElement, null);
         assertSame(this.request, result);
 
-        result = this.injector.getValue(this.request, null, 
HttpServletRequest.class, this.annotatedElement, null);
+        result = this.injector.getValue(adaptable(), null, 
HttpServletRequest.class, this.annotatedElement, null);
         assertSame(this.request, result);
     }
 
     @Test
     public void testResponse() {
-        Object result = this.injector.getValue(this.request, null, 
SlingHttpServletResponse.class,
-                this.annotatedElement, null);
+        Object result = this.injector.getValue(adaptable(), null, 
SlingHttpServletResponse.class, this.annotatedElement, null);
         assertSame(this.response, result);
 
-        result = this.injector.getValue(this.request, null, 
HttpServletResponse.class, this.annotatedElement, null);
+        result = this.injector.getValue(adaptable(), null, 
HttpServletResponse.class, this.annotatedElement, null);
         assertSame(this.response, result);
     }
 
     @Test
     public void testScriptHelper() {
-        Object result = this.injector
-                .getValue(this.request, null, SlingScriptHelper.class, 
this.annotatedElement, null);
+        Object result = this.injector.getValue(adaptable(), null, 
SlingScriptHelper.class, this.annotatedElement, null);
         assertSame(this.scriptHelper, result);
     }
 
     @Test
     public void testInvalid() {
-        Object result = this.injector.getValue(this, null, 
SlingScriptHelper.class, this.annotatedElement, null);
+        Object result = this.injector.getValue(new StringBuffer(), null, 
SlingScriptHelper.class, this.annotatedElement, null);
         assertNull(result);
     }
 

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,49 @@
+/*
+ * 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.models.impl.injectors;
+
+import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SlingObjectInjectorRequest_RequestContextTest extends 
SlingObjectInjectorRequestTest {
+
+    @Before
+    public void setupRequestContext() {
+      when(this.requestContext.getThreadRequest()).thenReturn(this.request);
+    }
+
+    @Override
+    protected Object adaptable() {
+      return this.request;
+    }
+    
+    @Test
+    public void testInvalid() {
+        Object result = this.injector.getValue(new StringBuffer(), null, 
SlingScriptHelper.class, this.annotatedElement, null);
+        assertSame(this.scriptHelper, result);
+    }
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorRequest_RequestContextTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,49 @@
+/*
+ * 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.models.impl.injectors;
+
+import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SlingObjectInjectorResourceResolver_RequestContextTest extends 
SlingObjectInjectorRequestTest {
+
+    @Before
+    public void setupRequestContext() {
+      when(this.requestContext.getThreadRequest()).thenReturn(this.request);
+    }
+
+    @Override
+    protected Object adaptable() {
+      return this.resourceResolver;
+    }
+    
+    @Test
+    public void testInvalid() {
+        Object result = this.injector.getValue(new StringBuffer(), null, 
SlingScriptHelper.class, this.annotatedElement, null);
+        assertSame(this.scriptHelper, result);
+    }
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResourceResolver_RequestContextTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java?rev=1633409&view=auto
==============================================================================
--- 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
 (added)
+++ 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
 Tue Oct 21 16:12:24 2014
@@ -0,0 +1,49 @@
+/*
+ * 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.models.impl.injectors;
+
+import static org.junit.Assert.assertSame;
+import static org.mockito.Mockito.when;
+
+import org.apache.sling.api.scripting.SlingScriptHelper;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class SlingObjectInjectorResource_RequestContextTest extends 
SlingObjectInjectorRequestTest {
+
+    @Before
+    public void setupRequestContext() {
+      when(this.requestContext.getThreadRequest()).thenReturn(this.request);
+    }
+
+    @Override
+    protected Object adaptable() {
+      return this.resource;
+    }
+        
+    @Test
+    public void testInvalid() {
+        Object result = this.injector.getValue(new StringBuffer(), null, 
SlingScriptHelper.class, this.annotatedElement, null);
+        assertSame(this.scriptHelper, result);
+    }
+
+}

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
------------------------------------------------------------------------------
--- svn:keywords (added)
+++ svn:keywords Tue Oct 21 16:12:24 2014
@@ -0,0 +1 @@
+LastChangedDate LastChangedRevision LastChangedBy HeadURL Id Author

Propchange: 
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/injectors/SlingObjectInjectorResource_RequestContextTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to