Java tutorial
/** * 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 static org.junit.Assert.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import junit.framework.Assert; import org.apache.commons.lang.StringUtils; import org.junit.Test; import com.ewcms.publication.ObjectBean; import com.ewcms.publication.freemarker.FreemarkerTest; import freemarker.template.Configuration; import freemarker.template.Template; /** * ObjectPropertyDirective? * * @author wangwei * */ public class PropertyDirectiveTest extends FreemarkerTest { @Override protected void currentConfiguration(Configuration cfg) { cfg.setSharedVariable("property", new PropertyDirective()); } /** * * * @throws Exception */ @Test public void testGetValue() throws Exception { ObjectBean objectValue = new ObjectBean(); objectValue.setId(1); objectValue.setReadOnly(true); ObjectBean parent = new ObjectBean(); parent.setTitle("test"); objectValue.setParent(parent); PropertyDirective directive = new PropertyDirective(); Integer id = (Integer) directive.getValue(objectValue, "id"); assertEquals(id, new Integer(1)); Boolean readOnly = (Boolean) directive.getValue(objectValue, "readOnly"); assertTrue(readOnly); String title = (String) directive.getValue(objectValue, "parent.title"); assertEquals(title, "test"); } /** * ? * * @param name ??? * @return */ private String getTemplatePath(String name) { return String.format("directive/objectproperty/%s", name); } /** * * * <p>???</p> * * @throws Exception */ @Test public void testVauleTemplate() throws Exception { Template template = cfg.getTemplate(getTemplatePath("value.html")); ObjectBean objectValue = new ObjectBean(); objectValue.setTitle("test"); Map<String, Object> map = new HashMap<String, Object>(); map.put("object", objectValue); List<ObjectBean> list = new ArrayList<ObjectBean>(); objectValue = new ObjectBean(); objectValue.setTitle("test"); list.add(objectValue); objectValue = new ObjectBean(); objectValue.setTitle("test1"); list.add(objectValue); map.put("objects", list); String content = process(template, map); content = StringUtils.deleteWhitespace(content); assertEquals(content, "test|testtest1"); } /** * * * @throws Exception */ @Test public void testPropertyIsNullOfValueTemplate() throws Exception { Template template = cfg.getTemplate(getTemplatePath("value.html")); ObjectBean objectValue = new ObjectBean(); Map<String, Object> map = new HashMap<String, Object>(); map.put("object", objectValue); List<ObjectBean> list = new ArrayList<ObjectBean>(); objectValue = new ObjectBean(); objectValue.setTitle("test"); list.add(objectValue); objectValue = new ObjectBean(); objectValue.setTitle("test1"); list.add(objectValue); map.put("objects", list); String content = process(template, map); content = StringUtils.deleteWhitespace(content); assertEquals(content, "|testtest1"); } /** * ? * * @throws Exception */ @Test public void testExceptionTemplate() throws Exception { Template template = cfg.getTemplate(getTemplatePath("exception.html")); ObjectBean objectValue = new ObjectBean(); objectValue.setTitle("test"); Map<String, Object> map = new HashMap<String, Object>(); map.put("object", objectValue); String content = process(template, map); content = StringUtils.deleteWhitespace(content); Assert.assertEquals("throwsException", content); } /** * * * @throws Exception */ @Test public void testLoopTemplate() throws Exception { Template template = cfg.getTemplate(getTemplatePath("loop.html")); ObjectBean objectValue = new ObjectBean(); objectValue.setTitle("test"); Map<String, Object> map = new HashMap<String, Object>(); map.put("object", objectValue); String content = process(template, map); content = StringUtils.deleteWhitespace(content); assertEquals(content, "testtest"); } /** * * * @throws Exception */ @Test public void testPropertyIsNullOfLoopTemplate() throws Exception { Template template = cfg.getTemplate(getTemplatePath("loop.html")); ObjectBean objectValue = new ObjectBean(); Map<String, Object> map = new HashMap<String, Object>(); map.put("object", objectValue); String content = process(template, map); content = StringUtils.deleteWhitespace(content); assertEquals(content, ""); } }