You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2018/06/26 21:04:00 UTC

[GitHub] coheigea closed pull request #425: CXF-7765 - URITemplate.compareTemplates returns inconsistent results

coheigea closed pull request #425: CXF-7765 - URITemplate.compareTemplates returns inconsistent results
URL: https://github.com/apache/cxf/pull/425
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
index b5059f3ddca..b9f0232d1a9 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java
@@ -377,28 +377,24 @@ public static URITemplate createExactTemplate(String pathValue) {
     }
     
     public static int compareTemplates(URITemplate t1, URITemplate t2) {
-        String l1 = t1.getLiteralChars();
-        String l2 = t2.getLiteralChars();
-        if (!l1.equals(l2)) {
-            // descending order
-            return l1.length() < l2.length() ? 1 : -1;
-        }
-
-        int g1 = t1.getVariables().size();
-        int g2 = t2.getVariables().size();
+        int l1 = t1.getLiteralChars().length();
+        int l2 = t2.getLiteralChars().length();
         // descending order
-        int result = g1 < g2 ? 1 : g1 > g2 ? -1 : 0;
+        int result = l1 < l2 ? 1 : l1 > l2 ? -1 : 0;
         if (result == 0) {
-            int gCustom1 = t1.getCustomVariables().size();
-            int gCustom2 = t2.getCustomVariables().size();
-            if (gCustom1 != gCustom2) {
-                // descending order
-                return gCustom1 < gCustom2 ? 1 : -1;
+            int g1 = t1.getVariables().size();
+            int g2 = t2.getVariables().size();
+            // descending order
+            result = g1 < g2 ? 1 : g1 > g2 ? -1 : 0;
+            if (result == 0) {
+                int gCustom1 = t1.getCustomVariables().size();
+                int gCustom2 = t2.getCustomVariables().size();
+                result = gCustom1 < gCustom2 ? 1 : gCustom1 > gCustom2 ? -1 : 0;
+                if (result == 0) {
+                    result = t1.getPatternValue().compareTo(t2.getPatternValue());
+                }
             }
         }
-        if (result == 0) {
-            result = t1.getPatternValue().compareTo(t2.getPatternValue());
-        }
 
         return result;
     }
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
index 97e53637479..7d2ac299ba0 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/URITemplateTest.java
@@ -740,4 +740,17 @@ public void testEncodeLiteralCharactersNotVariable() {
         //System.out.println(ut.encodeLiteralCharacters());
         assertEquals("a%20{digit:[0-9]}%20b", ut.encodeLiteralCharacters(false));
     }
+
+    @Test
+    public void testCompareNumberOfLiteralCharacters() {
+        URITemplate t1 = new URITemplate("/foo");
+        URITemplate t2 = new URITemplate("/bar");
+        URITemplate t3 = new URITemplate("/foo/bar");
+        assertEquals(0, URITemplate.compareTemplates(t1, t1));
+        assertTrue(URITemplate.compareTemplates(t1, t3) > 0);
+        assertTrue(URITemplate.compareTemplates(t3, t1) < 0);
+        assertEquals(Integer.signum(URITemplate.compareTemplates(t1, t2)),
+                -Integer.signum(URITemplate.compareTemplates(t2, t1)));
+    }
+
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services