1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
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
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 import org.junit.After;
26
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotNull;
29
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /***
34 * Uses a counting listener to make sure all the notifications came through
35 *
36 * @author Greg Luck
37 * @version $Id: CacheManagerEventListenerTest.html 13146 2011-08-01 17:12:39Z oletizi $
38 */
39 public class CacheManagerEventListenerTest {
40
41 /***
42 * {@inheritDoc}
43 *
44 * @throws Exception
45 */
46 @Before
47 public void setUp() throws Exception {
48 CountingCacheManagerEventListener.resetCounters();
49 }
50
51
52 /***
53 * {@inheritDoc}
54 *
55 * @throws Exception
56 */
57 @After
58 public void tearDown() throws Exception {
59 CountingCacheManagerEventListener.resetCounters();
60 }
61
62
63 /***
64 * Tests that we can set the listener through configuration, and that it gets notified of all events.
65 */
66 @Test
67 public void testListenerSpecifiedInConfigurationFile() throws CacheException {
68 CacheManager manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
69 assertNotNull(manager);
70 assertEquals(10, manager.getCacheNames().length);
71 assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
72
73
74 String[] cacheNames = manager.getCacheNames();
75 for (int i = 0; i < cacheNames.length; i++) {
76 String cacheName = cacheNames[i];
77 manager.removeCache(cacheName);
78 assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
79 }
80
81 manager.shutdown();
82 }
83
84
85 /***
86 * Tests we can programmatically set the listener, and that it gets notified of all events.
87 */
88 @Test
89 public void testListenerSpecifiedProgrammatically() throws CacheException {
90 CacheConfiguration defaultCache = new CacheConfiguration("cache", 10);
91
92 CountingCacheManagerEventListener countingCacheManagerEventListener = new CountingCacheManagerEventListener();
93
94 CacheManager manager = new CacheManager();
95 manager.removalAll();
96 manager.getCacheManagerEventListenerRegistry().registerListener(countingCacheManagerEventListener);
97
98 for (int i = 0; i < 10; i++) {
99 manager.addCache("" + i);
100 }
101
102 manager.replaceCacheWithDecoratedCache(manager.getCache("9"), new SelfPopulatingCache(manager.getCache("9"), null));
103
104 assertNotNull(manager);
105 assertEquals(10, manager.getCacheNames().length);
106 assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
107
108 for (int i = 0; i < 10; i++) {
109 String cacheName = (String) CountingCacheManagerEventListener.getCacheNamesAdded().get(i);
110 manager.removeCache(cacheName);
111 assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
112
113 }
114 manager.shutdown();
115 }
116
117
118 }