View Javadoc

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 net.sf.ehcache.CacheManager;
20  import net.sf.ehcache.AbstractCacheTest;
21  
22  /**
23   * Same as {@link CacheEventListenerTest} except that the listener is set programmatically. This test inherits because
24   * all of the tests should behave identically.
25   *
26   * @author Greg Luck
27   * @version $Id: ProgrammaticallyCreatedCacheEventListenerTest.java 28 2006-04-15 05:12:32Z gregluck $
28   */
29  public class ProgrammaticallyCreatedCacheEventListenerTest extends CacheEventListenerTest {
30      private CountingCacheEventListener countingCacheEventListener = new CountingCacheEventListener();
31  
32      /**
33       * {@inheritDoc}
34       * @throws Exception
35       */
36      protected void setUp() throws Exception {
37          CountingCacheEventListener.resetCounters();
38          manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-nolisteners.xml");
39          cache = manager.getCache(cacheName);
40          cache.removeAll();
41          //this call can be repeated. Attempts to further register the listener are ignored.
42          cache.getCacheEventNotificationService().registerListener(countingCacheEventListener);
43      }
44  
45      /**
46       * An instance that <code>equals</code> one already registered is ignored
47       */
48      public void testAttemptDoubleRegistrationOfSameInstance() {
49          cache.getCacheEventNotificationService().registerListener(countingCacheEventListener);
50          //should just be the one from setUp
51          assertEquals(1, cache.getCacheEventNotificationService().getCacheEventListeners().size());
52      }
53  
54      /**
55       * An new instance of the same class will be registered
56       */
57      public void testAttemptDoubleRegistrationOfSeparateInstance() {
58          cache.getCacheEventNotificationService().registerListener(new CountingCacheEventListener());
59          //should just be the one from setUp
60          assertEquals(2, cache.getCacheEventNotificationService().getCacheEventListeners().size());
61      }
62  
63  
64  
65  
66  
67  
68  }