1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.views.freemarker;
22
23 import java.util.List;
24
25 import junit.framework.TestCase;
26
27 import org.apache.struts2.util.ListEntry;
28 import org.apache.struts2.util.StrutsUtil;
29
30 import com.opensymphony.xwork2.ActionContext;
31 import com.opensymphony.xwork2.util.ValueStack;
32 import com.opensymphony.xwork2.util.ValueStackFactory;
33
34 import freemarker.ext.beans.CollectionModel;
35 import freemarker.template.ObjectWrapper;
36
37
38 /***
39 */
40 public class FreemarkerTest extends TestCase {
41
42 TestAction testAction = null;
43
44
45 /***
46 *
47 */
48 public FreemarkerTest(String name) {
49 super(name);
50 }
51
52
53 public void testSelectHelper() {
54 StrutsUtil wwUtil = new StrutsUtil(ActionContext.getContext().getValueStack(), null, null);
55
56 List selectList = null;
57
58 selectList = wwUtil.makeSelectList("ignored", "stringList", null, null);
59 assertEquals("one", ((ListEntry) selectList.get(0)).getKey());
60 assertEquals("one", ((ListEntry) selectList.get(0)).getValue());
61
62 selectList = wwUtil.makeSelectList("ignored", "beanList", "name", "value");
63 assertEquals("one", ((ListEntry) selectList.get(0)).getKey());
64 assertEquals("1", ((ListEntry) selectList.get(0)).getValue());
65 }
66
67 public void testValueStackMode() throws Exception {
68 ScopesHashModel model = new ScopesHashModel(ObjectWrapper.BEANS_WRAPPER, null, null, ActionContext.getContext().getValueStack());
69
70 CollectionModel stringList = null;
71
72 stringList = (CollectionModel) model.get("stringList");
73 assertEquals("one", stringList.get(0).toString());
74
75 assertEquals("one", model.get("stringList[0]").toString());
76 assertEquals("one", model.get("beanList[0].name").toString());
77 }
78
79 protected void setUp() throws Exception {
80 super.setUp();
81
82 ValueStack stack = ValueStackFactory.getFactory().createValueStack();
83 ActionContext.setContext(new ActionContext(stack.getContext()));
84
85 testAction = new TestAction();
86 ActionContext.getContext().getValueStack().push(testAction);
87 }
88
89 protected void tearDown() throws Exception {
90 super.tearDown();
91 ActionContext.setContext(null);
92 }
93 }