View Javadoc

1   /*
2    * $Id: FreemarkerTest.java 451724 2006-10-01 08:39:39Z tmjee $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }