1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.cache.ehcache;
20  
21  import org.apache.shiro.cache.Cache;
22  import org.apache.shiro.util.LifecycleUtils;
23  import org.junit.After;
24  import org.junit.Before;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.*;
28  
29  /**
30   * TODO - Class JavaDoc
31   *
32   * @since May 11, 2010 12:41:38 PM
33   */
34  public class EhCacheManagerTest {
35  
36      private EhCacheManager cacheManager;
37  
38      @Before
39      public void setUp() {
40          cacheManager = new EhCacheManager();
41      }
42  
43      @After
44      public void tearDown() {
45          LifecycleUtils.destroy(cacheManager);
46      }
47  
48      @Test
49      public void testCacheManagerCreationDuringInit() {
50          net.sf.ehcache.CacheManager ehCacheManager = cacheManager.getCacheManager();
51          assertNull(ehCacheManager);
52          cacheManager.init();
53          //now assert that an internal CacheManager has been created:
54          ehCacheManager = cacheManager.getCacheManager();
55          assertNotNull(ehCacheManager);
56      }
57  
58      @Test
59      public void testLazyCacheManagerCreationWithoutCallingInit() {
60          net.sf.ehcache.CacheManager ehCacheManager = cacheManager.getCacheManager();
61          assertNull(ehCacheManager);
62  
63          //don't call init here - the ehcache CacheManager should be lazily created
64          //because of the default Shiro ehcache.xml file in the classpath.  Just acquire a cache:
65          Cache<String, String> cache = cacheManager.getCache("test");
66  
67          //now assert that an internal CacheManager has been created:
68          ehCacheManager = cacheManager.getCacheManager();
69          assertNotNull(ehCacheManager);
70  
71          assertNotNull(cache);
72          cache.put("hello", "world");
73          String value = cache.get("hello");
74          assertNotNull(value);
75          assertEquals(value, "world");
76      }
77  
78  }