1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.event;
18  
19  import junit.framework.TestCase;
20  import net.sf.ehcache.AbstractCacheTest;
21  import net.sf.ehcache.CacheException;
22  import net.sf.ehcache.CacheManager;
23  import net.sf.ehcache.config.CacheConfiguration;
24  import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
25  
26  /**
27   * Uses a counting listener to make sure all the notifications came through
28   *
29   * @author Greg Luck
30   * @version $Id: CacheManagerEventListenerTest.java 512 2007-07-10 09:18:45Z gregluck $
31   */
32  public class CacheManagerEventListenerTest extends TestCase {
33  
34      /**
35       * {@inheritDoc}
36       * @throws Exception
37       */
38      protected void setUp() throws Exception {
39          CountingCacheManagerEventListener.resetCounters();
40      }
41  
42  
43      /**
44       * {@inheritDoc}
45       * @throws Exception
46       */
47      protected void tearDown() throws Exception {
48          CountingCacheManagerEventListener.resetCounters();
49      }
50  
51  
52      /**
53       * Tests that we can set the listener through configuration, and that it gets notified of all events.
54       */
55      public void testListenerSpecifiedInConfigurationFile() throws CacheException {
56          CacheManager manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
57          assertNotNull(manager);
58          assertEquals(10, manager.getCacheNames().length);
59          assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
60  
61  
62          String[] cacheNames = manager.getCacheNames();
63          for (int i = 0; i < cacheNames.length; i++) {
64              String cacheName = cacheNames[i];
65              manager.removeCache(cacheName);
66              assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
67          }
68  
69          manager.shutdown();
70      }
71  
72  
73      /**
74       * Tests we can programmatically set the listener, and that it gets notified of all events.
75       */
76      public void testListenerSpecifiedProgrammatically() throws CacheException {
77          CacheConfiguration defaultCache = new CacheConfiguration();
78          defaultCache.setEternal(false);
79          defaultCache.setMaxElementsInMemory(10);
80  
81          CountingCacheManagerEventListener countingCacheManagerEventListener = new CountingCacheManagerEventListener();
82  
83          CacheManager manager = new CacheManager();
84          manager.removalAll();
85          manager.getCacheManagerEventListenerRegistry().registerListener(countingCacheManagerEventListener);
86  
87          for (int i = 0; i < 10; i++) {
88              manager.addCache("" + i);
89          }
90          //make sure that the listener works with Ehcache interface, not just Cache
91          manager.replaceCacheWithDecoratedCache(manager.getCache("9"), new SelfPopulatingCache(manager.getCache("9"), null));
92  
93          assertNotNull(manager);
94          assertEquals(10, manager.getCacheNames().length);
95          assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
96  
97          for (int i = 0; i < 10; i++) {
98              String cacheName = (String) CountingCacheManagerEventListener.getCacheNamesAdded().get(i);
99              manager.removeCache(cacheName);
100             assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
101 
102         }
103         manager.shutdown();
104     }
105 
106 
107 
108 
109 
110 }