1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.web

File XWikiMessageToolBridgeTest.java

 

Code metrics

6
97
16
1
305
202
19
0.2
6.06
16
1.19

Classes

Class Line # Actions
XWikiMessageToolBridgeTest 43 97 0% 19 1
0.9915966499.2%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.util.Locale;
23   
24    import org.junit.Assert;
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.component.manager.ComponentLookupException;
29    import org.xwiki.environment.Environment;
30    import org.xwiki.extension.repository.internal.core.CoreExtensionScanner;
31    import org.xwiki.localization.ContextualLocalizationManager;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.rendering.syntax.Syntax;
34    import org.xwiki.test.annotation.AfterComponent;
35    import org.xwiki.test.annotation.AllComponents;
36   
37    import com.xpn.xwiki.XWikiException;
38    import com.xpn.xwiki.doc.XWikiDocument;
39    import com.xpn.xwiki.objects.BaseObject;
40    import com.xpn.xwiki.test.MockitoOldcoreRule;
41   
42    @AllComponents
 
43    public class XWikiMessageToolBridgeTest
44    {
45    @Rule
46    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
47   
48    private XWikiMessageTool tool;
49   
50    private DocumentReference preferencesDocumentReference;
51   
52    private DocumentReference defaultWikiTranslationReference;
53   
54    private static final String TRANSLATION_FULLNAME = "XWiki.Translations";
55   
 
56  10 toggle public XWikiMessageToolBridgeTest()
57    {
58  10 this.oldcore.notifyDocumentCreatedEvent(true);
59  10 this.oldcore.notifyDocumentUpdatedEvent(true);
60  10 this.oldcore.notifyDocumentDeletedEvent(true);
61    }
62   
 
63  10 toggle @AfterComponent
64    public void afterComponent() throws Exception
65    {
66    // Register a mock Environment since we're in a test and we don't want spurious logs in the console.
67  10 this.oldcore.getMocker().registerMockComponent(Environment.class);
68    // Skip core extension scanner
69  10 this.oldcore.getMocker().registerMockComponent(CoreExtensionScanner.class);
70    }
71   
 
72  10 toggle @Before
73    public void before() throws Exception
74    {
75  10 Locale.setDefault(Locale.ROOT);
76   
77    // checking
78   
79  10 this.preferencesDocumentReference =
80    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences");
81   
82  10 XWikiDocument preferencesDocument = new XWikiDocument(this.preferencesDocumentReference);
83  10 BaseObject preferencesObject = new BaseObject();
84  10 preferencesObject.setXClassReference(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki",
85    "XWikiPreferences"));
86  10 preferencesDocument.addXObject(preferencesObject);
87  10 preferencesDocument.setSyntax(Syntax.PLAIN_1_0);
88  10 this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());
89   
90  10 this.defaultWikiTranslationReference =
91    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "Translations");
92   
93  10 XWikiDocument defaultWikiTranslation = new XWikiDocument(this.defaultWikiTranslationReference);
94  10 defaultWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
95  10 this.oldcore.getSpyXWiki().saveDocument(defaultWikiTranslation, "", this.oldcore.getXWikiContext());
96   
97    // MessageTool
98   
99  10 this.tool =
100    new XWikiMessageTool(this.oldcore.getMocker().<ContextualLocalizationManager>getInstance(
101    ContextualLocalizationManager.class));
102    }
103   
 
104  13 toggle private void setBundles(String bundles) throws XWikiException
105    {
106  13 XWikiDocument preferencesDocument =
107    this.oldcore.getSpyXWiki().getDocument(this.preferencesDocumentReference, this.oldcore.getXWikiContext());
108  13 BaseObject preferencesObject = preferencesDocument.getXObject();
109   
110  13 if (!bundles.equals(preferencesObject.getStringValue("documentBundles"))) {
111  9 preferencesObject.setStringValue("documentBundles", bundles);
112   
113  9 this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());
114    }
115    }
116   
 
117  11 toggle private void addWikiTranslation(String key, String message, Locale locale) throws XWikiException
118    {
119  11 XWikiDocument document =
120    this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference,
121    this.oldcore.getXWikiContext());
122   
123  11 if (!locale.equals(Locale.ROOT)) {
124  3 XWikiDocument translatedDocument = document.getTranslatedDocument(locale, this.oldcore.getXWikiContext());
125  3 if (translatedDocument == document) {
126  3 translatedDocument = new XWikiDocument(document.getDocumentReference(), locale);
127  3 translatedDocument.setDefaultLocale(document.getDefaultLocale());
128    }
129  3 document = translatedDocument;
130    }
131   
132  11 document.setSyntax(Syntax.PLAIN_1_0);
133   
134  11 StringBuilder builder = new StringBuilder(document.getContent());
135   
136  11 builder.append('\n');
137  11 builder.append(key);
138  11 builder.append('=');
139  11 builder.append(message);
140   
141  11 document.setContent(builder.toString());
142   
143  11 this.oldcore.getSpyXWiki().saveDocument(document, "", this.oldcore.getXWikiContext());
144   
145  11 setBundles(document.getFullName());
146    }
147   
 
148  1 toggle private void deleteWikiTranslation() throws XWikiException
149    {
150  1 this.oldcore.getSpyXWiki().deleteAllDocuments(
151    this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference,
152    this.oldcore.getXWikiContext()), this.oldcore.getXWikiContext());
153    }
154   
155    // tests
156   
 
157  1 toggle @Test
158    public void getInvalidTranslation()
159    {
160  1 Assert.assertEquals("doesnotexists.translation", this.tool.get("doesnotexists.translation"));
161    }
162   
 
163  1 toggle @Test
164    public void getResourceTranslation()
165    {
166  1 Assert.assertEquals("Language", this.tool.get("language"));
167    }
168   
 
169  1 toggle @Test
170    public void getWikiTranslation() throws XWikiException, ComponentLookupException
171    {
172  1 addWikiTranslation("wiki.translation", "Wiki translation", Locale.ROOT);
173   
174  1 Assert.assertEquals("Wiki translation", this.tool.get("wiki.translation"));
175    }
176   
 
177  1 toggle @Test
178    public void getTranslatedWikiTranslation() throws XWikiException, ComponentLookupException
179    {
180  1 addWikiTranslation("wiki.translation", "English translation", Locale.ENGLISH);
181  1 addWikiTranslation("wiki.translation", "French translation", Locale.FRENCH);
182   
183  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
184   
185  1 Assert.assertEquals("French translation", this.tool.get("wiki.translation"));
186   
187  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
188   
189  1 Assert.assertEquals("English translation", this.tool.get("wiki.translation"));
190    }
191   
 
192  1 toggle @Test
193    public void fallackOnParentLocale() throws XWikiException, ComponentLookupException
194    {
195  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
196   
197  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
198   
199  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
200   
201  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
202   
203  1 this.oldcore.getXWikiContext().setLocale(Locale.US);
204   
205  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
206   
207  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
208  1 addWikiTranslation("wiki.frenchtranslation", "French translation", Locale.FRENCH);
209   
210  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
211  1 Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation"));
212   
213  1 this.oldcore.getXWikiContext().setLocale(Locale.FRANCE);
214   
215  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
216  1 Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation"));
217    }
218   
 
219  1 toggle @Test
220    public void updateWikiTranslationCache() throws XWikiException, ComponentLookupException
221    {
222  1 setBundles(TRANSLATION_FULLNAME);
223   
224  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
225   
226  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
227   
228  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
229   
230  1 addWikiTranslation("wiki.anothertranslation", "Another translation", Locale.ROOT);
231   
232  1 Assert.assertEquals("Another translation", this.tool.get("wiki.anothertranslation"));
233   
234  1 deleteWikiTranslation();
235   
236  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
237  1 Assert.assertEquals("wiki.anothertranslation", this.tool.get("wiki.anothertranslation"));
238    }
239   
 
240  1 toggle @Test
241    public void updateWikiPreferencesCache() throws XWikiException, ComponentLookupException
242    {
243  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
244   
245  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
246   
247  1 XWikiDocument otherWikiTranslation =
248    new XWikiDocument(new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki",
249    "OtherTranslations"));
250  1 otherWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
251  1 otherWikiTranslation.setContent("wiki.othertranslation=Other translation");
252  1 this.oldcore.getSpyXWiki().saveDocument(otherWikiTranslation, "", this.oldcore.getXWikiContext());
253   
254  1 setBundles(" " + otherWikiTranslation.getFullName());
255   
256  1 Assert.assertEquals("Other translation", this.tool.get("wiki.othertranslation"));
257   
258  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
259    }
260   
 
261  1 toggle @Test
262    public void getTranslationWithParameters() throws XWikiException, ComponentLookupException
263    {
264  1 addWikiTranslation("wiki.translation", "{1} {0}", Locale.ROOT);
265   
266  1 Assert.assertEquals("Parameter translation", this.tool.get("wiki.translation", "translation", "Parameter"));
267    }
268   
 
269  1 toggle @Test
270    public void getEmptyWikiTranslation() throws XWikiException, ComponentLookupException
271    {
272  1 addWikiTranslation("wiki.translation", "", Locale.ROOT);
273   
274  1 Assert.assertEquals("", this.tool.get("wiki.translation"));
275    }
276   
 
277  1 toggle @Test
278    public void fallbackOnResource() throws XWikiException, ComponentLookupException
279    {
280  1 Assert.assertEquals("Language", this.tool.get("language"));
281   
282  1 XWikiDocument defaultWikiTranslation =
283    this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference,
284    this.oldcore.getXWikiContext());
285  1 defaultWikiTranslation.setDefaultLocale(Locale.FRENCH);
286   
287  1 addWikiTranslation("language", "Overwritten language", Locale.ROOT);
288   
289    // ROOT language has been overwritten
290  1 this.oldcore.getXWikiContext().setLocale(Locale.ROOT);
291  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
292   
293    // The real locale of ROOT version in FRENCH so it's overwritten too
294  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
295  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
296   
297    // GERMAN hasn't been overwritten
298  1 this.oldcore.getXWikiContext().setLocale(Locale.GERMAN);
299  1 Assert.assertEquals("Sprache", this.tool.get("language"));
300   
301    // There is no ENGLISH translation for this key so it fallback on ROOT
302  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
303  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
304    }
305    }