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.test.misc.TestLogChute;
35  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
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 704299 2008-10-14 03:13:16Z nbubna $
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      /**
56       * Default constructor.
57       */
58      public StringResourceLoaderTestCase(String name)
59      {
60          super(name);
61      }
62  
63      public static Test suite()
64      {
65          return new TestSuite(StringResourceLoaderTestCase.class);
66      }
67  
68      public void setUp()
69              throws Exception
70      {
71          assureResultsDirectoryExists(RESULTS_DIR);
72  
73          Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
74          Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
75          Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
76  
77          // Silence the logger.
78          Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
79  
80          Velocity.init();
81      }
82  
83      public void  testSimpleTemplate()
84              throws Exception
85      {
86          StringResourceLoader.getRepository().putStringResource("simpletemplate.vm", "This is a test for ${foo}");
87  
88          Template template = RuntimeSingleton.getTemplate(getFileName(null, "simpletemplate", TMPL_FILE_EXT));
89  
90          FileOutputStream fos =
91              new FileOutputStream (
92                  getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));
93  
94          Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
95  
96          VelocityContext context = new VelocityContext();
97          context.put("foo", "a foo object");
98  
99          template.merge(context, writer);
100         writer.flush();
101         writer.close();
102 
103         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
104                         RESULT_FILE_EXT, CMP_FILE_EXT))
105         {
106             fail("Output incorrect.");
107         }
108     }
109 
110     public void  testMultipleTemplates()
111             throws Exception
112     {
113         StringResourceLoader.getRepository().putStringResource("multi1.vm", "I am the $first template.");
114         StringResourceLoader.getRepository().putStringResource("multi2.vm", "I am the $second template.");
115 
116         Template template1 = RuntimeSingleton.getTemplate(getFileName(null, "multi1", TMPL_FILE_EXT));
117 
118         FileOutputStream fos =
119             new FileOutputStream (
120                 getFileName(RESULTS_DIR, "multi1", RESULT_FILE_EXT));
121 
122         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
123 
124         VelocityContext context = new VelocityContext();
125         context.put("first", new Integer(1));
126         context.put("second", "two");
127 
128         template1.merge(context, writer);
129         writer.flush();
130         writer.close();
131 
132         Template template2 = RuntimeSingleton.getTemplate(getFileName(null, "multi2", TMPL_FILE_EXT));
133 
134         fos = new FileOutputStream (
135                 getFileName(RESULTS_DIR, "multi2", RESULT_FILE_EXT));
136 
137         writer = new BufferedWriter(new OutputStreamWriter(fos));
138         
139         template2.merge(context, writer);
140         writer.flush();
141         writer.close();
142 
143 
144 
145         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi1",
146                         RESULT_FILE_EXT, CMP_FILE_EXT))
147         {
148             fail("Template 1 incorrect.");
149         }
150 
151         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi2",
152                         RESULT_FILE_EXT, CMP_FILE_EXT))
153         {
154             fail("Template 2 incorrect.");
155         }
156     }
157 
158     public void  testContentChange()
159             throws Exception
160     {
161         StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $first template.");
162 
163         Template template = RuntimeSingleton.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
164 
165         FileOutputStream fos =
166             new FileOutputStream (
167                 getFileName(RESULTS_DIR, "change1", RESULT_FILE_EXT));
168 
169         Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
170 
171         VelocityContext context = new VelocityContext();
172         context.put("first", new Integer(1));
173         context.put("second", "two");
174 
175         template.merge(context, writer);
176         writer.flush();
177         writer.close();
178 
179         StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $second template.");
180         Thread.sleep(2000L);
181         template = RuntimeSingleton.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
182 
183         fos = new FileOutputStream (
184                 getFileName(RESULTS_DIR, "change2", RESULT_FILE_EXT));
185 
186         writer = new BufferedWriter(new OutputStreamWriter(fos));
187         
188         template.merge(context, writer);
189         writer.flush();
190         writer.close();
191 
192 
193 
194         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change1",
195                         RESULT_FILE_EXT, CMP_FILE_EXT))
196         {
197             fail("Template 1 incorrect.");
198         }
199 
200         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change2",
201                         RESULT_FILE_EXT, CMP_FILE_EXT))
202         {
203             fail("Template 2 incorrect.");
204         }
205     }
206 
207 }