Search in sources :

Example 1 with LocalizationManager

use of org.xwiki.localization.LocalizationManager 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)

Example 2 with LocalizationManager

use of org.xwiki.localization.LocalizationManager in project xwiki-platform by xwiki.

the class LocalizationScriptServiceTest method setUp.

@Before
public void setUp() throws Exception {
    componentManager = mock(ComponentManager.class);
    Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(componentManager);
    renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
    when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
    localizationContext = mocker.getInstance(LocalizationContext.class);
    localizationManager = mocker.getInstance(LocalizationManager.class);
    localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
    translation = mock(Translation.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
            printer.print("print result");
            return null;
        }
    }).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
    when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
    when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
    when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
    environment = mocker.getInstance(Environment.class);
}
Also used : Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Provider(javax.inject.Provider) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) LocalizationManager(org.xwiki.localization.LocalizationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Before(org.junit.Before)

Aggregations

LocalizationContext (org.xwiki.localization.LocalizationContext)2 LocalizationManager (org.xwiki.localization.LocalizationManager)2 Translation (org.xwiki.localization.Translation)2 WordBlock (org.xwiki.rendering.block.WordBlock)2 Locale (java.util.Locale)1 Provider (javax.inject.Provider)1 Expectations (org.jmock.Expectations)1 Mockery (org.jmock.Mockery)1 JUnit4Mockery (org.jmock.integration.junit4.JUnit4Mockery)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 Environment (org.xwiki.environment.Environment)1 TranslationBundle (org.xwiki.localization.TranslationBundle)1 Block (org.xwiki.rendering.block.Block)1 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)1 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)1