com.ewcms.publication.freemarker.directive.ArticleDirectiveTest.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.publication.freemarker.directive.ArticleDirectiveTest.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

package com.ewcms.publication.freemarker.directive;

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

import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.junit.Test;

import com.ewcms.content.document.model.Article;
import com.ewcms.publication.freemarker.FreemarkerTest;
import com.ewcms.publication.freemarker.GlobalVariable;
import com.ewcms.publication.freemarker.directive.out.DateDirectiveOut;
import com.ewcms.publication.freemarker.directive.out.DefaultDirectiveOut;
import com.ewcms.publication.freemarker.directive.out.DirectiveOutable;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * ArticleDirective?
 * 
 * @author wangwei
 */
public class ArticleDirectiveTest extends FreemarkerTest {

    @Test
    public void testNewPutDirective() throws Exception {
        ArticleDirective directive = new ArticleDirective();
        directive.putDirective("", "test", new DefaultDirectiveOut());

        String name = directive.getPropertyName("");
        Assert.assertEquals("test", name);
        name = directive.getPropertyName("test");
        Assert.assertEquals("test", name);

        DirectiveOutable out = directive.getDirectiveOut("test");
        Assert.assertNotNull(out);
    }

    @Test
    public void testUpdatePutDirective() throws Exception {
        ArticleDirective directive = new ArticleDirective();
        directive.putDirective("", "title", new DateDirectiveOut());

        String name = directive.getPropertyName("");
        Assert.assertEquals("title", name);
        name = directive.getPropertyName("");
        Assert.assertEquals("title", name);
        name = directive.getPropertyName("title");
        Assert.assertEquals("title", name);

        DirectiveOutable out = directive.getDirectiveOut("title");
        Assert.assertEquals(DateDirectiveOut.class, out.getClass());
    }

    @Test
    public void testAlreadyHasAlias() throws Exception {
        String[] aliases = initAliases();

        ArticleDirective directive = new ArticleDirective();

        for (String alias : aliases) {
            String name = directive.getPropertyName(alias);
            Assert.assertNotNull(name);
        }
    }

    @Test
    public void testAlreadyHasDirectiveOut() throws Exception {
        String[] aliases = initAliases();

        ArticleDirective directive = new ArticleDirective();

        for (String alias : aliases) {
            String name = directive.getPropertyName(alias);
            DirectiveOutable out = directive.getDirectiveOut(name);
            Assert.assertNotNull(out);
        }
    }

    private String[] initAliases() {
        return new String[] { "?", "", "", "", "", "", "?",
                "??", "", "", "", "?", "?", "", "",
                "", "?" };
    }

    @Override
    protected void currentConfiguration(Configuration cfg) {
        cfg.setSharedVariable("article", new ArticleDirective());
    }

    /**
     * ?
     * 
     * @param name ???
     * @return
     */
    private String getTemplatePath(String name) {
        return String.format("directive/article/%s", name);
    }

    @Test
    public void testArticleTemplate() throws Exception {
        Template template = cfg.getTemplate(getTemplatePath("article.html"));

        Map<Object, Object> params = new HashMap<Object, Object>();
        Article article = new Article();
        article.setTitle("");
        params.put(GlobalVariable.ARTICLE.toString(), article);
        String value = this.process(template, params);

        value = StringUtils.deleteWhitespace(value);
        Assert.assertTrue(value.indexOf("_alias") != -1);
        Assert.assertTrue(value.indexOf("_property") != -1);
        Assert.assertTrue(value.indexOf("</html>") != -1);
    }
}