001package org.apache.turbine.services.template; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022 023 024import static org.junit.Assert.assertEquals; 025 026import org.apache.turbine.services.TurbineServices; 027import org.apache.turbine.test.BaseTestCase; 028import org.apache.turbine.util.TurbineConfig; 029import org.junit.AfterClass; 030import org.junit.BeforeClass; 031import org.junit.Test; 032 033/** 034 * Tests all the various template mappings for Screen and Layout 035 * templates of the template service. 036 * 037 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a> 038 * @version $Id: TemplateTest.java 1606111 2014-06-27 14:46:47Z gk $ 039 */ 040 041public class TemplateTest 042 extends BaseTestCase 043{ 044 private static TurbineConfig tc = null; 045 private static TemplateService ts = null; 046 047 048 049 @BeforeClass 050 public static void setUp() throws Exception 051 { 052 tc = new TurbineConfig(".", "/conf/test/TemplateService.properties"); 053 tc.initialize(); 054 055 ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); 056 } 057 058 @AfterClass 059 public static void tearDown() throws Exception 060 { 061 if (tc != null) 062 { 063 tc.dispose(); 064 } 065 } 066 067 068 @Test public void testTemplateDefaults() 069 { 070 assertEquals("Default LayoutTemplate failed", TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayoutTemplate()); 071 } 072 073 @Test public void testVelocityDefaults() 074 { 075 assertEquals("Default LayoutTemplate failed", "Default.vm", ts.getDefaultLayoutTemplateName("foo.vm")); 076 } 077 078 @Test public void testTemplateExtension() 079 { 080 assertEquals("Extension extraction failed", "vm", ts.getExtension("Default.vm")); 081 assertEquals("Extension extraction failed", "txt", ts.getExtension("Default.txt")); 082 // TRB-82 083 assertEquals("Extension extraction failed", "vm", ts.getExtension("Default.txt.vm")); 084 } 085 086 @Test public void testNonExistingTemplate() 087 throws Exception 088 { 089 // 090 // Try a non existing Template. This should render with the default screen class, 091 // use the default Layout class and Navigation. It should be rendered with the 092 // default Layout Template but the Screen Template itself must not exist. 093 String templateName = "DoesNotExistPage.vm"; 094 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 095 assertEquals("ScreenTemplate translation failed", null, ts.getScreenTemplateName(templateName)); 096 } 097 098 @Test public void testNonExistingSublevelTemplate() 099 throws Exception 100 { 101 // 102 // Try a non existing Template in a sub-path. This should render with the default screen class, 103 // use the default Layout class and Navigation. It should be rendered with the 104 // default Layout Template but the Screen Template itself must not exist. 105 String templateName = "this,template,DoesNotExistPage.vm"; 106 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 107 assertEquals("ScreenTemplate translation failed", null, ts.getScreenTemplateName(templateName)); 108 } 109 110 @Test public void testExistingTemplate() 111 throws Exception 112 { 113 // 114 // Try an existing Template. As we already know, missing classes are found correctly 115 // so we test only Layout and Screen template. This should return the "Default" Layout 116 // template to render and the Screen Template for the Page to render 117 String templateName = "ExistPage.vm"; 118 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 119 assertEquals("ScreenTemplate translation failed", "ExistPage.vm", ts.getScreenTemplateName(templateName)); 120 } 121 122 public void testExistingSublevelTemplate() 123 throws Exception 124 { 125 // 126 // Try an existing Template. As we already know, missing classes are found correctly 127 // so we test only Layout and Screen template. This should return the "Default" Layout 128 // template to render and the Screen Template for the Page to render. The names returned 129 // by the template service are "/" separated so that e.g. Velocity can use this. 130 String templateName = "existing,Page.vm"; 131 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 132 assertEquals("ScreenTemplate translation failed", "existing/Page.vm", ts.getScreenTemplateName(templateName)); 133 } 134 135 @Test public void testExistingLayoutTemplate() 136 throws Exception 137 { 138 // 139 // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName 140 // method should not return the Default but our Layout page. 141 // 142 String templateName = "ExistPageWithLayout.vm"; 143 assertEquals("LayoutTemplate translation failed", "ExistPageWithLayout.vm", ts.getLayoutTemplateName(templateName)); 144 assertEquals("ScreenTemplate translation failed", "ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 145 } 146 147 @Test public void testExistingSublevelLayoutTemplate() 148 throws Exception 149 { 150 // 151 // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName 152 // method should not return the Default but our Layout page. 153 // 154 String templateName = "existing,ExistSublevelPageWithLayout.vm"; 155 assertEquals("LayoutTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getLayoutTemplateName(templateName)); 156 assertEquals("ScreenTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 157 } 158 159 public void testExistingDefaultLayoutTemplate() 160 throws Exception 161 { 162 // 163 // Try an existing Template in a sublevel. This has an equally named Layout in the root. This 164 // test must find the Template itself but the "Default" layout 165 // 166 String templateName = "existing,ExistPageWithLayout.vm"; 167 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 168 assertEquals("ScreenTemplate translation failed", "existing/ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 169 } 170} 171