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