SLING-5555 Make error message more explicit when a @Source annotation contains an...
authorStefan Seifert <sseifert@apache.org>
Wed, 24 Feb 2016 10:03:48 +0000 (10:03 +0000)
committerStefan Seifert <sseifert@apache.org>
Wed, 24 Feb 2016 10:03:48 +0000 (10:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1732052 13f79535-47bb-0310-9956-ffa450edef68

src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java

index 37d870b..657eb95 100644 (file)
@@ -420,6 +420,9 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable, ModelFacto
             }
             else {
                 injectorsToProcess = injectors.get(source);
+                if (injectorsToProcess == null) {
+                    throw new IllegalArgumentException("No Sling Models Injector registered for source '" + source + "'.");
+                }
             }
             
             // find the right injector
index 5936176..9e1b57b 100644 (file)
  */
 package org.apache.sling.models.impl;
 
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.mockito.Mockito.when;
 
-import java.lang.reflect.AnnotatedElement;
 import java.util.Hashtable;
 
 import javax.inject.Inject;
@@ -28,9 +29,9 @@ import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.scripting.SlingBindings;
 import org.apache.sling.models.annotations.Model;
 import org.apache.sling.models.annotations.Source;
+import org.apache.sling.models.factory.ModelClassException;
 import org.apache.sling.models.impl.injectors.BindingsInjector;
 import org.apache.sling.models.impl.injectors.RequestAttributeInjector;
-import org.apache.sling.models.spi.DisposalCallbackRegistry;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -77,7 +78,8 @@ public class MultipleInjectorTest {
         factory.bindStaticInjectAnnotationProcessorFactory(bindingsInjector, new ServicePropertiesMap(1, 1));
 
         when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings);
-        factory.adapterImplementations.addClassesAsAdapterAndImplementation(ForTwoInjectorsWithSource.class, ForTwoInjectors.class);
+        factory.adapterImplementations.addClassesAsAdapterAndImplementation(
+                ForTwoInjectorsWithSource.class, ForTwoInjectors.class, ForTwoInjectorsWithInvalidSource.class);
     }
 
     @Test
@@ -108,6 +110,17 @@ public class MultipleInjectorTest {
         assertEquals(obj.firstAttribute, attributeValue);
     }
 
+    @Test
+    public void testInjectorWithInvalidSource() {
+        ForTwoInjectorsWithInvalidSource obj = factory.getAdapter(request, ForTwoInjectorsWithInvalidSource.class);
+        assertNull(obj);
+    }
+
+    @Test(expected=ModelClassException.class)
+    public void testInjectorWithInvalidSourceWithException() {
+        factory.createModel(request, ForTwoInjectorsWithInvalidSource.class);
+    }
+
     @Model(adaptables = SlingHttpServletRequest.class)
     public static class ForTwoInjectors {
 
@@ -125,4 +138,13 @@ public class MultipleInjectorTest {
 
     }
 
+    @Model(adaptables = SlingHttpServletRequest.class)
+    public static class ForTwoInjectorsWithInvalidSource {
+
+        @Inject
+        @Source("this-is-an-invalid-source")
+        private String firstAttribute;
+
+    }
+
 }