1   package test.tuigwaa;
2   
3   import java.io.StringWriter;
4   import java.util.ArrayList;
5   import java.util.List;
6   import java.util.Properties;
7   
8   import junit.framework.TestCase;
9   
10  import org.apache.velocity.Template;
11  import org.apache.velocity.VelocityContext;
12  import org.apache.velocity.app.Velocity;
13  import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
14  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
15  import org.seasar.tuigwaa.cms.core.wiki.base.WikiHelper;
16  import org.seasar.tuigwaa.view.TgwDatabaseResourceLoader;
17  
18  import com.isenshi.util.ResourceUtils;
19  
20  public class VelocityTest extends TestCase {
21  
22  
23  	
24  	
25  	public void testVelocity() {
26  
27  		String ret = null;
28  
29  
30  		String[] args = new String[]{"arg1da", "arg2aaa"};
31  		
32  		List list = new ArrayList();
33  		
34  		list.add("hoge");
35  		list.add("hage");
36  		
37  
38  		
39  		
40  		try {
41  			
42  			Properties props = new Properties();
43  			props.put("resource.loader", "CLASSPATH");
44  			props.put("CLASSPATH.resource.loader.class",
45  			//		TgwDatabaseResourceLoader.class.getName());
46  					FileResourceLoader.class.getName());
47  			 props.put("CLASSPATH.resource.loader.path",
48  				ResourceUtils.getPath(""));
49  		//"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
50  //			Properties props = new Properties();
51  //			props.load(ResourceUtils.getInputStream("velocity.properties"));
52  			
53  			Velocity.init(props);
54  			//Velocity.init("velocity.properties");
55  			//Velocity.init();
56  			VelocityContext context = new VelocityContext();
57  			context.put("hoge", new Hoge("nishioka"));
58  			context.put("args", args);
59  			context.put("list", list);
60  			
61  			
62  			
63  			StringWriter writer = new StringWriter();
64  
65  			Template template = Velocity.getTemplate("test.vm", "Windows-31J");
66  			template.merge(context, writer);
67  
68  			ret = writer.toString();
69  
70  			
71  		} catch (Exception e) {
72  			// TODO Auto-generated catch block
73  			e.printStackTrace();
74  		}
75  
76  		System.out.println(ret);
77  	}
78  
79  	public void testTest(){
80  		String test = "\n#aaa<br /> bbbb" + "ccc<br />#abc(hoge)<br />add $itm"  + "<br />#foreach($itm in [&quot;aa,bb])";
81  		String str = WikiHelper.toVTL(test);
82  		
83  		System.out.println("------------------");
84  		System.out.println(str);
85  		System.out.println("------------------");
86  		
87  	}
88  	
89  	
90  	
91  	public static class Hoge {
92  
93  		private String name;
94  
95  		public Hoge(String name) {
96  			this.name = name;
97  		}
98  
99  		public void setName(String name) {
100 			this.name = name;
101 		}
102 
103 		public String getName() {
104 			return name;
105 		}
106 	}
107 }