Package org.strecks.injection.handler

Source Code of org.strecks.injection.handler.TestMessageResourcesHandler

/*
* 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.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.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.ActionWithDefaultMessageResources;
import org.strecks.injection.handler.impl.ActionWithMessageResources;
import org.strecks.injection.internal.InjectionAnnotationReader;
import org.strecks.injection.internal.InjectionWrapper;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* @author Phil Zoio
*/
public class TestMessageResourcesHandler
{

  private Map<String, InjectionWrapper> inputs;

  private ServletContext context;

  private HttpServletRequest request;

  private ModuleConfig moduleConfig;

  private MessageResources messageResources;

  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);

  }

  @Test
  public void testNamedBundle()
  {

    ActionWithMessageResources action = new ActionWithMessageResources();
    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);

    replay(context);
    replay(request);
    replay(moduleConfig);
    replay(messageResources);

    InjectionWrapper inputWrapper = inputs.get("resources");

    ActionContext injectionContext = new TestContextImpl(request, null, context, null, null);
    inputWrapper.inject(action, injectionContext);

    verify(context);
    verify(request);
    verify(moduleConfig);
    verify(messageResources);

    assert action.getResources() != null;

  }

  @Test
  public void testDefaultBundle()
  {

    ActionWithDefaultMessageResources action = new ActionWithDefaultMessageResources();
    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);

    replay(context);
    replay(request);
    replay(moduleConfig);
    replay(messageResources);

    InjectionWrapper inputWrapper = inputs.get("resources");

    ActionContext injectionContext = new TestContextImpl(request, null, context, null, null);
    inputWrapper.inject(action, injectionContext);

    verify(context);
    verify(request);
    verify(moduleConfig);
    verify(messageResources);

    assert action.getResources() != null;

  }

}
TOP

Related Classes of org.strecks.injection.handler.TestMessageResourcesHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.