cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-6067] Fixing ProviderFactory code checking the generic types
Date Tue, 28 Oct 2014 16:58:06 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.7.x-fixes ad2a189a1 -> 3493406b8


[CXF-6067] Fixing ProviderFactory code checking the generic types


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/3493406b
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/3493406b
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/3493406b

Branch: refs/heads/2.7.x-fixes
Commit: 3493406b86d5f3f4382091d56791cb2fb53344a1
Parents: ad2a189
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Tue Oct 28 16:52:00 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Tue Oct 28 16:57:43 2014 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/provider/ProviderFactory.java     | 10 +--
 .../cxf/jaxrs/provider/ProviderFactoryTest.java | 76 ++++++++++++++++++++
 2 files changed, 81 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/3493406b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 664d0d3..ee91720 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -1274,11 +1274,11 @@ public final class ProviderFactory {
     protected static int compareClasses(Class<?> expectedCls, Object o1, Object o2)
{
         Class<?> cl1 = ClassHelper.getRealClass(o1); 
         Class<?> cl2 = ClassHelper.getRealClass(o2);
-        
         Type[] types1 = getGenericInterfaces(cl1, expectedCls);
         Type[] types2 = getGenericInterfaces(cl2, expectedCls);
-        
-        if (types1.length == 0 && types2.length > 0) {
+        if (types1.length == 0 && types2.length == 0) {
+            return 0;
+        } else if (types1.length == 0 && types2.length > 0) {
             return 1;
         } else if (types1.length > 0 && types2.length == 0) {
             return -1;
@@ -1304,9 +1304,9 @@ public final class ProviderFactory {
             Type genericSuperType = cls.getGenericSuperclass();
             if (genericSuperType instanceof ParameterizedType) {       
                 Class<?> actualType = InjectionUtils.getActualType(genericSuperType);
-                if (expectedClass == actualType) {
+                if (actualType != null && actualType.isAssignableFrom(expectedClass))
{
                     return new Type[]{genericSuperType};
-                } else if (actualType != null && expectedClass.isAssignableFrom(actualType))
{
+                } else if (expectedClass.isAssignableFrom(actualType)) {
                     return new Type[]{};    
                 }
             }

http://git-wip-us.apache.org/repos/asf/cxf/blob/3493406b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
index 1a5930b..1257570 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/ProviderFactoryTest.java
@@ -969,4 +969,80 @@ public class ProviderFactoryTest extends Assert {
             return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build();
         }
     }
+    
+    @Test
+    public void testBadCustomExceptionMappersHierarchyWithGenerics() throws Exception {
+        ServerProviderFactory pf = ServerProviderFactory.getInstance();
+        BadExceptionMapperA badExceptionMapperA = new BadExceptionMapperA();
+        pf.registerUserProvider(badExceptionMapperA);
+        BadExceptionMapperB badExceptionMapperB = new BadExceptionMapperB();
+        pf.registerUserProvider(badExceptionMapperB);
+        Object mapperResponse1 = pf.createExceptionMapper(RuntimeExceptionA.class, new MessageImpl());
+        assertSame(badExceptionMapperA, mapperResponse1);
+        Object mapperResponse2 = pf.createExceptionMapper(RuntimeExceptionB.class, new MessageImpl());
+        assertSame(badExceptionMapperB, mapperResponse2);
+        Object mapperResponse3 = pf.createExceptionMapper(RuntimeExceptionAA.class, new MessageImpl());
+        assertSame(badExceptionMapperA, mapperResponse3);
+        Object mapperResponse4 = pf.createExceptionMapper(RuntimeExceptionBB.class, new MessageImpl());
+        assertSame(badExceptionMapperB, mapperResponse4);
+    }
+
+    @Test
+    public void testGoodExceptionMappersHierarchyWithGenerics() throws Exception {
+        ServerProviderFactory pf = ServerProviderFactory.getInstance();
+        GoodRuntimeExceptionAMapper runtimeExceptionAMapper = new GoodRuntimeExceptionAMapper();
+        pf.registerUserProvider(runtimeExceptionAMapper);
+        GoodRuntimeExceptionBMapper runtimeExceptionBMapper = new GoodRuntimeExceptionBMapper();
+        pf.registerUserProvider(runtimeExceptionBMapper);
+        Object mapperResponse1 = pf.createExceptionMapper(RuntimeExceptionA.class, new MessageImpl());
+        assertSame(runtimeExceptionAMapper, mapperResponse1);
+        Object mapperResponse2 = pf.createExceptionMapper(RuntimeExceptionB.class, new MessageImpl());
+        assertSame(runtimeExceptionBMapper, mapperResponse2);
+        Object mapperResponse3 = pf.createExceptionMapper(RuntimeExceptionAA.class, new MessageImpl());
+        assertSame(runtimeExceptionAMapper, mapperResponse3);
+        Object mapperResponse4 = pf.createExceptionMapper(RuntimeExceptionBB.class, new MessageImpl());
+        assertSame(runtimeExceptionBMapper, mapperResponse4);
+    }
+    private static class RuntimeExceptionA extends RuntimeException {
+        private static final long serialVersionUID = 1L;
+    }
+    private static class RuntimeExceptionAA extends RuntimeExceptionA {
+        private static final long serialVersionUID = 1L;
+    }
+    private static class RuntimeExceptionB extends RuntimeException {
+        private static final long serialVersionUID = 1L;
+    }
+    private static class RuntimeExceptionBB extends RuntimeExceptionB {
+        private static final long serialVersionUID = 1L;
+    }
+    private static class GoodRuntimeExceptionAMapper implements ExceptionMapper<RuntimeExceptionA>
{
+
+        @Override
+        public Response toResponse(RuntimeExceptionA exception) {
+            return null;
+        }
+    }
+    private static class GoodRuntimeExceptionBMapper implements ExceptionMapper<RuntimeExceptionB>
{
+
+        @Override
+        public Response toResponse(RuntimeExceptionB exception) {
+            return null;
+        }
+    }
+    public abstract static class BadParentExceptionMapper<T extends Throwable> implements
ExceptionMapper<T> {
+    }
+    public static class BadExceptionMapperA extends BadParentExceptionMapper<RuntimeExceptionA>
{
+
+        @Override
+        public Response toResponse(RuntimeExceptionA exception) {
+            return null;
+        }
+    }
+    public static class BadExceptionMapperB extends BadParentExceptionMapper<RuntimeExceptionB>
{
+
+        @Override
+        public Response toResponse(RuntimeExceptionB exception) {
+            return null;
+        }
+    }
 }


Mime
View raw message