1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.internal.manager; |
21 |
|
|
22 |
|
import javax.inject.Provider; |
23 |
|
|
24 |
|
import org.junit.Before; |
25 |
|
import org.junit.Rule; |
26 |
|
import org.junit.Test; |
27 |
|
import org.slf4j.Logger; |
28 |
|
import org.xwiki.bridge.event.WikiCopiedEvent; |
29 |
|
import org.xwiki.bridge.event.WikiCreateFailedEvent; |
30 |
|
import org.xwiki.bridge.event.WikiCreatedEvent; |
31 |
|
import org.xwiki.bridge.event.WikiCreatingEvent; |
32 |
|
import org.xwiki.bridge.event.WikiDeletedEvent; |
33 |
|
import org.xwiki.observation.ObservationManager; |
34 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
35 |
|
import org.xwiki.wiki.descriptor.WikiDescriptor; |
36 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
37 |
|
import org.xwiki.wiki.internal.descriptor.DefaultWikiDescriptor; |
38 |
|
import org.xwiki.wiki.manager.WikiManagerException; |
39 |
|
import org.xwiki.wiki.provisioning.WikiCopier; |
40 |
|
|
41 |
|
import com.xpn.xwiki.XWikiContext; |
42 |
|
import com.xpn.xwiki.store.XWikiStoreInterface; |
43 |
|
|
44 |
|
import static org.junit.Assert.assertFalse; |
45 |
|
import static org.junit.Assert.assertNotNull; |
46 |
|
import static org.junit.Assert.assertTrue; |
47 |
|
import static org.mockito.ArgumentMatchers.any; |
48 |
|
import static org.mockito.ArgumentMatchers.eq; |
49 |
|
import static org.mockito.Mockito.mock; |
50 |
|
import static org.mockito.Mockito.verify; |
51 |
|
import static org.mockito.Mockito.when; |
52 |
|
|
53 |
|
|
54 |
|
@link |
55 |
|
|
56 |
|
@version |
57 |
|
@since |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (87) |
Complexity: 13 |
Complexity Density: 0.17 |
|
59 |
|
public class DefaultWikiManagerTest |
60 |
|
{ |
61 |
|
@Rule |
62 |
|
public MockitoComponentMockingRule<DefaultWikiManager> mocker = |
63 |
|
new MockitoComponentMockingRule(DefaultWikiManager.class); |
64 |
|
|
65 |
|
private WikiDescriptorManager wikiDescriptorManager; |
66 |
|
|
67 |
|
private Provider<XWikiContext> xcontextProvider; |
68 |
|
|
69 |
|
private ObservationManager observationManager; |
70 |
|
|
71 |
|
private Logger logger; |
72 |
|
|
73 |
|
private XWikiContext xcontext; |
74 |
|
|
75 |
|
private com.xpn.xwiki.XWiki xwiki; |
76 |
|
|
77 |
|
private XWikiStoreInterface store; |
78 |
|
|
79 |
|
private WikiCreator wikiCreator; |
80 |
|
|
81 |
|
private WikiCopier wikiCopier; |
82 |
|
|
83 |
|
private WikiDeleter wikiDeleter; |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
85 |
8 |
@Before... |
86 |
|
public void setUp() throws Exception |
87 |
|
{ |
88 |
|
|
89 |
8 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
90 |
8 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
91 |
8 |
observationManager = mocker.getInstance(ObservationManager.class); |
92 |
8 |
wikiCopier = mocker.getInstance(WikiCopier.class); |
93 |
8 |
wikiDeleter = mocker.getInstance(WikiDeleter.class); |
94 |
8 |
wikiCreator = mocker.getInstance(WikiCreator.class); |
95 |
|
|
96 |
|
|
97 |
8 |
xcontext = mock(XWikiContext.class); |
98 |
8 |
when(xcontextProvider.get()).thenReturn(xcontext); |
99 |
8 |
xwiki = mock(com.xpn.xwiki.XWiki.class); |
100 |
8 |
when(xcontext.getWiki()).thenReturn(xwiki); |
101 |
8 |
when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki"); |
102 |
8 |
store = mock(XWikiStoreInterface.class); |
103 |
8 |
when(xwiki.getStore()).thenReturn(store); |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void idAvailable() throws Exception |
108 |
|
{ |
109 |
|
|
110 |
1 |
when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden,wikiid3,toto"); |
111 |
1 |
when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true); |
112 |
|
|
113 |
|
|
114 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(true); |
115 |
1 |
assertFalse(this.mocker.getComponentUnderTest().idAvailable("wikiid1")); |
116 |
|
|
117 |
|
|
118 |
1 |
when(wikiDescriptorManager.exists("wikiid2")).thenReturn(false); |
119 |
1 |
assertTrue(this.mocker.getComponentUnderTest().idAvailable("wikiid2")); |
120 |
|
|
121 |
|
|
122 |
1 |
when(wikiDescriptorManager.exists("wikiid3")).thenReturn(false); |
123 |
1 |
assertFalse(this.mocker.getComponentUnderTest().idAvailable("wikiid3")); |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
126 |
1 |
@Test... |
127 |
|
public void createWhenWikiExists() throws Exception |
128 |
|
{ |
129 |
|
|
130 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(true); |
131 |
|
|
132 |
1 |
boolean exceptionCaught = false; |
133 |
1 |
try { |
134 |
1 |
this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true); |
135 |
|
} catch (WikiManagerException e) { |
136 |
1 |
exceptionCaught = true; |
137 |
|
} |
138 |
1 |
assertTrue(exceptionCaught); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
141 |
1 |
@Test... |
142 |
|
public void createWhenWikiIdIsForbidden() throws Exception |
143 |
|
{ |
144 |
|
|
145 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false); |
146 |
|
|
147 |
|
|
148 |
1 |
when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden,wikiid1"); |
149 |
|
|
150 |
1 |
boolean exceptionCaught = false; |
151 |
1 |
try { |
152 |
1 |
this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true); |
153 |
|
} catch (WikiManagerException e) { |
154 |
1 |
exceptionCaught = true; |
155 |
|
} |
156 |
1 |
assertTrue(exceptionCaught); |
157 |
|
} |
158 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
159 |
1 |
@Test... |
160 |
|
public void createWhenWikiIdIsValid() throws Exception |
161 |
|
{ |
162 |
|
|
163 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false); |
164 |
|
|
165 |
|
|
166 |
1 |
when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden"); |
167 |
|
|
168 |
|
|
169 |
1 |
when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true); |
170 |
|
|
171 |
1 |
DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1"); |
172 |
1 |
when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor); |
173 |
|
|
174 |
|
|
175 |
1 |
WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true); |
176 |
|
|
177 |
|
|
178 |
1 |
assertNotNull(newWikiDescriptor); |
179 |
|
|
180 |
|
|
181 |
1 |
assertTrue(newWikiDescriptor instanceof DefaultWikiDescriptor); |
182 |
|
|
183 |
|
|
184 |
1 |
verify(wikiCreator).create("wikiid1", "wikialias1"); |
185 |
|
|
186 |
|
|
187 |
1 |
verify(observationManager).notify(new WikiCreatingEvent("wikiid1"), "wikiid1", xcontext); |
188 |
1 |
verify(observationManager).notify(new WikiCreatedEvent("wikiid1"), "wikiid1", xcontext); |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
191 |
1 |
@Test... |
192 |
|
public void createWhenWikiIdIsValidButFail() throws Exception |
193 |
|
{ |
194 |
|
|
195 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false); |
196 |
|
|
197 |
|
|
198 |
1 |
when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden"); |
199 |
|
|
200 |
|
|
201 |
1 |
when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true); |
202 |
|
|
203 |
1 |
DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1"); |
204 |
1 |
when(wikiCreator.create("wikiid1", "wikialias1")).thenThrow(new WikiManagerException("...")); |
205 |
|
|
206 |
|
|
207 |
1 |
boolean exceptionCaught = false; |
208 |
1 |
try { |
209 |
1 |
this.mocker.getComponentUnderTest().create("wikiid1", "wikialias1", true); |
210 |
|
} catch (WikiManagerException e) { |
211 |
1 |
exceptionCaught = true; |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
1 |
assertTrue(exceptionCaught); |
216 |
|
|
217 |
|
|
218 |
1 |
verify(observationManager).notify(new WikiCreatingEvent("wikiid1"), "wikiid1", xcontext); |
219 |
1 |
verify(observationManager).notify(new WikiCreateFailedEvent("wikiid1"), "wikiid1", xcontext); |
220 |
|
} |
221 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
222 |
1 |
@Test... |
223 |
|
public void deleteWiki() throws Exception |
224 |
|
{ |
225 |
1 |
this.mocker.getComponentUnderTest().delete("wikiid"); |
226 |
1 |
verify(wikiDeleter).delete("wikiid"); |
227 |
1 |
verify(observationManager).notify(eq(new WikiDeletedEvent("wikiid")), eq("wikiid")); |
228 |
|
} |
229 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
230 |
1 |
@Test... |
231 |
|
public void copyWhenWikiAlreadyExists() throws Exception |
232 |
|
{ |
233 |
1 |
when(store.isWikiNameAvailable(any(String.class), any(XWikiContext.class))).thenReturn(true); |
234 |
|
|
235 |
1 |
when(wikiDescriptorManager.exists("existingid")).thenReturn(true); |
236 |
1 |
boolean exceptionCaught = false; |
237 |
1 |
try { |
238 |
1 |
this.mocker.getComponentUnderTest().copy("wikiid", "existingid", "newwikialias", true, true, true); |
239 |
|
} catch (WikiManagerException e) { |
240 |
1 |
exceptionCaught = true; |
241 |
|
} |
242 |
1 |
assertTrue(exceptionCaught); |
243 |
|
} |
244 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
245 |
1 |
@Test... |
246 |
|
public void copyWhenWikiAvailable() throws Exception |
247 |
|
{ |
248 |
|
|
249 |
1 |
when(wikiDescriptorManager.exists("wikiid1")).thenReturn(false); |
250 |
|
|
251 |
|
|
252 |
1 |
when(xwiki.Param("xwiki.virtual.reserved_wikis")).thenReturn("forbidden"); |
253 |
|
|
254 |
|
|
255 |
1 |
when(store.isWikiNameAvailable(eq("wikiid1"), any(XWikiContext.class))).thenReturn(true); |
256 |
|
|
257 |
|
|
258 |
1 |
DefaultWikiDescriptor descriptor = new DefaultWikiDescriptor("wikiid1", "wikialias1"); |
259 |
1 |
when(wikiCreator.create("wikiid1", "wikialias1")).thenReturn(descriptor); |
260 |
|
|
261 |
|
|
262 |
1 |
WikiDescriptor newWikiDescriptor = this.mocker.getComponentUnderTest().copy("wikiid", "wikiid1", |
263 |
|
"wikialias1", true, true, true); |
264 |
1 |
assertNotNull(newWikiDescriptor); |
265 |
|
|
266 |
|
|
267 |
1 |
verify(wikiCreator).create("wikiid1", "wikialias1"); |
268 |
|
|
269 |
1 |
verify(wikiCopier).copyDocuments(eq("wikiid"), eq("wikiid1"), eq(true)); |
270 |
|
|
271 |
1 |
verify(wikiCopier).copyDeletedDocuments(eq("wikiid"), eq("wikiid1")); |
272 |
|
|
273 |
1 |
verify(observationManager).notify(new WikiCopiedEvent("wikiid", "wikiid1"), "wikiid", xcontext); |
274 |
|
} |
275 |
|
} |
276 |
|
|