Search in sources :

Example 1 with TranslationBundle

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);
    }
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) Scope(org.xwiki.localization.wiki.internal.TranslationDocumentModel.Scope)

Example 2 with TranslationBundle

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()));
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) Scope(org.xwiki.localization.wiki.internal.TranslationDocumentModel.Scope)

Example 3 with TranslationBundle

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());
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) ComponentManager(org.xwiki.component.manager.ComponentManager) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Example 4 with TranslationBundle

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());
}
Also used : TranslationBundle(org.xwiki.localization.TranslationBundle) Test(org.junit.Test)

Example 5 with TranslationBundle

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));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Locale(java.util.Locale) Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) LocalizationManager(org.xwiki.localization.LocalizationManager) TranslationBundle(org.xwiki.localization.TranslationBundle) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block)

Aggregations

TranslationBundle (org.xwiki.localization.TranslationBundle)11 ComponentManager (org.xwiki.component.manager.ComponentManager)4 Test (org.junit.Test)3 MalformedURLException (java.net.MalformedURLException)2 InitializationException (org.xwiki.component.phase.InitializationException)2 Scope (org.xwiki.localization.wiki.internal.TranslationDocumentModel.Scope)2 File (java.io.File)1 Locale (java.util.Locale)1 TreeSet (java.util.TreeSet)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 LocalizationContext (org.xwiki.localization.LocalizationContext)1 LocalizationManager (org.xwiki.localization.LocalizationManager)1 Translation (org.xwiki.localization.Translation)1 WikiReference (org.xwiki.model.reference.WikiReference)1 Block (org.xwiki.rendering.block.Block)1 WordBlock (org.xwiki.rendering.block.WordBlock)1