SLING-6846 Sling Models Impl: Avoid exceptions when resource has no resource type
authorStefan Seifert <sseifert@apache.org>
Tue, 9 May 2017 13:01:37 +0000 (13:01 +0000)
committerStefan Seifert <sseifert@apache.org>
Tue, 9 May 2017 13:01:37 +0000 (13:01 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1794571 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/AdapterImplementations.java
src/test/java/org/apache/sling/models/impl/AdapterImplementationsTest.java

index 65d9210..5a1f65b 100644 (file)
@@ -324,6 +324,9 @@ final class AdapterImplementations {
     }
 
     private static Class<?> getClassFromResourceTypeMap(final String resourceType, final Map<String, Class<?>> map, final ResourceResolver resolver) {
+        if (resourceType == null) {
+            return null;
+        }
         Class<?> modelClass = map.get(resourceType);
         if (modelClass == null) {
             for (String searchPath : resolver.getSearchPath()) {
index 434316f..10c761c 100644 (file)
@@ -244,6 +244,16 @@ public class AdapterImplementationsTest {
         assertNull(underTest.getModelClassForResource(resource));
     }
     
+    @Test
+    public void testResourceTypeRegistrationForResourceWithoutResourceType() {
+        when(resource.getResourceType()).thenReturn(null);
+        when(resource.getResourceResolver()).thenReturn(resourceResolver);
+        when(resourceResolver.getSearchPath()).thenReturn(new String[] { "/apps/", "/libs/" });
+
+        // ensure we don't have any registrations and no exception is thrown
+        assertNull(underTest.getModelClassForResource(resource));
+    }
+    
     static final class NoneImplementationPicker implements ImplementationPicker {
         @Override
         public Class<?> pick(Class<?> adapterType, Class<?>[] implementationsTypes, Object adaptable) {