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.test.misc.TestLogChute;
35  
36  /**
37   * Tests if the VM template-locality is working.
38   *
39   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
40   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
41   * @version $Id: InlineScopeVMTestCase.java 832247 2009-11-03 01:29:30Z wglass $
42   */
43  public class InlineScopeVMTestCase extends BaseTestCase implements TemplateTestBase
44  {
45      VelocityEngine engine;
46      public InlineScopeVMTestCase(String name)
47      {
48          super(name);
49      }
50  
51      public void setUp()
52              throws Exception
53      {
54          engine = new VelocityEngine();
55          
56          engine.setProperty(
57                  RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
58  
59          engine.setProperty(
60                  RuntimeConstants.VM_PERM_INLINE_LOCAL, "true");
61  
62          engine.setProperty(
63                  RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
64          
65          engine.setProperty(
66                  RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
67  
68          engine.init();
69      }
70  
71      public static Test suite ()
72      {
73          return new TestSuite(InlineScopeVMTestCase.class);
74      }
75  
76      /**
77       * Runs the test.
78       */
79      public void testInlineScopeVM ()
80              throws Exception
81      {
82          assureResultsDirectoryExists(RESULT_DIR);
83  
84          /*
85           * Get the template and the output. Do them backwards.
86           * vm_test2 uses a local VM and vm_test1 doesn't
87           */
88  
89          Template template2 = engine.getTemplate(
90              getFileName(null, "vm_test2", TMPL_FILE_EXT));
91  
92          Template template1 = engine.getTemplate(
93              getFileName(null, "vm_test1", TMPL_FILE_EXT));
94  
95          FileOutputStream fos1 =
96              new FileOutputStream (
97                  getFileName(RESULT_DIR, "vm_test1", RESULT_FILE_EXT));
98  
99          FileOutputStream fos2 =
100             new FileOutputStream (
101                 getFileName(RESULT_DIR, "vm_test2", RESULT_FILE_EXT));
102 
103         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
104         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
105 
106         /*
107          *  put the Vector into the context, and merge both
108          */
109 
110         VelocityContext context = new VelocityContext();
111 
112         template1.merge(context, writer1);
113         writer1.flush();
114         writer1.close();
115 
116         template2.merge(context, writer2);
117         writer2.flush();
118         writer2.close();
119 
120         if (!isMatch(RESULT_DIR,COMPARE_DIR,"vm_test1",
121                 RESULT_FILE_EXT,CMP_FILE_EXT) ||
122             !isMatch(RESULT_DIR,COMPARE_DIR,"vm_test2",
123                 RESULT_FILE_EXT,CMP_FILE_EXT))
124         {
125             fail("Output incorrect.");
126         }
127     }
128 }