View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one
    3    * or more contributor license agreements.  See the NOTICE file
    4    * distributed with this work for additional information
    5    * regarding copyright ownership.  The ASF licenses this file
    6    * to you under the Apache License, Version 2.0 (the
    7    * "License"); you may not use this file except in compliance
    8    * with the License.  You may obtain a copy of the License at
    9    *
    10   *   http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied.  See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19
    20  package org.apache.turbine.modules;
    21
    22  import static org.junit.Assert.assertEquals;
    23  import static org.junit.Assert.fail;
    24  import static org.mockito.Mockito.mock;
    25
    26  import javax.servlet.ServletConfig;
    27  import javax.servlet.http.HttpServletRequest;
    28  import javax.servlet.http.HttpServletResponse;
    29
    30  import org.apache.turbine.modules.layouts.TestVelocityOnlyLayout;
    31  import org.apache.turbine.pipeline.PipelineData;
    32  import org.apache.turbine.test.BaseTestCase;
    33  import org.apache.turbine.util.RunData;
    34  import org.apache.turbine.util.TurbineConfig;
    35  import org.junit.AfterClass;
    36  import org.junit.Before;
    37  import org.junit.BeforeClass;
    38  import org.junit.Test;
    39
    40  /**
    41   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    42   */
    43  public class LayoutLoaderTest extends BaseTestCase
    44  {
    45      private static TurbineConfig tc = null;
    46      private ServletConfig config = null;
    47      private HttpServletRequest request = null;
    48      private HttpServletResponse response = null;
    49
    50      @BeforeClass
    51      public static void init()
    52      {
    53          tc = new TurbineConfig(
    54                  ".",
    55                  "/conf/test/CompleteTurbineResources.properties");
    56          tc.initialize();
    57      }
    58
    59      @Before
    60      public void setUpBefore() throws Exception
    61      {
    62          config = mock(ServletConfig.class);
    63          request = getMockRequest();
    64          response = mock(HttpServletResponse.class);
    65      }
    66
    67      /*
    68       * @see TestCase#tearDown()
    69       */
    70      @AfterClass
    71      public static void tearDown() throws Exception
    72      {
    73          if (tc != null)
    74          {
    75              tc.dispose();
    76          }
    77      }
    78
    79      @Test
    80      public void testPipelineDataContainsRunData()
    81      {
    82          try
    83          {
    84              RunData data = getRunData(request, response, config);
    85              PipelineData pipelineData = data;
    86              data.setLayout("TestVelocityOnlyLayout");
    87              int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
    88              try
    89              {
    90                  LayoutLoader.getInstance().exec(pipelineData, data.getLayout());
    91              }
    92              catch (Exception e)
    93              {
    94                  e.printStackTrace();
    95                  fail("Should not have thrown an exception.");
    96              }
    97              assertEquals(numberOfCalls + 1, TestVelocityOnlyLayout.numberOfCalls);
    98          }
    99          catch (Exception e)
    100         {
    101             e.printStackTrace();
    102             fail("Should not have thrown an exception.");
    103         }
    104     }
    105
    106     @Test
    107     public void testDoBuildWithRunData()
    108     {
    109         try
    110         {
    111             RunData data = getRunData(request, response, config);
    112             data.setLayout("TestVelocityOnlyLayout");
    113             int numberOfCalls = TestVelocityOnlyLayout.numberOfCalls;
    114             try
    115             {
    116                 LayoutLoader.getInstance().exec(data, data.getLayout());
    117             }
    118             catch (Exception e)
    119             {
    120                 e.printStackTrace();
    121                 fail("Should not have thrown an exception.");
    122             }
    123             assertEquals(numberOfCalls + 1, TestVelocityOnlyLayout.numberOfCalls);
    124         }
    125         catch (Exception e)
    126         {
    127             e.printStackTrace();
    128             fail("Should not have thrown an exception.");
    129         }
    130     }
    131 }