View Javadoc

1   /*
2    * $Id: FreemarkerTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.views.freemarker;
23  
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.struts2.StrutsTestCase;
29  import org.apache.struts2.util.ListEntry;
30  import org.apache.struts2.util.StrutsUtil;
31  
32  import com.opensymphony.xwork2.ActionContext;
33  import com.opensymphony.xwork2.util.ValueStack;
34  import com.opensymphony.xwork2.util.ValueStackFactory;
35  
36  import freemarker.ext.beans.CollectionModel;
37  import freemarker.template.ObjectWrapper;
38  
39  
40  /***
41   */
42  public class FreemarkerTest extends StrutsTestCase {
43  
44      TestAction testAction = null;
45  
46  
47      public void testSelectHelper() {
48          StrutsUtil wwUtil = new StrutsUtil(ActionContext.getContext().getValueStack(), null, null);
49          List selectList = null;
50  
51          selectList = wwUtil.makeSelectList("ignored", "stringList", null, null);
52          assertEquals("one", ((ListEntry) selectList.get(0)).getKey());
53          assertEquals("one", ((ListEntry) selectList.get(0)).getValue());
54  
55          selectList = wwUtil.makeSelectList("ignored", "beanList", "name", "value");
56          assertEquals("one", ((ListEntry) selectList.get(0)).getKey());
57          assertEquals("1", ((ListEntry) selectList.get(0)).getValue());
58      }
59  
60      public void testValueStackMode() throws Exception {
61          ScopesHashModel model = new ScopesHashModel(ObjectWrapper.BEANS_WRAPPER, null, null, ActionContext.getContext().getValueStack());
62  
63          CollectionModel stringList = null;
64  
65          stringList = (CollectionModel) model.get("stringList");
66          assertEquals("one", stringList.get(0).toString());
67  
68          assertEquals("one", model.get("stringList[0]").toString());
69          assertEquals("one", model.get("beanList[0].name").toString());
70      }
71  
72      protected void setUp() throws Exception {
73          super.setUp();
74  
75          testAction = new TestAction();
76          ActionContext.getContext().getValueStack().push(testAction);
77      }
78  
79      protected void tearDown() throws Exception {
80          super.tearDown();
81          ActionContext.setContext(null);
82      }
83  }