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  import java.util.Vector;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.velocity.Template;
32  import org.apache.velocity.VelocityContext;
33  import org.apache.velocity.app.Velocity;
34  import org.apache.velocity.runtime.RuntimeSingleton;
35  import org.apache.velocity.test.misc.TestLogChute;
36  
37  /**
38   * Tests if we are context safe : can we switch objects in the context
39   * and re-merge the template safely.
40   *
41   * NOTE:
42   * This class should not extend RuntimeTestCase because this test
43   * is run from the VelocityTestSuite which in effect a runtime
44   * test suite and the test suite initializes the Runtime. Extending
45   * RuntimeTestCase causes the Runtime to be initialized twice.
46   *
47   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
48   * @version $Id: ContextSafetyTestCase.java 957852 2010-06-25 08:53:22Z apetrelli $
49   */
50  public class ContextSafetyTestCase extends BaseTestCase implements TemplateTestBase
51  {
52      public ContextSafetyTestCase(String name)
53      {
54          super(name);
55      }
56  
57      public void setUp()
58              throws Exception
59      {
60          Velocity.reset();
61          Velocity.setProperty(
62                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
63  
64          Velocity.setProperty(
65                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
66  
67          Velocity.init();
68      }
69  
70      public static Test suite()
71      {
72          return new TestSuite(ContextSafetyTestCase.class);
73      }
74  
75      /**
76       * Runs the test.
77       */
78      public void testContextSafety ()
79          throws Exception
80      {
81          /*
82           *  make a Vector and String array because
83           *  they are treated differently in Foreach()
84           */
85          Vector v = new Vector();
86  
87          v.addElement( new String("vector hello 1") );
88          v.addElement( new String("vector hello 2") );
89          v.addElement( new String("vector hello 3") );
90  
91          String strArray[] = new String[3];
92  
93          strArray[0] = "array hello 1";
94          strArray[1] = "array hello 2";
95          strArray[2] = "array hello 3";
96  
97          VelocityContext context = new VelocityContext();
98  
99          assureResultsDirectoryExists(RESULT_DIR);
100 
101         /*
102          *  get the template and the output
103          */
104 
105         Template template = RuntimeSingleton.getTemplate(
106             getFileName(null, "context_safety", TMPL_FILE_EXT));
107 
108         FileOutputStream fos1 =
109             new FileOutputStream (
110                 getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT));
111 
112         FileOutputStream fos2 =
113             new FileOutputStream (
114                 getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT));
115 
116         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
117         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
118 
119         /*
120          *  put the Vector into the context, and merge
121          */
122 
123         context.put("vector", v);
124         template.merge(context, writer1);
125         writer1.flush();
126         writer1.close();
127 
128         /*
129          *  now put the string array into the context, and merge
130          */
131 
132         context.put("vector", strArray);
133         template.merge(context, writer2);
134         writer2.flush();
135         writer2.close();
136 
137         if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1",
138                 RESULT_FILE_EXT,CMP_FILE_EXT) ||
139             !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2",
140                 RESULT_FILE_EXT,CMP_FILE_EXT))
141         {
142             fail("Output incorrect.");
143         }
144     }
145 }