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 java.util.Vector;
023
024import javax.servlet.ServletConfig;
025import javax.servlet.http.HttpServletResponse;
026
027import org.apache.turbine.om.security.User;
028import org.apache.turbine.services.TurbineServices;
029import org.apache.turbine.services.rundata.RunDataService;
030import org.apache.turbine.test.BaseTestCase;
031import org.apache.turbine.test.EnhancedMockHttpServletRequest;
032import org.apache.turbine.util.RunData;
033import org.apache.turbine.util.TurbineConfig;
034import org.junit.AfterClass;
035import org.junit.BeforeClass;
036import org.junit.Test;
037
038import com.mockobjects.servlet.MockHttpServletResponse;
039import com.mockobjects.servlet.MockHttpSession;
040import com.mockobjects.servlet.MockServletConfig;
041
042import static org.junit.Assert.*;
043/**
044 * Unit test for Localization Tool.  Verifies that localization works the same using the
045 * deprecated Turbine localization service as well as the new Fulcrum Localization
046 * component.
047 *
048 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
049 * @version $Id: LocalizationToolTest.java 1606111 2014-06-27 14:46:47Z gk $
050 */
051public class LocalizationToolTest extends BaseTestCase
052{
053    private static TurbineConfig tc = null;
054
055    @Test public void testGet() throws Exception
056    {
057        LocalizationTool lt = new LocalizationTool();
058        lt.init(getRunData());
059        assertEquals("value1", lt.get("key1"));
060        assertEquals("value3", lt.get("key3"));
061    }
062    @Test public void testGetLocale() throws Exception
063    {
064        LocalizationTool lt = new LocalizationTool();
065        lt.init(getRunData());
066        assertNotNull(lt.getLocale());
067        assertEquals("US", lt.getLocale().getCountry());
068        assertEquals("en", lt.getLocale().getLanguage());
069    }
070    @Test public void testInit() throws Exception
071    {
072        LocalizationTool lt = new LocalizationTool();
073        lt.init(getRunData());
074        assertNotNull(lt.getLocale());
075    }
076    @Test public void testRefresh() throws Exception
077    {
078        LocalizationTool lt = new LocalizationTool();
079        lt.init(getRunData());
080        assertNotNull(lt.getLocale());
081        lt.refresh();
082        assertNull(lt.getLocale());
083    }
084    private RunData getRunData() throws Exception
085    {
086        RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
087        EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
088        request.setupServerName("bob");
089        request.setupGetProtocol("http");
090        request.setupScheme("scheme");
091        request.setupPathInfo("damn");
092        request.setupGetServletPath("damn2");
093        request.setupGetContextPath("wow");
094        request.setupGetContentType("html/text");
095        request.setupAddHeader("Content-type", "html/text");
096        request.setupAddHeader("Accept-Language", "en-US");
097        Vector<String> v = new Vector<String>();
098        request.setupGetParameterNames(v.elements());
099        MockHttpSession session = new MockHttpSession();
100        session.setupGetAttribute(User.SESSION_KEY, null);
101        request.setSession(session);
102        HttpServletResponse response = new MockHttpServletResponse();
103        ServletConfig config = new MockServletConfig();
104        RunData runData = rds.getRunData(request, response, config);
105        return runData;
106    }
107    @BeforeClass
108    public static void setUp() throws Exception
109    {
110        tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
111        tc.initialize();
112    }
113    @AfterClass
114    public static void tearDown() throws Exception
115    {
116        if (tc != null)
117        {
118            tc.dispose();
119        } 
120    }
121}