1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.cache.test

File AbstractGenericTestCache.java

 

Code metrics

0
65
8
1
221
105
8
0.12
8.12
8
1

Classes

Class Line # Actions
AbstractGenericTestCache 33 65 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.cache.test;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24    import org.xwiki.cache.Cache;
25    import org.xwiki.cache.CacheFactory;
26    import org.xwiki.cache.config.CacheConfiguration;
27   
28    /**
29    * Base class for testing cache component implementation.
30    *
31    * @version $Id: 7ee0fbbe100680de092ac686a6f4f00384fc424d $
32    */
 
33    public abstract class AbstractGenericTestCache extends AbstractTestCache
34    {
35    /**
36    * @param roleHint the role hint of the cache component implementation to test.
37    */
 
38  11 toggle protected AbstractGenericTestCache(String roleHint)
39    {
40  11 super(roleHint);
41    }
42   
43    // ///////////////////////////////////////////////////////::
44    // Tests
45   
46    /**
47    * Validate factory initialization.
48    *
49    * @throws Exception error.
50    */
 
51  1 toggle @Test
52    public void getFactory() throws Exception
53    {
54  1 CacheFactory factory = getCacheFactory();
55   
56  1 CacheFactory factory2 = getCacheFactory();
57   
58  1 Assert.assertSame(factory, factory2);
59    }
60   
61    /**
62    * Validate some basic cache use case without any constraints.
63    *
64    * @throws Exception error.
65    */
 
66  1 toggle @Test
67    public void createAndDestroyCacheSimple() throws Exception
68    {
69  1 CacheFactory factory = getCacheFactory();
70   
71  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
72   
73  1 Assert.assertNotNull(cache);
74   
75  1 cache.set(KEY, VALUE);
76  1 cache.set(KEY2, VALUE2);
77   
78  1 Assert.assertEquals(VALUE, cache.get(KEY));
79  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
80   
81  1 cache.dispose();
82    }
83   
84    /**
85    * Validate {@link Cache#remove(String)}.
86    *
87    * @throws Exception error.
88    */
 
89  1 toggle @Test
90    public void remove() throws Exception
91    {
92  1 CacheFactory factory = getCacheFactory();
93   
94  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
95   
96  1 cache.set(KEY, VALUE);
97  1 cache.set(KEY2, VALUE2);
98   
99  1 cache.remove(KEY);
100   
101  1 Assert.assertNull(cache.get(KEY));
102  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
103    }
104   
105    /**
106    * Validate {@link Cache#removeAll()}.
107    *
108    * @throws Exception error.
109    */
 
110  1 toggle @Test
111    public void removeAll() throws Exception
112    {
113  1 CacheFactory factory = getCacheFactory();
114   
115  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
116   
117  1 cache.set(KEY, VALUE);
118  1 cache.set(KEY2, VALUE2);
119   
120  1 cache.removeAll();
121   
122  1 Assert.assertNull(cache.get(KEY));
123  1 Assert.assertNull(cache.get(KEY2));
124    }
125   
126    /**
127    * Validate event management.
128    *
129    * @throws Exception error.
130    */
 
131  1 toggle @Test
132    public void events() throws Exception
133    {
134  1 CacheFactory factory = getCacheFactory();
135   
136  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
137   
138  1 CacheEntryListenerTest eventListener = new CacheEntryListenerTest();
139   
140  1 cache.addCacheEntryListener(eventListener);
141   
142  1 cache.set(KEY, VALUE);
143   
144  1 Assert.assertNotNull(eventListener.getAddedEvent());
145  1 Assert.assertSame(cache, eventListener.getAddedEvent().getCache());
146  1 Assert.assertEquals(KEY, eventListener.getAddedEvent().getEntry().getKey());
147  1 Assert.assertEquals(VALUE, eventListener.getAddedEvent().getEntry().getValue());
148   
149  1 cache.set(KEY, VALUE2);
150   
151  1 Assert.assertNotNull(eventListener.getModifiedEvent());
152  1 Assert.assertSame(cache, eventListener.getModifiedEvent().getCache());
153  1 Assert.assertEquals(KEY, eventListener.getModifiedEvent().getEntry().getKey());
154  1 Assert.assertEquals(VALUE2, eventListener.getModifiedEvent().getEntry().getValue());
155   
156  1 cache.remove(KEY);
157  1 cache.get(KEY);
158   
159  1 Assert.assertNotNull(eventListener.getRemovedEvent());
160  1 Assert.assertSame(cache, eventListener.getRemovedEvent().getCache());
161  1 Assert.assertEquals(KEY, eventListener.getRemovedEvent().getEntry().getKey());
162  1 Assert.assertEquals(VALUE2, eventListener.getRemovedEvent().getEntry().getValue());
163   
164  1 eventListener.reinitRemovedEvent();
165  1 cache.remove(KEY);
166   
167  1 Assert.assertNull(eventListener.getRemovedEvent());
168    }
169   
170    /**
171    * Validate that two different caches are really different.
172    *
173    * @throws Exception error.
174    */
 
175  1 toggle @Test
176    public void severalCaches() throws Exception
177    {
178  1 CacheFactory factory = getCacheFactory();
179   
180  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
181  1 Cache<Object> cache2 = factory.newCache(new CacheConfiguration());
182   
183  1 cache.set(KEY, VALUE);
184   
185  1 Assert.assertNull(cache2.get(KEY));
186    }
187   
188    /**
189    * Validate that when recreating a cache with the same id the second instance is in a proper state.
190    *
191    * @throws Exception error
192    */
 
193  1 toggle @Test
194    public void recreateCache() throws Exception
195    {
196  1 CacheFactory factory = getCacheFactory();
197   
198  1 CacheConfiguration configuration = new CacheConfiguration();
199  1 configuration.setConfigurationId("test");
200   
201  1 Cache<Object> cache = factory.newCache(configuration);
202   
203  1 cache.set(KEY, VALUE);
204   
205  1 Assert.assertEquals(VALUE, cache.get(KEY));
206   
207    // dispose the first cache
208   
209  1 cache.dispose();
210   
211    // recreate it
212   
213  1 cache = factory.newCache(configuration);
214   
215  1 Assert.assertNull(cache.get(KEY));
216   
217  1 cache.set(KEY, VALUE);
218   
219  1 Assert.assertEquals(VALUE, cache.get(KEY));
220    }
221    }