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.StringWriter;
23  import junit.framework.Test;
24  import junit.framework.TestSuite;
25  import junit.framework.TestCase;
26  import org.apache.velocity.Template;
27  import org.apache.velocity.VelocityContext;
28  import org.apache.velocity.app.Velocity;
29  import org.apache.velocity.app.VelocityEngine;
30  import org.apache.velocity.runtime.RuntimeSingleton;
31  import org.apache.velocity.runtime.log.SystemLogChute;
32  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
33  import org.apache.velocity.runtime.resource.util.StringResourceRepository;
34  import org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl;
35  
36  /**
37   * Tests ability to have multiple repositories in the same app.
38   *
39   * @author Nathan Bubna
40   * @version $Id: StringResourceLoaderRepositoryTestCase.java 479058 2006-11-25 00:26:32Z henning $
41   */
42  public class StringResourceLoaderRepositoryTestCase extends TestCase
43  {
44      private VelocityContext context;
45  
46      public StringResourceLoaderRepositoryTestCase(String name)
47      {
48          super(name);
49      }
50  
51      public void setUp() throws Exception
52      {
53          Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
54          Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
55          Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
56          Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());
57          Velocity.init();
58  
59          StringResourceRepository repo = getRepo(null, null);
60          repo.putStringResource("foo", "This is $foo");
61          repo.putStringResource("bar", "This is $bar");
62  
63          context = new VelocityContext();
64          context.put("foo", "wonderful!");
65          context.put("bar", "horrible!");
66          context.put("woogie", "a woogie");
67      }
68  
69  
70      protected VelocityEngine newStringEngine(String repoName, boolean isStatic)
71      {
72          VelocityEngine engine = new VelocityEngine();
73          engine.setProperty(Velocity.RESOURCE_LOADER, "string");
74          engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
75          if (repoName != null)
76          {
77              engine.addProperty("string.resource.loader.repository.name", repoName);
78          }
79          if (!isStatic)
80          {
81              engine.addProperty("string.resource.loader.repository.static", "false");
82          }
83          engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
84          engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());
85          return engine;
86      }
87  
88      protected StringResourceRepository getRepo(String name, VelocityEngine engine)
89      {
90          if (engine == null)
91          {
92              if (name == null)
93              {
94                  return StringResourceLoader.getRepository();
95              }
96              else
97              {
98                  return StringResourceLoader.getRepository(name);
99              }
100         }
101         else
102         {
103             if (name == null)
104             {
105                 return (StringResourceRepository)engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
106             }
107             else
108             {
109                 return (StringResourceRepository)engine.getApplicationAttribute(name);
110             }
111         }
112     }
113 
114     protected String render(Template template) throws Exception
115     {
116         StringWriter out = new StringWriter();
117         template.merge(this.context, out);
118         return out.toString();
119     }
120 
121 
122     public void testSharedRepo() throws Exception
123     {
124         // this engine's string resource loader should share a repository
125         // with the singleton's string resource loader
126         VelocityEngine engine = newStringEngine(null, true);
127 
128         // get and merge the same template from both runtimes with the same context
129         String engineOut = render(engine.getTemplate("foo"));
130         String singletonOut = render(RuntimeSingleton.getTemplate("foo"));
131 
132         // make sure they're equal
133         assertEquals(engineOut, singletonOut);
134     }
135 
136     public void testAlternateStaticRepo() throws Exception
137     {
138         VelocityEngine engine = newStringEngine("alternate.repo", true);
139         // should be null be for init
140         StringResourceRepository repo = getRepo("alternate.repo", null);
141         assertNull(repo);
142         engine.init();
143         // and not null after init
144         repo = getRepo("alternate.repo", null);
145         assertNotNull(repo);
146         repo.putStringResource("foo", "This is NOT $foo");
147 
148         // get and merge template with the same name from both runtimes with the same context
149         String engineOut = render(engine.getTemplate("foo"));
150         String singletonOut = render(RuntimeSingleton.getTemplate("foo"));
151 
152         // make sure they're NOT equal
153         assertFalse(engineOut.equals(singletonOut));
154     }
155 
156     public void testPreCreatedStaticRepo() throws Exception
157     {
158         VelocityEngine engine = newStringEngine("my.repo", true);
159         MyRepo repo = new MyRepo();
160         repo.put("bar", "This is NOT $bar");
161         StringResourceLoader.setRepository("my.repo", repo);
162 
163         String out = render(engine.getTemplate("bar"));
164         assertEquals(out, "This is NOT horrible!");
165     }
166 
167     public void testAppRepo() throws Exception
168     {
169         VelocityEngine engine = newStringEngine(null, false);
170         engine.init();
171 
172         StringResourceRepository repo = getRepo(null, engine);
173         assertNotNull(repo);
174         repo.putStringResource("woogie", "What is $woogie?");
175 
176         String out = render(engine.getTemplate("woogie"));
177         assertEquals(out, "What is a woogie?");
178     }
179 
180     public void testAlternateAppRepo() throws Exception
181     {
182         VelocityEngine engine = newStringEngine("alternate.app.repo", false);
183         engine.init();
184 
185         StringResourceRepository repo = getRepo("alternate.app.repo", engine);
186         assertNotNull(repo);
187         repo.putStringResource("you/foo.vm", "You look $foo");
188 
189         String out = render(engine.getTemplate("you/foo.vm"));
190         assertEquals(out, "You look wonderful!");
191     }
192 
193     public void testPreCreatedAppRepo() throws Exception
194     {
195         VelocityEngine engine = newStringEngine("my.app.repo", false);
196         MyRepo repo = new MyRepo();
197         repo.put("you/bar.vm", "You look $bar");
198         engine.setApplicationAttribute("my.app.repo", repo);
199 
200         String out = render(engine.getTemplate("you/bar.vm"));
201         assertEquals(out, "You look horrible!");
202     }
203 
204     public static class MyRepo extends StringResourceRepositoryImpl
205     {
206         public void put(String name, String template)
207         {
208             putStringResource(name, template);
209         }
210     }
211 
212 }