1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.store

File XWikiCacheStoreTest.java

 

Code metrics

0
28
2
1
118
73
2
0.07
14
2
1

Classes

Class Line # Actions
XWikiCacheStoreTest 53 28 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.store;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.cache.Cache;
26    import org.xwiki.cache.CacheManager;
27    import org.xwiki.model.internal.reference.UidStringEntityReferenceSerializer;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.observation.ObservationManager;
30    import org.xwiki.observation.remote.RemoteObservationManagerContext;
31    import org.xwiki.test.annotation.ComponentList;
32   
33    import com.xpn.xwiki.doc.XWikiDocument;
34    import com.xpn.xwiki.test.MockitoOldcoreRule;
35   
36    import static com.xpn.xwiki.test.mockito.OldcoreMatchers.isCacheConfiguration;
37    import static org.junit.Assert.assertFalse;
38    import static org.junit.Assert.assertTrue;
39    import static org.mockito.ArgumentMatchers.any;
40    import static org.mockito.ArgumentMatchers.eq;
41    import static org.mockito.Mockito.mock;
42    import static org.mockito.Mockito.times;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.verifyNoMoreInteractions;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Validate {@link XWikiCacheStore} behavior.
49    *
50    * @version $Id: 6a3ed033f994b4eabc72e0df36bf6ff1d42952d4 $
51    */
52    @ComponentList(UidStringEntityReferenceSerializer.class)
 
53    public class XWikiCacheStoreTest
54    {
55    @Rule
56    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
57   
58    private Cache<XWikiDocument> cache;
59   
60    private Cache<Boolean> existCache;
61   
 
62  1 toggle @Before
63    public void before() throws Exception
64    {
65  1 this.oldcore.getMocker().registerMockComponent(RemoteObservationManagerContext.class);
66  1 this.oldcore.getMocker().registerMockComponent(ObservationManager.class);
67   
68  1 CacheManager cacheManager = this.oldcore.getMocker().registerMockComponent(CacheManager.class);
69  1 cache = mock(Cache.class);
70  1 when(cacheManager.<XWikiDocument>createNewCache(isCacheConfiguration("xwiki.store.pagecache"))).thenReturn(
71    cache);
72  1 existCache = mock(Cache.class);
73  1 when(cacheManager.<Boolean>createNewCache(isCacheConfiguration("xwiki.store.pageexistcache"))).thenReturn(
74    existCache);
75    }
76   
 
77  1 toggle @Test
78    public void testLoadXWikiDoc() throws Exception
79    {
80    // Set current wiki
81  1 this.oldcore.getXWikiContext().setWikiId("wiki");
82  1 this.oldcore.getSpyXWiki().saveDocument(new XWikiDocument(new DocumentReference("wiki", "space", "page")),
83    this.oldcore.getXWikiContext());
84   
85    // Save a document
86  1 DocumentReference reference = new DocumentReference("otherwiki", "space", "page");
87   
88  1 XWikiCacheStore store = new XWikiCacheStore(this.oldcore.getMockStore(), this.oldcore.getXWikiContext());
89   
90  1 XWikiDocument existingDocument =
91    store.loadXWikiDoc(new XWikiDocument(reference), this.oldcore.getXWikiContext());
92   
93  1 assertFalse(existingDocument.isNew());
94  1 verify(this.cache).set(eq("4:wiki5:space4:page"), any(XWikiDocument.class));
95  1 verify(this.existCache).set("4:wiki5:space4:page", Boolean.TRUE);
96  1 verify(this.cache).get(any());
97  1 verify(this.existCache).get(any());
98   
99  1 verifyNoMoreInteractions(this.cache);
100  1 verifyNoMoreInteractions(this.existCache);
101   
102  1 XWikiDocument notExistingDocument =
103    store.loadXWikiDoc(new XWikiDocument(new DocumentReference("otherwiki", "space", "nopage")),
104    this.oldcore.getXWikiContext());
105   
106  1 assertTrue(notExistingDocument.isNew());
107   
108    // Make sure only the existing document has been put in the cache
109  1 verify(this.cache).set(eq("4:wiki5:space4:page"), any(XWikiDocument.class));
110  1 verify(this.existCache).set("4:wiki5:space4:page", Boolean.TRUE);
111  1 verify(this.existCache).set("4:wiki5:space6:nopage", Boolean.FALSE);
112  1 verify(this.cache, times(2)).get(any());
113  1 verify(this.existCache, times(2)).get(any());
114   
115  1 verifyNoMoreInteractions(this.cache);
116  1 verifyNoMoreInteractions(this.existCache);
117    }
118    }