1   package org.apache.velocity.test.issues;
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 org.apache.velocity.app.VelocityEngine;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
25  import org.apache.velocity.runtime.resource.util.StringResourceRepository;
26  import org.apache.velocity.test.BaseTestCase;
27  
28  /**
29   * This class tests VELOCITY-702.
30   */
31  public class Velocity702TestCase extends BaseTestCase
32  {
33      public Velocity702TestCase(String name)
34      {
35          super(name);
36      }
37  
38      public void setUpEngine(VelocityEngine engine)
39      {
40          engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "high,low");
41          engine.addProperty("high.resource.loader.class", StringResourceLoader.class.getName());
42          engine.addProperty("high.resource.loader.cache", "false");
43          engine.addProperty("high.resource.loader.repository.name", "high");
44          engine.addProperty("high.resource.loader.repository.static", "false");
45          engine.addProperty("high.resource.loader.modificationCheckInterval", "1");
46          engine.addProperty("low.resource.loader.class", StringResourceLoader.class.getName());
47          engine.addProperty("low.resource.loader.cache", "true");
48          engine.addProperty("low.resource.loader.repository.name", "low");
49          engine.addProperty("low.resource.loader.repository.static", "false");
50          engine.addProperty("low.resource.loader.modificationCheckInterval", "1");
51          engine.init();
52      }
53  
54      public void testIt() throws Exception
55      {
56          addToHigh("foo", "foo");
57          addToLow("foo", "bar");
58          assertTmplEquals("foo", "foo");
59  
60          removeFromHigh("foo");
61          assertTmplEquals("bar", "foo");
62  
63          Thread.sleep(1500);
64          addToHigh("foo", "woogie");
65          assertTmplEquals("woogie", "foo");
66      }
67  
68      private void addToHigh(String name, String content)
69      {
70          getHighRepo().putStringResource(name, content);
71      }
72  
73      private void removeFromHigh(String name)
74      {
75          getHighRepo().removeStringResource(name);
76      }
77  
78      private StringResourceRepository getHighRepo()
79      {
80          return (StringResourceRepository)engine.getApplicationAttribute("high");
81      }
82  
83      private void addToLow(String name, String content)
84      {
85          getLowRepo().putStringResource(name, content);
86      }
87  
88      private void removeFromLow(String name)
89      {
90          getLowRepo().removeStringResource(name);
91      }
92  
93      private StringResourceRepository getLowRepo()
94      {
95          return (StringResourceRepository)engine.getApplicationAttribute("low");
96      }
97  
98  }