View Javadoc

1   /*
2    * $Id: FreemarkerTest.java 471756 2006-11-06 15:01:43Z husted $
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  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  }