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  
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 704299 2008-10-14 03:13:16Z nbubna $
42   */
43  public class InlineScopeVMTestCase extends BaseTestCase implements TemplateTestBase
44  {
45      public InlineScopeVMTestCase(String name)
46      {
47          super(name);
48      }
49  
50      public void setUp()
51              throws Exception
52      {
53          /*
54           *  do our properties locally, and just override the ones we want
55           *  changed
56           */
57  
58          Velocity.setProperty(
59              Velocity.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
60  
61          Velocity.setProperty(
62              Velocity.VM_PERM_INLINE_LOCAL, "true");
63  
64          Velocity.setProperty(
65              Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
66          
67          Velocity.setProperty(
68                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
69  
70          Velocity.init();
71      }
72  
73      public static Test suite ()
74      {
75          return new TestSuite(InlineScopeVMTestCase.class);
76      }
77  
78      /**
79       * Runs the test.
80       */
81      public void testInlineScopeVM ()
82              throws Exception
83      {
84          assureResultsDirectoryExists(RESULT_DIR);
85  
86          /*
87           * Get the template and the output. Do them backwards.
88           * vm_test2 uses a local VM and vm_test1 doesn't
89           */
90  
91          Template template2 = RuntimeSingleton.getTemplate(
92              getFileName(null, "vm_test2", TMPL_FILE_EXT));
93  
94          Template template1 = RuntimeSingleton.getTemplate(
95              getFileName(null, "vm_test1", TMPL_FILE_EXT));
96  
97          FileOutputStream fos1 =
98              new FileOutputStream (
99                  getFileName(RESULT_DIR, "vm_test1", RESULT_FILE_EXT));
100 
101         FileOutputStream fos2 =
102             new FileOutputStream (
103                 getFileName(RESULT_DIR, "vm_test2", RESULT_FILE_EXT));
104 
105         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
106         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
107 
108         /*
109          *  put the Vector into the context, and merge both
110          */
111 
112         VelocityContext context = new VelocityContext();
113 
114         template1.merge(context, writer1);
115         writer1.flush();
116         writer1.close();
117 
118         template2.merge(context, writer2);
119         writer2.flush();
120         writer2.close();
121 
122         if (!isMatch(RESULT_DIR,COMPARE_DIR,"vm_test1",
123                 RESULT_FILE_EXT,CMP_FILE_EXT) ||
124             !isMatch(RESULT_DIR,COMPARE_DIR,"vm_test2",
125                 RESULT_FILE_EXT,CMP_FILE_EXT))
126         {
127             fail("Output incorrect.");
128         }
129     }
130 }