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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.mockito.Mockito.mock;
26  
27  import javax.servlet.ServletConfig;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import org.apache.turbine.annotation.AnnotationProcessor;
32  import org.apache.turbine.services.TurbineServices;
33  import org.apache.turbine.services.rundata.RunDataService;
34  import org.apache.turbine.test.BaseTestCase;
35  import org.apache.turbine.util.RunData;
36  import org.apache.turbine.util.TurbineConfig;
37  import org.junit.AfterClass;
38  import org.junit.Before;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  
42  /**
43   * Unit test for Localization Tool. Verifies that localization works the same using the
44   * deprecated Turbine localization service as well as the new Fulcrum Localization
45   * component.
46   *
47   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
48   * @version $Id: LocalizationToolTest.java 1754909 2016-08-02 12:55:35Z tv $
49   */
50  public class LocalizationToolTest extends BaseTestCase
51  {
52      private static TurbineConfig tc = null;
53      private LocalizationTool lt;
54  
55      @Before
56      public void initTool() throws Exception
57      {
58          lt = new LocalizationTool();
59          AnnotationProcessor.process(lt);
60          lt.init(getRunData());
61      }
62  
63      @Test
64      public void testGet() throws Exception
65      {
66          assertEquals("value1", lt.get("key1"));
67          assertEquals("value3", lt.get("key3"));
68      }
69  
70      @Test
71      public void testGetLocale() throws Exception
72      {
73          assertNotNull(lt.getLocale());
74          assertEquals("US", lt.getLocale().getCountry());
75          assertEquals("en", lt.getLocale().getLanguage());
76      }
77  
78      @Test
79      public void testInit() throws Exception
80      {
81          assertNotNull(lt.getLocale());
82      }
83  
84      @Test
85      public void testRefresh() throws Exception
86      {
87          assertNotNull(lt.getLocale());
88          lt.refresh();
89          assertNull(lt.getLocale());
90      }
91  
92      private RunData getRunData() throws Exception
93      {
94          RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
95          ServletConfig config = mock(ServletConfig.class);
96          HttpServletRequest request = getMockRequest();
97          HttpServletResponse response = mock(HttpServletResponse.class);
98          RunData runData = rds.getRunData(request, response, config);
99          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 }