<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.localization.jar.internal;

import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import junit.framework.Assert;

import org.jmock.Expectations;
import org.jmock.api.Invocation;
import org.jmock.lib.action.CustomAction;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.xwiki.extension.ExtensionManagerConfiguration;
import org.xwiki.extension.internal.DefaultExtensionManagerConfiguration;
import org.xwiki.localization.TranslationBundleFactory;
import org.xwiki.localization.LocalizationManager;
import org.xwiki.localization.Translation;
import org.xwiki.observation.ObservationManager;
import org.xwiki.rendering.syntax.Syntax;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentManagerRule;

@ComponentList({
    JARTranslationBundleFactory.class,
    DefaultObservationManager.class,
    
})
public class JARTranslationBundleFactoryTest
{
    @Rule
    public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();

    @Before
    public void setUp() throws Exception
    {
        this.configuration = this.componentManager.getInstance(ExtensionManagerConfiguration.class);
        
        // checking

        this.observation = getComponentManager().getInstance(ObservationManager.class);

        // Initialiaze document bundle factory
        getComponentManager().getInstance(TranslationBundleFactory.class, "document");

        this.localization = getComponentManager().getInstance(LocalizationManager.class);
    }

    private void assertTranslation(String key, String message, Locale locale)
    {
        Translation translation = this.localization.getTranslation(key, locale);

        if (message != null) {
            Assert.assertNotNull(translation);
            Assert.assertEquals(message, translation.getRawSource());
        } else {
            Assert.assertNull(translation);
        }
    }

    // tests

    @Test
    public void getTranslation() throws XWikiException
    {
        assertTranslation("wiki.translation", null, Locale.ROOT);

        addTranslation("wiki.translation", "Wiki translation", new DocumentReference(getContext().getDatabase(),
            "space", "translation"), Locale.ROOT, Scope.WIKI);

        assertTranslation("wiki.translation", "Wiki translation", Locale.ROOT);
    }
}<commit_msg>XWIKI-8263: Allow providing translations in a jar extension<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.localization.jar.internal;

import java.util.Arrays;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mockito;
import org.xwiki.component.internal.ContextComponentManagerProvider;
import org.xwiki.component.internal.multi.ComponentManagerManager;
import org.xwiki.extension.InstalledExtension;
import org.xwiki.extension.repository.InstalledExtensionRepository;
import org.xwiki.localization.TranslationBundleDoesNotExistsException;
import org.xwiki.localization.TranslationBundleFactory;
import org.xwiki.localization.messagetool.internal.MessageToolTranslationMessageParser;
import org.xwiki.observation.ObservationManager;
import org.xwiki.rendering.internal.parser.plain.PlainTextBlockParser;
import org.xwiki.test.annotation.ComponentList;
import org.xwiki.test.mockito.MockitoComponentManagerRule;

@ComponentList({JARTranslationBundleFactory.class, MessageToolTranslationMessageParser.class,
PlainTextBlockParser.class, ContextComponentManagerProvider.class})
public class JARTranslationBundleFactoryTest
{
    @Rule
    public final MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();

    private TranslationBundleFactory factory;

    private InstalledExtensionRepository mockInstalledExtensionRepository;

    @Before
    public void setUp() throws Exception
    {
        // Mocks

        this.componentManager.registerMockComponent(ComponentManagerManager.class);
        this.componentManager.registerMockComponent(ObservationManager.class);
        this.mockInstalledExtensionRepository =
            this.componentManager.registerMockComponent(InstalledExtensionRepository.class);

        // Components

        this.factory = this.componentManager.getInstance(TranslationBundleFactory.class, "jar");
    }

    // tests

    @Test
    public void getBundle() throws TranslationBundleDoesNotExistsException
    {
        Mockito.when(mockInstalledExtensionRepository.getInstalledExtensions()).thenReturn(
            Arrays.<InstalledExtension> asList());

        this.factory.getBundle("toto");
    }
}
{'fraction_non_alphanumeric': 0.07916471871591087, 'fraction_numerical': 0.004675081813931743, 'mean_word_length': 4.39327731092437, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7066039', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1584, 'n_words': 476}