001 package 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
022 import java.util.Vector;
023 import javax.servlet.ServletConfig;
024 import javax.servlet.http.HttpServletResponse;
025 import org.apache.turbine.om.security.User;
026 import org.apache.turbine.services.TurbineServices;
027 import org.apache.turbine.services.rundata.RunDataService;
028 import org.apache.turbine.test.BaseTestCase;
029 import org.apache.turbine.test.EnhancedMockHttpServletRequest;
030 import org.apache.turbine.util.RunData;
031 import org.apache.turbine.util.TurbineConfig;
032 import com.mockobjects.servlet.MockHttpServletResponse;
033 import com.mockobjects.servlet.MockHttpSession;
034 import com.mockobjects.servlet.MockServletConfig;
035 /**
036 * Unit test for Localization Tool. Verifies that localization works the same using the
037 * deprecated Turbine localization service as well as the new Fulcrum Localization
038 * component.
039 *
040 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
041 * @version $Id: LocalizationToolTest.java 615328 2008-01-25 20:25:05Z tv $
042 */
043 public class LocalizationToolTest extends BaseTestCase
044 {
045 private static TurbineConfig tc = null;
046 public LocalizationToolTest(String name) throws Exception
047 {
048 super(name);
049 }
050 public void testGet() throws Exception
051 {
052 LocalizationTool lt = new LocalizationTool();
053 lt.init(getRunData());
054 assertEquals("value1", lt.get("key1"));
055 assertEquals("value3", lt.get("key3"));
056 }
057 public void testGetLocale() throws Exception
058 {
059 LocalizationTool lt = new LocalizationTool();
060 lt.init(getRunData());
061 assertNotNull(lt.getLocale());
062 assertEquals("US", lt.getLocale().getCountry());
063 assertEquals("en", lt.getLocale().getLanguage());
064 }
065 public void testInit() throws Exception
066 {
067 LocalizationTool lt = new LocalizationTool();
068 lt.init(getRunData());
069 assertNotNull(lt.getLocale());
070 }
071 public void testRefresh() throws Exception
072 {
073 LocalizationTool lt = new LocalizationTool();
074 lt.init(getRunData());
075 assertNotNull(lt.getLocale());
076 lt.refresh();
077 assertNull(lt.getLocale());
078 }
079 private RunData getRunData() throws Exception
080 {
081 RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
082 EnhancedMockHttpServletRequest request = new EnhancedMockHttpServletRequest();
083 request.setupServerName("bob");
084 request.setupGetProtocol("http");
085 request.setupScheme("scheme");
086 request.setupPathInfo("damn");
087 request.setupGetServletPath("damn2");
088 request.setupGetContextPath("wow");
089 request.setupGetContentType("html/text");
090 request.setupAddHeader("Content-type", "html/text");
091 request.setupAddHeader("Accept-Language", "en-US");
092 Vector v = new Vector();
093 request.setupGetParameterNames(v.elements());
094 MockHttpSession session = new MockHttpSession();
095 session.setupGetAttribute(User.SESSION_KEY, null);
096 request.setSession(session);
097 HttpServletResponse response = new MockHttpServletResponse();
098 ServletConfig config = new MockServletConfig();
099 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 }