PermissionCacheBuilder { boolean init(@NotNull Set<String> principalNames, @NotNull CacheStrategy cacheStrategy) { existingNames = new HashSet<>(); long cnt = 0; for (String name : principalNames) { NumEntries ne = store.getNumEntries(name, cacheStrategy.maxSize()); long n = ne.size; if (n > 0) { existingNames.add(name); if (cacheStrategy.loadFully(n, cnt)) { peCache.getFullyLoadedEntries(store, name); } else { long expectedSize = (ne.isExact) ? n : Long.MAX_VALUE; peCache.init(name, expectedSize); } } if (cnt < Long.MAX_VALUE) { if (Long.MAX_VALUE == n) { cnt = Long.MAX_VALUE; } else { cnt = LongUtils.safeAdd(cnt, n); } } } usePathEntryMap = cacheStrategy.usePathEntryMap(cnt); initialized = true; return existingNames.isEmpty(); } PermissionCacheBuilder(@NotNull PermissionStore store);   }
@Test public void testInitNumEntriesExceedMaxPathExact() { long maxPaths = 2; when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(maxPaths+1, true)); assertFalse(permissionCacheBuilder.init(ImmutableSet.of("a", "b", "c"), createStrategy(Long.MAX_VALUE, maxPaths, false))); verify(store, times(3)).getNumEntries(anyString(), anyLong()); verify(store, never()).load(anyString()); verify(store, never()).load(anyString(), anyString()); }
@Test public void testInitNumEntriesExceedMaxPathNotExact() { long maxPaths = 5; when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(maxPaths+1, false)); assertFalse(permissionCacheBuilder.init(ImmutableSet.of("a", "b", "c"), createStrategy(Long.MAX_VALUE, maxPaths, false))); verify(store, times(3)).getNumEntries(anyString(), anyLong()); verify(store, never()).load(anyString()); verify(store, never()).load(anyString(), anyString()); }
@Test public void testInitNumEntriesExceedsMaxLong() { when(store.getNumEntries(anyString(), anyLong())).thenReturn(NumEntries.valueOf(Long.MAX_VALUE, false)); assertFalse(permissionCacheBuilder.init(ImmutableSet.of("a", "b", "c"), createStrategy(Long.MAX_VALUE, 25, false))); verify(store, times(3)).getNumEntries(anyString(), anyLong()); verify(store, never()).load(anyString()); verify(store, never()).load(anyString(), anyString()); }