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

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.spring.context.PageMetaModelFactoryBeanTest.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 static org.junit.Assert.*;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.BeanInitializationException;

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

import pl.chilldev.web.spring.context.PageMetaModelFactoryBean;
import pl.chilldev.web.spring.model.Link;
import pl.chilldev.web.spring.model.Script;
import pl.chilldev.web.spring.model.Stylesheet;

public class PageMetaModelFactoryBeanTest {
    @Test
    public void setXmlNamespaces() throws URISyntaxException {
        Map<URI, String> xmlNamespaces = new HashMap<>();
        xmlNamespaces.put(new URI("http://chilldev.pl/"), "cdv");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setXmlNamespaces(xmlNamespaces);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setXmlNamespaces() should set XML namespaces list.",
                " xmlns:cdv=\"http://chilldev.pl/\"", page.generateXmlnsAttributes());
    }

    @Test
    public void setTitle() {
        List<String> title = new ArrayList<>();
        title.add("Test");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setTitle(title);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setTitle() should set initial title parts.", "Test",
                page.generateTitleContent());
    }

    @Test
    public void setTitleSeparator() {
        List<String> title = new ArrayList<>();
        title.add("Foo");
        title.add("Bar");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setTitle(title);
        factory.setTitleSeparator(" :: ");

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setTitleSeparator() should set title parts separator.", "Foo :: Bar",
                page.generateTitleContent());
    }

    @Test
    public void setMetaHttpEquiv() {
        Map<String, Object> metaData = new HashMap<>();
        metaData.put("foo", "bar");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setMetaHttpEquiv(metaData);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setMetaHttpEquiv() should set http-equiv=\"\" meta data.",
                "<meta content=\"bar\" http-equiv=\"foo\"/><meta content=\"\" name=\"keywords\"/>",
                page.generateMetaElements());
    }

    @Test
    public void setMetaNames() {
        Map<String, Object> metaData = new HashMap<>();
        metaData.put("foo", "bar");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setMetaNames(metaData);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setMetaNames() should set name=\"\" meta data.",
                "<meta content=\"\" name=\"keywords\"/><meta content=\"bar\" name=\"foo\"/>",
                page.generateMetaElements());
    }

    @Test
    public void setMetaProperties() {
        Map<String, Object> metaData = new HashMap<>();
        metaData.put("foo", "bar");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setMetaProperties(metaData);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setMetaProperties() should set property=\"\" meta data.",
                "<meta content=\"\" name=\"keywords\"/><meta content=\"bar\" property=\"foo\"/>",
                page.generateMetaElements());
    }

    @Test
    public void setKeywords() {
        List<String> phrases = new ArrayList<>();
        phrases.add("foo");

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setKeywords(phrases);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setKeywords() should set keywords.",
                "<meta content=\"foo\" name=\"keywords\"/>", page.generateMetaElements());
    }

    @Test
    public void setScripts() {
        List<Script> scripts = new ArrayList<>();
        scripts.add(new Script("foo.js", "application/javascript", "ASYNC", "utf-8"));

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setScripts(scripts);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setScripts() should set initial scripts.",
                "<script async=\"async\" charset=\"utf-8\" src=\"foo.js\" type=\"application/javascript\"/>",
                page.generateScriptElements());
    }

    @Test
    public void setLinks() {
        Set<String> rels = new HashSet<>();
        rels.add("alternate");

        List<Link> links = new ArrayList<>();
        links.add(new Link("blog.rss", rels, "application/rss+xml", "print"));

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setLinks(links);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setLinks() should set initial links.",
                "<link href=\"blog.rss\" media=\"print\" rel=\"alternate\" type=\"application/rss+xml\"/>",
                page.generateLinkElements());
    }

    @Test
    public void setStylesheets() {
        List<Stylesheet> stylesheets = new ArrayList<>();
        stylesheets.add(new Stylesheet("foo.css", "text/css", "print"));

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();
        factory.setStylesheets(stylesheets);

        PageMetaModel page = factory.createPageMetaModel();

        assertEquals("PageMetaModelFactoryBean.setStylesheets() should set initial stylesheets.",
                "<link href=\"foo.css\" media=\"print\" rel=\"stylesheet\" type=\"text/css\"/>",
                page.generateLinkElements());
    }

    @Test
    public void createPageMetaModelWithGenerator() {
        Generator generator = new Generator(false);

        PageMetaModelFactoryBean factory = new PageMetaModelFactoryBean();

        PageMetaModel page = factory.createPageMetaModel(generator);
        page.addScript("foo");

        assertEquals(
                "PageMetaModelFactoryBean.createPageMetaModel() should pass given generator instance into constructor.",
                "<script src=\"foo\"></script>", page.generateScriptElements());
    }
}