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  
29  import org.apache.velocity.Template;
30  import org.apache.velocity.VelocityContext;
31  import org.apache.velocity.app.VelocityEngine;
32  import org.apache.velocity.runtime.RuntimeConstants;
33  import org.apache.velocity.test.misc.TestLogChute;
34  
35  /**
36   * Test use of an absolute path with the FileResourceLoader
37   *
38   * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
39   * @version $Id: AbsoluteFileResourceLoaderTestCase.java 898032 2010-01-11 19:51:03Z nbubna $
40   */
41  public class AbsoluteFileResourceLoaderTestCase extends BaseTestCase
42  {
43       /**
44       * VTL file extension.
45       */
46      private static final String TMPL_FILE_EXT = "vm";
47  
48      /**
49       * Comparison file extension.
50       */
51      private static final String CMP_FILE_EXT = "cmp";
52  
53      /**
54       * Comparison file extension.
55       */
56      private static final String RESULT_FILE_EXT = "res";
57  
58      /**
59       * Path to template file.  This will get combined with the
60       * application directory to form an absolute path
61       */
62      private final static String TEMPLATE_PATH = TEST_COMPARE_DIR + "/absolute/absolute";
63  
64      /**
65       * Results relative to the build directory.
66       */
67      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/absolute/results";
68  
69      /**
70       * Results relative to the build directory.
71       */
72      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/absolute/compare";
73  
74      VelocityEngine engine;
75      
76      /**
77       * Default constructor.
78       */
79      AbsoluteFileResourceLoaderTestCase()
80      {
81          super("AbsoluteFileResourceLoaderTest");
82  
83          try
84          {
85              assureResultsDirectoryExists(RESULTS_DIR);
86  
87              engine = new VelocityEngine();
88              
89              // signify we want to use an absolute path
90              engine.addProperty(
91                  RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
92  
93              engine.setProperty(
94                      RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
95  
96              engine.init();
97          }
98          catch (Exception e)
99          {
100             fail("Cannot setup AbsoluteFileResourceLoaderTest!");
101         }
102     }
103 
104     public static Test suite ()
105     {
106         return new AbsoluteFileResourceLoaderTestCase();
107     }
108 
109     /**
110      * Runs the test.
111      */
112     public void runTest ()
113     {
114         try
115         {
116 
117             String curdir = System.getProperty("user.dir");
118             String f = getFileName(curdir, TEMPLATE_PATH, TMPL_FILE_EXT);
119 
120             System.out.println("Retrieving template at absolute path: " + f);
121 
122             Template template1 = engine.getTemplate(f);
123 
124             FileOutputStream fos1 =
125                 new FileOutputStream (
126                     getFileName(RESULTS_DIR, "absolute", RESULT_FILE_EXT));
127 
128             Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
129 
130             /*
131              *  put the Vector into the context, and merge both
132              */
133             VelocityContext context = new VelocityContext();
134 
135             template1.merge(context, writer1);
136             writer1.flush();
137             writer1.close();
138 
139             if (!isMatch(RESULTS_DIR, COMPARE_DIR, "absolute",
140                     RESULT_FILE_EXT, CMP_FILE_EXT))
141             {
142                 fail("Output incorrect.");
143             }
144         }
145         catch (Exception e)
146         {
147             fail(e.getMessage());
148         }
149     }
150 }