View Javadoc

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.extension;
18  
19  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.Cache;
21  import net.sf.ehcache.CacheManager;
22  import net.sf.ehcache.Status;
23  import net.sf.ehcache.event.CountingCacheEventListener;
24  import org.junit.After;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  
29  import org.junit.Before;
30  import org.junit.Test;
31  
32  /***
33   * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
34   * @version $Id: CacheExtensionTest.html 13146 2011-08-01 17:12:39Z oletizi $
35   */
36  public class CacheExtensionTest {
37  
38      /***
39       * manager
40       */
41      protected CacheManager manager;
42  
43  
44      /***
45       * {@inheritDoc}
46       *
47       * @throws Exception
48       */
49      @Before
50      public void setUp() throws Exception {
51          CountingCacheEventListener.resetCounters();
52          manager = CacheManager.create(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-cacheextension.xml");
53      }
54  
55  
56      /***
57       * {@inheritDoc}
58       *
59       * @throws Exception
60       */
61      @After
62      public void tearDown() throws Exception {
63          if (!manager.getStatus().equals(Status.STATUS_SHUTDOWN)) {
64              manager.shutdown();
65          }
66      }
67  
68  
69      /***
70       * Tests the put listener.
71       */
72      @Test
73      public void testExtensionDirectly() {
74  
75          manager.addCache("test");
76          TestCacheExtension testCacheExtension = new TestCacheExtension(manager.getCache("test"), "valueA");
77          assertEquals(Status.STATUS_UNINITIALISED, testCacheExtension.getStatus());
78          assertEquals("valueA", testCacheExtension.getPropertyA());
79  
80          testCacheExtension.init();
81          assertEquals(Status.STATUS_ALIVE, testCacheExtension.getStatus());
82  
83          testCacheExtension.dispose();
84          assertEquals(Status.STATUS_SHUTDOWN, testCacheExtension.getStatus());
85  
86      }
87  
88  
89      /***
90       * Tests the put listener.
91       */
92      @Test
93      public void testExtensionFromConfiguration() {
94  
95          assertEquals(Status.STATUS_ALIVE, TestCacheExtension.getStaticStatus());
96          assertEquals("valueA", TestCacheExtension.getPropertyA());
97  
98          Cache cache = manager.getCache("testCacheExtensionCache");
99  
100         //Our cache extension should have populated the cache for this key
101         assertNotNull(cache.get("key1"));
102 
103         manager.shutdown();
104         assertEquals(Status.STATUS_SHUTDOWN, TestCacheExtension.getStaticStatus());
105 
106     }
107 
108     /***
109      * Tests the put listener.
110      */
111     @Test
112     public void testProgrammaticAdd() {
113 
114         manager.addCache("test");
115         Cache cache = manager.getCache("test");
116         TestCacheExtension testCacheExtension = new TestCacheExtension(cache, "valueA");
117         assertEquals(Status.STATUS_UNINITIALISED, testCacheExtension.getStatus());
118         assertEquals("valueA", testCacheExtension.getPropertyA());
119 
120         testCacheExtension.init();
121         assertEquals(Status.STATUS_ALIVE, testCacheExtension.getStatus());
122 
123         cache.registerCacheExtension(testCacheExtension);
124         manager.shutdown();
125         assertEquals(Status.STATUS_SHUTDOWN, testCacheExtension.getStatus());
126     }
127 
128 
129     /***
130      * We need to make sure that cloning a default cache results in a new cache with its own
131      * set of cache extensions.
132      */
133     @Test
134     public void testClone() {
135 
136         //just test it does not blow up
137         manager.addCache("clonedCache");
138     }
139 }