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 704299 2008-10-14 03:13:16Z nbubna $
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.setProperty(
61                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
62  
63          Velocity.setProperty(
64                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
65  
66          Velocity.init();
67      }
68  
69      public static Test suite()
70      {
71          return new TestSuite(ContextSafetyTestCase.class);
72      }
73  
74      /**
75       * Runs the test.
76       */
77      public void testContextSafety ()
78          throws Exception
79      {
80          /*
81           *  make a Vector and String array because
82           *  they are treated differently in Foreach()
83           */
84          Vector v = new Vector();
85  
86          v.addElement( new String("vector hello 1") );
87          v.addElement( new String("vector hello 2") );
88          v.addElement( new String("vector hello 3") );
89  
90          String strArray[] = new String[3];
91  
92          strArray[0] = "array hello 1";
93          strArray[1] = "array hello 2";
94          strArray[2] = "array hello 3";
95  
96          VelocityContext context = new VelocityContext();
97  
98          assureResultsDirectoryExists(RESULT_DIR);
99  
100         /*
101          *  get the template and the output
102          */
103 
104         Template template = RuntimeSingleton.getTemplate(
105             getFileName(null, "context_safety", TMPL_FILE_EXT));
106 
107         FileOutputStream fos1 =
108             new FileOutputStream (
109                 getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT));
110 
111         FileOutputStream fos2 =
112             new FileOutputStream (
113                 getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT));
114 
115         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
116         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
117 
118         /*
119          *  put the Vector into the context, and merge
120          */
121 
122         context.put("vector", v);
123         template.merge(context, writer1);
124         writer1.flush();
125         writer1.close();
126 
127         /*
128          *  now put the string array into the context, and merge
129          */
130 
131         context.put("vector", strArray);
132         template.merge(context, writer2);
133         writer2.flush();
134         writer2.close();
135 
136         if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1",
137                 RESULT_FILE_EXT,CMP_FILE_EXT) ||
138             !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2",
139                 RESULT_FILE_EXT,CMP_FILE_EXT))
140         {
141             fail("Output incorrect.");
142         }
143     }
144 }