cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: [CXF-7124] Better support for finding a shared package, patch from Neal Hu applied
Date Fri, 04 Nov 2016 12:39:09 GMT
Repository: cxf
Updated Branches:
  refs/heads/3.0.x-fixes 99f7aacbb -> 01c0f802b


[CXF-7124] Better support for finding a shared package, patch from Neal Hu applied


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

Branch: refs/heads/3.0.x-fixes
Commit: 01c0f802bed165359edf68489552061f601ed95e
Parents: 99f7aac
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Nov 4 12:22:47 2016 +0000
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Nov 4 12:38:48 2016 +0000

----------------------------------------------------------------------
 .../apache/cxf/common/util/PackageUtils.java    | 30 +++++++++++
 .../cxf/common/util/PackageUtilsTest.java       | 57 ++++++++++++++++++++
 2 files changed, 87 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/01c0f802/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
index ada4e1b..9ff5510 100644
--- a/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
+++ b/core/src/main/java/org/apache/cxf/common/util/PackageUtils.java
@@ -49,6 +49,36 @@ public final class PackageUtils {
         return getPackageName(className);
     }
     
+    public static String getSharedPackageName(List<Class<?>> classes) {
+        if (classes.isEmpty()) {
+            return "";
+        }
+        List<List<String>> lParts = new  ArrayList<List<String>>(classes.size());
+        List<String> currentParts = new ArrayList<String>();
+        for (Class<?> cls : classes) {
+            lParts.add(StringUtils.getParts(getPackageName(cls), "\\."));
+        }
+        for (int i = 0; i < lParts.get(0).size(); i++) {
+            int j = 1;
+            for (; j < lParts.size(); j++) {
+                if (i > (lParts.get(j).size() - 1) || !lParts.get(j).get(i).equals(lParts.get(0).get(i)))
{
+                    break;
+                }  
+            }
+            if (j == lParts.size()) {
+                currentParts.add(lParts.get(j - 1).get(i));
+            }
+        }
+        StringBuilder sb = new StringBuilder();
+        for (String part : currentParts) {
+            if (sb.length() > 0) {
+                sb.append(".");
+            }
+            sb.append(part);
+        }
+        return sb.toString();
+    }
+    
     public static String parsePackageName(String namespace, String defaultPackageName) {
         String packageName = (defaultPackageName != null && defaultPackageName.trim().length()
> 0)
             ? defaultPackageName : null;

http://git-wip-us.apache.org/repos/asf/cxf/blob/01c0f802/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
index f8d49a0..9fb3d49 100644
--- a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
+++ b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java
@@ -19,6 +19,10 @@
 
 package org.apache.cxf.common.util;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.Collections;
 
 import org.junit.Assert;
 import org.junit.Test;
@@ -35,4 +39,57 @@ public class PackageUtilsTest extends Assert {
         String className = "HelloWorld";
         assertEquals("Should return empty string", "", PackageUtils.getPackageName(className));
     }
+
+    @Test
+    public void testSharedPackageNameSingleClass() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Collections.<Class<?>>singletonList(this.getClass()));       
+        assertEquals(this.getClass().getPackage().getName(), packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesInSamePackage() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.<Class<?>>asList(Integer.class, Number.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesInDiffPackages() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Integer.class, this.getClass()));       
+        assertEquals("", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Integer.class, Annotation.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot2() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Annotation.class, Integer.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot3() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(Annotation.class, Array.class));       
+        assertEquals("java.lang", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot4() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(org.apache.cxf.common.util.PackageUtils.class, 
+                    org.apache.cxf.bus.CXFBusFactory.class,
+                    org.apache.cxf.common.jaxb.JAXBContextCache.class));       
+        assertEquals("org.apache.cxf", packageName);
+    }
+    @Test
+    public void testSharedPackageNameManyClassesCommonRoot5() throws Exception {
+        String packageName = PackageUtils.getSharedPackageName(
+            Arrays.asList(java.lang.annotation.Annotation.class, 
+                    org.apache.cxf.bus.CXFBusFactory.class,
+                    org.apache.cxf.common.jaxb.JAXBContextCache.class));       
+        assertEquals("", packageName);
+    }
 }


Mime
View raw message