1   package org.apache.turbine.services.template;
2   
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  
22  import junit.framework.Test;
23  import junit.framework.TestSuite;
24  
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.test.BaseTurbineTest;
27  
28  /***
29   * Tests all the various template mappings for Screen and Layout
30   * templates of the template service.
31   *
32   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
33   * @version $Id: TemplateTest.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  
36  public class TemplateTest
37      extends BaseTurbineTest
38  {
39      private static TemplateService ts = null;
40  
41      public TemplateTest(String name)
42              throws Exception
43      {
44          super(name, "/conf/test/TemplateService.properties");
45  
46          ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
47      }
48  
49      public static Test suite()
50      {
51          return new TestSuite(TemplateTest.class);
52      }
53  
54      public void testTemplateDefaults()
55      {
56          assertEquals("Default LayoutTemplate failed", TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayoutTemplate());
57      }
58  
59      public void testVelocityDefaults()
60      {
61          assertEquals("Default LayoutTemplate failed", "Default.vm",         ts.getDefaultLayoutTemplateName("foo.vm"));
62      }
63  
64      public void testNonExistingTemplate()
65          throws Exception
66      {
67          //
68          // Try a non existing Template. This should render with the default screen class,
69          // use the default Layout class and Navigation. It should be rendered with the
70          // default Layout Template but the Screen Template itself must not exist.
71          String templateName = "DoesNotExistPage.vm";
72          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
73          assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
74      }
75  
76      public void testNonExistingSublevelTemplate()
77          throws Exception
78      {
79          //
80          // Try a non existing Template in a sub-path. This should render with the default screen class,
81          // use the default Layout class and Navigation. It should be rendered with the
82          // default Layout Template but the Screen Template itself must not exist.
83          String templateName = "this,template,DoesNotExistPage.vm";
84          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
85          assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
86      }
87  
88      public void testExistingTemplate()
89          throws Exception
90      {
91          //
92          // Try an existing Template. As we already know, missing classes are found correctly
93          // so we test only Layout and Screen template. This should return the "Default" Layout
94          // template to render and the Screen Template for the Page to render
95          String templateName = "ExistPage.vm";
96          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
97          assertEquals("ScreenTemplate translation failed", "ExistPage.vm",       ts.getScreenTemplateName(templateName));
98      }
99  
100     public void testExistingSublevelTemplate()
101         throws Exception
102     {
103         //
104         // Try an existing Template. As we already know, missing classes are found correctly
105         // so we test only Layout and Screen template. This should return the "Default" Layout
106         // template to render and the Screen Template for the Page to render. The names returned
107         // by the template service are "/" separated so that e.g. Velocity can use this.
108         String templateName = "existing,Page.vm";
109         assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
110         assertEquals("ScreenTemplate translation failed", "existing/Page.vm",   ts.getScreenTemplateName(templateName));
111     }
112 
113     public void testExistingLayoutTemplate()
114         throws Exception
115     {
116         //
117         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
118         // method should not return the Default but our Layout page.
119         //
120         String templateName = "ExistPageWithLayout.vm";
121         assertEquals("LayoutTemplate translation failed", "ExistPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
122         assertEquals("ScreenTemplate translation failed", "ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
123     }
124 
125     public void testExistingSublevelLayoutTemplate()
126         throws Exception
127     {
128         //
129         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
130         // method should not return the Default but our Layout page.
131         //
132         String templateName = "existing,ExistSublevelPageWithLayout.vm";
133         assertEquals("LayoutTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
134         assertEquals("ScreenTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getScreenTemplateName(templateName));
135     }
136 
137     public void testExistingDefaultLayoutTemplate()
138         throws Exception
139     {
140         //
141         // Try an existing Template in a sublevel. This has an equally named Layout in the root. This
142         // test must find the Template itself but the "Default" layout
143         //
144         String templateName = "existing,ExistPageWithLayout.vm";
145         assertEquals("LayoutTemplate translation failed", "Default.vm",                      ts.getLayoutTemplateName(templateName));
146         assertEquals("ScreenTemplate translation failed", "existing/ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
147     }
148 }
149