    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 all the various template mappings for Screen and Layout
    30   * templates of the template service.
    31   *
    32   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
    33   * @version $Id: TemplateTest.java 615328 2008-01-25 20:25:05Z tv $
    34   */
    35
    36  public class TemplateTest
    37      extends BaseTestCase
    38  {
    39      private static TurbineConfig tc = null;
    40      private static TemplateService ts = null;
    41
    42      public TemplateTest(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
    53      public void testTemplateDefaults()
    54      {
    55          assertEquals("Default LayoutTemplate failed", TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayoutTemplate());
    56      }
    57
    58      public void testVelocityDefaults()
    59      {
    60          assertEquals("Default LayoutTemplate failed", "Default.vm",         ts.getDefaultLayoutTemplateName("foo.vm"));
    61      }
    62
    63      public void testNonExistingTemplate()
    64          throws Exception
    65      {
    66          //
    67          // Try a non existing Template. This should render with the default screen class,
    68          // use the default Layout class and Navigation. It should be rendered with the
    69          // default Layout Template but the Screen Template itself must not exist.
    70          String templateName = "DoesNotExistPage.vm";
    71          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    72          assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
    73      }
    74
    75      public void testNonExistingSublevelTemplate()
    76          throws Exception
    77      {
    78          //
    79          // Try a non existing Template in a sub-path. This should render with the default screen class,
    80          // use the default Layout class and Navigation. It should be rendered with the
    81          // default Layout Template but the Screen Template itself must not exist.
    82          String templateName = "this,template,DoesNotExistPage.vm";
    83          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    84          assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
    85      }
    86
    87      public void testExistingTemplate()
    88          throws Exception
    89      {
    90          //
    91          // Try an existing Template. As we already know, missing classes are found correctly
    92          // so we test only Layout and Screen template. This should return the "Default" Layout
    93          // template to render and the Screen Template for the Page to render
    94          String templateName = "ExistPage.vm";
    95          assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    96          assertEquals("ScreenTemplate translation failed", "ExistPage.vm",       ts.getScreenTemplateName(templateName));
    97      }
    98
    99      public void testExistingSublevelTemplate()
    100         throws Exception
    101     {
    102         //
    103         // Try an existing Template. As we already know, missing classes are found correctly
    104         // so we test only Layout and Screen template. This should return the "Default" Layout
    105         // template to render and the Screen Template for the Page to render. The names returned
    106         // by the template service are "/" separated so that e.g. Velocity can use this.
    107         String templateName = "existing,Page.vm";
    108         assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    109         assertEquals("ScreenTemplate translation failed", "existing/Page.vm",   ts.getScreenTemplateName(templateName));
    110     }
    111
    112     public void testExistingLayoutTemplate()
    113         throws Exception
    114     {
    115         //
    116         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
    117         // method should not return the Default but our Layout page.
    118         //
    119         String templateName = "ExistPageWithLayout.vm";
    120         assertEquals("LayoutTemplate translation failed", "ExistPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
    121         assertEquals("ScreenTemplate translation failed", "ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    122     }
    123
    124     public void testExistingSublevelLayoutTemplate()
    125         throws Exception
    126     {
    127         //
    128         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
    129         // method should not return the Default but our Layout page.
    130         //
    131         String templateName = "existing,ExistSublevelPageWithLayout.vm";
    132         assertEquals("LayoutTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
    133         assertEquals("ScreenTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    134     }
    135
    136     public void testExistingDefaultLayoutTemplate()
    137         throws Exception
    138     {
    139         //
    140         // Try an existing Template in a sublevel. This has an equally named Layout in the root. This
    141         // test must find the Template itself but the "Default" layout
    142         //
    143         String templateName = "existing,ExistPageWithLayout.vm";
    144         assertEquals("LayoutTemplate translation failed", "Default.vm",                      ts.getLayoutTemplateName(templateName));
    145         assertEquals("ScreenTemplate translation failed", "existing/ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    146     }
    147 }
    148