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

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.spring.config.XmlnsBeanDefinitionParserTest.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.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.Map;

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

import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.beans.factory.xml.XmlReaderContext;

import org.w3c.dom.Element;

import pl.chilldev.web.spring.config.XmlnsBeanDefinitionParser;

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

    @Mock
    private XmlReaderContext readerContext;

    @Test
    public void parse() throws URISyntaxException {
        GenericBeanDefinition bean = new GenericBeanDefinition();
        XmlnsBeanDefinitionParser parser = new XmlnsBeanDefinitionParser(bean);

        String alias = "cdv";
        String namespace = "http://chilldev.pl/";

        when(this.element.getAttribute("alias")).thenReturn(alias);
        when(this.element.getAttribute("namespace")).thenReturn(namespace);

        parser.parse(this.element, null);

        URI uri = new URI(namespace);
        Map<URI, String> xmlNamespaces = (Map<URI, String>) bean.getPropertyValues()
                .getPropertyValue(XmlnsBeanDefinitionParser.PROPERTY_XMLNAMESPACES).getValue();

        assertTrue("XmlnsBeanDefinitionParser.parse() should register XML namespace.",
                xmlNamespaces.containsKey(uri));
        assertEquals("XmlnsBeanDefinitionParser.parse() should register alias for XML namespace.", alias,
                xmlNamespaces.get(uri));
    }

    @Test
    public void parseWithInvalidUri() {
        XmlnsBeanDefinitionParser parser = new XmlnsBeanDefinitionParser(new GenericBeanDefinition());

        when(this.element.getAttribute("alias")).thenReturn("cdv");
        when(this.element.getAttribute("namespace")).thenReturn("Chillout Development");

        parser.parse(this.element, new ParserContext(this.readerContext, null));

        verify(this.readerContext).error(anyString(), same(this.element), isA(URISyntaxException.class));
    }
}