use of org.xwiki.localization.TranslationBundle in project xwiki-platform by xwiki.
the class DocumentTranslationBundleFactory method registerTranslationBundle.
/**
* @param document the translation document
* @throws TranslationBundleDoesNotExistsException when no translation bundle could be created from the provided
* document
* @throws ComponentRepositoryException when the actual registration of the document bundle failed
* @throws AccessDeniedException when the document author does not have enough right to register the translation
* bundle
*/
private void registerTranslationBundle(XWikiDocument document) throws TranslationBundleDoesNotExistsException, ComponentRepositoryException, AccessDeniedException {
Scope scope = getScope(document);
if (scope != null && scope != Scope.ON_DEMAND) {
checkRegistrationAuthorization(document, scope);
ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(document.getDocumentReference());
ComponentDocumentTranslationBundle bundle = createComponentDocumentBundle(document, descriptor);
getComponentManager(document, scope, true).registerComponent(descriptor, bundle);
}
}
use of org.xwiki.localization.TranslationBundle in project xwiki-platform by xwiki.
the class DocumentTranslationBundleFactory method unregisterTranslationBundle.
/**
* @param document the translation document
*/
private void unregisterTranslationBundle(XWikiDocument document) {
Scope scope = getScope(document);
// Unregister component
if (scope != null && scope != Scope.ON_DEMAND) {
ComponentDescriptor<TranslationBundle> descriptor = createComponentDescriptor(document.getDocumentReference());
getComponentManager(document, scope, true).unregisterComponent(descriptor);
}
// Remove from cache
this.onDemandBundleCache.remove(this.uidSerializer.serialize(document.getDocumentReference()));
}
use of org.xwiki.localization.TranslationBundle in project xwiki-platform by xwiki.
the class DefaultTranslationBundleContextTest method getBundlesSwitchContext.
@Test
public void getBundlesSwitchContext() throws Exception {
// Mock the first wiki bundles.
ComponentManager mockWiki1ComponentManager = mock(ComponentManager.class);
when(this.mockContextComponentManagerProvider.get()).thenReturn(mockWiki1ComponentManager);
TranslationBundle mockWiki1TranslationBundle = mock(TranslationBundle.class);
List<TranslationBundle> wiki1Bundles = Arrays.asList(mockWiki1TranslationBundle);
when(mockWiki1ComponentManager.<TranslationBundle>getInstanceList(TranslationBundle.class)).thenReturn(wiki1Bundles);
Collection<TranslationBundle> firstBundles = this.mocker.getComponentUnderTest().getBundles();
// Check the output
assertEquals(1, firstBundles.size());
assertEquals(mockWiki1TranslationBundle, firstBundles.iterator().next());
// Switch context wiki to the second wiki.
when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("otherWiki"));
// Mock the second wiki bundles.
ComponentManager mockWiki2ComponentManager = mock(ComponentManager.class);
when(this.mockContextComponentManagerProvider.get()).thenReturn(mockWiki2ComponentManager);
TranslationBundle mockWiki2TranslationBundle = mock(TranslationBundle.class);
List<TranslationBundle> wiki2Bundles = Arrays.asList(mockWiki2TranslationBundle);
when(mockWiki2ComponentManager.<TranslationBundle>getInstanceList(TranslationBundle.class)).thenReturn(wiki2Bundles);
Collection<TranslationBundle> secondBundles = this.mocker.getComponentUnderTest().getBundles();
// Check the output.
assertEquals(1, secondBundles.size());
assertEquals(mockWiki2TranslationBundle, secondBundles.iterator().next());
// Check that we get different results on different wikis.
// Note: We are comparing values instead of the actual sets because we did not mock the compareTo method on the
// bundles to simplify the test.
assertNotEquals(firstBundles.iterator().next(), secondBundles.iterator().next());
// Switch back to the first wiki and get the same (cached) first bundles.
when(this.mockModelContext.getCurrentEntityReference()).thenReturn(new WikiReference("currentWiki"));
Collection<TranslationBundle> thirdBundles = this.mocker.getComponentUnderTest().getBundles();
assertEquals(firstBundles.iterator().next(), thirdBundles.iterator().next());
}
use of org.xwiki.localization.TranslationBundle in project xwiki-platform by xwiki.
the class DefaultTranslationBundleContextTest method getBundlesNewContext.
@Test
public void getBundlesNewContext() throws Exception {
// Map the context component manager to the test component manager for easier test setup.
when(this.mockContextComponentManagerProvider.get()).thenReturn(this.mocker);
TranslationBundle mockTranslationBundle = this.mocker.registerMockComponent(TranslationBundle.class);
Collection<TranslationBundle> bundles = this.mocker.getComponentUnderTest().getBundles();
// Verify that an internal bundles cache is created and stored in the ExecutionContext.
assertNotNull(this.mockExecutionContext.getProperty(DefaultTranslationBundleContext.CKEY_BUNDLES));
assertEquals(1, bundles.size());
assertEquals(mockTranslationBundle, bundles.iterator().next());
}
use of org.xwiki.localization.TranslationBundle in project xwiki-platform by xwiki.
the class IntegrationTests method initialize.
@RenderingTestSuite.Initialized
public void initialize(MockingComponentManager componentManager) throws Exception {
Mockery mockery = new JUnit4Mockery();
final LocalizationManager localizationManager = componentManager.registerMockComponent(mockery, LocalizationManager.class);
final LocalizationContext localizationContext = componentManager.registerMockComponent(mockery, LocalizationContext.class);
mockery.checking(new Expectations() {
{
allowing(localizationManager).getTranslation("some.translation", Locale.ENGLISH);
will(returnValue(new Translation() {
@Override
public Block render(Locale locale, Object... parameters) {
return parameters.length > 0 ? new WordBlock("entranslationmessage" + Arrays.toString(parameters)) : new WordBlock("entranslationmessage");
}
@Override
public Block render(Object... parameters) {
return render(null, parameters);
}
@Override
public String getRawSource() {
return "entranslationmessagesource";
}
@Override
public Locale getLocale() {
return Locale.ENGLISH;
}
@Override
public String getKey() {
return "some.translation";
}
@Override
public TranslationBundle getBundle() {
return null;
}
}));
allowing(localizationManager).getTranslation("some.translation", Locale.FRENCH);
will(returnValue(new Translation() {
@Override
public Block render(Locale locale, Object... parameters) {
return parameters.length > 0 ? new WordBlock("frtranslationmessage" + Arrays.toString(parameters)) : new WordBlock("frtranslationmessage");
}
@Override
public Block render(Object... parameters) {
return render(null, parameters);
}
@Override
public String getRawSource() {
return "frtranslationmessagesource";
}
@Override
public Locale getLocale() {
return Locale.FRENCH;
}
@Override
public String getKey() {
return "some.translation";
}
@Override
public TranslationBundle getBundle() {
return null;
}
}));
allowing(localizationManager).getTranslation("unexisting.translation", Locale.ENGLISH);
will(returnValue(null));
allowing(localizationContext).getCurrentLocale();
will(returnValue(Locale.ENGLISH));
}
});
}
Aggregations