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.VelocityEngine;
33  import org.apache.velocity.runtime.RuntimeConstants;
34  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
35  import org.apache.velocity.test.misc.TestLogChute;
36  
37  /**
38   * Multiple paths in the file resource loader.
39   *
40   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
41   * @version $Id: StringResourceLoaderTestCase.java 832247 2009-11-03 01:29:30Z wglass $
42   */
43  public class StringResourceLoaderTestCase extends BaseTestCase
44  {
45      /**
46       * Results relative to the build directory.
47       */
48      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/stringloader";
49  
50      /**
51       * Results relative to the build directory.
52       */
53      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/stringloader/compare";
54  
55      VelocityEngine engine;
56      
57      /**
58       * Default constructor.
59       */
60      public StringResourceLoaderTestCase(String name)
61      {
62          super(name);
63      }
64  
65      public static Test suite()
66      {
67          return new TestSuite(StringResourceLoaderTestCase.class);
68      }
69  
70      public void setUp()
71              throws Exception
72      {
73          assureResultsDirectoryExists(RESULTS_DIR);
74  
75          engine = new VelocityEngine();
76          
77          engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "string");
78          engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
79          engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
80  
81          // Silence the logger.
82          engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
83  
84          engine.init();
85      }
86  
87      public void  testSimpleTemplate()
88              throws Exception
89      {
90          StringResourceLoader.getRepository().putStringResource("simpletemplate.vm", "This is a test for ${foo}");
91  
92          Template template = engine.getTemplate(getFileName(null, "simpletemplate", TMPL_FILE_EXT));
93  
94          FileOutputStream fos =
95              new FileOutputStream (
96                  getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));
97  
98          Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
99  
100         VelocityContext context = new VelocityContext();
101         context.put("foo", "a foo object");
102 
103         template.merge(context, writer);
104         writer.flush();
105         writer.close();
106 
107         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
108                         RESULT_FILE_EXT, CMP_FILE_EXT))
109         {
110             fail("Output incorrect.");
111         }
112     }
113 
114     public void  testMultipleTemplates()
115             throws Exception
116     {
117         StringResourceLoader.getRepository().putStringResource("multi1.vm", "I am the $first template.");
118         StringResourceLoader.getRepository().putStringResource("multi2.vm", "I am the $second template.");
119 
120         Template template1 = engine.getTemplate(getFileName(null, "multi1", TMPL_FILE_EXT));
121 
122         FileOutputStream fos =
123             new FileOutputStream (
124                 getFileName(RESULTS_DIR, "multi1", RESULT_FILE_EXT));
125 
126         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
127 
128         VelocityContext context = new VelocityContext();
129         context.put("first", new Integer(1));
130         context.put("second", "two");
131 
132         template1.merge(context, writer);
133         writer.flush();
134         writer.close();
135 
136         Template template2 = engine.getTemplate(getFileName(null, "multi2", TMPL_FILE_EXT));
137 
138         fos = new FileOutputStream (
139                 getFileName(RESULTS_DIR, "multi2", RESULT_FILE_EXT));
140 
141         writer = new BufferedWriter(new OutputStreamWriter(fos));
142         
143         template2.merge(context, writer);
144         writer.flush();
145         writer.close();
146 
147 
148 
149         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi1",
150                         RESULT_FILE_EXT, CMP_FILE_EXT))
151         {
152             fail("Template 1 incorrect.");
153         }
154 
155         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi2",
156                         RESULT_FILE_EXT, CMP_FILE_EXT))
157         {
158             fail("Template 2 incorrect.");
159         }
160     }
161 
162     public void  testContentChange()
163             throws Exception
164     {
165         StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $first template.");
166 
167         Template template = engine.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
168 
169         FileOutputStream fos =
170             new FileOutputStream (
171                 getFileName(RESULTS_DIR, "change1", RESULT_FILE_EXT));
172 
173         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
174 
175         VelocityContext context = new VelocityContext();
176         context.put("first", new Integer(1));
177         context.put("second", "two");
178 
179         template.merge(context, writer);
180         writer.flush();
181         writer.close();
182 
183         StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $second template.");
184         Thread.sleep(2000L);
185         template = engine.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
186 
187         fos = new FileOutputStream (
188                 getFileName(RESULTS_DIR, "change2", RESULT_FILE_EXT));
189 
190         writer = new BufferedWriter(new OutputStreamWriter(fos));
191         
192         template.merge(context, writer);
193         writer.flush();
194         writer.close();
195 
196 
197 
198         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change1",
199                         RESULT_FILE_EXT, CMP_FILE_EXT))
200         {
201             fail("Template 1 incorrect.");
202         }
203 
204         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change2",
205                         RESULT_FILE_EXT, CMP_FILE_EXT))
206         {
207             fail("Template 2 incorrect.");
208         }
209     }
210 
211 }