de.perdian.commons.i18n.polyglot.PolyglotImplTest.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.commons.i18n.polyglot.PolyglotImplTest.java

Source

/*
 * Copyright 2013 Christian Robert
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.perdian.commons.i18n.polyglot;

import java.util.Arrays;
import java.util.Locale;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.support.StaticMessageSource;

public class PolyglotImplTest {

    @Test(expected = IllegalArgumentException.class)
    public void testGetMetaNullLocale() {
        PolyglotImpl<?> polyglotImpl = new PolyglotImpl<>();
        polyglotImpl.getMeta(null);
    }

    @Test
    public void testGetMeta() {

        StaticMessageSource messageSource = new StaticMessageSource();
        messageSource.addMessage("key1", Locale.GERMANY, "value1");
        messageSource.addMessage("key2", Locale.GERMANY, "value2");

        PolyglotImpl<ExamplePolyglotSimple> polyglotImpl = new PolyglotImpl<>();
        polyglotImpl.setInstanceClass(ExamplePolyglotSimple.class);
        polyglotImpl.setMessageSource(messageSource);
        PolyglotMeta polyglotMeta = polyglotImpl.getMeta(Locale.GERMANY);

        Assert.assertEquals(Locale.GERMANY, polyglotMeta.getLocale());
        Assert.assertEquals(2, polyglotMeta.getKeys().size());
        Assert.assertEquals(0, polyglotMeta.getMissingValueKeys().size());
        Assert.assertTrue(polyglotMeta.getKeys().containsAll(Arrays.asList("key1", "key2")));

    }

    @Test
    public void testGetMetaWithConfiguration() {

        StaticMessageSource messageSource = new StaticMessageSource();
        messageSource.addMessage("prefix.key1.postfix", Locale.GERMANY, "value1");

        PolyglotImpl<ExamplePolyglotWithConfiguration> polyglotImpl = new PolyglotImpl<>();
        polyglotImpl.setInstanceClass(ExamplePolyglotWithConfiguration.class);
        polyglotImpl.setMessageSource(messageSource);
        PolyglotMeta polyglotMeta = polyglotImpl.getMeta(Locale.GERMANY);

        Assert.assertEquals(Locale.GERMANY, polyglotMeta.getLocale());
        Assert.assertEquals(1, polyglotMeta.getKeys().size());
        Assert.assertEquals(0, polyglotMeta.getMissingValueKeys().size());
        Assert.assertTrue(polyglotMeta.getKeys().containsAll(Arrays.asList("prefix.key1.postfix")));

    }

    @Test
    public void testGetMetaWithConfigurationAndNoValues() {

        PolyglotImpl<ExamplePolyglotWithConfiguration> polyglotImpl = new PolyglotImpl<>();
        polyglotImpl.setInstanceClass(ExamplePolyglotWithConfiguration.class);
        polyglotImpl.setMessageSource(new StaticMessageSource());
        PolyglotMeta polyglotMeta = polyglotImpl.getMeta(Locale.GERMANY);

        Assert.assertEquals(Locale.GERMANY, polyglotMeta.getLocale());
        Assert.assertEquals(1, polyglotMeta.getKeys().size());
        Assert.assertEquals(1, polyglotMeta.getMissingValueKeys().size());
        Assert.assertTrue(polyglotMeta.getKeys().containsAll(Arrays.asList("prefix.key1.postfix")));
        Assert.assertTrue(polyglotMeta.getMissingValueKeys().containsAll(Arrays.asList("prefix.key1.postfix")));

    }

    @Test
    public void testGetMetaWithDefault() {

        PolyglotImpl<ExamplePolyglotWithDefault> polyglotImpl = new PolyglotImpl<>();
        polyglotImpl.setInstanceClass(ExamplePolyglotWithDefault.class);
        polyglotImpl.setMessageSource(new StaticMessageSource());
        PolyglotMeta polyglotMeta = polyglotImpl.getMeta(Locale.GERMANY);

        Assert.assertEquals(Locale.GERMANY, polyglotMeta.getLocale());
        Assert.assertEquals(1, polyglotMeta.getKeys().size());
        Assert.assertEquals(0, polyglotMeta.getMissingValueKeys().size());
        Assert.assertTrue(polyglotMeta.getKeys().containsAll(Arrays.asList("key")));

    }

}