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:55:56 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes c9e85e762 -> 02a7533c9


[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/02a7533c
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/02a7533c
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/02a7533c

Branch: refs/heads/3.0.x-fixes
Commit: 02a7533c91c1e3a0ecc1366b136c2ee6e40ebf52
Parents: c9e85e7
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:53:23 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/02a7533c/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 e86f93b..e38bdb4 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
@@ -959,11 +959,11 @@ public abstract 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;
@@ -989,9 +989,9 @@ public abstract 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/02a7533c/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 b142814..1268538 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
@@ -817,4 +817,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