1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.internal.cache

File DefaultDocumentCacheTest.java

 

Code metrics

0
26
5
1
105
61
5
0.19
5.2
5
1

Classes

Class Line # Actions
DefaultDocumentCacheTest 42 26 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 2 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 com.xpn.xwiki.internal.cache;
21   
22    import org.junit.Assert;
23   
24    import org.jmock.Expectations;
25    import org.junit.Test;
26    import org.xwiki.bridge.event.DocumentUpdatedEvent;
27    import org.xwiki.cache.config.CacheConfiguration;
28    import org.xwiki.cache.eviction.LRUEvictionConfiguration;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.observation.ObservationManager;
31   
32    import com.xpn.xwiki.XWiki;
33    import com.xpn.xwiki.doc.XWikiDocument;
34    import com.xpn.xwiki.test.AbstractBridgedComponentTestCase;
35   
36    /**
37    * Unit test for {@link DefaultDocumentCache}.
38    *
39    * @version $Id: 2942ccf32f67c78dc72e05976fdb64a08b81cf0d $
40    * @since 2.4M1
41    */
 
42    public class DefaultDocumentCacheTest extends AbstractBridgedComponentTestCase
43    {
44    private XWiki mockXWiki;
45   
46    private XWikiDocument document;
47   
48    private DefaultDocumentCache<String> cache;
49   
 
50  2 toggle @Override
51    public void setUp() throws Exception
52    {
53  2 super.setUp();
54   
55  2 this.cache = (DefaultDocumentCache<String>) getComponentManager().getInstance(DocumentCache.class);
56   
57  2 CacheConfiguration cacheConfiguration = new CacheConfiguration();
58  2 cacheConfiguration.setConfigurationId("documentcachetest");
59  2 LRUEvictionConfiguration lru = new LRUEvictionConfiguration();
60  2 cacheConfiguration.put(LRUEvictionConfiguration.CONFIGURATIONID, lru);
61  2 this.cache.create(cacheConfiguration);
62   
63  2 this.document = new XWikiDocument(new DocumentReference("wiki", "space", "page"));
64  2 this.document.setOriginalDocument(this.document.clone());
65   
66  2 this.mockXWiki = getMockery().mock(XWiki.class);
67  2 getContext().setWiki(this.mockXWiki);
68   
 
69  2 toggle getMockery().checking(new Expectations() {{
70  2 allowing(mockXWiki).getDocument(document.getDocumentReference(), getContext()); will(returnValue(document));
71    }});
72    }
73   
 
74  2 toggle @Override
75    public void tearDown() throws Exception
76    {
77  2 this.cache.dispose();
78   
79  2 super.tearDown();
80    }
81   
 
82  1 toggle @Test
83    public void testGetSet()
84    {
85  1 this.cache.set("data", this.document.getDocumentReference());
86  1 this.cache.set("data2", this.document.getDocumentReference(), "ext1", "ext2");
87   
88  1 Assert.assertEquals("data", this.cache.get(this.document.getDocumentReference()));
89  1 Assert.assertEquals("data2", this.cache.get(this.document.getDocumentReference(), "ext1", "ext2"));
90    }
91   
 
92  1 toggle @Test
93    public void testEventBasedCleanup() throws Exception
94    {
95  1 this.cache.set("data", this.document.getDocumentReference());
96  1 this.cache.set("data", this.document.getDocumentReference(), "ext1", "ext2");
97   
98  1 ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
99  1 observationManager.notify(
100    new DocumentUpdatedEvent(this.document.getDocumentReference()), this.document, getContext());
101   
102  1 Assert.assertNull(this.cache.get(this.document.getDocumentReference()));
103  1 Assert.assertNull(this.cache.get(this.document.getDocumentReference(), "ext1", "ext2"));
104    }
105    }