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

File XWikiContextContextStoreTest.java

 

Code metrics

0
68
7
1
219
147
7
0.1
9.71
7
1

Classes

Class Line # Actions
XWikiContextContextStoreTest 63 68 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 6 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.context;
21   
22    import java.io.Serializable;
23    import java.net.MalformedURLException;
24    import java.net.URL;
25    import java.util.Collections;
26    import java.util.HashMap;
27    import java.util.Map;
28   
29    import org.junit.jupiter.api.BeforeEach;
30    import org.junit.jupiter.api.Test;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.test.junit5.mockito.InjectMockComponents;
34    import org.xwiki.wiki.descriptor.WikiDescriptor;
35    import org.xwiki.wiki.manager.WikiManagerException;
36   
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.test.MockitoOldcore;
40    import com.xpn.xwiki.test.junit5.mockito.InjectMockitoOldcore;
41    import com.xpn.xwiki.test.junit5.mockito.OldcoreTest;
42    import com.xpn.xwiki.test.reference.ReferenceComponentList;
43    import com.xpn.xwiki.user.api.XWikiRightService;
44    import com.xpn.xwiki.web.XWikiServletRequestStub;
45   
46    import static org.junit.jupiter.api.Assertions.assertEquals;
47    import static org.junit.jupiter.api.Assertions.assertFalse;
48    import static org.junit.jupiter.api.Assertions.assertNotEquals;
49    import static org.junit.jupiter.api.Assertions.assertNotNull;
50    import static org.junit.jupiter.api.Assertions.assertNotSame;
51    import static org.junit.jupiter.api.Assertions.assertNull;
52    import static org.junit.jupiter.api.Assertions.assertTrue;
53    import static org.mockito.Mockito.doReturn;
54    import static org.mockito.Mockito.when;
55   
56    /**
57    * Validate {@link XWikiContextContextStore}.
58    *
59    * @version $Id: e737fb0754c0c18fdc77a004542d76cc02e1279e $
60    */
61    @OldcoreTest
62    @ReferenceComponentList
 
63    public class XWikiContextContextStoreTest
64    {
65    private static final String WIKI = "wiki";
66   
67    private static final String REQUESTWIKI = "requestwiki";
68   
69    @InjectMockitoOldcore
70    private MockitoOldcore oldcore;
71   
72    @InjectMockComponents
73    private XWikiContextContextStore store;
74   
75    private WikiDescriptor descriptor;
76   
77    private URL wikiURL;
78   
79    private URL requestwikiURL;
80   
 
81  6 toggle @BeforeEach
82    public void beforeEach() throws WikiManagerException, ComponentLookupException, MalformedURLException
83    {
84  6 this.descriptor = new WikiDescriptor(WIKI, WIKI);
85  6 this.descriptor.setMainPageReference(new DocumentReference(this.descriptor.getId(), "Space", "MainPage"));
86   
87  6 when(this.oldcore.getWikiDescriptorManager().getById(this.descriptor.getId())).thenReturn(this.descriptor);
88   
89  6 doReturn("webapppath").when(this.oldcore.getSpyXWiki()).getWebAppPath(this.oldcore.getXWikiContext());
90   
91  6 this.wikiURL = new URL("http", "host", 42, "file");
92  6 doReturn(this.wikiURL).when(this.oldcore.getSpyXWiki()).getServerURL(this.descriptor.getId(),
93    this.oldcore.getXWikiContext());
94   
95  6 this.requestwikiURL = new URL("https", "host2", 84, "file2");
96  6 doReturn(this.requestwikiURL).when(this.oldcore.getSpyXWiki()).getServerURL(REQUESTWIKI,
97    this.oldcore.getXWikiContext());
98    }
99   
 
100  1 toggle @Test
101    public void saveEmpty()
102    {
103  1 Map<String, Serializable> contextStore = new HashMap<>();
104   
105  1 this.store.save(contextStore, Collections.emptySet());
106   
107  1 assertTrue(contextStore.isEmpty());
108    }
109   
 
110  1 toggle @Test
111    public void save()
112    {
113  1 this.oldcore.getXWikiContext().setWikiId(WIKI);
114   
115  1 Map<String, Serializable> contextStore = new HashMap<>();
116   
117  1 this.store.save(contextStore, this.store.getSupportedEntries());
118   
119  1 assertEquals(WIKI, contextStore.get(XWikiContextContextStore.PROP_WIKI));
120    }
121   
 
122  1 toggle @Test
123    public void saveRequestwiki()
124    {
125  1 this.oldcore.getXWikiContext().setWikiId(WIKI);
126  1 this.oldcore.getXWikiContext().setOriginalWikiId(this.oldcore.getXWikiContext().getWikiId());
127   
128  1 Map<String, Serializable> contextStore = new HashMap<>();
129   
130  1 this.store.save(contextStore, this.store.getSupportedEntries());
131   
132  1 assertFalse(contextStore.containsKey(XWikiContextContextStore.PROP_REQUEST_WIKI));
133   
134  1 this.oldcore.getXWikiContext().setOriginalWikiId(REQUESTWIKI);
135   
136  1 this.store.save(contextStore, this.store.getSupportedEntries());
137   
138  1 assertEquals(REQUESTWIKI, contextStore.get(XWikiContextContextStore.PROP_REQUEST_WIKI));
139    }
140   
 
141  1 toggle @Test
142    public void restoreEmpty()
143    {
144  1 this.store.restore(new HashMap<>());
145    }
146   
 
147  1 toggle @Test
148    public void restoreWiki()
149    {
150  1 assertNotEquals(WIKI, this.oldcore.getXWikiContext().getWikiId());
151  1 assertNull(this.oldcore.getXWikiContext().getRequest());
152  1 assertNull(this.oldcore.getXWikiContext().getDoc());
153  1 assertNull(this.oldcore.getXWikiContext().getUserReference());
154   
155  1 Map<String, Serializable> contextStore = new HashMap<>();
156  1 contextStore.put(XWikiContextContextStore.PROP_WIKI, WIKI);
157   
158  1 this.store.restore(contextStore);
159   
160  1 assertEquals(WIKI, this.oldcore.getXWikiContext().getWikiId());
161  1 assertEquals(this.oldcore.getXWikiContext().getUserReference(),
162    new DocumentReference("xwiki", "XWiki", XWikiRightService.SUPERADMIN_USER));
163  1 assertEquals(this.descriptor.getMainPageReference(),
164    this.oldcore.getXWikiContext().getDoc().getDocumentReference());
165  1 assertEquals(this.wikiURL.toString(), this.oldcore.getXWikiContext().getRequest().getRequestURL().toString());
166  1 assertFalse(((XWikiServletRequestStub) this.oldcore.getXWikiContext().getRequest()).isDaemon());
167  1 assertNull(this.oldcore.getXWikiContext().get(XWikiDocument.CKEY_SDOC));
168    }
169   
 
170  1 toggle @Test
171    public void restoreAuthor() throws XWikiException
172    {
173  1 assertNull(this.oldcore.getXWikiContext().getAuthorReference());
174  1 assertNull(this.oldcore.getXWikiContext().get(XWikiDocument.CKEY_SDOC));
175  1 assertNull(this.oldcore.getXWikiContext().getUserReference());
176   
177  1 DocumentReference authorReference = new DocumentReference("authorwiki", "authorspace", "author");
178   
179  1 Map<String, Serializable> contextStore = new HashMap<>();
180  1 contextStore.put(XWikiContextContextStore.PROP_SECURE_AUTHOR, authorReference);
181   
182  1 this.store.restore(contextStore);
183   
184  1 assertEquals(this.oldcore.getXWikiContext().getUserReference(), authorReference);
185   
186  1 XWikiDocument secureDocument1 = (XWikiDocument) this.oldcore.getXWikiContext().get(XWikiDocument.CKEY_SDOC);
187  1 assertNotNull(secureDocument1);
188  1 assertEquals(new DocumentReference("authorwiki", "SUSpace", "SUPage"), secureDocument1.getDocumentReference());
189  1 assertEquals(authorReference, secureDocument1.getContentAuthorReference());
190  1 assertEquals(authorReference, this.oldcore.getXWikiContext().getAuthorReference());
191   
192  1 this.store.restore(contextStore);
193   
194  1 assertEquals(this.oldcore.getXWikiContext().getUserReference(), authorReference);
195   
196  1 XWikiDocument secureDocument2 = (XWikiDocument) this.oldcore.getXWikiContext().get(XWikiDocument.CKEY_SDOC);
197  1 assertNotNull(secureDocument2);
198  1 assertNotSame(secureDocument1, secureDocument2);
199  1 assertEquals(new DocumentReference("authorwiki", "SUSpace", "SUPage"), secureDocument2.getDocumentReference());
200  1 assertEquals(authorReference, secureDocument2.getContentAuthorReference());
201  1 assertEquals(authorReference, this.oldcore.getXWikiContext().getAuthorReference());
202   
203  1 DocumentReference secureDocumentReference =
204    new DocumentReference("securewiki", "securespace", "securedocument");
205  1 contextStore.put(XWikiContextContextStore.PROP_SECURE_DOCUMENT, secureDocumentReference);
206   
207  1 this.store.restore(contextStore);
208   
209  1 assertEquals(this.oldcore.getXWikiContext().getUserReference(), authorReference);
210   
211  1 XWikiDocument secureDocument3 = (XWikiDocument) this.oldcore.getXWikiContext().get(XWikiDocument.CKEY_SDOC);
212  1 assertNotNull(secureDocument3);
213  1 assertEquals(secureDocumentReference, secureDocument3.getDocumentReference());
214  1 assertEquals(authorReference, secureDocument3.getContentAuthorReference());
215  1 assertEquals(authorReference, this.oldcore.getXWikiContext().getAuthorReference());
216  1 assertNotSame(this.oldcore.getSpyXWiki().getDocument(secureDocumentReference, this.oldcore.getXWikiContext()),
217    secureDocument3);
218    }
219    }