Author: hlship
Date: Fri Jan  2 09:39:37 2009
New Revision: 730799

URL: http://svn.apache.org/viewvc?rev=730799&view=rev
Log:
TAP5-341: When a contribute method requests the wrong configuration interface 
(say, Configuration instead of OrderedConfiguration) the error message is 
confusing: "No service implements the Configuration interface"

Added:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/WrongConfigurationTypeGuard.java
Modified:
    
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ContributionDefImpl.java
    
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ContributionDefImplTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ContributionDefImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ContributionDefImpl.java?rev=730799&r1=730798&r2=730799&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ContributionDefImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/ContributionDefImpl.java
 Fri Jan  2 09:39:37 2009
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,10 +16,7 @@
 
 import org.apache.tapestry5.ioc.*;
 import org.apache.tapestry5.ioc.def.ContributionDef;
-import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
-import org.apache.tapestry5.ioc.internal.util.InjectionResources;
-import org.apache.tapestry5.ioc.internal.util.InternalUtils;
-import org.apache.tapestry5.ioc.internal.util.MapInjectionResources;
+import org.apache.tapestry5.ioc.internal.util.*;
 import org.apache.tapestry5.ioc.services.ClassFactory;
 
 import java.lang.reflect.InvocationTargetException;
@@ -35,6 +32,9 @@
 
     private final ClassFactory classFactory;
 
+    private static final Class[] CONFIGURATION_TYPES = new Class[] 
{Configuration.class, MappedConfiguration.class,
+            OrderedConfiguration.class};
+
     public ContributionDefImpl(String serviceId, Method contributorMethod, 
ClassFactory classFactory)
     {
         this.serviceId = serviceId;
@@ -82,6 +82,19 @@
 
         InjectionResources injectionResources = new 
MapInjectionResources(resourceMap);
 
+        // For each of the other configuration types that is not expected, add 
a guard.
+
+        for (Class t : CONFIGURATION_TYPES)
+        {
+            if (parameterType != t)
+            {
+                injectionResources = new DelegatingInjectionResources(
+                        new 
WrongConfigurationTypeGuard(resources.getServiceId(), t, parameterType),
+                        injectionResources);
+            }
+        }
+
+
         Throwable fail = null;
 
         Object moduleBuilder = InternalUtils.isStatic(contributorMethod) ? 
null : source

Added: 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/WrongConfigurationTypeGuard.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/WrongConfigurationTypeGuard.java?rev=730799&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/WrongConfigurationTypeGuard.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/main/java/org/apache/tapestry5/ioc/internal/util/WrongConfigurationTypeGuard.java
 Fri Jan  2 09:39:37 2009
@@ -0,0 +1,46 @@
+// Copyright 2009 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.ioc.internal.util;
+
+import java.lang.reflect.Type;
+
+/**
+ * Used when invoking a contribute method to guard against a request for the 
wrong type of configuration interface.
+ */
+public class WrongConfigurationTypeGuard implements InjectionResources
+{
+    private final String serviceId;
+
+    private final Class guardType;
+
+    private final Class expectedType;
+
+    public WrongConfigurationTypeGuard(String serviceId, Class guardType, 
Class expectedType)
+    {
+        this.serviceId = serviceId;
+        this.guardType = guardType;
+        this.expectedType = expectedType;
+    }
+
+    public <T> T findResource(Class<T> type, Type genericType)
+    {
+        if (type == guardType)
+            throw new IllegalArgumentException(String.format("Service '%s' is 
configured using %s, not %s.",
+                                                             serviceId,
+                                                             
expectedType.getName(), guardType.getName()));
+
+        return null;
+    }
+}

Modified: 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ContributionDefImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ContributionDefImplTest.java?rev=730799&r1=730798&r2=730799&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ContributionDefImplTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-ioc/src/test/java/org/apache/tapestry5/ioc/internal/ContributionDefImplTest.java
 Fri Jan  2 09:39:37 2009
@@ -1,4 +1,4 @@
-// Copyright 2006, 2007, 2008 The Apache Software Foundation
+// Copyright 2006, 2007, 2008, 2009 The Apache Software Foundation
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -18,8 +18,6 @@
 import org.apache.tapestry5.ioc.annotations.InjectService;
 import org.apache.tapestry5.ioc.def.ContributionDef;
 import org.apache.tapestry5.ioc.test.IOCTestCase;
-import static org.easymock.EasyMock.eq;
-import static org.easymock.EasyMock.isA;
 import org.slf4j.Logger;
 import org.testng.annotations.Test;
 
@@ -46,6 +44,7 @@
         Logger logger = mockLogger();
 
         train_getLogger(serviceResources, logger);
+        train_getServiceId(serviceResources, "Bif");
 
         configuration.add(toContribute);
 
@@ -70,6 +69,7 @@
 
         train_getLogger(resources, logger);
         train_getService(resources, "zip.Zap", UpcaseService.class, service);
+        train_getServiceId(resources, "Bif");
 
         configuration.add(service);
 
@@ -91,11 +91,7 @@
         Logger logger = mockLogger();
 
         train_getLogger(resources, logger);
-
-        Throwable t = new RuntimeException("Missing service.");
-
-        expect(resources.getObject(eq(MappedConfiguration.class), 
isA(AnnotationProvider.class)))
-                .andThrow(t);
+        train_getServiceId(resources, "Bif");
 
         replay();
 
@@ -109,9 +105,10 @@
         }
         catch (RuntimeException ex)
         {
-            assertEquals(ex.getMessage(), "Error invoking service contribution 
method "
-                    + getClass().getName()
-                    + 
".contributeUnorderedWrongParameter(MappedConfiguration): Missing service.");
+            assertMessageContains(ex,
+                                  "Error invoking service contribution method 
org.apache.tapestry5.ioc.internal.ContributionDefImplTest.contributeUnorderedWrongParameter(MappedConfiguration)",
+                                  "Service 'Bif' is configured using 
org.apache.tapestry5.ioc.Configuration, not 
org.apache.tapestry5.ioc.MappedConfiguration."
+            );
         }
 
         verify();
@@ -132,6 +129,7 @@
         train_getLogger(resources, logger);
 
         train_getService(resources, "zip.Zap", UpcaseService.class, service);
+        train_getServiceId(resources, "Bif");
 
         configuration.add("fred", service);
 
@@ -157,6 +155,7 @@
         train_getLogger(resources, logger);
 
         train_getService(resources, "zip.Zap", UpcaseService.class, service);
+        train_getServiceId(resources, "Bif");
 
         configuration.add("upcase", service);
 


Reply via email to