1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.sheet.internal

File SheetDocumentDisplayerTest.java

 

Code metrics

0
42
5
1
189
89
5
0.12
8.4
5
1

Classes

Class Line # Actions
SheetDocumentDisplayerTest 48 42 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 org.xwiki.sheet.internal;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.apache.commons.lang3.RandomStringUtils;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.bridge.DocumentAccessBridge;
31    import org.xwiki.bridge.DocumentModelBridge;
32    import org.xwiki.display.internal.DocumentDisplayer;
33    import org.xwiki.display.internal.DocumentDisplayerParameters;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.rendering.block.Block;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.sheet.SheetManager;
38    import org.xwiki.test.mockito.MockitoComponentMockingRule;
39   
40    import static org.junit.Assert.*;
41    import static org.mockito.Mockito.*;
42   
43    /**
44    * Unit tests for {@link SheetDocumentDisplayer}.
45    *
46    * @version $Id: e853ff0b1efb355c7089e83b9942d655737ce443 $
47    */
 
48    public class SheetDocumentDisplayerTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<DocumentDisplayer> mocker = new MockitoComponentMockingRule<DocumentDisplayer>(
52    SheetDocumentDisplayer.class);
53   
54    /**
55    * The reference to the displayed document.
56    */
57    private static final DocumentReference DOCUMENT_REFERENCE = new DocumentReference("wiki1", "Space", "Page");
58   
59    /**
60    * The reference to the sheet document.
61    */
62    private static final DocumentReference SHEET_REFERENCE = new DocumentReference("wiki2", "Code", "Sheet");
63   
64    /**
65    * The component used to access the documents.
66    */
67    private DocumentAccessBridge documentAccessBridge;
68   
69    /**
70    * The component used to access the model.
71    */
72    private ModelBridge modelBridge;
73   
 
74  2 toggle @Before
75    public void configure() throws Exception
76    {
77  2 this.modelBridge = this.mocker.getInstance(ModelBridge.class);
78    // Assume the current action is view.
79  2 when(this.modelBridge.getCurrentAction()).thenReturn("view");
80   
81  2 this.documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class);
82    // Assume all documents are viewable by the current user.
83  2 when(this.documentAccessBridge.isDocumentViewable(any(DocumentReference.class))).thenReturn(true);
84    }
85   
86    /**
87    * Creates a mock {@link DocumentModelBridge} that has the specified reference.
88    *
89    * @param documentReference the document reference
90    * @return the mock {@link DocumentModelBridge}
91    * @throws Exception if creating the mock fails
92    */
 
93  5 toggle private DocumentModelBridge mockDocument(DocumentReference documentReference) throws Exception
94    {
95  5 StringBuilder id = new StringBuilder(documentReference.getLastSpaceReference().getName());
96    // Allow different instances of the same document to exist.
97  5 id.append('.').append(documentReference.getName()).append(RandomStringUtils.randomAlphanumeric(3));
98  5 DocumentModelBridge document = mock(DocumentModelBridge.class, id.toString());
99   
100  5 when(document.getDocumentReference()).thenReturn(documentReference);
101  5 when(this.documentAccessBridge.getDocument(documentReference)).thenReturn(document);
102  5 when(this.modelBridge.getDefaultEditMode(document)).thenReturn("edit");
103  5 when(this.modelBridge.getDefaultTranslation(document)).thenReturn(document);
104   
105  5 return document;
106    }
107   
108    /**
109    * Sets the given document as the current document, adding the necessary expectations.
110    *
111    * @param document an XWiki document
112    */
 
113  2 toggle private void setCurrentDocument(DocumentModelBridge document)
114    {
115  2 DocumentReference documentReference = document.getDocumentReference();
116  2 when(this.documentAccessBridge.getCurrentDocumentReference()).thenReturn(documentReference);
117  2 when(this.modelBridge.isCurrentDocument(document)).thenReturn(true);
118    }
119   
120    /**
121    * Tests if the programming rights of the sheet are preserved when the document is already on the context.
122    *
123    * @throws Exception if something wrong happens
124    */
 
125  1 toggle @Test
126    public void testPreserveSheetPRWhenDocumentIsOnContext() throws Exception
127    {
128  1 DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
129  1 DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
130   
131  1 setCurrentDocument(document);
132   
133  1 SheetManager sheetManager = this.mocker.getInstance(SheetManager.class);
134  1 when(sheetManager.getSheets(document, "view")).thenReturn(Collections.singletonList(SHEET_REFERENCE));
135   
136  1 DocumentModelBridge originalSecurityDoc = mock(DocumentModelBridge.class, "sdoc");
137    // Required in order to preserve the programming rights of the sheet.
138  1 when(this.modelBridge.setSecurityDocument(sheet)).thenReturn(originalSecurityDoc);
139   
140  1 XDOM output = new XDOM(Collections.<Block>emptyList());
141  1 DocumentDisplayer documentDisplayer = this.mocker.getInstance(DocumentDisplayer.class);
142  1 when(documentDisplayer.display(eq(sheet), any(DocumentDisplayerParameters.class))).thenReturn(output);
143   
144  1 assertSame(output, this.mocker.getComponentUnderTest().display(document, new DocumentDisplayerParameters()));
145   
146    // The security document must be reverted.
147  1 verify(this.modelBridge).setSecurityDocument(originalSecurityDoc);
148    }
149   
150    /**
151    * Tests if the programming rights of the sheet are preserved when the document is not on the context.
152    *
153    * @throws Exception if something wrong happens
154    */
 
155  1 toggle @Test
156    public void testPreserveSheetPRWhenDocumentIsNotOnContext() throws Exception
157    {
158  1 DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE);
159  1 DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE);
160   
161    // We test that the displayed document is put on the context even if the current document is just a different
162    // instance of the displayed document. This is needed because the displayed document can have unsaved changes.
163  1 setCurrentDocument(mockDocument(DOCUMENT_REFERENCE));
164   
165    // The sheet must be determined and displayed in a new execution context that has the target document as
166    // the current document.
167  1 Map<String, Object> backupObjects = new HashMap<String, Object>();
168  1 when(this.modelBridge.pushDocumentInContext(document)).thenReturn(backupObjects);
169   
170  1 SheetManager sheetManager = this.mocker.getInstance(SheetManager.class);
171  1 when(sheetManager.getSheets(document, "view")).thenReturn(Collections.singletonList(SHEET_REFERENCE));
172   
173  1 DocumentModelBridge originalSecurityDoc = mock(DocumentModelBridge.class, "sdoc");
174    // Required in order to preserve the programming rights of the sheet.
175  1 when(this.modelBridge.setSecurityDocument(sheet)).thenReturn(originalSecurityDoc);
176   
177  1 XDOM output = new XDOM(Collections.<Block>emptyList());
178  1 DocumentDisplayer documentDisplayer = this.mocker.getInstance(DocumentDisplayer.class);
179  1 when(documentDisplayer.display(eq(sheet), any(DocumentDisplayerParameters.class))).thenReturn(output);
180   
181  1 assertSame(output, this.mocker.getComponentUnderTest().display(document, new DocumentDisplayerParameters()));
182   
183    // The security document must be reverted.
184  1 verify(this.modelBridge).setSecurityDocument(originalSecurityDoc);
185   
186    // The previous execution context must be restored.
187  1 verify(this.documentAccessBridge).popDocumentFromContext(backupObjects);
188    }
189    }