1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
20   */
21  
22  import java.io.FileInputStream;
23  import java.util.Properties;
24  
25  import junit.framework.TestSuite;
26  
27  import org.apache.velocity.app.Velocity;
28  import org.apache.velocity.test.misc.TestLogChute;
29  
30  /**
31   * Test suite for Templates.
32   *
33   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
34   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
35   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
36   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
37   * @version $Id: TemplateTestSuite.java 704299 2008-10-14 03:13:16Z nbubna $
38   */
39  public class TemplateTestSuite extends TestSuite implements TemplateTestBase
40  {
41      private Properties testProperties;
42  
43      /**
44       * Creates an instace of the Apache Velocity test suite.
45       */
46      public TemplateTestSuite()
47      {
48          try
49          {
50              Velocity.setProperty(
51                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
52  
53              Velocity.setProperty(
54                      Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
55  
56              Velocity.init();
57  
58              testProperties = new Properties();
59              testProperties.load(new FileInputStream(TEST_CASE_PROPERTIES));
60          }
61          catch (Exception e)
62          {
63              System.err.println("Cannot setup TemplateTestSuite!");
64              e.printStackTrace();
65              System.exit(1);
66          }
67  
68          addTemplateTestCases();
69      }
70  
71      /**
72       * Adds the template test cases to run to this test suite.  Template test
73       * cases are listed in the <code>TEST_CASE_PROPERTIES</code> file.
74       */
75      private void addTemplateTestCases()
76      {
77          String template;
78          for (int i = 1 ;; i++)
79          {
80              template = testProperties.getProperty(getTemplateTestKey(i));
81  
82              if (template != null)
83              {
84                  System.out.println("Adding TemplateTestCase : " + template);
85                  addTest(new TemplateTestCase(template));
86              }
87              else
88              {
89                  // Assume we're done adding template test cases.
90                  break;
91              }
92          }
93      }
94  
95      /**
96       * Macro which returns the properties file key for the specified template
97       * test number.
98       *
99       * @param nbr The template test number to return a property key for.
100      * @return    The property key.
101      */
102     private static final String getTemplateTestKey(int nbr)
103     {
104         return ("test.template." + nbr);
105     }
106 }