com.ewcms.publication.freemarker.directive.page.SkipDirectiveTest.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.publication.freemarker.directive.page.SkipDirectiveTest.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.page;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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.publication.freemarker.FreemarkerTest;
import com.ewcms.publication.freemarker.GlobalVariable;
import com.ewcms.publication.uri.UriRuleable;

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

/**
 * SkipDirective?
 * 
 * @author wangwei
 */
public class SkipDirectiveTest extends FreemarkerTest {

    @Override
    protected void currentConfiguration(Configuration cfg) {
        cfg.setSharedVariable("page_skip", new SkipDirective());
        cfg.setSharedVariable("page", new PageOutDirective());
    }

    @Test
    public void testInitAliasesMapProperties() throws Exception {

        String[] aliases = initAliases();
        SkipDirective directive = new SkipDirective();
        for (String alias : aliases) {
            SkipPageable skipPage = directive.getSkipPage(alias);
            Assert.assertNotNull(skipPage);
        }
    }

    private String[] initAliases() {
        return new String[] { "f", "first", "", "", "n", "next", "", "", "p", "prev", "",
                "", "l", "last", "", "" };
    }

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

    @Test
    public void testPageCountIsOnlyOne() throws Exception {
        Template template = cfg.getTemplate(getTemplatePath("skip.html"));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(GlobalVariable.PAGE_NUMBER.toString(), Integer.valueOf(0));
        params.put(GlobalVariable.PAGE_COUNT.toString(), Integer.valueOf(1));
        String value = this.process(template, params);
        value = StringUtils.deleteWhitespace(value);
        Assert.assertEquals("|||", value);
    }

    @Test
    public void testSkipTemplate() throws Exception {
        Template template = cfg.getTemplate(getTemplatePath("skip.html"));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(GlobalVariable.PAGE_NUMBER.toString(), Integer.valueOf(0));
        params.put(GlobalVariable.PAGE_COUNT.toString(), Integer.valueOf(5));
        UriRuleable rule = mock(UriRuleable.class);
        when(rule.getUri()).thenReturn("");
        params.put(GlobalVariable.URI_RULE.toString(), rule);
        String value = this.process(template, params);
        value = StringUtils.deleteWhitespace(value);
        Assert.assertEquals("|||", value);
    }

    @Test
    public void testLoopTemplate() throws Exception {
        Template template = cfg.getTemplate(getTemplatePath("skiploop.html"));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(GlobalVariable.PAGE_NUMBER.toString(), Integer.valueOf(0));
        params.put(GlobalVariable.PAGE_COUNT.toString(), Integer.valueOf(5));
        UriRuleable rule = mock(UriRuleable.class);
        when(rule.getUri()).thenReturn("");
        params.put(GlobalVariable.URI_RULE.toString(), rule);
        String value = this.process(template, params);
        value = StringUtils.deleteWhitespace(value);
        Assert.assertEquals("loop|5|1", value);
    }
}