@Override
 public void stop() {
   Collection<FilteringListenerInvocation<?, ?>> invocations = filteringInvocations.values();
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryActivated.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryCreated.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryInvalidated.class)
       .removeAll(invocations);
   cacheNotifier.getListenerCollectionForAnnotation(CacheEntryLoaded.class).removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryModified.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryPassivated.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryRemoved.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryVisited.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntriesEvicted.class)
       .removeAll(invocations);
   cacheNotifier
       .getListenerCollectionForAnnotation(CacheEntryExpired.class)
       .removeAll(invocations);
   filteringInvocations.clear();
 }
 @BeforeMethod
 public void setUp() {
   n = new CacheNotifierImpl();
   mockCache = mock(Cache.class, RETURNS_DEEP_STUBS);
   Configuration config = mock(Configuration.class, RETURNS_DEEP_STUBS);
   when(mockCache.getAdvancedCache().getStatus()).thenReturn(ComponentStatus.INITIALIZING);
   Answer answer =
       new Answer<Object>() {
         @Override
         public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
           return Mockito.mock((Class) invocationOnMock.getArguments()[0]);
         }
       };
   when(mockCache.getAdvancedCache().getComponentRegistry().getComponent(any(Class.class)))
       .then(answer);
   when(mockCache
           .getAdvancedCache()
           .getComponentRegistry()
           .getComponent(any(Class.class), anyString()))
       .then(answer);
   n.injectDependencies(mockCache, cdl, null, config);
   cl = new PrimaryOwnerCacheListener();
   n.start();
   n.addListener(cl);
   ctx = new NonTxInvocationContext(AnyEquivalence.getInstance());
 }
 private void addFilteringInvocationForMatcher(Matcher matcher) {
   if (!filteringInvocations.containsKey(matcher)) {
     FilteringListenerInvocation filteringInvocation = new FilteringListenerInvocation(matcher);
     if (filteringInvocations.putIfAbsent(matcher, filteringInvocation) == null) {
       // todo these are added but never removed until stop is called
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryActivated.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryCreated.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryInvalidated.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryLoaded.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryModified.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryPassivated.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryRemoved.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryVisited.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntriesEvicted.class)
           .add(filteringInvocation);
       cacheNotifier
           .getListenerCollectionForAnnotation(CacheEntryExpired.class)
           .add(filteringInvocation);
     }
   }
 }
  public void testOwnership() {
    // Is not owner nor primary owner
    cdl.isOwner = false;
    cdl.isPrimaryOwner = false;
    n.notifyCacheEntryCreated("reject", null, true, ctx, null);
    n.notifyCacheEntryCreated("reject", "v1", false, ctx, null);

    assert !cl.isReceivedPost();
    assert !cl.isReceivedPre();
    assert cl.getInvocationCount() == 0;

    // Is an owner but not primary owner
    cdl.isOwner = true;
    cdl.isPrimaryOwner = false;
    n.notifyCacheEntryCreated("reject", null, true, ctx, null);
    n.notifyCacheEntryCreated("reject", "v1", false, ctx, null);

    assert !cl.isReceivedPost();
    assert !cl.isReceivedPre();
    assert cl.getInvocationCount() == 0;

    // Is primary owner
    cdl.isOwner = true;
    cdl.isPrimaryOwner = true;
    n.notifyCacheEntryCreated("accept", null, true, ctx, null);
    n.notifyCacheEntryCreated("accept", "v1", false, ctx, null);

    assert cl.isReceivedPost();
    assert cl.isReceivedPre();
    assert cl.getInvocationCount() == 2;
    assert cl.getEvents().get(0).getCache() == mockCache;
    assert cl.getEvents().get(0).getType() == Event.Type.CACHE_ENTRY_CREATED;
    assert ((CacheEntryCreatedEvent) cl.getEvents().get(0)).getKey().equals("accept");
    assert ((CacheEntryCreatedEvent) cl.getEvents().get(0)).getValue() == null;
    assert cl.getEvents().get(1).getCache() == mockCache;
    assert cl.getEvents().get(1).getType() == Event.Type.CACHE_ENTRY_CREATED;
    assert ((CacheEntryCreatedEvent) cl.getEvents().get(1)).getKey().equals("accept");
    assert ((CacheEntryCreatedEvent) cl.getEvents().get(1)).getValue().equals("v1");
  }