This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.0.6 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 5d87969fb62eddb8f3f4a992e4a9f244e0a72d19 Author: Justin Edelson <[email protected]> AuthorDate: Wed Jun 25 15:38:50 2014 +0000 SLING-3700 - adding a ResourceResolver injector git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1605451 13f79535-47bb-0310-9956-ffa450edef68 --- .../impl/injectors/ResourceResolverInjector.java | 58 ++++++++++++++++++++ .../injectors/ResourceResolverInjectorTest.java | 62 ++++++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/src/main/java/org/apache/sling/models/impl/injectors/ResourceResolverInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/ResourceResolverInjector.java new file mode 100644 index 0000000..a84bd89 --- /dev/null +++ b/src/main/java/org/apache/sling/models/impl/injectors/ResourceResolverInjector.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.models.impl.injectors; + +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Type; + +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.Service; +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.Resource; +import org.apache.sling.models.spi.DisposalCallbackRegistry; +import org.apache.sling.models.spi.Injector; +import org.osgi.framework.Constants; + +@Component +@Service +@Property(name = Constants.SERVICE_RANKING, intValue = 900) +public class ResourceResolverInjector implements Injector { + + private static final String NAME_RESOURCE_RESOLVER = "resourceResolver"; + + @Override + public String getName() { + return "resource-resolver"; + } + + @Override + public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element, + DisposalCallbackRegistry callbackRegistry) { + if (NAME_RESOURCE_RESOLVER.equals(name)) { + if (adaptable instanceof Resource) { + return ((Resource) adaptable).getResourceResolver(); + } else if (adaptable instanceof SlingHttpServletRequest) { + return ((SlingHttpServletRequest) adaptable).getResourceResolver(); + } + } + return null; + } + +} diff --git a/src/test/java/org/apache/sling/models/impl/injectors/ResourceResolverInjectorTest.java b/src/test/java/org/apache/sling/models/impl/injectors/ResourceResolverInjectorTest.java new file mode 100644 index 0000000..58e82bd --- /dev/null +++ b/src/test/java/org/apache/sling/models/impl/injectors/ResourceResolverInjectorTest.java @@ -0,0 +1,62 @@ +/* + * 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 static org.mockito.Mockito.*; + +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.junit.Test; + +public class ResourceResolverInjectorTest { + + private ResourceResolverInjector injector = new ResourceResolverInjector(); + + @Test + public void testFromResource() { + Resource resource = mock(Resource.class); + ResourceResolver resourceResolver = mock(ResourceResolver.class); + when(resource.getResourceResolver()).thenReturn(resourceResolver); + + Object result = injector.getValue(resource, "resourceResolver", null, null, null); + assertEquals(resourceResolver, result); + } + + @Test + public void testFromRequest() { + SlingHttpServletRequest request = mock(SlingHttpServletRequest.class); + ResourceResolver resourceResolver = mock(ResourceResolver.class); + when(request.getResourceResolver()).thenReturn(resourceResolver); + + Object result = injector.getValue(request, "resourceResolver", null, null, null); + assertEquals(resourceResolver, result); + } + + @Test + public void testFromSomethingElse() { + SlingHttpServletResponse response = mock(SlingHttpServletResponse.class); + + Object result = injector.getValue(response, "resourceResolver", null, null, null); + assertNull(result); + } + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
