/*
 * Copyright (C), 2013-2014, 上海汽车集团股份有限公司
 */
package com.tojaoomy.freemarker.test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.tojaoomy.freemarker.bean.Tupple;
import com.tojaoomy.freemarker.cache.Holder;
import com.tojaoomy.freemarker.method.FreemarkerMapFunction;
import com.tojaoomy.freemarker.util.FreemarkerConfiguration;

/**
 * @author hejian
 *
 */
public class FreemarkerTest {
	
	private static final String TEMPLATE = "debug.ftl";
	private static final String USERNAME = "michael";
	
	private Map<String,Object> param = null;
	
	@Before
	public void before(){
		
		param = new HashMap<String, Object>();
		param.put("username", USERNAME);
		param.put("salary", new BigDecimal(0.01));
		param.put("salary", 2.4512);
		param.put("boolean", true);
		param.put("bigger", 12222.34);
		param.put("lowwer", 10000.22);
		
		Map<Object,Object> map = new HashMap<Object,Object>();
		map.put("1111", 1234);
		map.put("abcd", 1234);
		map.put(123, "int");
		map.put(123L, "long");
		map.put(123f, "float");
		map.put(123d, "double");
		map.put((short) 123, "short");
		map.put((byte) 123, "byte");
		map.put(true, "true");
		map.put(false, "false");
		map.put(BigDecimal.valueOf(123), "BigDecimal");
		
		Holder holder = new Holder();
		map.put(holder, "Holder");
		
		Map<String,Object> mapNew = new HashMap<String, Object>();
		mapNew.put("test", "test");
		map.put(mapNew, "Map");
		
		List<String> list = new ArrayList<String>();
		list.add("list");
		map.put(list, "List");
		
		Collection<String> collection = new ArrayList<String>();
		collection.add("collection");
		Iterator<String> it = collection.iterator(); 
		map.put(it, "Collection");
		
		Tupple<String,String> tupple = new Tupple<String,String>();
		tupple.put("demo", "demo");
		
		param.put("holder", holder);
		param.put("mapTest", map);
		param.put("list", list);
		param.put("mapNew", mapNew);
		param.put("collection", it);
		param.put("tupple", tupple);
		param.put("object", new PlaceHolderTest());
		param.put("testMap", false);
		//注册自定义函数
		param.put("mapFun", new FreemarkerMapFunction());
	}
	
	@Test
	public void test(){
		FreemarkerConfiguration.outputConsole(TEMPLATE, param);
//		FreemarkerUtil.outputFile(TEMPLATE, param, "Demo.html");
	}
	
	public static void main(String[] args) {
		Map<String,List<Object>> map = new HashMap<String, List<Object>>();
		List<Object> data = new ArrayList<Object>();
		data.add(null);
		map.put("封面", data);
		System.out.println(data.contains(null));
		System.out.println(JSONObject.toJSONString(map));
	}
}
