PolicyComparator implements Comparator<JackrabbitAccessControlPolicy> { @Override public int compare(JackrabbitAccessControlPolicy policy1, JackrabbitAccessControlPolicy policy2) { if (policy1.equals(policy2)) { return 0; } else { String p1 = policy1.getPath(); String p2 = policy2.getPath(); if (p1 == null) { return -1; } else if (p2 == null) { return 1; } else { int depth1 = PathUtils.getDepth(p1); int depth2 = PathUtils.getDepth(p2); return (depth1 == depth2) ? p1.compareTo(p2) : Ints.compare(depth1, depth2); } } }  @Override int compare(JackrabbitAccessControlPolicy policy1, JackrabbitAccessControlPolicy policy2);  }
@Test public void testSame() { JackrabbitAccessControlPolicy policy = mock(JackrabbitAccessControlPolicy.class); assertEquals(0, comparator.compare(policy, policy)); }
@Test public void testEquals() { JackrabbitAccessControlPolicy policy1 = new JackrabbitAccessControlPolicy() { @Override public String getPath() { throw new UnsupportedOperationException(); } @Override public boolean equals(Object object) { return object instanceof JackrabbitAccessControlPolicy; } }; JackrabbitAccessControlPolicy policy2 = () -> { throw new UnsupportedOperationException(); }; assertEquals(0, comparator.compare(policy1, policy2)); }
@Test public void testNullPath1() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn(null).getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path").getMock(); assertEquals(-1, comparator.compare(policy1, policy2)); }
@Test public void testNullPath2() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path").getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn(null).getMock(); assertEquals(1, comparator.compare(policy1, policy2)); }
@Test public void testEqualPath() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path").getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path").getMock(); assertEquals(0, comparator.compare(policy1, policy2)); }
@Test public void testEqualDepth() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path1").getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/path2").getMock(); int expected = "/some/path1".compareTo("/some/path2"); assertEquals(expected, comparator.compare(policy1, policy2)); }
@Test public void testPath1Deeper() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/some/deeper/path").getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/").getMock(); int expected = Ints.compare(PathUtils.getDepth("/some/deeper/path"), PathUtils.getDepth("/")); assertEquals(expected, comparator.compare(policy1, policy2)); }
@Test public void testPath2Deeper() { JackrabbitAccessControlPolicy policy1 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/path").getMock(); JackrabbitAccessControlPolicy policy2 = when(mock(JackrabbitAccessControlPolicy.class).getPath()).thenReturn("/a/deeper/path").getMock(); int expected = Ints.compare(PathUtils.getDepth("/path"), PathUtils.getDepth("/a/deeper/path")); assertEquals(expected, comparator.compare(policy1, policy2)); }