Package org.strecks.source

Source Code of org.strecks.source.TestSpringActionBeanSource

/*
* 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.source;

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 javax.servlet.ServletContext;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.web.context.WebApplicationContext;
import org.strecks.context.impl.TestContextImpl;
import org.strecks.controller.impl.TestAction;
import org.testng.Assert;
import org.testng.annotations.Test;

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

  @Test
  public void testSource()
  {
    Class actionBeanClass = TestAction.class;

    ServletContext servletContext = createStrictMock(ServletContext.class);
    WebApplicationContext wac = createStrictMock(WebApplicationContext.class);

    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.isSingleton("beanName")).andReturn(false);
    expect(wac.getBean("beanName", TestAction.class)).andReturn(new TestAction());

    // second call - note that isSingleton is not called second time
    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.getBean("beanName", TestAction.class)).andReturn(new TestAction());

    replay(servletContext);
    replay(wac);

    SpringActionBeanSource source = new SpringActionBeanSource(actionBeanClass, "beanName");
    TestContextImpl context = new TestContextImpl(servletContext);
    Object bean1 = source.createBean(context);
    source.createBean(context);

    assert bean1 instanceof TestAction;

    verify(servletContext);
    verify(wac);

  }

  @Test
  public void testNoWac()
  {
    Class actionBeanClass = TestAction.class;

    ServletContext servletContext = createStrictMock(ServletContext.class);

    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)).andReturn(
        null);

    replay(servletContext);

    SpringActionBeanSource source = new SpringActionBeanSource(actionBeanClass, "beanName");
    TestContextImpl context = new TestContextImpl(servletContext);

    try
    {
      source.createBean(context);
      Assert.fail();
    }
    catch (IllegalStateException e)
    {
    }

    verify(servletContext);

  }

  @Test
  public void testNoBeanRegistered()
  {

    Class actionBeanClass = TestAction.class;

    ServletContext servletContext = createStrictMock(ServletContext.class);
    WebApplicationContext wac = createStrictMock(WebApplicationContext.class);

    expect(servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE))
        .andReturn(wac);
    expect(wac.isSingleton("beanName")).andReturn(false);
    expect(wac.getBean("beanName", TestAction.class)).andThrow(
        new NoSuchBeanDefinitionException("beanName", "message"));

    replay(servletContext);
    replay(wac);

    SpringActionBeanSource source = new SpringActionBeanSource(actionBeanClass, "beanName");
    TestContextImpl context = new TestContextImpl(servletContext);

    try
    {
      source.createBean(context);
      Assert.fail();
    }
    catch (NoSuchBeanDefinitionException e)
    {
    }

    verify(servletContext);
    verify(wac);

  }

}
TOP

Related Classes of org.strecks.source.TestSpringActionBeanSource

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.