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

File XWikiCacheStoreTest.java

 

Code metrics

0
28
2
1
117
72
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: c13a883135c185e03ee14b370126f20b0291cfc0 $
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   
83    // Save a document
84  1 DocumentReference reference = new DocumentReference("otherwiki", "space", "page");
85  1 this.oldcore.getSpyXWiki().saveDocument(new XWikiDocument(reference), this.oldcore.getXWikiContext());
86   
87  1 XWikiCacheStore store = new XWikiCacheStore(this.oldcore.getMockStore(), this.oldcore.getXWikiContext());
88   
89  1 XWikiDocument existingDocument =
90    store.loadXWikiDoc(new XWikiDocument(reference), this.oldcore.getXWikiContext());
91   
92  1 assertFalse(existingDocument.isNew());
93  1 verify(this.cache).set(eq("4:wiki5:space4:page"), any(XWikiDocument.class));
94  1 verify(this.existCache).set("4:wiki5:space4:page", Boolean.TRUE);
95  1 verify(this.cache).get(any());
96  1 verify(this.existCache).get(any());
97   
98  1 verifyNoMoreInteractions(this.cache);
99  1 verifyNoMoreInteractions(this.existCache);
100   
101  1 XWikiDocument notExistingDocument =
102    store.loadXWikiDoc(new XWikiDocument(new DocumentReference("otherwiki", "space", "nopage")),
103    this.oldcore.getXWikiContext());
104   
105  1 assertTrue(notExistingDocument.isNew());
106   
107    // Make sure only the existing document has been put in the cache
108  1 verify(this.cache).set(eq("4:wiki5:space4:page"), any(XWikiDocument.class));
109  1 verify(this.existCache).set("4:wiki5:space4:page", Boolean.TRUE);
110  1 verify(this.existCache).set("4:wiki5:space6:nopage", Boolean.FALSE);
111  1 verify(this.cache, times(2)).get(any());
112  1 verify(this.existCache, times(2)).get(any());
113   
114  1 verifyNoMoreInteractions(this.cache);
115  1 verifyNoMoreInteractions(this.existCache);
116    }
117    }