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 the class mapping of the Template Service for screen,
30   * layout and navigation.
31   *
32   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
33   * @version $Id: ClassTest.java 534527 2007-05-02 16:10:59Z tv $
34   */
35  
36  public class ClassTest
37      extends BaseTurbineTest
38  {
39  	private TemplateService ts = null;
40  
41      public ClassTest(String name)
42              throws Exception
43      {
44          super(name, "/conf/test/TemplateService.properties");
45          ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
46      }
47  
48      public static Test suite()
49      {
50          return new TestSuite(ClassTest.class);
51      }
52  
53      public void testTemplateDefaults()
54      {
55          // Test if the Default-Values for the Screen, Layout and Navigation classes
56          assertEquals("Default Page failed",           TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultPage());
57          assertEquals("Default Screen failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultScreen());
58          assertEquals("Default Layout failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayout());
59          assertEquals("Default Navigation failed",     TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultNavigation());
60      }
61  
62      public void testVelocityDefaults()
63      {
64          // Test if all the Velocity based Defaults for Page, Screen, Layout, Navigation
65          assertEquals("Default Page failed",           "VelocityPage",       ts.getDefaultPageName("foo.vm"));
66          assertEquals("Default Screen failed",         "VelocityScreen",     ts.getDefaultScreenName("foo.vm"));
67          assertEquals("Default Layout failed",         "VelocityOnlyLayout", ts.getDefaultLayoutName("foo.vm"));
68          assertEquals("Default Navigation failed",     "VelocityNavigation", ts.getDefaultNavigationName("foo.vm"));
69      }
70  
71      // Here comes the fun
72  
73      public void testNonExistingTemplate()
74          throws Exception
75      {
76          //
77          // Try a non existing Template. This should render with the default screen class,
78          // use the default Layout class and Navigation. It should be rendered with the
79          // default Layout Template but the Screen Template itself must not exist.
80          String templateName = "DoesNotExistPage.vm";
81          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
82          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
83          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
84      }
85  
86      public void testNonExistingSublevelTemplate()
87          throws Exception
88      {
89          //
90          // Try a non existing Template in a sub-path. This should render with the default screen class,
91          // use the default Layout class and Navigation.
92          String templateName = "this,template,DoesNotExistPage.vm";
93          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
94          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
95          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
96      }
97  
98      public void testExistingTemplate()
99          throws Exception
100     {
101         //
102         // Try an existing Template without any backing class. Should also return the default classes
103         String templateName = "ExistPage.vm";
104         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
105         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
106         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
107     }
108 
109     public void testExistingSublevelTemplate()
110         throws Exception
111     {
112         //
113         // Try an existing Sublevel Template without any backing class. Should also return the default classes
114         String templateName = "existing,Page.vm";
115         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
116         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
117         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
118     }
119 
120     // Now we start checking existing classes.
121 
122     public void testExistingClass()
123         throws Exception
124     {
125         //
126         // Now we have a class backed template. It has a separate Class for Screen, Navigation and
127         // Layout. It should find the matching class names in the screens, navigations and layout
128         // packages.
129         String templateName = "ExistPageWithClass.vm";
130         assertEquals("Screen translation failed",         "ExistPageWithClass", ts.getScreenName(templateName));
131         assertEquals("Layout translation failed",         "ExistPageWithClass", ts.getLayoutName(templateName));
132         assertEquals("Navigation translation failed",     "ExistPageWithClass", ts.getNavigationName(templateName));
133     }
134 
135     public void testExistingSublevelClass()
136         throws Exception
137     {
138         //
139         // Now we have a class backed template. It has a separate Class for Screen, Navigation and
140         // Layout. It should find the matching class names in the screens, navigations and layout
141         // packages. For a twist, the classes are in a subpackage, so they should also find the
142         // classes in the sub packages.
143         String templateName = "existing,PageWithClass.vm";
144         assertEquals("Screen translation failed",         "existing.PageWithClass", ts.getScreenName(templateName));
145         assertEquals("Layout translation failed",         "existing.PageWithClass", ts.getLayoutName(templateName));
146         assertEquals("Navigation translation failed",     "existing.PageWithClass", ts.getNavigationName(templateName));
147     }
148 
149     public void testDefaultClass()
150         throws Exception
151     {
152         //
153         // We look for a specific Template but it has no class. It has, however
154         // a Default class in its package. So the Loader should find the default
155         String templateName = "existing,dflt,PageWithClass.vm";
156         assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
157         assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
158         assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
159     }
160 
161     public void testDefaultSublevelClass()
162         throws Exception
163     {
164         //
165         // We look for a specific Template but it has no class. It has, however
166         // a Default class in an upper package. So the Loader should find this.
167         String templateName = "existing,dflt,onelevel,twolevel,threelevel,PageWithClass.vm";
168         assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
169         assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
170         assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
171     }
172 
173     public void testIgnoreExistingClass()
174         throws Exception
175     {
176         //
177         // This is a test, whether matching classes in upper level packages are ignored.
178         // We're looking for classes which don't exist. We have, however, matching names
179         // in an upper package. This should still match the Default classes, and not these.
180         String templateName = "sublevel,ExistPageWithClass.vm";
181         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
182         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
183         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
184     }
185 
186 
187 }