View Javadoc

1   /*
2    * $Id: TemplateEngineManagerTest.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;
23  
24  import java.util.HashSet;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.struts2.components.template.FreemarkerTemplateEngine;
29  import org.apache.struts2.components.template.JspTemplateEngine;
30  import org.apache.struts2.components.template.Template;
31  import org.apache.struts2.components.template.TemplateEngine;
32  import org.apache.struts2.components.template.TemplateEngineManager;
33  import org.apache.struts2.components.template.VelocityTemplateEngine;
34  import org.apache.struts2.dispatcher.mapper.CompositeActionMapper;
35  
36  import com.mockobjects.dynamic.C;
37  import com.mockobjects.dynamic.Mock;
38  import com.opensymphony.xwork2.inject.Container;
39  
40  /***
41   * TemplateEngineManagerTest
42   *
43   */
44  public class TemplateEngineManagerTest extends TestCase {
45      
46      TemplateEngineManager mgr;
47      Mock mockContainer;
48      
49      public void setUp() throws Exception {
50          mgr = new TemplateEngineManager();
51          mockContainer = new Mock(Container.class);
52          mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("jsp")), new JspTemplateEngine());
53          mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("vm")), new VelocityTemplateEngine());
54          mockContainer.matchAndReturn("getInstance", C.args(C.eq(TemplateEngine.class), C.eq("ftl")), new FreemarkerTemplateEngine());
55          mockContainer.matchAndReturn("getInstanceNames", C.args(C.eq(TemplateEngine.class)), new HashSet() {{
56              add("jsp");
57              add("vm");
58              add("ftl");
59          }});
60          
61          mgr.setContainer((Container)mockContainer.proxy());
62          mgr.setDefaultTemplateType("jsp");
63      }
64      
65      public void testTemplateTypeFromTemplateNameAndDefaults() {
66          
67          TemplateEngine engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo"), null);
68          assertTrue(engine instanceof JspTemplateEngine);
69          engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo.vm"), null);
70          assertTrue(engine instanceof VelocityTemplateEngine);
71      }
72  
73      public void testTemplateTypeOverrides() {
74          TemplateEngine engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo"), "ftl");
75          assertTrue(engine instanceof FreemarkerTemplateEngine);
76          engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo.vm"), "ftl");
77          assertTrue(engine instanceof VelocityTemplateEngine);
78          engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo.ftl"), "");
79          assertTrue(engine instanceof FreemarkerTemplateEngine);
80      }
81  
82      public void testTemplateTypeUsesDefaultWhenNotSetInConfiguration() {
83          mgr.setDefaultTemplateType(null);
84          TemplateEngine engine = mgr.getTemplateEngine(new Template("/template", "simple", "foo"), null);
85          Template template = new Template("/template", "simple", "foo." + TemplateEngineManager.DEFAULT_TEMPLATE_TYPE);
86          TemplateEngine defaultTemplateEngine = mgr.getTemplateEngine(template, null);
87          assertTrue(engine.getClass().equals(defaultTemplateEngine.getClass()));
88      }
89  
90      protected void tearDown() throws Exception {
91          super.tearDown();
92      }
93  }