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  import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
36  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
37  
38  /**
39   * Test that an instance of a ResourceLoader can be successfully passed in.
40   *
41   * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
42   * @version $Id: ResourceLoaderInstanceTestCase.java 477002 2006-11-20 01:07:43Z henning $
43   */
44  public class ResourceLoaderInstanceTestCase extends BaseTestCase
45  {
46      /**
47       * VTL file extension.
48       */
49      private static final String TMPL_FILE_EXT = "vm";
50  
51      /**
52       * Comparison file extension.
53       */
54      private static final String CMP_FILE_EXT = "cmp";
55  
56      /**
57       * Comparison file extension.
58       */
59      private static final String RESULT_FILE_EXT = "res";
60  
61      /**
62       * Path for templates. This property will override the
63       * value in the default velocity properties file.
64       */
65      private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/resourceinstance";
66  
67      /**
68       * Results relative to the build directory.
69       */
70      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/resourceinstance";
71  
72      /**
73       * Results relative to the build directory.
74       */
75      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/resourceinstance/compare";
76  
77      /**
78       * Default constructor.
79       */
80      public ResourceLoaderInstanceTestCase(String name)
81      {
82          super(name);
83      }
84  
85      public void setUp()
86              throws Exception
87      {
88  
89          ResourceLoader rl = new FileResourceLoader();
90  
91          // pass in an instance to Velocity
92          Velocity.addProperty( "resource.loader", "testrl" );
93          Velocity.setProperty( "testrl.resource.loader.instance", rl );
94          Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
95  
96          Velocity.setProperty(
97                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
98  
99          Velocity.init();
100     }
101 
102     public static Test suite ()
103     {
104         return new TestSuite(ResourceLoaderInstanceTestCase.class);
105     }
106 
107     /**
108      * Runs the test.
109      */
110     public void testResourceLoaderInstance ()
111             throws Exception
112     {
113         assureResultsDirectoryExists(RESULTS_DIR);
114 
115         Template template = RuntimeSingleton.getTemplate(
116                 getFileName(null, "testfile", TMPL_FILE_EXT));
117 
118         FileOutputStream fos =
119                 new FileOutputStream (
120                         getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
121 
122 
123         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
124 
125         /*
126          *  put the Vector into the context, and merge both
127          */
128 
129         VelocityContext context = new VelocityContext();
130 
131         template.merge(context, writer);
132         writer.flush();
133         writer.close();
134 
135         if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
136                         RESULT_FILE_EXT, CMP_FILE_EXT) )
137         {
138             fail("Output incorrect.");
139         }
140     }
141 }