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 import java.util.Iterator;
20
21 import junit.framework.Assert;
22
23 import net.sf.ehcache.AbstractCacheTest;
24 import net.sf.ehcache.CacheManager;
25 import net.sf.ehcache.statistics.LiveCacheStatisticsData;
26
27 import static org.junit.Assert.assertEquals;
28
29 import org.junit.Before;
30 import org.junit.Test;
31
32 /***
33 * Same as {@link CacheEventListenerTest} except that the listener is set programmatically. This test inherits because
34 * all of the tests should behave identically.
35 *
36 * @author Greg Luck
37 * @version $Id: ProgrammaticallyCreatedCacheEventListenerTest.html 13146 2011-08-01 17:12:39Z oletizi $
38 */
39 public class ProgrammaticallyCreatedCacheEventListenerTest extends CacheEventListenerTest {
40 private CountingCacheEventListener countingCacheEventListener = new CountingCacheEventListener();
41
42 /***
43 * {@inheritDoc}
44 *
45 * @throws Exception
46 */
47 @Override
48 @Before
49 public void setUp() throws Exception {
50 CountingCacheEventListener.resetCounters();
51 manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-nolisteners.xml");
52 cache = manager.getCache(cacheName);
53 cache.removeAll();
54
55 cache.getCacheEventNotificationService().registerListener(countingCacheEventListener);
56 }
57
58 /***
59 * An instance that <code>equals</code> one already registered is ignored
60 * <p/>
61 * Since ehcache-1.7, there is a defaut CacheUsageStatisticsData listener
62 */
63 @Test
64 public void testAttemptDoubleRegistrationOfSameInstance() {
65 cache.getCacheEventNotificationService().registerListener(
66 countingCacheEventListener);
67
68 assertEquals(2, cache.getCacheEventNotificationService()
69 .getCacheEventListeners().size());
70 for (Iterator<CacheEventListener> iter = cache
71 .getCacheEventNotificationService().getCacheEventListeners()
72 .iterator(); iter.hasNext();) {
73 CacheEventListener next = iter.next();
74 Assert.assertTrue(next instanceof LiveCacheStatisticsData
75 || next instanceof CountingCacheEventListener);
76 }
77 }
78
79 /***
80 * An new instance of the same class will be registered
81 * <p/>
82 * Since ehcache-1.7, there is a defaut CacheUsageStatisticsData listener
83 */
84 @Test
85 public void testAttemptDoubleRegistrationOfSeparateInstance() {
86 cache.getCacheEventNotificationService().registerListener(
87 new CountingCacheEventListener());
88
89 assertEquals(3, cache.getCacheEventNotificationService()
90 .getCacheEventListeners().size());
91 int count = 0;
92 for (Iterator<CacheEventListener> iter = cache
93 .getCacheEventNotificationService().getCacheEventListeners()
94 .iterator(); iter.hasNext();) {
95 CacheEventListener next = iter.next();
96 Assert.assertTrue(next instanceof LiveCacheStatisticsData
97 || next instanceof CountingCacheEventListener);
98 if (next instanceof CountingCacheEventListener) {
99 count++;
100 }
101 }
102 assertEquals(2, count);
103 }
104
105
106 }