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 all the various template mappings for Screen and Layout
    35   * templates of the template service.
    36   *
    37   * @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
    38   * @version $Id: TemplateTest.java 1606111 2014-06-27 14:46:47Z gk $
    39   */
    40
    41  public class TemplateTest
    42      extends BaseTestCase
    43  {
    44      private static TurbineConfig tc = null;
    45      private static TemplateService ts = null;
    46
    47
    48
    49      @BeforeClass
    50      public static void setUp() throws Exception
    51      {
    52          tc = new TurbineConfig(".", "/conf/test/TemplateService.properties");
    53          tc.initialize();
    54
    55          ts = (TemplateService) TurbineServices.getInstance().getService(TemplateService.SERVICE_NAME);
    56      }
    57
    58      @AfterClass
    59      public static void tearDown() throws Exception
    60      {
    61          if (tc != null)
    62          {
    63              tc.dispose();
    64          }
    65      }
    66
    67
    68      @Test public void testTemplateDefaults()
    69      {
    70          assertEquals("Default LayoutTemplate failed", TemplateService.DEFAULT_TEMPLATE_VALUE, ts.getDefaultLayoutTemplate());
    71      }
    72
    73      @Test public void testVelocityDefaults()
    74      {
    75          assertEquals("Default LayoutTemplate failed", "Default.vm",         ts.getDefaultLayoutTemplateName("foo.vm"));
    76      }
    77
    78      @Test public void testTemplateExtension()
    79      {
    80          assertEquals("Extension extraction failed", "vm", ts.getExtension("Default.vm"));
    81          assertEquals("Extension extraction failed", "txt", ts.getExtension("Default.txt"));
    82          // TRB-82
    83          assertEquals("Extension extraction failed", "vm", ts.getExtension("Default.txt.vm"));
    84      }
    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("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    95          assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
    96      }
    97
    98      @Test public void testNonExistingSublevelTemplate()
    99          throws Exception
    100     {
    101         //
    102         // Try a non existing Template in a sub-path. This should render with the default screen class,
    103         // use the default Layout class and Navigation. It should be rendered with the
    104         // default Layout Template but the Screen Template itself must not exist.
    105         String templateName = "this,template,DoesNotExistPage.vm";
    106         assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    107         assertEquals("ScreenTemplate translation failed", null,                 ts.getScreenTemplateName(templateName));
    108     }
    109
    110     @Test public void testExistingTemplate()
    111         throws Exception
    112     {
    113         //
    114         // Try an existing Template. As we already know, missing classes are found correctly
    115         // so we test only Layout and Screen template. This should return the "Default" Layout
    116         // template to render and the Screen Template for the Page to render
    117         String templateName = "ExistPage.vm";
    118         assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    119         assertEquals("ScreenTemplate translation failed", "ExistPage.vm",       ts.getScreenTemplateName(templateName));
    120     }
    121
    122     public void testExistingSublevelTemplate()
    123         throws Exception
    124     {
    125         //
    126         // Try an existing Template. As we already know, missing classes are found correctly
    127         // so we test only Layout and Screen template. This should return the "Default" Layout
    128         // template to render and the Screen Template for the Page to render. The names returned
    129         // by the template service are "/" separated so that e.g. Velocity can use this.
    130         String templateName = "existing,Page.vm";
    131         assertEquals("LayoutTemplate translation failed", "Default.vm",         ts.getLayoutTemplateName(templateName));
    132         assertEquals("ScreenTemplate translation failed", "existing/Page.vm",   ts.getScreenTemplateName(templateName));
    133     }
    134
    135     @Test public void testExistingLayoutTemplate()
    136         throws Exception
    137     {
    138         //
    139         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
    140         // method should not return the Default but our Layout page.
    141         //
    142         String templateName = "ExistPageWithLayout.vm";
    143         assertEquals("LayoutTemplate translation failed", "ExistPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
    144         assertEquals("ScreenTemplate translation failed", "ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    145     }
    146
    147     @Test public void testExistingSublevelLayoutTemplate()
    148         throws Exception
    149     {
    150         //
    151         // Try an existing Template. This time we have a backing Layout page. So the getLayoutTemplateName
    152         // method should not return the Default but our Layout page.
    153         //
    154         String templateName = "existing,ExistSublevelPageWithLayout.vm";
    155         assertEquals("LayoutTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getLayoutTemplateName(templateName));
    156         assertEquals("ScreenTemplate translation failed", "existing/ExistSublevelPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    157     }
    158
    159     public void testExistingDefaultLayoutTemplate()
    160         throws Exception
    161     {
    162         //
    163         // Try an existing Template in a sublevel. This has an equally named Layout in the root. This
    164         // test must find the Template itself but the "Default" layout
    165         //
    166         String templateName = "existing,ExistPageWithLayout.vm";
    167         assertEquals("LayoutTemplate translation failed", "Default.vm",                      ts.getLayoutTemplateName(templateName));
    168         assertEquals("ScreenTemplate translation failed", "existing/ExistPageWithLayout.vm", ts.getScreenTemplateName(templateName));
    169     }
    170 }
    171