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

Java tutorial

Introduction

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

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

    @Mock
    private Element keyword;

    @Mock
    private NodeList children;

    @Mock
    private CharacterData data;

    @Mock
    private NodeList dataContainer;

    private String phrase = "foo";

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

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

        parser.parse(this.element, null);

        List<String> keywords = (List<String>) bean.getPropertyValues()
                .getPropertyValue(KeywordsBeanDefinitionParser.PROPERTY_KEYWORDS).getValue();

        assertTrue("KeywordsBeanDefinitionParser.parse() should register keywords.",
                keywords.contains(this.phrase));
    }
}