View Javadoc

1   /*
2    * $Id: TemplateEngineManagerTest.java 439747 2006-09-03 09:22:46Z mrdon $
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;
19  
20  import junit.framework.TestCase;
21  
22  import org.apache.struts2.components.template.FreemarkerTemplateEngine;
23  import org.apache.struts2.components.template.JspTemplateEngine;
24  import org.apache.struts2.components.template.Template;
25  import org.apache.struts2.components.template.TemplateEngine;
26  import org.apache.struts2.components.template.TemplateEngineManager;
27  import org.apache.struts2.components.template.VelocityTemplateEngine;
28  import org.apache.struts2.config.Settings;
29  
30  /***
31   * TemplateEngineManagerTest
32   *
33   */
34  public class TemplateEngineManagerTest extends TestCase {
35      public void testTemplateTypeFromTemplateNameAndDefaults() {
36          Settings.setInstance(new Settings() {
37              public boolean isSetImpl(String name) {
38                  return name.equals(TemplateEngineManager.DEFAULT_TEMPLATE_TYPE_CONFIG_KEY);
39              }
40  
41              public String getImpl(String aName) throws IllegalArgumentException {
42                  if (aName.equals(TemplateEngineManager.DEFAULT_TEMPLATE_TYPE_CONFIG_KEY)) {
43                      return "jsp";
44                  }
45                  return null;
46              }
47          });
48          TemplateEngine engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo"), null);
49          assertTrue(engine instanceof JspTemplateEngine);
50          engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo.vm"), null);
51          assertTrue(engine instanceof VelocityTemplateEngine);
52      }
53  
54      public void testTemplateTypeOverrides() {
55          TemplateEngine engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo"), "ftl");
56          assertTrue(engine instanceof FreemarkerTemplateEngine);
57          engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo.vm"), "ftl");
58          assertTrue(engine instanceof VelocityTemplateEngine);
59          engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo.ftl"), "");
60          assertTrue(engine instanceof FreemarkerTemplateEngine);
61      }
62  
63      public void testTemplateTypeUsesDefaultWhenNotSetInConfiguration() {
64          TemplateEngine engine = TemplateEngineManager.getTemplateEngine(new Template("/template", "simple", "foo"), null);
65          Template template = new Template("/template", "simple", "foo." + TemplateEngineManager.DEFAULT_TEMPLATE_TYPE);
66          TemplateEngine defaultTemplateEngine = TemplateEngineManager.getTemplateEngine(template, null);
67          assertTrue(engine.getClass().equals(defaultTemplateEngine.getClass()));
68      }
69  
70      protected void tearDown() throws Exception {
71          super.tearDown();
72          Settings.setInstance(null);
73      }
74  }