| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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}) |
| |
|
| 100% |
Uncovered Elements: 0 (69) |
Complexity: 14 |
Complexity Density: 0.26 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 79 |
5 |
@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 |
|
|
| 86 |
|
|
| 87 |
5 |
this.mockInstalledExtensionRepository = |
| 88 |
|
this.componentManager.registerMockComponent(InstalledExtensionRepository.class); |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
5 |
this.localizationManager = this.componentManager.getInstance(LocalizationManager.class); |
| 93 |
|
} |
| 94 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 95 |
6 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 104 |
8 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 116 |
4 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 123 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 133 |
1 |
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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 140 |
14 |
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 |
|
|
| 154 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
| 155 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 167 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
| 179 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 199 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
| 213 |
1 |
@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 |
|
|
| 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 |
|
} |