### Question:
GroupManager { public Group getGroup(JID jid) throws GroupNotFoundException { JID groupJID = GroupJID.fromJID(jid); return (groupJID instanceof GroupJID) ? getGroup(((GroupJID)groupJID).getGroupName()) : null; } private  GroupManager(); static GroupManager getInstance(); Group createGroup(String name); Group getGroup(JID jid); Group getGroup(String name); Group getGroup(String name, boolean forceLookup); void deleteGroup(Group group); void deleteUser(User user); int getGroupCount(); Collection<Group> getGroups(); Collection<Group> getSharedGroups(); Collection<Group> getSharedGroups(String userName); Collection<Group> getVisibleGroups(Group groupToCheck); Collection<Group> getPublicSharedGroups(); Collection<Group> search(String propName, String propValue); Collection<Group> getGroups(int startIndex, int numResults); Collection<Group> getGroups(User user); Collection<Group> getGroups(JID user); boolean isReadOnly(); boolean isSearchSupported(); Collection<Group> search(String query); Collection<Group> search(String query, int startIndex, int numResults); GroupProvider getProvider(); static final SystemProperty<Class> GROUP_PROVIDER; }### Answer:
@Test public void willUseACacheHit() throws Exception { groupCache.put(GROUP_NAME, CacheableOptional.of(cachedGroup)); final Group returnedGroup = groupManager.getGroup(GROUP_NAME, false); assertThat(returnedGroup, is(cachedGroup)); verifyNoMoreInteractions(groupProvider); }
@Test public void willUseACacheMiss() { groupCache.put(GROUP_NAME, CacheableOptional.of(null)); try { groupManager.getGroup(GROUP_NAME, false); } catch (final GroupNotFoundException ignored) { verifyNoMoreInteractions(groupProvider); return; } fail(); }
@Test public void willCacheAHitIfNotAlreadyCached() throws Exception { doReturn(unCachedGroup).when(groupProvider).getGroup(GROUP_NAME); final Group returnedGroup = groupManager.getGroup(GROUP_NAME, false); assertThat(returnedGroup, is(unCachedGroup)); verify(groupProvider).getGroup(GROUP_NAME); assertThat(groupCache.get(GROUP_NAME), is(CacheableOptional.of(unCachedGroup))); }
@Test public void willCacheAMissIfNotAlreadyCached() throws Exception { doThrow(new GroupNotFoundException()).when(groupProvider).getGroup(GROUP_NAME); try { groupManager.getGroup(GROUP_NAME, false); } catch (final GroupNotFoundException ignored) { verify(groupProvider).getGroup(GROUP_NAME); assertThat(groupCache.get(GROUP_NAME), is(CacheableOptional.of(null))); return; } fail(); }
@Test public void aForceLookupHitWillIgnoreTheExistingCache() throws Exception { groupCache.put(GROUP_NAME, CacheableOptional.of(cachedGroup)); doReturn(unCachedGroup).when(groupProvider).getGroup(GROUP_NAME); final Group returnedGroup = groupManager.getGroup(GROUP_NAME, true); assertThat(returnedGroup, is(unCachedGroup)); verify(groupProvider).getGroup(GROUP_NAME); assertThat(groupCache.get(GROUP_NAME), is(CacheableOptional.of(unCachedGroup))); }
@Test public void aForceLookupMissWillIgnoreTheExistingCache() throws Exception { groupCache.put(GROUP_NAME, CacheableOptional.of(cachedGroup)); doThrow(new GroupNotFoundException()).when(groupProvider).getGroup(GROUP_NAME); try { groupManager.getGroup(GROUP_NAME, true); } catch (final GroupNotFoundException ignored) { verify(groupProvider).getGroup(GROUP_NAME); assertThat(groupCache.get(GROUP_NAME), is(CacheableOptional.of(null))); return; } fail(); }