View Javadoc

    1   package org.apache.turbine.services.localization;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import java.util.Vector;
    23
    24  import javax.servlet.ServletConfig;
    25  import javax.servlet.http.HttpServletResponse;
    26
    27  import org.apache.turbine.om.security.User;
    28  import org.apache.turbine.services.TurbineServices;
    29  import org.apache.turbine.services.rundata.RunDataService;
    30  import org.apache.turbine.test.BaseTestCase;
    31  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    32  import org.apache.turbine.util.RunData;
    33  import org.apache.turbine.util.TurbineConfig;
    34  import org.junit.AfterClass;
    35  import org.junit.BeforeClass;
    36  import org.junit.Test;
    37
    38  import com.mockobjects.servlet.MockHttpServletResponse;
    39  import com.mockobjects.servlet.MockHttpSession;
    40  import com.mockobjects.servlet.MockServletConfig;
    41
    42  import static org.junit.Assert.*;
    43  /**
    44   * Unit test for Localization Tool.  Verifies that localization works the same using the
    45   * deprecated Turbine localization service as well as the new Fulcrum Localization
    46   * component.
    47   *
    48   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    49   * @version $Id: LocalizationToolTest.java 1606111 2014-06-27 14:46:47Z gk $
    50   */
    51  public class LocalizationToolTest extends BaseTestCase
    52  {
    53      private static TurbineConfig tc = null;
    54
    55      @Test public void testGet() throws Exception
    56      {
    57          LocalizationTool lt = new LocalizationTool();
    58          lt.init(getRunData());
    59          assertEquals("value1", lt.get("key1"));
    60          assertEquals("value3", lt.get("key3"));
    61      }
    62      @Test public void testGetLocale() throws Exception
    63      {
    64          LocalizationTool lt = new LocalizationTool();
    65          lt.init(getRunData());
    66          assertNotNull(lt.getLocale());
    67          assertEquals("US", lt.getLocale().getCountry());
    68          assertEquals("en", lt.getLocale().getLanguage());
    69      }
    70      @Test public void testInit() throws Exception
    71      {
    72          LocalizationTool lt = new LocalizationTool();
    73          lt.init(getRunData());
    74          assertNotNull(lt.getLocale());
    75      }
    76      @Test public void testRefresh() throws Exception
    77      {
    78          LocalizationTool lt = new LocalizationTool();
    79          lt.init(getRunData());
    80          assertNotNull(lt.getLocale());
    81          lt.refresh();
    82          assertNull(lt.getLocale());
    83      }
    84      private RunData getRunData() throws Exception
    85      {
    86          RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
    87          EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
    88          request.setupServerName("bob");
    89          request.setupGetProtocol("http");
    90          request.setupScheme("scheme");
    91          request.setupPathInfo("damn");
    92          request.setupGetServletPath("damn2");
    93          request.setupGetContextPath("wow");
    94          request.setupGetContentType("html/text");
    95          request.setupAddHeader("Content-type", "html/text");
    96          request.setupAddHeader("Accept-Language", "en-US");
    97          Vector<String> v = new Vector<String>();
    98          request.setupGetParameterNames(v.elements());
    99          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 }