001 package 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 024 import org.apache.turbine.services.TurbineServices; 025 import org.apache.turbine.test.BaseTestCase; 026 import org.apache.turbine.util.TurbineConfig; 027 028 /** 029 * Tests all the various template mappings for Screen and Layout 030 * templates of the template service. 031 * 032 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a> 033 * @version $Id: TemplateTest.java 615328 2008-01-25 20:25:05Z tv $ 034 */ 035 036 public class TemplateTest 037 extends BaseTestCase 038 { 039 private static TurbineConfig tc = null; 040 private static TemplateService ts = null; 041 042 public TemplateTest(String name) 043 throws Exception 044 { 045 super(name); 046 tc = new TurbineConfig(".", "/conf/test/TemplateService.properties"); 047 tc.initialize(); 048 049 ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME); 050 } 051 052 053 public void testTemplateDefaults() 054 { 055 assertEquals("Default LayoutTemplate failed", TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayoutTemplate()); 056 } 057 058 public void testVelocityDefaults() 059 { 060 assertEquals("Default LayoutTemplate failed", "Default.vm", ts.getDefaultLayoutTemplateName("foo.vm")); 061 } 062 063 public void testNonExistingTemplate() 064 throws Exception 065 { 066 // 067 // Try a non existing Template. This should render with the default screen class, 068 // use the default Layout class and Navigation. It should be rendered with the 069 // default Layout Template but the Screen Template itself must not exist. 070 String templateName = "DoesNotExistPage.vm"; 071 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 072 assertEquals("ScreenTemplate translation failed", null, ts.getScreenTemplateName(templateName)); 073 } 074 075 public void testNonExistingSublevelTemplate() 076 throws Exception 077 { 078 // 079 // Try a non existing Template in a sub-path. This should render with the default screen class, 080 // use the default Layout class and Navigation. It should be rendered with the 081 // default Layout Template but the Screen Template itself must not exist. 082 String templateName = "this,template,DoesNotExistPage.vm"; 083 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 084 assertEquals("ScreenTemplate translation failed", null, ts.getScreenTemplateName(templateName)); 085 } 086 087 public void testExistingTemplate() 088 throws Exception 089 { 090 // 091 // Try an existing Template. As we already know, missing classes are found correctly 092 // so we test only Layout and Screen template. This should return the "Default" Layout 093 // template to render and the Screen Template for the Page to render 094 String templateName = "ExistPage.vm"; 095 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 096 assertEquals("ScreenTemplate translation failed", "ExistPage.vm", ts.getScreenTemplateName(templateName)); 097 } 098 099 public void testExistingSublevelTemplate() 100 throws Exception 101 { 102 // 103 // Try an existing Template. As we already know, missing classes are found correctly 104 // so we test only Layout and Screen template. This should return the "Default" Layout 105 // template to render and the Screen Template for the Page to render. The names returned 106 // by the template service are "/" separated so that e.g. Velocity can use this. 107 String templateName = "existing,Page.vm"; 108 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 109 assertEquals("ScreenTemplate translation failed", "existing/Page.vm", ts.getScreenTemplateName(templateName)); 110 } 111 112 public void testExistingLayoutTemplate() 113 throws Exception 114 { 115 // 116 // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName 117 // method should not return the Default but our Layout page. 118 // 119 String templateName = "ExistPageWithLayout.vm"; 120 assertEquals("LayoutTemplate translation failed", "ExistPageWithLayout.vm", ts.getLayoutTemplateName(templateName)); 121 assertEquals("ScreenTemplate translation failed", "ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 122 } 123 124 public void testExistingSublevelLayoutTemplate() 125 throws Exception 126 { 127 // 128 // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName 129 // method should not return the Default but our Layout page. 130 // 131 String templateName = "existing,ExistSublevelPageWithLayout.vm"; 132 assertEquals("LayoutTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getLayoutTemplateName(templateName)); 133 assertEquals("ScreenTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 134 } 135 136 public void testExistingDefaultLayoutTemplate() 137 throws Exception 138 { 139 // 140 // Try an existing Template in a sublevel. This has an equally named Layout in the root. This 141 // test must find the Template itself but the "Default" layout 142 // 143 String templateName = "existing,ExistPageWithLayout.vm"; 144 assertEquals("LayoutTemplate translation failed", "Default.vm", ts.getLayoutTemplateName(templateName)); 145 assertEquals("ScreenTemplate translation failed", "existing/ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName)); 146 } 147 } 148