1 /**
2 * Copyright 2003-2006 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.CacheException;
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.AbstractCacheTest;
23 import net.sf.ehcache.config.CacheConfiguration;
24
25 /**
26 * Uses a counting listener to make sure all the notifications came through
27 *
28 * @author Greg Luck
29 * @version $Id: CacheManagerEventListenerTest.java 28 2006-04-15 05:12:32Z gregluck $
30 */
31 public class CacheManagerEventListenerTest extends TestCase {
32
33 /**
34 * {@inheritDoc}
35 * @throws Exception
36 */
37 protected void setUp() throws Exception {
38 CountingCacheManagerEventListener.resetCounters();
39 }
40
41
42 /**
43 * {@inheritDoc}
44 * @throws Exception
45 */
46 protected void tearDown() throws Exception {
47 CountingCacheManagerEventListener.resetCounters();
48 }
49
50
51 /**
52 * Tests that we can set the listener through configuration, and that it gets notified of all events.
53 */
54 public void testListenerSpecifiedInConfigurationFile() throws CacheException {
55 CacheManager manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-countinglisteners.xml");
56 assertNotNull(manager);
57 assertEquals(10, manager.getCacheNames().length);
58 assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
59
60 for (int i = 0; i < 10; i++) {
61 String cacheName = (String) CountingCacheManagerEventListener.getCacheNamesAdded().get(i);
62 manager.removeCache(cacheName);
63 assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
64
65 }
66 manager.shutdown();
67 }
68
69
70 /**
71 * Tests we can programmatically set the listener, and that it gets notified of all events.
72 */
73 public void testListenerSpecifiedProgrammatically() throws CacheException {
74 CacheConfiguration defaultCache = new CacheConfiguration();
75 defaultCache.setEternal(false);
76 defaultCache.setMaxElementsInMemory(10);
77
78 CountingCacheManagerEventListener countingCacheManagerEventListener = new CountingCacheManagerEventListener();
79
80 CacheManager manager = new CacheManager();
81 manager.removalAll();
82 manager.setCacheManagerEventListener(countingCacheManagerEventListener);
83
84 for (int i = 0; i < 10; i++) {
85 manager.addCache("" + i);
86 }
87
88 assertNotNull(manager);
89 assertEquals(10, manager.getCacheNames().length);
90 assertEquals(10, CountingCacheManagerEventListener.getCacheNamesAdded().size());
91
92 for (int i = 0; i < 10; i++) {
93 String cacheName = (String) CountingCacheManagerEventListener.getCacheNamesAdded().get(i);
94 manager.removeCache(cacheName);
95 assertEquals(i + 1, CountingCacheManagerEventListener.getCacheNamesRemoved().size());
96
97 }
98 manager.shutdown();
99 }
100
101
102
103
104
105 }