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.Velocity;
32  import org.apache.velocity.runtime.RuntimeSingleton;
33  import org.apache.velocity.runtime.log.NullLogChute;
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 477002 2006-11-20 01:07:43Z henning $
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      /**
75       * Default constructor.
76       */
77      AbsoluteFileResourceLoaderTestCase()
78      {
79          super("AbsoluteFileResourceLoaderTest");
80  
81          try
82          {
83              assureResultsDirectoryExists(RESULTS_DIR);
84  
85  
86              // signify we want to use an absolute path
87              Velocity.addProperty(
88                  Velocity.FILE_RESOURCE_LOADER_PATH, "");
89  
90              Velocity.setProperty(
91                      Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
92  
93              Velocity.init();
94          }
95          catch (Exception e)
96          {
97              fail("Cannot setup AbsoluteFileResourceLoaderTest!");
98          }
99      }
100 
101     public static Test suite ()
102     {
103         return new AbsoluteFileResourceLoaderTestCase();
104     }
105 
106     /**
107      * Runs the test.
108      */
109     public void runTest ()
110     {
111         try
112         {
113 
114             String curdir = System.getProperty("user.dir");
115             String f = getFileName(curdir, TEMPLATE_PATH, TMPL_FILE_EXT);
116 
117             System.out.println("Retrieving template at absolute path: " + f);
118 
119             Template template1 = RuntimeSingleton.getTemplate(f);
120 
121             FileOutputStream fos1 =
122                 new FileOutputStream (
123                     getFileName(RESULTS_DIR, "absolute", RESULT_FILE_EXT));
124 
125             Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
126 
127             /*
128              *  put the Vector into the context, and merge both
129              */
130             VelocityContext context = new VelocityContext();
131 
132             template1.merge(context, writer1);
133             writer1.flush();
134             writer1.close();
135 
136             if (!isMatch(RESULTS_DIR, COMPARE_DIR, "absolute",
137                     RESULT_FILE_EXT, CMP_FILE_EXT))
138             {
139                 fail("Output incorrect.");
140             }
141         }
142         catch (Exception e)
143         {
144             fail(e.getMessage());
145         }
146     }
147 }