test.pl.chilldev.web.core.markup.GeneratorTest.java Source code

Java tutorial

Introduction

Here is the source code for test.pl.chilldev.web.core.markup.GeneratorTest.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.core.markup;

import org.junit.Test;
import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.text.translate.CharSequenceTranslator;

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

public class GeneratorTest {
    @Test
    public void isXhtml() {
        Generator generator = new Generator(true);
        assertTrue("Generator.isXhtml() should return XHTML switch state.", generator.isXhtml());
    }

    @Test
    public void getEscaper() {
        CharSequenceTranslator escaper = StringEscapeUtils.ESCAPE_JSON;
        Generator generator = new Generator(true, escaper);
        assertSame("Generator.getEscaper() should return assigned escaper.", escaper, generator.getEscaper());
    }

    @Test
    public void generateElement() {
        String element = "script";
        String key = "foo";
        String value = "bar";

        Generator generator = new Generator(false);
        Map<String, String> attributes = new HashMap<>();
        attributes.put(key, value);

        assertEquals(
                "Generator.generateElement() should generate markup for given element name with all attributes.",
                "<script foo=\"bar\">", generator.generateElement(element, attributes));
    }

    @Test
    public void generateElementXhtml() {
        String element = "script";
        String key = "foo";
        String value = "bar";

        Generator generator = new Generator(true);
        Map<String, String> attributes = new HashMap<>();
        attributes.put(key, value);

        assertEquals("Generator.generateElement() should generate short close tags for XHTML markup.",
                "<script foo=\"bar\"/>", generator.generateElement(element, attributes));
    }

    @Test
    public void generateElementEscaping() {
        String element = "script";
        String key = "foo";
        String value = "bar & baz";

        Generator generator = new Generator(false);
        Map<String, String> attributes = new HashMap<>();
        attributes.put(key, value);

        assertEquals("Generator.generateElement() should use XML escaping by default to escape attributes.",
                "<script foo=\"bar &amp; baz\">", generator.generateElement(element, attributes));
    }

    @Test
    public void generateElementCustomEscaping() {
        String element = "script";
        String key = "foo";
        String value = "bar \" baz";

        Generator generator = new Generator(false, StringEscapeUtils.ESCAPE_JSON);
        Map<String, String> attributes = new HashMap<>();
        attributes.put(key, value);

        assertEquals("Generator.generateElement() should use specified escaping translator to escape attributes.",
                "<script foo=\"bar \\\" baz\">", generator.generateElement(element, attributes));
    }
}