LocalizationSetterImplTest.java :  » Web-Framework » Tapestry » org » apache » tapestry » internal » services » Java Open Source

Java Open Source » Web Framework » Tapestry 
Tapestry » org » apache » tapestry » internal » services » LocalizationSetterImplTest.java
// Copyright 2006 The Apache Software Foundation
//
// 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 org.apache.tapestry.internal.services;

import java.util.Locale;

import org.apache.tapestry.ioc.services.ThreadLocale;
import org.apache.tapestry.services.PersistentLocale;
import org.testng.Assert;
import org.testng.annotations.Test;

public class LocalizationSetterImplTest extends Assert
{
    private PersistentLocale _nullPersistentLocale = new PersistentLocale()
    {
        public boolean isSet()
        {
            return false;
        }

        public Locale get()
        {
            return null;
        }

        public void set(Locale locale)
        {

        }

    };

    private PersistentLocale _frenchPersistentLocale = new PersistentLocale()
    {
        public boolean isSet()
        {
            return true;
        }

        public Locale get()
        {
            return Locale.FRENCH;
        }

        public void set(Locale locale)
        {

        }

    };

    private static class ThreadLocaleImpl implements ThreadLocale
    {
        public Locale _locale;

        public void setLocale(Locale locale)
        {
            _locale = locale;
        }

        public Locale getLocale()
        {
            return _locale;
        }

    }

    @Test
    public void locale_split()
    {
        assertEquals(LocalizationSetterImpl.stripTerm("foo_bar_Baz"), "foo_bar");
        assertEquals(LocalizationSetterImpl.stripTerm("foo_bar"), "foo");
        assertEquals(LocalizationSetterImpl.stripTerm("foo"), "");
    }

    @Test
    public void to_locale_is_cached()
    {
        LocalizationSetterImpl filter = new LocalizationSetterImpl(_nullPersistentLocale, null,
                "en");

        Locale l1 = filter.toLocale("en");

        assertEquals(l1.toString(), "en");

        checkLocale(l1, "en", "", "");

        assertSame(filter.toLocale("en"), l1);
    }

    private void checkLocale(Locale l, String expectedLanguage, String expectedCountry,
            String expectedVariant)
    {
        assertEquals(l.getLanguage(), expectedLanguage);
        assertEquals(l.getCountry(), expectedCountry);
        assertEquals(l.getVariant(), expectedVariant);
    }

    @Test
    public void to_locale()
    {
        LocalizationSetterImpl filter = new LocalizationSetterImpl(_nullPersistentLocale, null,
                "en");

        checkLocale(filter.toLocale("en"), "en", "", "");
        checkLocale(filter.toLocale("klingon_Gach"), "klingon", "GACH", "");
        checkLocale(filter.toLocale("klingon_Gach_snuff"), "klingon", "GACH", "snuff");
    }

    @Test
    public void known_locale()
    {
        ThreadLocale threadLocale = new ThreadLocaleImpl();
        threadLocale.setLocale(Locale.FRENCH);
        LocalizationSetter setter = new LocalizationSetterImpl(_nullPersistentLocale, threadLocale,
                "en,fr");
        setter.setThreadLocale(Locale.CANADA_FRENCH);
        assertEquals(threadLocale.getLocale(), Locale.FRENCH);

    }

    @Test
    public void unknown_locale_uses_default_locale()
    {
        ThreadLocale threadLocale = new ThreadLocaleImpl();
        threadLocale.setLocale(Locale.FRENCH);
        LocalizationSetter setter = new LocalizationSetterImpl(_nullPersistentLocale, threadLocale,
                "en,fr");
        setter.setThreadLocale(Locale.JAPANESE);
        assertEquals(threadLocale.getLocale(), Locale.ENGLISH);
    }

    @Test
    public void use_persistent_locale()
    {
        ThreadLocale threadLocale = new ThreadLocaleImpl();
        LocalizationSetter setter = new LocalizationSetterImpl(_frenchPersistentLocale,
                threadLocale, "en,fr");
        setter.setThreadLocale(Locale.ENGLISH);
        assertEquals(threadLocale.getLocale(), Locale.FRENCH);
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.