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