1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
45 |
|
|
46 |
|
@version |
47 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 5 |
Complexity Density: 0.12 |
|
48 |
|
public class SheetDocumentDisplayerTest |
49 |
|
{ |
50 |
|
@Rule |
51 |
|
public MockitoComponentMockingRule<DocumentDisplayer> mocker = new MockitoComponentMockingRule<DocumentDisplayer>( |
52 |
|
SheetDocumentDisplayer.class); |
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
private static final DocumentReference DOCUMENT_REFERENCE = new DocumentReference("wiki1", "Space", "Page"); |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final DocumentReference SHEET_REFERENCE = new DocumentReference("wiki2", "Code", "Sheet"); |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private DocumentAccessBridge documentAccessBridge; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private ModelBridge modelBridge; |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
74 |
2 |
@Before... |
75 |
|
public void configure() throws Exception |
76 |
|
{ |
77 |
2 |
this.modelBridge = this.mocker.getInstance(ModelBridge.class); |
78 |
|
|
79 |
2 |
when(this.modelBridge.getCurrentAction()).thenReturn("view"); |
80 |
|
|
81 |
2 |
this.documentAccessBridge = this.mocker.getInstance(DocumentAccessBridge.class); |
82 |
|
|
83 |
2 |
when(this.documentAccessBridge.isDocumentViewable(any(DocumentReference.class))).thenReturn(true); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
@link |
88 |
|
|
89 |
|
@param |
90 |
|
@return@link |
91 |
|
@throws |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
93 |
5 |
private DocumentModelBridge mockDocument(DocumentReference documentReference) throws Exception... |
94 |
|
{ |
95 |
5 |
StringBuilder id = new StringBuilder(documentReference.getLastSpaceReference().getName()); |
96 |
|
|
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 |
|
|
110 |
|
|
111 |
|
@param |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
113 |
2 |
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 |
|
|
122 |
|
|
123 |
|
@throws |
124 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
125 |
1 |
@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 |
|
|
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 |
|
|
147 |
1 |
verify(this.modelBridge).setSecurityDocument(originalSecurityDoc); |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
@throws |
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
155 |
1 |
@Test... |
156 |
|
public void testPreserveSheetPRWhenDocumentIsNotOnContext() throws Exception |
157 |
|
{ |
158 |
1 |
DocumentModelBridge document = mockDocument(DOCUMENT_REFERENCE); |
159 |
1 |
DocumentModelBridge sheet = mockDocument(SHEET_REFERENCE); |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
1 |
setCurrentDocument(mockDocument(DOCUMENT_REFERENCE)); |
164 |
|
|
165 |
|
|
166 |
|
|
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 |
|
|
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 |
|
|
184 |
1 |
verify(this.modelBridge).setSecurityDocument(originalSecurityDoc); |
185 |
|
|
186 |
|
|
187 |
1 |
verify(this.documentAccessBridge).popDocumentFromContext(backupObjects); |
188 |
|
} |
189 |
|
} |