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.StringWriter;
26  import java.io.Writer;
27  import java.util.ArrayList;
28  
29  import junit.framework.Test;
30  import junit.framework.TestSuite;
31  
32  import org.apache.velocity.Template;
33  import org.apache.velocity.VelocityContext;
34  import org.apache.velocity.app.VelocityEngine;
35  import org.apache.velocity.exception.MethodInvocationException;
36  import org.apache.velocity.runtime.RuntimeConstants;
37  import org.apache.velocity.runtime.RuntimeServices;
38  import org.apache.velocity.runtime.log.LogChute;
39  
40  /**
41   * Tests event handling for all event handlers when multiple event handlers are
42   * assigned for each type.
43   *
44   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
45   * @version $Id: FilteredEventHandlingTestCase.java 463298 2006-10-12 16:10:32Z henning $
46   */
47  public class FilteredEventHandlingTestCase extends BaseTestCase implements LogChute
48  {
49  
50      /**
51      * VTL file extension.
52      */
53     private static final String TMPL_FILE_EXT = "vm";
54  
55     /**
56      * Comparison file extension.
57      */
58     private static final String CMP_FILE_EXT = "cmp";
59  
60     /**
61      * Comparison file extension.
62      */
63     private static final String RESULT_FILE_EXT = "res";
64  
65     /**
66      * Path for templates. This property will override the
67      * value in the default velocity properties file.
68      */
69     private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/includeevent";
70  
71     /**
72      * Results relative to the build directory.
73      */
74     private static final String RESULTS_DIR = TEST_RESULT_DIR + "/includeevent";
75  
76     /**
77      * Results relative to the build directory.
78      */
79     private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/includeevent/compare";
80  
81  
82      private String logString = null;
83  
84      /**
85       * Default constructor.
86       */
87      public FilteredEventHandlingTestCase(String name)
88      {
89          super(name);
90      }
91  
92  
93      /**
94       * Required by LogChute
95       */
96      public void init( RuntimeServices rs )
97      {
98          /* don't need it...*/
99      }
100 
101     public static Test suite ()
102     {
103         return new TestSuite(FilteredEventHandlingTestCase.class);
104     }
105 
106     public void testFilteredEventHandling() throws Exception
107     {
108         String handler1 = "org.apache.velocity.test.eventhandler.Handler1";
109         String handler2 = "org.apache.velocity.test.eventhandler.Handler2";
110         String sequence1 = handler1 + "," + handler2;
111         String sequence2 = handler2 + "," + handler1;
112 
113         assureResultsDirectoryExists(RESULTS_DIR);
114 
115         /**
116          * Set up two VelocityEngines that will apply the handlers in both orders
117          */
118         VelocityEngine ve = new VelocityEngine();
119         ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this);
120         ve.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, sequence1);
121         ve.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET, sequence1);
122         ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, sequence1);
123         ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, sequence1);
124         ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
125         ve.init();
126 
127         VelocityEngine ve2 = new VelocityEngine();
128         ve2.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this);
129         ve2.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, sequence2);
130         ve2.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET, sequence2);
131         ve2.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, sequence2);
132         ve2.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, sequence2);
133         ve2.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
134         ve2.init();
135 
136         VelocityContext context;
137         StringWriter w;
138 
139 
140         // check reference insertion with both sequences
141         context = new VelocityContext();
142         w = new StringWriter();
143         context.put("test","abc");
144         ve.evaluate( context, w, "test", "$test" );
145         if ( !w.toString().equals( "ABCABC" ))
146         {
147             fail( "Reference insertion test 1");
148         }
149 
150         context = new VelocityContext();
151         w = new StringWriter();
152         context.put("test","abc");
153         ve2.evaluate( context, w, "test", "$test" );
154         if ( !w.toString().equals( "ABCabc" ))
155         {
156             fail( "Reference insertion test 2");
157         }
158 
159         // check method exception with both sequences
160 
161         // sequence 1
162         context = new VelocityContext();
163         w = new StringWriter();
164         context.put("test",new ArrayList());
165 
166         try
167         {
168             ve.evaluate( context, w, "test", "$test.get(0)");
169             fail ( "Method exception event test 1" );
170         }
171         catch( MethodInvocationException mee )
172         {
173             // do nothing
174         }
175 
176         // sequence2
177         context = new VelocityContext();
178         w = new StringWriter();
179         context.put("test",new ArrayList());
180 
181         ve2.evaluate( context, w, "test", "$test.get(0)");
182 
183         // check log on null set with both sequences
184         // sequence 1
185         context = new VelocityContext();
186         w = new StringWriter();
187         logString = null;
188         ve.evaluate( context, w, "test", "#set($test1 = $test2)" );
189         if ( logString != null)
190         {
191             fail( "log null set test 1");
192         }
193 
194         // sequence 2
195         context = new VelocityContext();
196         w = new StringWriter();
197         logString = null;
198         ve2.evaluate( context, w, "test", "#set($test1 = $test2)" );
199         if ( logString != null)
200         {
201             fail( "log null set test 2");
202         }
203 
204 
205         // check include event handler with both sequences
206 
207         // sequence 1
208         Template template;
209         FileOutputStream fos;
210         Writer fwriter;
211 
212         template = ve.getTemplate( getFileName(null, "test4", TMPL_FILE_EXT) );
213 
214         fos = new FileOutputStream (
215                 getFileName(RESULTS_DIR, "test4", RESULT_FILE_EXT));
216 
217         fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
218 
219         context = new VelocityContext();
220         template.merge(context, fwriter);
221         fwriter.flush();
222         fwriter.close();
223 
224         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test4", RESULT_FILE_EXT, CMP_FILE_EXT))
225         {
226             fail("Output incorrect.");
227         }
228 
229         // sequence 2
230         template = ve2.getTemplate( getFileName(null, "test5", TMPL_FILE_EXT) );
231 
232         fos = new FileOutputStream (
233                 getFileName(RESULTS_DIR, "test5", RESULT_FILE_EXT));
234 
235         fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
236 
237         context = new VelocityContext();
238         template.merge(context, fwriter);
239         fwriter.flush();
240         fwriter.close();
241 
242         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test5", RESULT_FILE_EXT, CMP_FILE_EXT))
243         {
244             fail("Output incorrect.");
245         }
246 
247     }
248 
249 
250 
251 
252     /**
253      *  handler for LogChute interface
254      */
255     public void log(int level, String message)
256     {
257         logString = message;
258     }
259 
260     public void log(int level, String message, Throwable t)
261     {
262         logString = message;
263     }
264 
265     public boolean isLevelEnabled(int level)
266     {
267         return true;
268     }
269 
270 }