1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wiki.template.internal.migration; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.List; |
24 |
|
|
25 |
|
import org.junit.Before; |
26 |
|
import org.junit.Rule; |
27 |
|
import org.junit.Test; |
28 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
29 |
|
import org.xwiki.context.Execution; |
30 |
|
import org.xwiki.context.ExecutionContext; |
31 |
|
import org.xwiki.model.reference.DocumentReference; |
32 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
33 |
|
import org.xwiki.query.Query; |
34 |
|
import org.xwiki.query.QueryManager; |
35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
36 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
37 |
|
|
38 |
|
import com.xpn.xwiki.XWiki; |
39 |
|
import com.xpn.xwiki.XWikiContext; |
40 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
41 |
|
import com.xpn.xwiki.objects.BaseObject; |
42 |
|
import com.xpn.xwiki.store.migration.XWikiDBVersion; |
43 |
|
import com.xpn.xwiki.store.migration.hibernate.HibernateDataMigration; |
44 |
|
|
45 |
|
import static org.junit.Assert.assertFalse; |
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 |
|
@since |
57 |
|
@version |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (95) |
Complexity: 4 |
Complexity Density: 0.04 |
|
59 |
|
public class WikiTemplateMigrationTest |
60 |
|
{ |
61 |
|
@Rule |
62 |
|
public MockitoComponentMockingRule<WikiTemplateMigration> mocker = |
63 |
|
new MockitoComponentMockingRule(WikiTemplateMigration.class, HibernateDataMigration.class, |
64 |
|
"R54000WikiTemplateMigration"); |
65 |
|
|
66 |
|
private WikiDescriptorManager wikiDescriptorManager; |
67 |
|
|
68 |
|
private QueryManager queryManager; |
69 |
|
|
70 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
71 |
|
|
72 |
|
private Execution execution; |
73 |
|
|
74 |
|
private XWikiContext xcontext; |
75 |
|
|
76 |
|
private XWiki xwiki; |
77 |
|
|
78 |
|
private Query query; |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
80 |
3 |
@Before... |
81 |
|
public void setUp() throws Exception |
82 |
|
{ |
83 |
3 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
84 |
3 |
queryManager = mocker.getInstance(QueryManager.class); |
85 |
|
|
86 |
3 |
documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null, DocumentReferenceResolver.class, String.class)); |
87 |
3 |
execution = mock(Execution.class); |
88 |
3 |
mocker.registerComponent(Execution.class, execution); |
89 |
3 |
xcontext = mock(XWikiContext.class); |
90 |
3 |
xwiki = mock(XWiki.class); |
91 |
|
|
92 |
3 |
ExecutionContext executionContext = mock(ExecutionContext.class); |
93 |
3 |
when(execution.getContext()).thenReturn(executionContext); |
94 |
3 |
when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext); |
95 |
3 |
when(xcontext.getWiki()).thenReturn(xwiki); |
96 |
3 |
when(wikiDescriptorManager.getMainWikiId()).thenReturn("mainWiki"); |
97 |
|
|
98 |
3 |
query = mock(Query.class); |
99 |
3 |
when(queryManager.createQuery(any(), eq(Query.XWQL))).thenReturn(query); |
100 |
|
} |
101 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (71) |
Complexity: 1 |
Complexity Density: 0.01 |
1PASS
|
|
102 |
1 |
@Test... |
103 |
|
public void upgrade() throws Exception |
104 |
|
{ |
105 |
|
|
106 |
1 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki"); |
107 |
|
|
108 |
1 |
String document1 = "XWiki.XWikiServerTemplate"; |
109 |
1 |
String document2 = "XWiki.XWikiServerNotATemplateAnymore"; |
110 |
1 |
String document3 = "XWiki.XWikiServerNotATemplate"; |
111 |
1 |
String document4 = "XWiki.XWikiServerTemplateNow"; |
112 |
|
|
113 |
1 |
DocumentReference documentReference1 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplate"); |
114 |
1 |
DocumentReference documentReference2 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplateAnymore"); |
115 |
1 |
DocumentReference documentReference3 = new DocumentReference("mainWiki", "XWiki", "XWikiServerNotATemplate"); |
116 |
1 |
DocumentReference documentReference4 = new DocumentReference("mainWiki", "XWiki", "XWikiServerTemplateNow"); |
117 |
|
|
118 |
1 |
XWikiDocument doc1 = mock(XWikiDocument.class); |
119 |
1 |
XWikiDocument doc2 = mock(XWikiDocument.class); |
120 |
1 |
XWikiDocument doc3 = mock(XWikiDocument.class); |
121 |
1 |
XWikiDocument doc4 = mock(XWikiDocument.class); |
122 |
|
|
123 |
|
|
124 |
1 |
List<String> documents = new ArrayList<String>(); |
125 |
1 |
documents.add(document1); |
126 |
1 |
documents.add(document2); |
127 |
1 |
documents.add(document3); |
128 |
1 |
documents.add(document4); |
129 |
1 |
when(query.<String>execute()).thenReturn(documents); |
130 |
|
|
131 |
|
|
132 |
1 |
when(documentReferenceResolver.resolve(document1)).thenReturn(documentReference1); |
133 |
1 |
when(documentReferenceResolver.resolve(document2)).thenReturn(documentReference2); |
134 |
1 |
when(documentReferenceResolver.resolve(document3)).thenReturn(documentReference3); |
135 |
1 |
when(documentReferenceResolver.resolve(document4)).thenReturn(documentReference4); |
136 |
|
|
137 |
|
|
138 |
1 |
when(xwiki.getDocument(documentReference1, xcontext)).thenReturn(doc1); |
139 |
1 |
when(xwiki.getDocument(documentReference2, xcontext)).thenReturn(doc2); |
140 |
1 |
when(xwiki.getDocument(documentReference3, xcontext)).thenReturn(doc3); |
141 |
1 |
when(xwiki.getDocument(documentReference4, xcontext)).thenReturn(doc4); |
142 |
|
|
143 |
|
|
144 |
1 |
DocumentReference templateClassReference = new DocumentReference("mainWiki", "WikiManager", "WikiTemplateClass"); |
145 |
|
|
146 |
|
|
147 |
1 |
DocumentReference descriptorClassReference = new DocumentReference("mainWiki", "XWiki", "XWikiServerClass"); |
148 |
|
|
149 |
1 |
BaseObject descriptorObj1 = mock(BaseObject.class); |
150 |
1 |
BaseObject descriptorObj2 = mock(BaseObject.class); |
151 |
1 |
BaseObject descriptorObj3 = mock(BaseObject.class); |
152 |
1 |
BaseObject descriptorObj4 = mock(BaseObject.class); |
153 |
1 |
when(doc1.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj1); |
154 |
1 |
when(doc2.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj2); |
155 |
1 |
when(doc3.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj3); |
156 |
1 |
when(doc4.getXObject(eq(descriptorClassReference))).thenReturn(descriptorObj4); |
157 |
|
|
158 |
1 |
when(descriptorObj1.getIntValue("iswikitemplate", 0)).thenReturn(1); |
159 |
1 |
when(descriptorObj2.getIntValue("iswikitemplate", 0)).thenReturn(1); |
160 |
1 |
when(descriptorObj3.getIntValue("iswikitemplate", 0)).thenReturn(0); |
161 |
1 |
when(descriptorObj4.getIntValue("iswikitemplate", 0)).thenReturn(0); |
162 |
|
|
163 |
1 |
BaseObject wikitemplateObj1 = mock(BaseObject.class); |
164 |
1 |
BaseObject wikitemplateObj2 = mock(BaseObject.class); |
165 |
1 |
BaseObject wikitemplateObj3 = mock(BaseObject.class); |
166 |
1 |
BaseObject wikitemplateObj4 = mock(BaseObject.class); |
167 |
1 |
when(doc1.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj1); |
168 |
1 |
when(doc2.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj2); |
169 |
1 |
when(doc3.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj3); |
170 |
1 |
when(doc4.getXObject(eq(templateClassReference), eq(true), any(XWikiContext.class))).thenReturn(wikitemplateObj4); |
171 |
|
|
172 |
1 |
when(wikitemplateObj1.getIntValue("iswikitemplate", 1)).thenReturn(1); |
173 |
1 |
when(wikitemplateObj2.getIntValue("iswikitemplate", 1)).thenReturn(0); |
174 |
1 |
when(wikitemplateObj3.getIntValue("iswikitemplate", 0)).thenReturn(0); |
175 |
1 |
when(wikitemplateObj4.getIntValue("iswikitemplate", 0)).thenReturn(1); |
176 |
|
|
177 |
|
|
178 |
1 |
mocker.getComponentUnderTest().hibernateMigrate(); |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
1 |
verify(wikitemplateObj1).setIntValue("iswikitemplate", 1); |
184 |
|
|
185 |
1 |
verify(wikitemplateObj2).setIntValue("iswikitemplate", 0); |
186 |
|
|
187 |
1 |
verify(wikitemplateObj3).setIntValue("iswikitemplate", 0); |
188 |
|
|
189 |
1 |
verify(wikitemplateObj4).setIntValue("iswikitemplate", 1); |
190 |
|
|
191 |
|
|
192 |
1 |
verify(descriptorObj1).removeField("iswikitemplate"); |
193 |
1 |
verify(descriptorObj2).removeField("iswikitemplate"); |
194 |
1 |
verify(descriptorObj3).removeField("iswikitemplate"); |
195 |
1 |
verify(descriptorObj4).removeField("iswikitemplate"); |
196 |
|
|
197 |
|
|
198 |
1 |
DocumentReference superadmin = new DocumentReference("mainWiki", "XWiki", "superadmin"); |
199 |
1 |
verify(doc1).setAuthorReference(eq(superadmin)); |
200 |
1 |
verify(doc2).setAuthorReference(eq(superadmin)); |
201 |
1 |
verify(doc3).setAuthorReference(eq(superadmin)); |
202 |
1 |
verify(doc4).setAuthorReference(eq(superadmin)); |
203 |
|
|
204 |
|
|
205 |
1 |
verify(xwiki).saveDocument(doc1, "[UPGRADE] Upgrade the template section.", xcontext); |
206 |
1 |
verify(xwiki).saveDocument(doc2, "[UPGRADE] Upgrade the template section.", xcontext); |
207 |
1 |
verify(xwiki).saveDocument(doc3, "[UPGRADE] Upgrade the template section.", xcontext); |
208 |
1 |
verify(xwiki).saveDocument(doc4, "[UPGRADE] Upgrade the template section.", xcontext); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void shouldExecuteTrue() throws Exception |
213 |
|
{ |
214 |
1 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("mainWiki"); |
215 |
1 |
XWikiDBVersion version = new XWikiDBVersion(52000); |
216 |
1 |
assertTrue(mocker.getComponentUnderTest().shouldExecute(version)); |
217 |
|
} |
218 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
219 |
1 |
@Test... |
220 |
|
public void shouldExecuteFalse() throws Exception |
221 |
|
{ |
222 |
1 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("subwiki"); |
223 |
1 |
XWikiDBVersion version = new XWikiDBVersion(52000); |
224 |
1 |
assertFalse(mocker.getComponentUnderTest().shouldExecute(version)); |
225 |
|
} |
226 |
|
} |