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.RuntimeConstants;
34  import org.apache.velocity.runtime.RuntimeSingleton;
35  import org.apache.velocity.test.misc.TestLogChute;
36  import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
37  import org.apache.velocity.runtime.resource.loader.ResourceLoader;
38  
39  /**
40   * Test that an instance of a ResourceLoader can be successfully passed in.
41   *
42   * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
43   * @version $Id: ResourceLoaderInstanceTestCase.java 691334 2008-09-02 18:10:41Z nbubna $
44   */
45  public class ResourceLoaderInstanceTestCase extends BaseTestCase
46  {
47      /**
48       * VTL file extension.
49       */
50      private static final String TMPL_FILE_EXT = "vm";
51  
52      /**
53       * Comparison file extension.
54       */
55      private static final String CMP_FILE_EXT = "cmp";
56  
57      /**
58       * Comparison file extension.
59       */
60      private static final String RESULT_FILE_EXT = "res";
61  
62      /**
63       * Path for templates. This property will override the
64       * value in the default velocity properties file.
65       */
66      private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/resourceinstance";
67  
68      /**
69       * Results relative to the build directory.
70       */
71      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/resourceinstance";
72  
73      /**
74       * Results relative to the build directory.
75       */
76      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/resourceinstance/compare";
77  
78      private TestLogChute logger = new TestLogChute();
79  
80      /**
81       * Default constructor.
82       */
83      public ResourceLoaderInstanceTestCase(String name)
84      {
85          super(name);
86      }
87  
88      public void setUp()
89              throws Exception
90      {
91  
92          ResourceLoader rl = new FileResourceLoader();
93  
94          // pass in an instance to Velocity
95          Velocity.setProperty( "resource.loader", "testrl" );
96          Velocity.setProperty( "testrl.resource.loader.instance", rl );
97          Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
98  
99          // actual instance of logger
100         logger.on();
101         Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
102         Velocity.setProperty("runtime.log.logsystem.test.level", "debug");
103 
104         Velocity.init();
105     }
106 
107     public static Test suite ()
108     {
109         return new TestSuite(ResourceLoaderInstanceTestCase.class);
110     }
111 
112     /**
113      * Runs the test.
114      */
115     public void testResourceLoaderInstance ()
116             throws Exception
117     {
118 //caveman hacks to get gump to give more info
119 try
120 {
121         assureResultsDirectoryExists(RESULTS_DIR);
122 
123         Template template = RuntimeSingleton.getTemplate(
124                 getFileName(null, "testfile", TMPL_FILE_EXT));
125 
126         FileOutputStream fos =
127                 new FileOutputStream (
128                         getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
129 //caveman hack to get gump to give more info
130 System.out.println("All needed files exist");
131 
132         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
133 
134         /*
135          *  put the Vector into the context, and merge both
136          */
137 
138         VelocityContext context = new VelocityContext();
139 
140         template.merge(context, writer);
141         writer.flush();
142         writer.close();
143 }
144 catch (Exception e)
145 {
146     System.out.println("Log was: "+logger.getLog());
147     System.out.println(e);
148     e.printStackTrace();
149 }
150 
151         if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
152                         RESULT_FILE_EXT, CMP_FILE_EXT) )
153         {
154             String result = getFileContents(RESULT_DIR, "testfile", RESULT_FILE_EXT);
155             String compare = getFileContents(COMPARE_DIR, "testfile", CMP_FILE_EXT);
156 
157             String msg = "Processed template did not match expected output\n"+
158                 "-----Result-----\n"+ result +
159                 "----Expected----\n"+ compare +
160                 "----------------";
161             
162 //caveman hack to get gump to give more info
163 System.out.println(msg);
164 System.out.println("Log was: "+logger.getLog());
165             fail(msg);
166         }
167     }
168 }