001package org.apache.turbine.services.localization;
002
003import static org.junit.jupiter.api.Assertions.assertEquals;
004import static org.junit.jupiter.api.Assertions.assertNotNull;
005
006/*
007 * Licensed to the Apache Software Foundation (ASF) under one
008 * or more contributor license agreements.  See the NOTICE file
009 * distributed with this work for additional information
010 * regarding copyright ownership.  The ASF licenses this file
011 * to you under the Apache License, Version 2.0 (the
012 * "License"); you may not use this file except in compliance
013 * with the License.  You may obtain a copy of the License at
014 *
015 *   http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing,
018 * software distributed under the License is distributed on an
019 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020 * KIND, either express or implied.  See the License for the
021 * specific language governing permissions and limitations
022 * under the License.
023 */
024
025import static org.mockito.Mockito.mock;
026
027import java.util.Locale;
028
029import javax.servlet.ServletConfig;
030import javax.servlet.http.HttpServletRequest;
031import javax.servlet.http.HttpServletResponse;
032
033import org.apache.fulcrum.security.model.turbine.entity.TurbineUser;
034import org.apache.fulcrum.testcontainer.BaseUnit5Test;
035import org.apache.turbine.annotation.AnnotationProcessor;
036import org.apache.turbine.annotation.TurbineTool;
037import org.apache.turbine.om.security.DefaultUserImpl;
038import org.apache.turbine.om.security.User;
039import org.apache.turbine.services.TurbineServices;
040import org.apache.turbine.services.rundata.RunDataService;
041import org.apache.turbine.util.RunData;
042import org.apache.turbine.util.TurbineConfig;
043import org.junit.jupiter.api.AfterAll;
044import org.junit.jupiter.api.BeforeAll;
045import org.junit.jupiter.api.BeforeEach;
046import org.junit.jupiter.api.Test;
047
048/**
049 * Unit test for Localization Tool. Verifies that localization works the same using the
050 * deprecated Turbine localization service as well as the new Fulcrum Localization
051 * component.
052 *
053 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
054 * @version $Id$
055 */
056public class RundataLocalizationToolTest extends BaseUnit5Test
057{
058    private static TurbineConfig tc = null;
059    
060    @TurbineTool(LocalizationTool.class)
061    private LocalizationTool lt;
062    
063    @BeforeAll
064    public static void setUp() throws Exception
065    {
066        tc = new TurbineConfig(".", "/conf/test/rundataTemplateService.properties");
067        tc.initialize();
068        
069    }
070
071    @BeforeEach
072    public void initTool() throws Exception
073    {
074        AnnotationProcessor.process(this);   
075        lt.init(getRunData());
076    }
077
078    @Test
079    public void testGet() throws Exception
080    {
081        assertEquals("wert1", lt.get("key1"));
082        assertEquals("wert3", lt.get("key3"));
083    }
084
085    @Test
086    public void testGetLocale() throws Exception
087    {
088        assertNotNull(lt.getLocale());
089        assertEquals("DE", lt.getLocale().getCountry());
090        assertEquals("de", lt.getLocale().getLanguage());
091    }
092
093    @Test
094    public void testInit() throws Exception
095    {
096        assertNotNull(lt.getLocale());
097    }
098
099    private RunData getRunData() throws Exception
100    {
101        RunDataService rds = (RunDataService) TurbineServices.getInstance().getService(RunDataService.SERVICE_NAME);
102        ServletConfig config = mock(ServletConfig.class);
103        HttpServletRequest request = getMockRequest();
104        HttpServletResponse response = mock(HttpServletResponse.class);
105        RunData runData = rds.getRunData(request, response, config);
106        
107        User user = null;
108        try {
109            user = new DefaultUserImpl(mock(TurbineUser.class));
110            user.setTemp("locale", new Locale("de","DE") );
111            runData.setUser(user);            
112        } catch (Exception e) {
113            e.printStackTrace();
114        }
115        
116        return runData;
117    }
118
119    @AfterAll
120    public static void destroy() {
121        tc.dispose();
122    }
123}