    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 org.apache.turbine.services.TurbineServices;
    25  import org.apache.turbine.test.BaseTestCase;
    26  import org.apache.turbine.util.TurbineConfig;
    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 615328 2008-01-25 20:25:05Z tv $
    34   */
    35
    36  public class ClassTest
    37      extends BaseTestCase
    38  {
    39      private static TurbineConfig tc = null;
    40      private static TemplateService ts = null;
    41
    42      public ClassTest(String name)
    43              throws Exception
    44      {
    45          super(name);
    46          tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
    47          tc.initialize();
    48
    49          ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
    50      }
    51
    52      public void testTemplateDefaults()
    53      {
    54          // Test if the Default-Values for the Screen, Layout and Navigation classes
    55          assertEquals("Default Page failed",           TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultPage());
    56          assertEquals("Default Screen failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultScreen());
    57          assertEquals("Default Layout failed",         TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayout());
    58          assertEquals("Default Navigation failed",     TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultNavigation());
    59      }
    60
    61      public void testVelocityDefaults()
    62      {
    63          // Test if all the Velocity based Defaults for Page, Screen, Layout, Navigation
    64          assertEquals("Default Page failed",           "VelocityPage",       ts.getDefaultPageName("foo.vm"));
    65          assertEquals("Default Screen failed",         "VelocityScreen",     ts.getDefaultScreenName("foo.vm"));
    66          assertEquals("Default Layout failed",         "VelocityOnlyLayout", ts.getDefaultLayoutName("foo.vm"));
    67          assertEquals("Default Navigation failed",     "VelocityNavigation", ts.getDefaultNavigationName("foo.vm"));
    68      }
    69
    70      // Here comes the fun
    71
    72      public void testNonExistingTemplate()
    73          throws Exception
    74      {
    75          //
    76          // Try a non existing Template. This should render with the default screen class,
    77          // use the default Layout class and Navigation. It should be rendered with the
    78          // default Layout Template but the Screen Template itself must not exist.
    79          String templateName = "DoesNotExistPage.vm";
    80          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
    81          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
    82          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
    83      }
    84
    85      public void testNonExistingSublevelTemplate()
    86          throws Exception
    87      {
    88          //
    89          // Try a non existing Template in a sub-path. This should render with the default screen class,
    90          // use the default Layout class and Navigation.
    91          String templateName = "this,template,DoesNotExistPage.vm";
    92          assertEquals("Screen translation failed",         "VelocityScreen",     ts.getScreenName(templateName));
    93          assertEquals("Layout translation failed",         "VelocityOnlyLayout", ts.getLayoutName(templateName));
    94          assertEquals("Navigation translation failed",     "VelocityNavigation", ts.getNavigationName(templateName));
    95      }
    96
    97      public void testExistingTemplate()
    98          throws Exception
    99      {
    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 }