View Javadoc

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