/*
* Copyright 2005-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.strecks.injection.handler;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createStrictMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.Globals;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.util.MessageResources;
import org.strecks.context.ActionContext;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.injection.handler.impl.ActionWithMessageResourcesHelper;
import org.strecks.injection.internal.InjectionAnnotationReader;
import org.strecks.injection.internal.InjectionWrapper;
import org.strecks.message.MessageResourcesHelper;
import org.strecks.message.MessageResourcesHelperImpl;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
/**
* @author Phil Zoio
*/
public class TestMessageResourcesHelperHandler
{
private Map<String, InjectionWrapper> inputs;
private ServletContext context;
private HttpServletRequest request;
private ModuleConfig moduleConfig;
private MessageResources messageResources;
private Locale locale;
private InjectionAnnotationReader reader;
@BeforeMethod
public void beforeTest()
{
reader = new InjectionAnnotationReader();
context = createStrictMock(ServletContext.class);
request = createStrictMock(HttpServletRequest.class);
moduleConfig = createStrictMock(ModuleConfig.class);
messageResources = createStrictMock(MessageResources.class);
locale = Locale.getDefault();
}
@Test
public void testNamedBundle()
{
ActionWithMessageResourcesHelper action = new ActionWithMessageResourcesHelper();
reader.readAnnotations(action.getClass());
inputs = reader.getInjectionMap();
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.getPrefix()).andReturn("/mymodule");
expect(context.getAttribute("my.key/mymodule")).andReturn(messageResources);
expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(locale);
//expected from helper getMessage() invocation
expect(messageResources.getMessage(eq(Locale.getDefault()), eq("some.key"))).andReturn("My Message");
replay(context);
replay(request);
replay(moduleConfig);
replay(messageResources);
InjectionWrapper inputWrapper = inputs.get("namedHelper");
ActionContext injectionContext = new TestContextImpl(request, null, context, null, null);
inputWrapper.inject(action, injectionContext);
MessageResourcesHelper namedHelper = action.getNamedHelper();
namedHelper.getMessage("some.key");
verify(context);
verify(request);
verify(moduleConfig);
verify(messageResources);
assert namedHelper != null;
assert namedHelper instanceof MessageResourcesHelperImpl;
assert namedHelper.getMessageResources() == messageResources;
assert namedHelper.getLocale() == locale;
}
@Test
public void testDefaultBundle()
{
ActionWithMessageResourcesHelper action = new ActionWithMessageResourcesHelper();
reader.readAnnotations(action.getClass());
inputs = reader.getInjectionMap();
expect(request.getAttribute(Globals.MODULE_KEY)).andReturn(moduleConfig);
expect(moduleConfig.getPrefix()).andReturn("");
expect(context.getAttribute(Globals.MESSAGES_KEY)).andReturn(messageResources);
expect(request.getAttribute(Globals.LOCALE_KEY)).andReturn(locale);
//expected from helper getMessage() invocation
expect(messageResources.getMessage(eq(Locale.getDefault()), eq("some.key"))).andReturn("My Message");
replay(context);
replay(request);
replay(moduleConfig);
replay(messageResources);
InjectionWrapper inputWrapper = inputs.get("defaultHelper");
ActionContext injectionContext = new TestContextImpl(request, null, context, null, null);
inputWrapper.inject(action, injectionContext);
MessageResourcesHelper defaultHelper = action.getDefaultHelper();
defaultHelper.getMessage("some.key");
verify(context);
verify(request);
verify(moduleConfig);
verify(messageResources);
assert defaultHelper != null;
assert defaultHelper instanceof MessageResourcesHelperImpl;
assert defaultHelper.getMessageResources() == messageResources;
assert defaultHelper.getLocale() == locale;
}
}