    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  import javax.servlet.ServletConfig;
    24  import javax.servlet.http.HttpServletResponse;
    25  import org.apache.turbine.om.security.User;
    26  import org.apache.turbine.services.TurbineServices;
    27  import org.apache.turbine.services.rundata.RunDataService;
    28  import org.apache.turbine.test.BaseTestCase;
    29  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    30  import org.apache.turbine.util.RunData;
    31  import org.apache.turbine.util.TurbineConfig;
    32  import com.mockobjects.servlet.MockHttpServletResponse;
    33  import com.mockobjects.servlet.MockHttpSession;
    34  import com.mockobjects.servlet.MockServletConfig;
    35  /**
    36   * Unit test for Localization Tool.  Verifies that localization works the same using the
    37   * deprecated Turbine localization service as well as the new Fulcrum Localization
    38   * component.
    39   *
    40   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    41   * @version $Id: LocalizationToolTest.java 615328 2008-01-25 20:25:05Z tv $
    42   */
    43  public class LocalizationToolTest extends BaseTestCase
    44  {
    45      private static TurbineConfig tc = null;
    46      public LocalizationToolTest(String name) throws Exception
    47      {
    48          super(name);
    49      }
    50      public void testGet() throws Exception
    51      {
    52          LocalizationTool lt = new LocalizationTool();
    53          lt.init(getRunData());
    54          assertEquals("value1", lt.get("key1"));
    55          assertEquals("value3", lt.get("key3"));
    56      }
    57      public void testGetLocale() throws Exception
    58      {
    59          LocalizationTool lt = new LocalizationTool();
    60          lt.init(getRunData());
    61          assertNotNull(lt.getLocale());
    62          assertEquals("US", lt.getLocale().getCountry());
    63          assertEquals("en", lt.getLocale().getLanguage());
    64      }
    65      public void testInit() throws Exception
    66      {
    67          LocalizationTool lt = new LocalizationTool();
    68          lt.init(getRunData());
    69          assertNotNull(lt.getLocale());
    70      }
    71      public void testRefresh() throws Exception
    72      {
    73          LocalizationTool lt = new LocalizationTool();
    74          lt.init(getRunData());
    75          assertNotNull(lt.getLocale());
    76          lt.refresh();
    77          assertNull(lt.getLocale());
    78      }
    79      private RunData getRunData() throws Exception
    80      {
    81          RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
    82          EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
    83          request.setupServerName("bob");
    84          request.setupGetProtocol("http");
    85          request.setupScheme("scheme");
    86          request.setupPathInfo("damn");
    87          request.setupGetServletPath("damn2");
    88          request.setupGetContextPath("wow");
    89          request.setupGetContentType("html/text");
    90          request.setupAddHeader("Content-type", "html/text");
    91          request.setupAddHeader("Accept-Language", "en-US");
    92          Vector v = new Vector();
    93          request.setupGetParameterNames(v.elements());
    94          MockHttpSession session = new MockHttpSession();
    95          session.setupGetAttribute(User.SESSION_KEY, null);
    96          request.setSession(session);
    97          HttpServletResponse response = new MockHttpServletResponse();
    98          ServletConfig config = new MockServletConfig();
    99          RunData runData = rds.getRunData(request, response, config);
    100         return runData;
    101     }
    102     public void setUp() throws Exception
    103     {
    104         tc = new TurbineConfig(".", "/conf/test/TestFulcrumComponents.properties");
    105         tc.initialize();
    106     }
    107     public void tearDown() throws Exception
    108     {
    109         if (tc != null)
    110         {
    111             tc.dispose();
    112         }
    113     }
    114 }