1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File XWikiMessageToolBridgeTest.java

 

Code metrics

6
96
16
1
297
195
19
0.2
6
16
1.19

Classes

Class Line # Actions
XWikiMessageToolBridgeTest 43 96 0% 19 1
0.991525499.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.localization.ContextualLocalizationManager;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.LocalDocumentReference;
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    }
69   
 
70  10 toggle @Before
71    public void before() throws Exception
72    {
73  10 Locale.setDefault(Locale.ROOT);
74   
75    // checking
76   
77  10 this.preferencesDocumentReference =
78    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "XWikiPreferences");
79   
80  10 XWikiDocument preferencesDocument = new XWikiDocument(this.preferencesDocumentReference);
81  10 BaseObject preferencesObject = new BaseObject();
82  10 preferencesObject.setXClassReference(new LocalDocumentReference("XWiki", "XWikiPreferences"));
83  10 preferencesDocument.addXObject(preferencesObject);
84  10 preferencesDocument.setSyntax(Syntax.PLAIN_1_0);
85  10 this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());
86   
87  10 this.defaultWikiTranslationReference =
88    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "Translations");
89   
90  10 XWikiDocument defaultWikiTranslation = new XWikiDocument(this.defaultWikiTranslationReference);
91  10 defaultWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
92  10 this.oldcore.getSpyXWiki().saveDocument(defaultWikiTranslation, "", this.oldcore.getXWikiContext());
93   
94    // MessageTool
95   
96  10 this.tool = new XWikiMessageTool(
97    this.oldcore.getMocker().<ContextualLocalizationManager>getInstance(ContextualLocalizationManager.class));
98    }
99   
 
100  13 toggle private void setBundles(String bundles) throws XWikiException
101    {
102  13 XWikiDocument preferencesDocument =
103    this.oldcore.getSpyXWiki().getDocument(this.preferencesDocumentReference, this.oldcore.getXWikiContext());
104  13 BaseObject preferencesObject = preferencesDocument.getXObject();
105   
106  13 if (!bundles.equals(preferencesObject.getStringValue("documentBundles"))) {
107  9 preferencesObject.setStringValue("documentBundles", bundles);
108   
109  9 this.oldcore.getSpyXWiki().saveDocument(preferencesDocument, "", this.oldcore.getXWikiContext());
110    }
111    }
112   
 
113  11 toggle private void addWikiTranslation(String key, String message, Locale locale) throws XWikiException
114    {
115  11 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(this.defaultWikiTranslationReference,
116    this.oldcore.getXWikiContext());
117   
118  11 if (!locale.equals(Locale.ROOT)) {
119  3 XWikiDocument translatedDocument = document.getTranslatedDocument(locale, this.oldcore.getXWikiContext());
120  3 if (translatedDocument == document) {
121  3 translatedDocument = new XWikiDocument(document.getDocumentReference(), locale);
122  3 translatedDocument.setDefaultLocale(document.getDefaultLocale());
123    }
124  3 document = translatedDocument;
125    }
126   
127  11 document.setSyntax(Syntax.PLAIN_1_0);
128   
129  11 StringBuilder builder = new StringBuilder(document.getContent());
130   
131  11 builder.append('\n');
132  11 builder.append(key);
133  11 builder.append('=');
134  11 builder.append(message);
135   
136  11 document.setContent(builder.toString());
137   
138  11 this.oldcore.getSpyXWiki().saveDocument(document, "", this.oldcore.getXWikiContext());
139   
140  11 setBundles(document.getFullName());
141    }
142   
 
143  1 toggle private void deleteWikiTranslation() throws XWikiException
144    {
145  1 this.oldcore.getSpyXWiki().deleteAllDocuments(this.oldcore.getSpyXWiki().getDocument(
146    this.defaultWikiTranslationReference, this.oldcore.getXWikiContext()), this.oldcore.getXWikiContext());
147    }
148   
149    // tests
150   
 
151  1 toggle @Test
152    public void getInvalidTranslation()
153    {
154  1 Assert.assertEquals("doesnotexists.translation", this.tool.get("doesnotexists.translation"));
155    }
156   
 
157  1 toggle @Test
158    public void getResourceTranslation()
159    {
160  1 Assert.assertEquals("Language", this.tool.get("language"));
161    }
162   
 
163  1 toggle @Test
164    public void getWikiTranslation() throws XWikiException, ComponentLookupException
165    {
166  1 addWikiTranslation("wiki.translation", "Wiki translation", Locale.ROOT);
167   
168  1 Assert.assertEquals("Wiki translation", this.tool.get("wiki.translation"));
169    }
170   
 
171  1 toggle @Test
172    public void getTranslatedWikiTranslation() throws XWikiException, ComponentLookupException
173    {
174  1 addWikiTranslation("wiki.translation", "English translation", Locale.ENGLISH);
175  1 addWikiTranslation("wiki.translation", "French translation", Locale.FRENCH);
176   
177  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
178   
179  1 Assert.assertEquals("French translation", this.tool.get("wiki.translation"));
180   
181  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
182   
183  1 Assert.assertEquals("English translation", this.tool.get("wiki.translation"));
184    }
185   
 
186  1 toggle @Test
187    public void fallackOnParentLocale() throws XWikiException, ComponentLookupException
188    {
189  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
190   
191  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
192   
193  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
194   
195  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
196   
197  1 this.oldcore.getXWikiContext().setLocale(Locale.US);
198   
199  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
200   
201  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
202  1 addWikiTranslation("wiki.frenchtranslation", "French translation", Locale.FRENCH);
203   
204  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
205  1 Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation"));
206   
207  1 this.oldcore.getXWikiContext().setLocale(Locale.FRANCE);
208   
209  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
210  1 Assert.assertEquals("French translation", this.tool.get("wiki.frenchtranslation"));
211    }
212   
 
213  1 toggle @Test
214    public void updateWikiTranslationCache() throws XWikiException, ComponentLookupException
215    {
216  1 setBundles(TRANSLATION_FULLNAME);
217   
218  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
219   
220  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
221   
222  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
223   
224  1 addWikiTranslation("wiki.anothertranslation", "Another translation", Locale.ROOT);
225   
226  1 Assert.assertEquals("Another translation", this.tool.get("wiki.anothertranslation"));
227   
228  1 deleteWikiTranslation();
229   
230  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
231  1 Assert.assertEquals("wiki.anothertranslation", this.tool.get("wiki.anothertranslation"));
232    }
233   
 
234  1 toggle @Test
235    public void updateWikiPreferencesCache() throws XWikiException, ComponentLookupException
236    {
237  1 addWikiTranslation("wiki.defaulttranslation", "Default translation", Locale.ROOT);
238   
239  1 Assert.assertEquals("Default translation", this.tool.get("wiki.defaulttranslation"));
240   
241  1 XWikiDocument otherWikiTranslation = new XWikiDocument(
242    new DocumentReference(this.oldcore.getXWikiContext().getWikiId(), "XWiki", "OtherTranslations"));
243  1 otherWikiTranslation.setSyntax(Syntax.PLAIN_1_0);
244  1 otherWikiTranslation.setContent("wiki.othertranslation=Other translation");
245  1 this.oldcore.getSpyXWiki().saveDocument(otherWikiTranslation, "", this.oldcore.getXWikiContext());
246   
247  1 setBundles(" " + otherWikiTranslation.getFullName());
248   
249  1 Assert.assertEquals("Other translation", this.tool.get("wiki.othertranslation"));
250   
251  1 Assert.assertEquals("wiki.defaulttranslation", this.tool.get("wiki.defaulttranslation"));
252    }
253   
 
254  1 toggle @Test
255    public void getTranslationWithParameters() throws XWikiException, ComponentLookupException
256    {
257  1 addWikiTranslation("wiki.translation", "{1} {0}", Locale.ROOT);
258   
259  1 Assert.assertEquals("Parameter translation", this.tool.get("wiki.translation", "translation", "Parameter"));
260    }
261   
 
262  1 toggle @Test
263    public void getEmptyWikiTranslation() throws XWikiException, ComponentLookupException
264    {
265  1 addWikiTranslation("wiki.translation", "", Locale.ROOT);
266   
267  1 Assert.assertEquals("", this.tool.get("wiki.translation"));
268    }
269   
 
270  1 toggle @Test
271    public void fallbackOnResource() throws XWikiException, ComponentLookupException
272    {
273  1 Assert.assertEquals("Language", this.tool.get("language"));
274   
275  1 XWikiDocument defaultWikiTranslation = this.oldcore.getSpyXWiki()
276    .getDocument(this.defaultWikiTranslationReference, this.oldcore.getXWikiContext());
277  1 defaultWikiTranslation.setDefaultLocale(Locale.FRENCH);
278   
279  1 addWikiTranslation("language", "Overwritten language", Locale.ROOT);
280   
281    // ROOT language has been overwritten
282  1 this.oldcore.getXWikiContext().setLocale(Locale.ROOT);
283  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
284   
285    // The real locale of ROOT version in FRENCH so it's overwritten too
286  1 this.oldcore.getXWikiContext().setLocale(Locale.FRENCH);
287  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
288   
289    // GERMAN hasn't been overwritten
290  1 this.oldcore.getXWikiContext().setLocale(Locale.GERMAN);
291  1 Assert.assertEquals("Sprache", this.tool.get("language"));
292   
293    // There is no ENGLISH translation for this key so it fallback on ROOT
294  1 this.oldcore.getXWikiContext().setLocale(Locale.ENGLISH);
295  1 Assert.assertEquals("Overwritten language", this.tool.get("language"));
296    }
297    }