View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
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 org.springframework.cache.concurrent;
18  
19  import java.util.concurrent.ConcurrentHashMap;
20  import java.util.concurrent.ConcurrentMap;
21  
22  import org.junit.Before;
23  import org.junit.Test;
24  
25  import org.springframework.cache.Cache;
26  
27  import static org.junit.Assert.*;
28  
29  /**
30   * @author Costin Leau
31   * @author Juergen Hoeller
32   * @author Stephane Nicoll
33   */
34  public class ConcurrentCacheTests  {
35  
36  	protected final static String CACHE_NAME = "testCache";
37  
38  	protected ConcurrentMap<Object, Object> nativeCache;
39  
40  	protected Cache cache;
41  
42  
43  	@Before
44  	public void setUp() throws Exception {
45  		nativeCache = new ConcurrentHashMap<Object, Object>();
46  		cache = new ConcurrentMapCache(CACHE_NAME, nativeCache, true);
47  		cache.clear();
48  	}
49  
50  
51  	@Test
52  	public void testCacheName() throws Exception {
53  		assertEquals(CACHE_NAME, cache.getName());
54  	}
55  
56  	@Test
57  	public void testNativeCache() throws Exception {
58  		assertSame(nativeCache, cache.getNativeCache());
59  	}
60  
61  	@Test
62  	public void testCachePut() throws Exception {
63  		Object key = "enescu";
64  		Object value = "george";
65  
66  		assertNull(cache.get(key));
67  		assertNull(cache.get(key, String.class));
68  		assertNull(cache.get(key, Object.class));
69  
70  		cache.put(key, value);
71  		assertEquals(value, cache.get(key).get());
72  		assertEquals(value, cache.get(key, String.class));
73  		assertEquals(value, cache.get(key, Object.class));
74  		assertEquals(value, cache.get(key, null));
75  
76  		cache.put(key, null);
77  		assertNotNull(cache.get(key));
78  		assertNull(cache.get(key).get());
79  		assertNull(cache.get(key, String.class));
80  		assertNull(cache.get(key, Object.class));
81  	}
82  
83  	@Test
84  	public void testCachePutIfAbsent() throws Exception {
85  		Object key = new Object();
86  		Object value = "initialValue";
87  
88  		assertNull(cache.get(key));
89  		assertNull(cache.putIfAbsent(key, value));
90  		assertEquals(value, cache.get(key).get());
91  		assertEquals("initialValue", cache.putIfAbsent(key, "anotherValue").get());
92  		assertEquals(value, cache.get(key).get()); // not changed
93  	}
94  
95  	@Test
96  	public void testCacheRemove() throws Exception {
97  		Object key = "enescu";
98  		Object value = "george";
99  
100 		assertNull(cache.get(key));
101 		cache.put(key, value);
102 	}
103 
104 	@Test
105 	public void testCacheClear() throws Exception {
106 		assertNull(cache.get("enescu"));
107 		cache.put("enescu", "george");
108 		assertNull(cache.get("vlaicu"));
109 		cache.put("vlaicu", "aurel");
110 		cache.clear();
111 		assertNull(cache.get("vlaicu"));
112 		assertNull(cache.get("enescu"));
113 	}
114 
115 }