1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.localization.wiki.internal; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.Locale; |
24 |
|
|
25 |
|
import org.junit.Before; |
26 |
|
import org.junit.Rule; |
27 |
|
import org.junit.Test; |
28 |
|
import org.mockito.Mockito; |
29 |
|
import org.xwiki.component.manager.ComponentLookupException; |
30 |
|
import org.xwiki.configuration.ConfigurationSource; |
31 |
|
import org.xwiki.localization.LocalizationManager; |
32 |
|
import org.xwiki.localization.Translation; |
33 |
|
import org.xwiki.localization.TranslationBundleDoesNotExistsException; |
34 |
|
import org.xwiki.localization.TranslationBundleFactory; |
35 |
|
import org.xwiki.localization.TranslationBundleFactoryDoesNotExistsException; |
36 |
|
import org.xwiki.localization.internal.DefaultTranslationBundleContext; |
37 |
|
import org.xwiki.localization.wiki.internal.TranslationDocumentModel.Scope; |
38 |
|
import org.xwiki.model.reference.DocumentReference; |
39 |
|
import org.xwiki.query.Query; |
40 |
|
import org.xwiki.query.QueryManager; |
41 |
|
import org.xwiki.rendering.syntax.Syntax; |
42 |
|
import org.xwiki.test.annotation.AfterComponent; |
43 |
|
import org.xwiki.test.annotation.AllComponents; |
44 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
45 |
|
|
46 |
|
import com.xpn.xwiki.XWikiContext; |
47 |
|
import com.xpn.xwiki.XWikiException; |
48 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
49 |
|
import com.xpn.xwiki.objects.BaseObject; |
50 |
|
import com.xpn.xwiki.test.MockitoOldcoreRule; |
51 |
|
|
52 |
|
import org.junit.Assert; |
53 |
|
|
54 |
|
import static org.mockito.Mockito.doReturn; |
55 |
|
import static org.mockito.Mockito.mock; |
56 |
|
import static org.mockito.Mockito.when; |
57 |
|
|
58 |
|
@AllComponents |
|
|
| 85.5% |
Uncovered Elements: 12 (83) |
Complexity: 15 |
Complexity Density: 0.24 |
|
59 |
|
public class DocumentTranslationBundleFactoryTest |
60 |
|
{ |
61 |
|
@Rule |
62 |
|
public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(); |
63 |
|
|
64 |
|
private QueryManager mockQueryManager; |
65 |
|
|
66 |
|
private Query mockQuery; |
67 |
|
|
68 |
|
private WikiDescriptorManager mockWikiDescriptorManager; |
69 |
|
|
70 |
|
private LocalizationManager localization; |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
72 |
3 |
public DocumentTranslationBundleFactoryTest()... |
73 |
|
{ |
74 |
3 |
this.oldcore.notifyDocumentCreatedEvent(true); |
75 |
3 |
this.oldcore.notifyDocumentUpdatedEvent(true); |
76 |
3 |
this.oldcore.notifyDocumentDeletedEvent(true); |
77 |
|
} |
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
79 |
3 |
@Before... |
80 |
|
public void before() throws Exception |
81 |
|
{ |
82 |
3 |
this.oldcore.getXWikiContext().setMainXWiki("xwiki"); |
83 |
3 |
this.oldcore.getXWikiContext().setWikiId("xwiki"); |
84 |
|
|
85 |
3 |
doReturn("plain/1.0").when(this.oldcore.getSpyXWiki()).getCurrentContentSyntaxId(Mockito.any(String.class), |
86 |
|
Mockito.any(XWikiContext.class)); |
87 |
|
|
88 |
3 |
this.mockQuery = mock(Query.class); |
89 |
|
|
90 |
3 |
when(this.mockQueryManager.createQuery(Mockito.any(String.class), Mockito.any(String.class))) |
91 |
|
.thenReturn(this.mockQuery); |
92 |
3 |
when(this.mockQuery.execute()).thenReturn(Collections.EMPTY_LIST); |
93 |
|
|
94 |
3 |
when(this.mockWikiDescriptorManager.getMainWikiId()).thenReturn(this.oldcore.getXWikiContext().getMainXWiki()); |
95 |
3 |
when(this.mockWikiDescriptorManager.getCurrentWikiId()).thenReturn(this.oldcore.getXWikiContext().getWikiId()); |
96 |
|
|
97 |
|
|
98 |
3 |
this.oldcore.getMocker().getInstance(TranslationBundleFactory.class, DocumentTranslationBundleFactory.ID); |
99 |
|
|
100 |
3 |
this.localization = this.oldcore.getMocker().getInstance(LocalizationManager.class); |
101 |
|
|
102 |
3 |
this.oldcore.getMocker().registerMockComponent(ConfigurationSource.class); |
103 |
|
|
104 |
|
|
105 |
3 |
this.oldcore.notifyComponentDescriptorEvent(); |
106 |
|
} |
107 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
108 |
3 |
@AfterComponent... |
109 |
|
public void registerComponents() throws Exception |
110 |
|
{ |
111 |
3 |
this.mockQueryManager = this.oldcore.getMocker().registerMockComponent(QueryManager.class); |
112 |
3 |
this.mockWikiDescriptorManager = this.oldcore.getMocker().registerMockComponent(WikiDescriptorManager.class); |
113 |
|
} |
114 |
|
|
|
|
| 63.6% |
Uncovered Elements: 12 (33) |
Complexity: 6 |
Complexity Density: 0.26 |
|
115 |
3 |
private void addTranslation(String key, String message, DocumentReference reference, Locale locale, Scope scope)... |
116 |
|
throws XWikiException |
117 |
|
{ |
118 |
3 |
XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(reference, this.oldcore.getXWikiContext()); |
119 |
|
|
120 |
3 |
if (document.getXObject(TranslationDocumentModel.TRANSLATIONCLASS_REFERENCE) == null) { |
121 |
3 |
BaseObject translationObject = new BaseObject(); |
122 |
3 |
translationObject.setXClassReference( |
123 |
|
new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "TranslationDocumentClass")); |
124 |
3 |
if (scope != null) { |
125 |
3 |
translationObject.setStringValue(TranslationDocumentModel.TRANSLATIONCLASS_PROP_SCOPE, |
126 |
|
scope.toString()); |
127 |
|
} |
128 |
3 |
document.addXObject(translationObject); |
129 |
|
|
130 |
3 |
if (!locale.equals(Locale.ROOT)) { |
131 |
0 |
this.oldcore.getSpyXWiki().saveDocument(document, "", this.oldcore.getXWikiContext()); |
132 |
|
} |
133 |
|
} |
134 |
|
|
135 |
3 |
if (!locale.equals(Locale.ROOT)) { |
136 |
0 |
XWikiDocument tdocument = document.getTranslatedDocument(locale, this.oldcore.getXWikiContext()); |
137 |
0 |
if (tdocument == document) { |
138 |
0 |
tdocument = new XWikiDocument(document.getDocumentReference(), locale); |
139 |
0 |
tdocument.setDefaultLocale(document.getDefaultLocale()); |
140 |
|
} |
141 |
0 |
document = tdocument; |
142 |
|
} |
143 |
|
|
144 |
3 |
document.setSyntax(Syntax.PLAIN_1_0); |
145 |
|
|
146 |
3 |
StringBuilder builder = new StringBuilder(document.getContent()); |
147 |
|
|
148 |
3 |
builder.append('\n'); |
149 |
3 |
builder.append(key); |
150 |
3 |
builder.append('='); |
151 |
3 |
builder.append(message); |
152 |
|
|
153 |
3 |
document.setContent(builder.toString()); |
154 |
|
|
155 |
3 |
this.oldcore.getSpyXWiki().saveDocument(document, "", this.oldcore.getXWikiContext()); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
158 |
8 |
private void assertTranslation(String key, String message, Locale locale)... |
159 |
|
{ |
160 |
8 |
Translation translation = this.localization.getTranslation(key, locale); |
161 |
|
|
162 |
8 |
if (message != null) { |
163 |
2 |
Assert.assertNotNull("No translation could be found for key [" + key + "]", translation); |
164 |
2 |
Assert.assertEquals(message, translation.getRawSource()); |
165 |
|
} else { |
166 |
6 |
Assert.assertNull(translation); |
167 |
|
} |
168 |
|
} |
169 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
170 |
3 |
private void resetContext() throws ComponentLookupException... |
171 |
|
{ |
172 |
3 |
this.oldcore.getExecutionContext().removeProperty(DefaultTranslationBundleContext.CKEY_BUNDLES); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
177 |
1 |
@Test... |
178 |
|
public void getTranslationScopeWiki() throws XWikiException, ComponentLookupException |
179 |
|
{ |
180 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
181 |
|
|
182 |
1 |
addTranslation("wiki.translation", "Wiki translation", |
183 |
|
new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "space", "translation"), Locale.ROOT, |
184 |
|
Scope.WIKI); |
185 |
|
|
186 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
187 |
|
|
188 |
|
|
189 |
1 |
resetContext(); |
190 |
|
|
191 |
1 |
assertTranslation("wiki.translation", "Wiki translation", Locale.ROOT); |
192 |
|
} |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
194 |
1 |
@Test... |
195 |
|
public void getTranslationScopeWikiFromOtherWiki() throws XWikiException, ComponentLookupException |
196 |
|
{ |
197 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
198 |
|
|
199 |
1 |
addTranslation("wiki.translation", "Wiki translation", |
200 |
|
new DocumentReference("otherwiki", "space", "translation"), Locale.ROOT, Scope.WIKI); |
201 |
|
|
202 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
203 |
|
|
204 |
|
|
205 |
1 |
resetContext(); |
206 |
|
|
207 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
208 |
|
} |
209 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
210 |
1 |
@Test... |
211 |
|
public void getTranslationScopeONDemand() throws XWikiException, TranslationBundleDoesNotExistsException, |
212 |
|
TranslationBundleFactoryDoesNotExistsException, ComponentLookupException |
213 |
|
{ |
214 |
1 |
assertTranslation("wiki.translation", null, Locale.ROOT); |
215 |
|
|
216 |
1 |
DocumentReference translationDocument = |
217 |
|
new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "space", "translation"); |
218 |
|
|
219 |
1 |
addTranslation("wiki.translation", "Wiki translation", translationDocument, Locale.ROOT, Scope.ON_DEMAND); |
220 |
|
|
221 |
|
|
222 |
1 |
resetContext(); |
223 |
|
|
224 |
1 |
this.localization.use(DocumentTranslationBundleFactory.ID, translationDocument.toString()); |
225 |
|
|
226 |
1 |
assertTranslation("wiki.translation", "Wiki translation", Locale.ROOT); |
227 |
|
} |
228 |
|
} |