001package org.apache.turbine.services.localization;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNotNull;
024import static org.junit.Assert.assertNull;
025import static org.mockito.Mockito.mock;
026
027import javax.servlet.ServletConfig;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030
031import org.apache.turbine.annotation.AnnotationProcessor;
032import org.apache.turbine.services.TurbineServices;
033import org.apache.turbine.services.rundata.RunDataService;
034import org.apache.turbine.test.BaseTestCase;
035import org.apache.turbine.util.RunData;
036import org.apache.turbine.util.TurbineConfig;
037import org.junit.AfterClass;
038import org.junit.Before;
039import org.junit.BeforeClass;
040import org.junit.Test;
041
042/**
043 * Unit test for Localization Tool. Verifies that localization works the same using the
044 * deprecated Turbine localization service as well as the new Fulcrum Localization
045 * component.
046 *
047 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
048 * @version $Id: LocalizationToolTest.java 1754909 2016-08-02 12:55:35Z tv $
049 */
050public class LocalizationToolTest extends BaseTestCase
051{
052    private static TurbineConfig tc = null;
053    private LocalizationTool lt;
054
055    @Before
056    public void initTool() throws Exception
057    {
058        lt = new LocalizationTool();
059        AnnotationProcessor.process(lt);
060        lt.init(getRunData());
061    }
062
063    @Test
064    public void testGet() throws Exception
065    {
066        assertEquals("value1", lt.get("key1"));
067        assertEquals("value3", lt.get("key3"));
068    }
069
070    @Test
071    public void testGetLocale() throws Exception
072    {
073        assertNotNull(lt.getLocale());
074        assertEquals("US", lt.getLocale().getCountry());
075        assertEquals("en", lt.getLocale().getLanguage());
076    }
077
078    @Test
079    public void testInit() throws Exception
080    {
081        assertNotNull(lt.getLocale());
082    }
083
084    @Test
085    public void testRefresh() throws Exception
086    {
087        assertNotNull(lt.getLocale());
088        lt.refresh();
089        assertNull(lt.getLocale());
090    }
091
092    private RunData getRunData() throws Exception
093    {
094        RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
095        ServletConfig config = mock(ServletConfig.class);
096        HttpServletRequest request = getMockRequest();
097        HttpServletResponse response = mock(HttpServletResponse.class);
098        RunData runData = rds.getRunData(request, response, config);
099        return runData;
100    }
101
102    @BeforeClass
103    public static void setUp() throws Exception
104    {
105        tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
106        tc.initialize();
107    }
108
109    @AfterClass
110    public static void tearDown() throws Exception
111    {
112        if (tc != null)
113        {
114            tc.dispose();
115        }
116    }
117}