1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.internal.sheet.scripting; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
|
24 |
|
import org.junit.Assert; |
25 |
|
import org.junit.Before; |
26 |
|
import org.junit.Rule; |
27 |
|
import org.junit.Test; |
28 |
|
import org.mockito.ArgumentMatcher; |
29 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
30 |
|
import org.xwiki.bridge.DocumentModelBridge; |
31 |
|
import org.xwiki.configuration.ConfigurationSource; |
32 |
|
import org.xwiki.model.reference.DocumentReference; |
33 |
|
import org.xwiki.rendering.syntax.Syntax; |
34 |
|
import org.xwiki.script.service.ScriptService; |
35 |
|
import org.xwiki.sheet.SheetBinder; |
36 |
|
import org.xwiki.sheet.SheetManager; |
37 |
|
import org.xwiki.test.annotation.AfterComponent; |
38 |
|
import org.xwiki.test.annotation.AllComponents; |
39 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
40 |
|
|
41 |
|
import com.xpn.xwiki.api.Document; |
42 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
43 |
|
import com.xpn.xwiki.script.sheet.SheetScriptService; |
44 |
|
import com.xpn.xwiki.web.Utils; |
45 |
|
|
46 |
|
import static org.mockito.ArgumentMatchers.argThat; |
47 |
|
import static org.mockito.ArgumentMatchers.same; |
48 |
|
import static org.mockito.Mockito.when; |
49 |
|
|
50 |
|
|
51 |
|
@link |
52 |
|
|
53 |
|
@version |
54 |
|
|
55 |
|
|
56 |
|
@AllComponents |
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 5 |
Complexity Density: 0.19 |
|
57 |
|
public class SheetScriptServiceTest |
58 |
|
{ |
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@Rule |
63 |
|
public final MockitoComponentMockingRule<ScriptService> mocker = |
64 |
|
new MockitoComponentMockingRule<ScriptService>(SheetScriptService.class); |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private SheetScriptService sheetScriptService; |
70 |
|
|
71 |
|
private SheetBinder mockClassSheetBinder; |
72 |
|
|
73 |
|
private SheetBinder mockDocumentSheetBinder; |
74 |
|
|
75 |
|
private DocumentAccessBridge mockDocumentAccessBridge; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@throws |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
82 |
2 |
@Before... |
83 |
|
public void setUp() throws Exception |
84 |
|
{ |
85 |
|
|
86 |
2 |
Utils.setComponentManager(mocker); |
87 |
2 |
this.sheetScriptService = (SheetScriptService) mocker.getComponentUnderTest(); |
88 |
|
} |
89 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
90 |
2 |
@AfterComponent... |
91 |
|
public void afterComponent() throws Exception |
92 |
|
{ |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
2 |
this.mockClassSheetBinder = this.mocker.registerMockComponent(SheetBinder.class, "class"); |
97 |
2 |
this.mockDocumentSheetBinder = this.mocker.registerMockComponent(SheetBinder.class, "document"); |
98 |
2 |
this.mockDocumentAccessBridge = this.mocker.registerMockComponent(DocumentAccessBridge.class); |
99 |
|
|
100 |
2 |
this.mocker.registerMockComponent(SheetManager.class); |
101 |
2 |
this.mocker.registerMockComponent(ConfigurationSource.class, "all"); |
102 |
2 |
this.mocker.registerMockComponent(ConfigurationSource.class, "xwikiproperties"); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
@link |
107 |
|
@link |
108 |
|
|
109 |
|
@throws |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void bindClassSheetClonesDocument() throws Exception |
113 |
|
{ |
114 |
1 |
DocumentReference classReference = new DocumentReference("wiki", "Space", "MyClass"); |
115 |
1 |
final XWikiDocument classDocument = new XWikiDocument(classReference); |
116 |
1 |
classDocument.setSyntax(Syntax.PLAIN_1_0); |
117 |
1 |
Document classDocumentApi = new Document(classDocument, null); |
118 |
|
|
119 |
1 |
final DocumentReference sheetReference = |
120 |
|
new DocumentReference("MySheet", classReference.getLastSpaceReference()); |
121 |
|
|
122 |
1 |
when(this.mockClassSheetBinder.bind(argThat(new ArgumentMatcher<DocumentModelBridge>() |
123 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
124 |
1 |
@Override... |
125 |
|
public boolean matches(DocumentModelBridge argument) |
126 |
|
{ |
127 |
1 |
return classDocument.equals(argument) && classDocument != argument; |
128 |
|
} |
129 |
|
}), same(sheetReference))).thenReturn(true); |
130 |
|
|
131 |
1 |
Assert.assertTrue(this.sheetScriptService.bindClassSheet(classDocumentApi, sheetReference)); |
132 |
|
} |
133 |
|
|
134 |
|
|
135 |
|
@link |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void getDocuments() throws Exception |
139 |
|
{ |
140 |
1 |
DocumentReference sheetReference = new DocumentReference("wiki", "Space", "Sheet"); |
141 |
1 |
DocumentReference publicDocumentReference = new DocumentReference("wiki", "Space", "PublicPage"); |
142 |
1 |
DocumentReference privateDocumentReference = new DocumentReference("wiki", "Space", "PrivatePage"); |
143 |
1 |
DocumentReference publicClassReference = new DocumentReference("wiki", "Space", "PublicClass"); |
144 |
1 |
DocumentReference privateClassReference = new DocumentReference("wiki", "Space", "PrivateClass"); |
145 |
|
|
146 |
1 |
when(this.mockDocumentSheetBinder.getDocuments(sheetReference)) |
147 |
|
.thenReturn(Arrays.asList(publicDocumentReference, privateDocumentReference)); |
148 |
|
|
149 |
1 |
when(this.mockClassSheetBinder.getDocuments(sheetReference)) |
150 |
|
.thenReturn(Arrays.asList(privateClassReference, publicClassReference)); |
151 |
|
|
152 |
1 |
when(this.mockDocumentAccessBridge.isDocumentViewable(publicClassReference)).thenReturn(true); |
153 |
1 |
when(this.mockDocumentAccessBridge.isDocumentViewable(publicDocumentReference)).thenReturn(true); |
154 |
|
|
155 |
1 |
Assert.assertEquals(Arrays.asList(publicDocumentReference, publicClassReference), |
156 |
|
this.sheetScriptService.getDocuments(sheetReference)); |
157 |
|
} |
158 |
|
} |