1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.localization.wiki.internal

File DocumentTranslationBundleFactoryTest.java

 

Code metrics

12
62
9
1
228
153
15
0.24
6.89
9
1.67

Classes

Class Line # Actions
DocumentTranslationBundleFactoryTest 59 62 0% 15 12
0.8554216685.5%
 

Contributing tests

This file is covered by 3 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 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
 
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   
 
72  3 toggle public DocumentTranslationBundleFactoryTest()
73    {
74  3 this.oldcore.notifyDocumentCreatedEvent(true);
75  3 this.oldcore.notifyDocumentUpdatedEvent(true);
76  3 this.oldcore.notifyDocumentDeletedEvent(true);
77    }
78   
 
79  3 toggle @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    // Initialize document bundle factory
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    // We want to be notified about new components registrations
105  3 this.oldcore.notifyComponentDescriptorEvent();
106    }
107   
 
108  3 toggle @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   
 
115  3 toggle 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   
 
158  8 toggle 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   
 
170  3 toggle private void resetContext() throws ComponentLookupException
171    {
172  3 this.oldcore.getExecutionContext().removeProperty(DefaultTranslationBundleContext.CKEY_BUNDLES);
173    }
174   
175    // tests
176   
 
177  1 toggle @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    // Reset the cache of translation bundles associated to the current context
189  1 resetContext();
190   
191  1 assertTranslation("wiki.translation", "Wiki translation", Locale.ROOT);
192    }
193   
 
194  1 toggle @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    // Reset the cache of translation bundles associated to the current context
205  1 resetContext();
206   
207  1 assertTranslation("wiki.translation", null, Locale.ROOT);
208    }
209   
 
210  1 toggle @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    // Reset the cache of translation bundles associated to the current context
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    }