001: /*
002: * $Id: TemplateEngineManagerTest.java 491656 2007-01-01 22:10:12Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views;
022:
023: import java.util.HashSet;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.struts2.components.template.FreemarkerTemplateEngine;
028: import org.apache.struts2.components.template.JspTemplateEngine;
029: import org.apache.struts2.components.template.Template;
030: import org.apache.struts2.components.template.TemplateEngine;
031: import org.apache.struts2.components.template.TemplateEngineManager;
032: import org.apache.struts2.components.template.VelocityTemplateEngine;
033: import org.apache.struts2.dispatcher.mapper.CompositeActionMapper;
034:
035: import com.mockobjects.dynamic.C;
036: import com.mockobjects.dynamic.Mock;
037: import com.opensymphony.xwork2.inject.Container;
038:
039: /**
040: * TemplateEngineManagerTest
041: *
042: */
043: public class TemplateEngineManagerTest extends TestCase {
044:
045: TemplateEngineManager mgr;
046: Mock mockContainer;
047:
048: public void setUp() throws Exception {
049: mgr = new TemplateEngineManager();
050: mockContainer = new Mock(Container.class);
051: mockContainer.matchAndReturn("getInstance", C.args(C
052: .eq(TemplateEngine.class), C.eq("jsp")),
053: new JspTemplateEngine());
054: mockContainer.matchAndReturn("getInstance", C.args(C
055: .eq(TemplateEngine.class), C.eq("vm")),
056: new VelocityTemplateEngine());
057: mockContainer.matchAndReturn("getInstance", C.args(C
058: .eq(TemplateEngine.class), C.eq("ftl")),
059: new FreemarkerTemplateEngine());
060: mockContainer.matchAndReturn("getInstanceNames", C.args(C
061: .eq(TemplateEngine.class)), new HashSet() {
062: {
063: add("jsp");
064: add("vm");
065: add("ftl");
066: }
067: });
068:
069: mgr.setContainer((Container) mockContainer.proxy());
070: mgr.setDefaultTemplateType("jsp");
071: }
072:
073: public void testTemplateTypeFromTemplateNameAndDefaults() {
074:
075: TemplateEngine engine = mgr.getTemplateEngine(new Template(
076: "/template", "simple", "foo"), null);
077: assertTrue(engine instanceof JspTemplateEngine);
078: engine = mgr.getTemplateEngine(new Template("/template",
079: "simple", "foo.vm"), null);
080: assertTrue(engine instanceof VelocityTemplateEngine);
081: }
082:
083: public void testTemplateTypeOverrides() {
084: TemplateEngine engine = mgr.getTemplateEngine(new Template(
085: "/template", "simple", "foo"), "ftl");
086: assertTrue(engine instanceof FreemarkerTemplateEngine);
087: engine = mgr.getTemplateEngine(new Template("/template",
088: "simple", "foo.vm"), "ftl");
089: assertTrue(engine instanceof VelocityTemplateEngine);
090: engine = mgr.getTemplateEngine(new Template("/template",
091: "simple", "foo.ftl"), "");
092: assertTrue(engine instanceof FreemarkerTemplateEngine);
093: }
094:
095: public void testTemplateTypeUsesDefaultWhenNotSetInConfiguration() {
096: mgr.setDefaultTemplateType(null);
097: TemplateEngine engine = mgr.getTemplateEngine(new Template(
098: "/template", "simple", "foo"), null);
099: Template template = new Template("/template", "simple", "foo."
100: + TemplateEngineManager.DEFAULT_TEMPLATE_TYPE);
101: TemplateEngine defaultTemplateEngine = mgr.getTemplateEngine(
102: template, null);
103: assertTrue(engine.getClass().equals(
104: defaultTemplateEngine.getClass()));
105: }
106:
107: protected void tearDown() throws Exception {
108: super.tearDown();
109: }
110: }
|