CookiePathComparator implements Serializable, Comparator<Cookie> { public int compare(final Cookie c1, final Cookie c2) { String path1 = normalizePath(c1); String path2 = normalizePath(c2); if (path1.equals(path2)) { return 0; } else if (path1.startsWith(path2)) { return -1; } else if (path2.startsWith(path1)) { return 1; } else { return 0; } }  int compare(final Cookie c1, final Cookie c2);  }
@Test public void testUnequality1() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath("/a/b/"); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/a/"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) < 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) > 0); }
@Test public void testUnequality2() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath("/a/b"); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/a"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) < 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) > 0); }
@Test public void testEquality1() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath("/a"); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/a"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0); }
@Test public void testEquality2() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath("/a/"); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/a"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0); }
@Test public void testEquality3() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath(null); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0); }
@Test public void testEquality4() { BasicClientCookie cookie1 = new BasicClientCookie("name1", "value"); cookie1.setPath("/this"); BasicClientCookie cookie2 = new BasicClientCookie("name1", "value"); cookie2.setPath("/that"); Comparator<Cookie> comparator = new CookiePathComparator(); Assert.assertTrue(comparator.compare(cookie1, cookie2) == 0); Assert.assertTrue(comparator.compare(cookie2, cookie1) == 0); }