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

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.spring.config.TitleBeanDefinitionParserTest.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 org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.mockito.Mockito.*;

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.TitleBeanDefinitionParser;

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

    @Mock
    private Element part1;

    @Mock
    private Element part2;

    @Mock
    private NodeList children;

    @Mock
    private CharacterData data;

    @Mock
    private NodeList dataContainer;

    private String titlePart = "Title";

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

    @Test
    public void parseParts() {
        GenericBeanDefinition bean = new GenericBeanDefinition();
        TitleBeanDefinitionParser parser = new TitleBeanDefinitionParser(bean);

        when(this.element.hasAttribute("separator")).thenReturn(false);

        parser.parse(this.element, null);

        List<String> titleParts = (List<String>) bean.getPropertyValues()
                .getPropertyValue(TitleBeanDefinitionParser.PROPERTY_TITLE).getValue();

        assertTrue("TitleBeanDefinitionParser.parse() should register title parts.",
                titleParts.contains(this.titlePart));
    }

    @Test
    public void parseSeparator() {
        GenericBeanDefinition bean = new GenericBeanDefinition();
        TitleBeanDefinitionParser parser = new TitleBeanDefinitionParser(bean);

        String separator = " :: ";

        when(this.element.hasAttribute("separator")).thenReturn(true);
        when(this.element.getAttribute("separator")).thenReturn(separator);

        parser.parse(this.element, null);

        assertEquals("TitleBeanDefinitionParser.parse() should change title parts separator.", separator,
                bean.getPropertyValues().getPropertyValue(TitleBeanDefinitionParser.PROPERTY_TITLESEPARATOR)
                        .getValue());
    }
}