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

File JARTranslationBundleFactoryTest.java

 

Code metrics

4
53
12
1
228
157
14
0.26
4.42
12
1.17

Classes

Class Line # Actions
JARTranslationBundleFactoryTest 66 53 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 5 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.jar.internal;
21   
22    import java.io.File;
23    import java.util.Arrays;
24    import java.util.Date;
25    import java.util.Locale;
26   
27    import org.junit.Assert;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.mockito.Mockito;
32    import org.xwiki.component.internal.ContextComponentManagerProvider;
33    import org.xwiki.component.internal.embed.EmbeddableComponentManagerFactory;
34    import org.xwiki.component.internal.multi.DefaultComponentManagerManager;
35    import org.xwiki.component.manager.ComponentLookupException;
36    import org.xwiki.context.internal.DefaultExecution;
37    import org.xwiki.extension.ExtensionId;
38    import org.xwiki.extension.InstalledExtension;
39    import org.xwiki.extension.event.ExtensionInstalledEvent;
40    import org.xwiki.extension.event.ExtensionUninstalledEvent;
41    import org.xwiki.extension.event.ExtensionUpgradedEvent;
42    import org.xwiki.extension.repository.InstalledExtensionRepository;
43    import org.xwiki.extension.repository.internal.installed.DefaultInstalledExtension;
44    import org.xwiki.extension.repository.internal.local.DefaultLocalExtension;
45    import org.xwiki.extension.test.ExtensionPackager;
46    import org.xwiki.localization.LocalizationManager;
47    import org.xwiki.localization.Translation;
48    import org.xwiki.localization.TranslationBundleDoesNotExistsException;
49    import org.xwiki.localization.TranslationBundleFactoryDoesNotExistsException;
50    import org.xwiki.localization.internal.DefaultLocalizationManager;
51    import org.xwiki.localization.internal.DefaultTranslationBundleContext;
52    import org.xwiki.localization.messagetool.internal.MessageToolTranslationMessageParser;
53    import org.xwiki.model.internal.DefaultModelContext;
54    import org.xwiki.observation.EventListener;
55    import org.xwiki.observation.ObservationManager;
56    import org.xwiki.observation.internal.DefaultObservationManager;
57    import org.xwiki.rendering.internal.parser.plain.PlainTextBlockParser;
58    import org.xwiki.test.annotation.ComponentList;
59    import org.xwiki.test.mockito.MockitoComponentManagerRule;
60   
61    @ComponentList({JARTranslationBundleFactory.class, MessageToolTranslationMessageParser.class,
62    PlainTextBlockParser.class, ContextComponentManagerProvider.class, DefaultLocalizationManager.class,
63    DefaultTranslationBundleContext.class, DefaultModelContext.class, DefaultExecution.class,
64    DefaultObservationManager.class, JARTranslationBundleFactoryListener.class, DefaultComponentManagerManager.class,
65    EmbeddableComponentManagerFactory.class})
 
66    public class JARTranslationBundleFactoryTest
67    {
68    @Rule
69    public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
70   
71    private LocalizationManager localizationManager;
72   
73    private ObservationManager observationManager;
74   
75    private InstalledExtensionRepository mockInstalledExtensionRepository;
76   
77    private ExtensionPackager extensionPackager;
78   
 
79  5 toggle @Before
80    public void setUp() throws Exception
81    {
82  5 this.extensionPackager = new ExtensionPackager(null, new File("target/test-" + new Date().getTime()));
83  5 this.extensionPackager.generateExtensions();
84   
85    // Mocks
86   
87  5 this.mockInstalledExtensionRepository =
88    this.componentManager.registerMockComponent(InstalledExtensionRepository.class);
89   
90    // Components
91   
92  5 this.localizationManager = this.componentManager.getInstance(LocalizationManager.class);
93    }
94   
 
95  6 toggle private ObservationManager getObservationManager() throws ComponentLookupException
96    {
97  6 if (this.observationManager == null) {
98  4 this.observationManager = this.componentManager.getInstance(ObservationManager.class);
99    }
100   
101  6 return this.observationManager;
102    }
103   
 
104  8 toggle private DefaultInstalledExtension mockInstalledExtension(ExtensionId extensionId, String namespace)
105    {
106  8 DefaultLocalExtension localExtension = new DefaultLocalExtension(null, extensionId, "jar");
107  8 localExtension.setFile(this.extensionPackager.getExtensionFile(extensionId));
108   
109  8 DefaultInstalledExtension installedExtension = new DefaultInstalledExtension(localExtension, null);
110   
111  8 installedExtension.setInstalled(true, namespace);
112   
113  8 return installedExtension;
114    }
115   
 
116  4 toggle private void mockInstallExtension(ExtensionId extensionId, String namespace) throws ComponentLookupException
117    {
118  4 DefaultInstalledExtension installedExtension = mockInstalledExtension(extensionId, namespace);
119   
120  4 getObservationManager().notify(new ExtensionInstalledEvent(extensionId, namespace), installedExtension);
121    }
122   
 
123  1 toggle private void mockUpgradeExtension(ExtensionId previousExtensionId, ExtensionId newExtensionId, String namespace)
124    throws ComponentLookupException
125    {
126  1 DefaultInstalledExtension previousInstalledExtension = mockInstalledExtension(previousExtensionId, namespace);
127  1 DefaultInstalledExtension newInstalledExtension = mockInstalledExtension(newExtensionId, namespace);
128   
129  1 getObservationManager().notify(new ExtensionUpgradedEvent(newExtensionId, namespace), newInstalledExtension,
130    Arrays.asList(previousInstalledExtension));
131    }
132   
 
133  1 toggle private void mockUninstallExtension(ExtensionId extensionId, String namespace) throws ComponentLookupException
134    {
135  1 DefaultInstalledExtension installedExtension = mockInstalledExtension(extensionId, namespace);
136   
137  1 getObservationManager().notify(new ExtensionUninstalledEvent(extensionId, namespace), installedExtension);
138    }
139   
 
140  14 toggle private void assertTranslation(String key, String message, Locale locale)
141    {
142  14 Translation translation = this.localizationManager.getTranslation(key, locale);
143   
144  14 if (message != null) {
145  13 Assert.assertNotNull("Could not find translation for key [" + key + "] and locale [" + locale + "]",
146    translation);
147  13 Assert.assertEquals(message, translation.getRawSource());
148    } else {
149  1 Assert.assertNull("Found translation for key [" + key + "] and locale [" + locale + "]", translation);
150    }
151    }
152   
153    // tests
154   
 
155  1 toggle @Test
156    public void installEmptyJar() throws TranslationBundleDoesNotExistsException,
157    TranslationBundleFactoryDoesNotExistsException, ComponentLookupException
158    {
159  1 ExtensionId extensionId = new ExtensionId("emptyjar", "1.0");
160   
161  1 mockInstallExtension(extensionId, null);
162   
163  1 Assert.assertNotNull(this.localizationManager.getTranslationBundle("jar", this.extensionPackager
164    .getExtensionFile(extensionId).toURI().toString()));
165    }
166   
 
167  1 toggle @Test
168    public void installJar() throws ComponentLookupException
169    {
170  1 ExtensionId extensionId = new ExtensionId("jar", "1.0");
171   
172  1 mockInstallExtension(extensionId, null);
173   
174  1 assertTranslation("test.key", "default translation", Locale.ROOT);
175  1 assertTranslation("test.key", "en translation", Locale.ENGLISH);
176  1 assertTranslation("test.key", "en_US translation", Locale.US);
177    }
178   
 
179  1 toggle @Test
180    public void upgradeJar() throws ComponentLookupException
181    {
182  1 ExtensionId previousExtensionId = new ExtensionId("jar", "1.0");
183   
184  1 mockInstallExtension(previousExtensionId, null);
185   
186  1 assertTranslation("test.key", "default translation", Locale.ROOT);
187  1 assertTranslation("test.key", "en translation", Locale.ENGLISH);
188  1 assertTranslation("test.key", "en_US translation", Locale.US);
189   
190  1 ExtensionId newExtensionId = new ExtensionId("jar", "2.0");
191   
192  1 mockUpgradeExtension(previousExtensionId, newExtensionId, null);
193   
194  1 assertTranslation("test.key", "default translation 2.0", Locale.ROOT);
195  1 assertTranslation("test.key", "default translation 2.0", Locale.ENGLISH);
196  1 assertTranslation("test.key", "default translation 2.0", Locale.US);
197    }
198   
 
199  1 toggle @Test
200    public void uninstallExtension() throws ComponentLookupException
201    {
202  1 ExtensionId extensionId = new ExtensionId("jar", "1.0");
203   
204  1 mockInstallExtension(extensionId, null);
205   
206  1 assertTranslation("test.key", "default translation", Locale.ROOT);
207   
208  1 mockUninstallExtension(extensionId, null);
209   
210  1 assertTranslation("test.key", null, Locale.ROOT);
211    }
212   
 
213  1 toggle @Test
214    public void getInstalledJarTranslations() throws ComponentLookupException
215    {
216  1 ExtensionId extensionId = new ExtensionId("jar", "1.0");
217   
218  1 Mockito.when(mockInstalledExtensionRepository.getInstalledExtensions()).thenReturn(
219    Arrays.<InstalledExtension> asList(mockInstalledExtension(extensionId, null)));
220   
221    // Trigger initialization
222  1 this.componentManager.getInstance(EventListener.class, JARTranslationBundleFactoryListener.NAME);
223   
224  1 assertTranslation("test.key", "default translation", Locale.ROOT);
225  1 assertTranslation("test.key", "en translation", Locale.ENGLISH);
226  1 assertTranslation("test.key", "en_US translation", Locale.US);
227    }
228    }