test.pl.chilldev.web.spring.context.SpringBeansJspPageMetaModelResolverTest.java Source code

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.spring.context.SpringBeansJspPageMetaModelResolverTest.java

Source

/**
 * This file is part of the ChillDev-Web.
 *
 * @license http://mit-license.org/ The MIT license
 * @copyright 2014  by Rafa Wrzeszcz - Wrzasq.pl.
 */

package test.pl.chilldev.web.spring.context;

import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.PageContext;

import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;

import org.springframework.mock.web.MockPageContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

import pl.chilldev.web.core.page.PageMetaModel;

import pl.chilldev.web.spring.context.SpringBeansJspPageMetaModelResolver;

import pl.chilldev.web.tags.context.PageMetaModelContextException;
import pl.chilldev.web.tags.context.PageMetaModelResolver;

@RunWith(MockitoJUnitRunner.class)
public class SpringBeansJspPageMetaModelResolverTest {
    @Mock
    private JspContext jspContext;

    @Mock
    private WebApplicationContext appContext;

    @Test
    public void getPageMetaModel() throws PageMetaModelContextException {
        PageContext jspContext = new MockPageContext();
        jspContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                this.appContext);

        PageMetaModelResolver resolver = new SpringBeansJspPageMetaModelResolver();
        PageMetaModel page = new PageMetaModel();

        when(this.appContext.getBean(PageMetaModel.class)).thenReturn(page);

        assertSame(
                "SpringBeansJspPageMetaModelResolver.getPageMetaModel() should return page model from Spring container.",
                page, resolver.getPageMetaModel(jspContext));
    }

    @Test(expected = PageMetaModelContextException.class)
    public void getPageMetaModelWithoutPageContext() throws PageMetaModelContextException {
        PageMetaModelResolver resolver = new SpringBeansJspPageMetaModelResolver();
        resolver.getPageMetaModel(this.jspContext);
    }

    @Test(expected = PageMetaModelContextException.class)
    public void getPageMetaModelWithoutBean() throws PageMetaModelContextException {
        GenericWebApplicationContext appContext = new GenericWebApplicationContext();
        appContext.refresh();
        PageContext jspContext = new MockPageContext();
        jspContext.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
                appContext);

        PageMetaModelResolver resolver = new SpringBeansJspPageMetaModelResolver();
        resolver.getPageMetaModel(jspContext);
    }
}