    001package 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
    024import static org.junit.Assert.assertEquals;
    025
    026import org.apache.turbine.services.TurbineServices;
    027import org.apache.turbine.test.BaseTestCase;
    028import org.apache.turbine.util.TurbineConfig;
    029import org.junit.AfterClass;
    030import org.junit.BeforeClass;
    031import org.junit.Test;
    032
    033/**
    034 * Tests the class mapping of the Template Service for screen,
    035 * layout and navigation.
    036 *
    037 * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
    038 * @version $Id: ClassTest.java 1606111 2014-06-27 14:46:47Z gk $
    039 */
    040
    041public class ClassTest
    042    extends BaseTestCase
    043{
    044    private static TurbineConfig tc = null;
    045    private static TemplateService ts = null;
    046
    047
    048    @BeforeClass
    049    public static void setUp() throws Exception
    050    {
    051        tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
    052        tc.initialize();
    053
    054        ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
    055    }
    056
    057    @AfterClass
    058    public static void tearDown() throws Exception
    059    {
    060        if (tc != null)
    061        {
    062            tc.dispose();
    063        }
    064    }
    065
    066    @Test public void testTemplateDefaults()
    067    {
    068        // Test if the Default-Values for the Screen, Layout and Navigation classes
    069        assertEquals("Default Page failed",           TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultPage());
    070        assertEquals("Default Screen failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultScreen());
    071        assertEquals("Default Layout failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayout());
    072        assertEquals("Default Navigation failed",     TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultNavigation());
    073    }
    074
    075    @Test public void testVelocityDefaults()
    076    {
    077        // Test if all the Velocity based Defaults for Page, Screen, Layout, Navigation
    078        assertEquals("Default Page failed",           "VelocityPage",       ts.getDefaultPageName("foo.vm"));
    079        assertEquals("Default Screen failed",         "VelocityScreen",     ts.getDefaultScreenName("foo.vm"));
    080        assertEquals("Default Layout failed",         "VelocityOnlyLayout", ts.getDefaultLayoutName("foo.vm"));
    081        assertEquals("Default Navigation failed",     "VelocityNavigation", ts.getDefaultNavigationName("foo.vm"));
    082    }
    083
    084    // Here comes the fun
    085
    086    @Test public void testNonExistingTemplate()
    087        throws Exception
    088    {
    089        //
    090        // Try a non existing Template. This should render with the default screen class,
    091        // use the default Layout class and Navigation. It should be rendered with the
    092        // default Layout Template but the Screen Template itself must not exist.
    093        String templateName = "DoesNotExistPage.vm";
    094        assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
    095        assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
    096        assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
    097    }
    098
    099    @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}