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 957852 2010-06-25 08:53:22Z apetrelli $
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.reset();
96          Velocity.setProperty( "resource.loader", "testrl" );
97          Velocity.setProperty( "testrl.resource.loader.instance", rl );
98          Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
99  
100         // actual instance of logger
101         logger.on();
102         Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
103         Velocity.setProperty("runtime.log.logsystem.test.level", "debug");
104 
105         Velocity.init();
106     }
107 
108     public static Test suite ()
109     {
110         return new TestSuite(ResourceLoaderInstanceTestCase.class);
111     }
112 
113     /**
114      * Runs the test.
115      */
116     public void testResourceLoaderInstance ()
117             throws Exception
118     {
119 //caveman hacks to get gump to give more info
120 try
121 {
122         assureResultsDirectoryExists(RESULTS_DIR);
123 
124         Template template = RuntimeSingleton.getTemplate(
125                 getFileName(null, "testfile", TMPL_FILE_EXT));
126 
127         FileOutputStream fos =
128                 new FileOutputStream (
129                         getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
130 //caveman hack to get gump to give more info
131 System.out.println("All needed files exist");
132 
133         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
134 
135         /*
136          *  put the Vector into the context, and merge both
137          */
138 
139         VelocityContext context = new VelocityContext();
140 
141         template.merge(context, writer);
142         writer.flush();
143         writer.close();
144 }
145 catch (Exception e)
146 {
147     System.out.println("Log was: "+logger.getLog());
148     System.out.println(e);
149     e.printStackTrace();
150 }
151 
152         if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
153                         RESULT_FILE_EXT, CMP_FILE_EXT) )
154         {
155             String result = getFileContents(RESULT_DIR, "testfile", RESULT_FILE_EXT);
156             String compare = getFileContents(COMPARE_DIR, "testfile", CMP_FILE_EXT);
157 
158             String msg = "Processed template did not match expected output\n"+
159                 "-----Result-----\n"+ result +
160                 "----Expected----\n"+ compare +
161                 "----------------";
162 
163 //caveman hack to get gump to give more info
164 System.out.println(msg);
165 System.out.println("Log was: "+logger.getLog());
166             fail(msg);
167         }
168     }
169 }