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.BufferedWriter;
23  import java.io.FileOutputStream;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.Template;
31  import org.apache.velocity.VelocityContext;
32  import org.apache.velocity.app.Velocity;
33  import org.apache.velocity.runtime.RuntimeSingleton;
34  import org.apache.velocity.runtime.log.NullLogChute;
35  
36  /**
37   * Load templates from the Classpath.
38   *
39   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
40   * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
41   * @version $Id: ClasspathResourceTestCase.java 477002 2006-11-20 01:07:43Z henning $
42   */
43  public class ClasspathResourceTestCase extends BaseTestCase
44  {
45       /**
46       * VTL file extension.
47       */
48      private static final String TMPL_FILE_EXT = "vm";
49  
50      /**
51       * Comparison file extension.
52       */
53      private static final String CMP_FILE_EXT = "cmp";
54  
55      /**
56       * Comparison file extension.
57       */
58      private static final String RESULT_FILE_EXT = "res";
59  
60      /**
61       * Results relative to the build directory.
62       */
63      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/cpload";
64  
65      /**
66       * Results relative to the build directory.
67       */
68      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/cpload/compare";
69  
70      /**
71       * Default constructor.
72       */
73      public ClasspathResourceTestCase(String name)
74      {
75          super(name);
76      }
77  
78      public void setUp()
79              throws Exception
80      {
81          assureResultsDirectoryExists(RESULTS_DIR);
82  
83          Velocity.setProperty(Velocity.RESOURCE_LOADER, "classpath");
84  
85          /*
86           * I don't think I should have to do this, these should
87           * be in the default config file.
88           */
89  
90          Velocity.addProperty(
91                  "classpath." + Velocity.RESOURCE_LOADER + ".class",
92                  "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
93  
94          Velocity.setProperty(
95                  "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
96  
97          Velocity.setProperty(
98                  "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
99                  "2");
100 
101         Velocity.setProperty(
102                 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
103 
104         Velocity.init();
105     }
106 
107     public static Test suite ()
108     {
109         return new TestSuite(ClasspathResourceTestCase.class);
110     }
111 
112     /**
113      * Runs the test.
114      */
115     public void testClasspathResource ()
116             throws Exception
117     {
118         /*
119          *  lets ensure the results directory exists
120          */
121         assureResultsDirectoryExists(RESULTS_DIR);
122 
123         Template template1 = RuntimeSingleton.getTemplate("template/test1." + TMPL_FILE_EXT);
124 
125         // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
126         //            Template template2 = RuntimeSingleton.getTemplate(
127         //                getFileName(null, "template/test2", TMPL_FILE_EXT));
128 
129         FileOutputStream fos1 =
130             new FileOutputStream (
131                 getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));
132 
133         // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
134         //            FileOutputStream fos2 =
135         //                new FileOutputStream (
136         //                    getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
137 
138         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
139         // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
140         //            Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
141 
142         /*
143          *  put the Vector into the context, and merge both
144          */
145 
146         VelocityContext context = new VelocityContext();
147 
148         template1.merge(context, writer1);
149         writer1.flush();
150         writer1.close();
151 
152         // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
153         //            template2.merge(context, writer2);
154         //            writer2.flush();
155         //            writer2.close();
156 
157         if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test1",RESULT_FILE_EXT,CMP_FILE_EXT)
158                 // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
159                 //                || !isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT)
160             )
161         {
162             fail("Output is incorrect!");
163         }
164     }
165 }