1   package org.apache.turbine.services.template;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.Test;
20  import junit.framework.TestSuite;
21  
22  import org.apache.turbine.services.TurbineServices;
23  import org.apache.turbine.test.BaseTurbineTest;
24  
25  /***
26   * Tests the class mapping of the Template Service for screen,
27   * layout and navigation.
28   *
29   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
30   * @version $Id: ClassTest.java 264148 2005-08-29 14:21:04Z henning $
31   */
32  
33  public class ClassTest
34      extends BaseTurbineTest
35  {
36  	private TemplateService ts = null;
37  
38      public ClassTest(String name)
39              throws Exception
40      {
41          super(name, "/conf/test/TemplateService.properties");
42          ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
43      }
44  
45      public static Test suite()
46      {
47          return new TestSuite(ClassTest.class);
48      }
49  
50      public void testTemplateDefaults()
51      {
52          // Test if the Default-Values for the Screen, Layout and Navigation classes
53          assertEquals("Default Page failed",           TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultPage());
54          assertEquals("Default Screen failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultScreen());
55          assertEquals("Default Layout failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayout());
56          assertEquals("Default Navigation failed",     TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultNavigation());
57      }
58  
59      public void testVelocityDefaults()
60      {
61          // Test if all the Velocity based Defaults for Page, Screen, Layout, Navigation
62          assertEquals("Default Page failed",           "VelocityPage",       ts.getDefaultPageName("foo.vm"));
63          assertEquals("Default Screen failed",         "VelocityScreen",     ts.getDefaultScreenName("foo.vm"));
64          assertEquals("Default Layout failed",         "VelocityOnlyLayout", ts.getDefaultLayoutName("foo.vm"));
65          assertEquals("Default Navigation failed",     "VelocityNavigation", ts.getDefaultNavigationName("foo.vm"));
66      }
67  
68      // Here comes the fun
69  
70      public void testNonExistingTemplate()
71          throws Exception
72      {
73          //
74          // Try a non existing Template. This should render with the default screen class,
75          // use the default Layout class and Navigation. It should be rendered with the
76          // default Layout Template but the Screen Template itself must not exist.
77          String templateName = "DoesNotExistPage.vm";
78          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
79          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
80          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
81      }
82  
83      public void testNonExistingSublevelTemplate()
84          throws Exception
85      {
86          //
87          // Try a non existing Template in a sub-path. This should render with the default screen class,
88          // use the default Layout class and Navigation.
89          String templateName = "this,template,DoesNotExistPage.vm";
90          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
91          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
92          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
93      }
94  
95      public void testExistingTemplate()
96          throws Exception
97      {
98          //
99          // Try an existing Template without any backing class. Should also return the default classes
100         String templateName = "ExistPage.vm";
101         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
102         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
103         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
104     }
105 
106     public void testExistingSublevelTemplate()
107         throws Exception
108     {
109         //
110         // Try an existing Sublevel Template without any backing class. Should also return the default classes
111         String templateName = "existing,Page.vm";
112         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
113         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
114         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
115     }
116 
117     // Now we start checking existing classes.
118 
119     public void testExistingClass()
120         throws Exception
121     {
122         //
123         // Now we have a class backed template. It has a separate Class for Screen, Navigation and
124         // Layout. It should find the matching class names in the screens, navigations and layout
125         // packages.
126         String templateName = "ExistPageWithClass.vm";
127         assertEquals("Screen translation failed",         "ExistPageWithClass", ts.getScreenName(templateName));
128         assertEquals("Layout translation failed",         "ExistPageWithClass", ts.getLayoutName(templateName));
129         assertEquals("Navigation translation failed",     "ExistPageWithClass", ts.getNavigationName(templateName));
130     }
131 
132     public void testExistingSublevelClass()
133         throws Exception
134     {
135         //
136         // Now we have a class backed template. It has a separate Class for Screen, Navigation and
137         // Layout. It should find the matching class names in the screens, navigations and layout
138         // packages. For a twist, the classes are in a subpackage, so they should also find the
139         // classes in the sub packages.
140         String templateName = "existing,PageWithClass.vm";
141         assertEquals("Screen translation failed",         "existing.PageWithClass", ts.getScreenName(templateName));
142         assertEquals("Layout translation failed",         "existing.PageWithClass", ts.getLayoutName(templateName));
143         assertEquals("Navigation translation failed",     "existing.PageWithClass", ts.getNavigationName(templateName));
144     }
145 
146     public void testDefaultClass()
147         throws Exception
148     {
149         //
150         // We look for a specific Template but it has no class. It has, however
151         // a Default class in its package. So the Loader should find the default
152         String templateName = "existing,dflt,PageWithClass.vm";
153         assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
154         assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
155         assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
156     }
157 
158     public void testDefaultSublevelClass()
159         throws Exception
160     {
161         //
162         // We look for a specific Template but it has no class. It has, however
163         // a Default class in an upper package. So the Loader should find this.
164         String templateName = "existing,dflt,onelevel,twolevel,threelevel,PageWithClass.vm";
165         assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
166         assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
167         assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
168     }
169 
170     public void testIgnoreExistingClass()
171         throws Exception
172     {
173         //
174         // This is a test, whether matching classes in upper level packages are ignored.
175         // We're looking for classes which don't exist. We have, however, matching names
176         // in an upper package. This should still match the Default classes, and not these.
177         String templateName = "sublevel,ExistPageWithClass.vm";
178         assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
179         assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
180         assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
181     }
182 
183 
184 }