### Question:
CacheGroupsHandler implements InvalidationHandler { @Override public Function<Persistent, Collection<CacheGroupDescriptor>> canHandle(Class<? extends Persistent> type) { CacheGroup multipleCacheGroups = type.getAnnotation(CacheGroup.class); CacheGroups cacheGroups = type.getAnnotation(CacheGroups.class); if (cacheGroups == null && multipleCacheGroups == null) { return null; } final Collection<CacheGroupDescriptor> groupsList = new ArrayList<>(); extractCacheGroups(cacheGroups, groupsList); extractCacheGroups(multipleCacheGroups, groupsList); return p -> groupsList; } CacheGroupsHandler(); @Override Function<Persistent, Collection<CacheGroupDescriptor>> canHandle(Class<? extends Persistent> type);  }### Answer:
@Test public void canHandleE1() throws Exception { CacheGroupsHandler handler = new CacheGroupsHandler(); Function<Persistent, Collection<CacheGroupDescriptor>> function = handler.canHandle(E1.class); Collection<CacheGroupDescriptor> result = function.apply(null); assertEquals(2, result.size()); String[] names = {"g1", "g2"}; Collection<String> extractedNames = new ArrayList<>(); for(CacheGroupDescriptor descriptor : result) { extractedNames.add(descriptor.getCacheGroupName()); } assertArrayEquals(names, extractedNames.toArray()); }
@Test public void canHandleE2() throws Exception { CacheGroupsHandler handler = new CacheGroupsHandler(); Function<Persistent, Collection<CacheGroupDescriptor>> function = handler.canHandle(E2.class); Collection<CacheGroupDescriptor> result = function.apply(null); assertEquals(6, result.size()); String[] names = {"g1", "g2", "g3", "g4", "g5", "g6"}; Collection<String> extractedNames = new ArrayList<>(); for(CacheGroupDescriptor descriptor : result) { extractedNames.add(descriptor.getCacheGroupName()); } assertArrayEquals(names, extractedNames.toArray()); }