test.pl.chilldev.web.spring.config.LinkBeanDefinitionParserTest.java Source code

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.spring.config.LinkBeanDefinitionParserTest.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.config;

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

import java.util.List;
import java.util.Set;

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

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.GenericBeanDefinition;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import pl.chilldev.web.spring.config.LinkBeanDefinitionParser;
import pl.chilldev.web.spring.model.Link;

@RunWith(MockitoJUnitRunner.class)
public class LinkBeanDefinitionParserTest {
    @Mock
    private Element element;

    @Mock
    private Element rel;

    @Mock
    private NodeList children;

    @Mock
    private CharacterData data;

    @Mock
    private NodeList dataContainer;

    private String relation = "alternate";

    @Before
    public void setUp() {
        String rel = "rel";
        when(this.element.getChildNodes()).thenReturn(this.children);
        when(this.children.getLength()).thenReturn(1);
        when(this.children.item(0)).thenReturn(this.rel);
        when(this.rel.getNodeName()).thenReturn(rel);
        when(this.rel.getLocalName()).thenReturn(rel);
        when(this.data.getNodeValue()).thenReturn(this.relation);
        when(this.dataContainer.getLength()).thenReturn(1);
        when(this.dataContainer.item(0)).thenReturn(this.data);
        when(this.rel.getChildNodes()).thenReturn(this.dataContainer);
    }

    @Test
    public void parse() {
        GenericBeanDefinition bean = new GenericBeanDefinition();
        LinkBeanDefinitionParser parser = new LinkBeanDefinitionParser(bean);

        String href = "blog.rss";
        String media = "print";
        String type = "application/rss+xml";

        when(this.element.getAttribute("href")).thenReturn(href);
        when(this.element.hasAttribute("media")).thenReturn(true);
        when(this.element.getAttribute("media")).thenReturn(media);
        when(this.element.hasAttribute("type")).thenReturn(true);
        when(this.element.getAttribute("type")).thenReturn(type);

        parser.parse(this.element, null);

        List<BeanDefinition> links = (List<BeanDefinition>) bean.getPropertyValues()
                .getPropertyValue(LinkBeanDefinitionParser.PROPERTY_LINKS).getValue();
        BeanDefinition link = links.get(0);
        ConstructorArgumentValues arguments = link.getConstructorArgumentValues();

        assertEquals("LinkBeanDefinitionParser.parse() should register link with it's location.", href,
                arguments.getIndexedArgumentValue(0, null).getValue());
        assertTrue("LinkBeanDefinitionParser.parse() should register link with all it's relations.",
                ((Set) (arguments.getIndexedArgumentValue(1, null).getValue())).contains(this.relation));
        assertEquals("LinkBeanDefinitionParser.parse() should register link with it's MIME type.", type,
                arguments.getIndexedArgumentValue(2, null).getValue());
        assertEquals("LinkBeanDefinitionParser.parse() should register link with it's media query.", media,
                arguments.getIndexedArgumentValue(3, null).getValue());
    }
}