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 the class mapping of the Template Service for screen,
030     * layout and navigation.
031     *
032     * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
033     * @version $Id: ClassTest.java 615328 2008-01-25 20:25:05Z tv $
034     */
035    
036    public class ClassTest
037        extends BaseTestCase
038    {
039        private static TurbineConfig tc = null;
040        private static TemplateService ts = null;
041    
042        public ClassTest(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        public void testTemplateDefaults()
053        {
054            // Test if the Default-Values for the Screen, Layout and Navigation classes
055            assertEquals("Default Page failed",           TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultPage());
056            assertEquals("Default Screen failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultScreen());
057            assertEquals("Default Layout failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayout());
058            assertEquals("Default Navigation failed",     TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultNavigation());
059        }
060    
061        public void testVelocityDefaults()
062        {
063            // Test if all the Velocity based Defaults for Page, Screen, Layout, Navigation
064            assertEquals("Default Page failed",           "VelocityPage",       ts.getDefaultPageName("foo.vm"));
065            assertEquals("Default Screen failed",         "VelocityScreen",     ts.getDefaultScreenName("foo.vm"));
066            assertEquals("Default Layout failed",         "VelocityOnlyLayout", ts.getDefaultLayoutName("foo.vm"));
067            assertEquals("Default Navigation failed",     "VelocityNavigation", ts.getDefaultNavigationName("foo.vm"));
068        }
069    
070        // Here comes the fun
071    
072        public void testNonExistingTemplate()
073            throws Exception
074        {
075            //
076            // Try a non existing Template. This should render with the default screen class,
077            // use the default Layout class and Navigation. It should be rendered with the
078            // default Layout Template but the Screen Template itself must not exist.
079            String templateName = "DoesNotExistPage.vm";
080            assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
081            assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
082            assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
083        }
084    
085        public void testNonExistingSublevelTemplate()
086            throws Exception
087        {
088            //
089            // Try a non existing Template in a sub-path. This should render with the default screen class,
090            // use the default Layout class and Navigation.
091            String templateName = "this,template,DoesNotExistPage.vm";
092            assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
093            assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
094            assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
095        }
096    
097        public void testExistingTemplate()
098            throws Exception
099        {
100            //
101            // Try an existing Template without any backing class. Should also return the default classes
102            String templateName = "ExistPage.vm";
103            assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
104            assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
105            assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
106        }
107    
108        public void testExistingSublevelTemplate()
109            throws Exception
110        {
111            //
112            // Try an existing Sublevel Template without any backing class. Should also return the default classes
113            String templateName = "existing,Page.vm";
114            assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
115            assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
116            assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
117        }
118    
119        // Now we start checking existing classes.
120    
121        public void testExistingClass()
122            throws Exception
123        {
124            //
125            // Now we have a class backed template. It has a separate Class for Screen, Navigation and
126            // Layout. It should find the matching class names in the screens, navigations and layout
127            // packages.
128            String templateName = "ExistPageWithClass.vm";
129            assertEquals("Screen translation failed",         "ExistPageWithClass", ts.getScreenName(templateName));
130            assertEquals("Layout translation failed",         "ExistPageWithClass", ts.getLayoutName(templateName));
131            assertEquals("Navigation translation failed",     "ExistPageWithClass", ts.getNavigationName(templateName));
132        }
133    
134        public void testExistingSublevelClass()
135            throws Exception
136        {
137            //
138            // Now we have a class backed template. It has a separate Class for Screen, Navigation and
139            // Layout. It should find the matching class names in the screens, navigations and layout
140            // packages. For a twist, the classes are in a subpackage, so they should also find the
141            // classes in the sub packages.
142            String templateName = "existing,PageWithClass.vm";
143            assertEquals("Screen translation failed",         "existing.PageWithClass", ts.getScreenName(templateName));
144            assertEquals("Layout translation failed",         "existing.PageWithClass", ts.getLayoutName(templateName));
145            assertEquals("Navigation translation failed",     "existing.PageWithClass", ts.getNavigationName(templateName));
146        }
147    
148        public void testDefaultClass()
149            throws Exception
150        {
151            //
152            // We look for a specific Template but it has no class. It has, however
153            // a Default class in its package. So the Loader should find the default
154            String templateName = "existing,dflt,PageWithClass.vm";
155            assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
156            assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
157            assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
158        }
159    
160        public void testDefaultSublevelClass()
161            throws Exception
162        {
163            //
164            // We look for a specific Template but it has no class. It has, however
165            // a Default class in an upper package. So the Loader should find this.
166            String templateName = "existing,dflt,onelevel,twolevel,threelevel,PageWithClass.vm";
167            assertEquals("Screen translation failed",         "existing.dflt.Default", ts.getScreenName(templateName));
168            assertEquals("Layout translation failed",         "existing.dflt.Default", ts.getLayoutName(templateName));
169            assertEquals("Navigation translation failed",     "existing.dflt.Default", ts.getNavigationName(templateName));
170        }
171    
172        public void testIgnoreExistingClass()
173            throws Exception
174        {
175            //
176            // This is a test, whether matching classes in upper level packages are ignored.
177            // We're looking for classes which don't exist. We have, however, matching names
178            // in an upper package. This should still match the Default classes, and not these.
179            String templateName = "sublevel,ExistPageWithClass.vm";
180            assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
181            assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
182            assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
183        }
184    
185    
186    }