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.StringWriter;
23  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.velocity.Template;
28  import org.apache.velocity.VelocityContext;
29  import org.apache.velocity.app.Velocity;
30  import org.apache.velocity.app.VelocityEngine;
31  import org.apache.velocity.context.Context;
32  import org.apache.velocity.exception.ParseErrorException;
33  import org.apache.velocity.exception.ResourceNotFoundException;
34  
35  
36  
37  
38  /**
39   * Test that #parse and #include pass errors to calling code.
40   * Specifically checking against VELOCITY-95 and VELOCITY-96.
41   *
42   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
43   * @version $Id: IncludeErrorTestCase.java 463298 2006-10-12 16:10:32Z henning $
44   */
45  public class IncludeErrorTestCase extends BaseTestCase implements TemplateTestBase
46  {
47      VelocityEngine ve;
48  
49      /**
50       * Default constructor.
51       */
52      public IncludeErrorTestCase(String name)
53      {
54          super(name);
55      }
56  
57      public static Test suite ()
58      {
59          return new TestSuite(IncludeErrorTestCase.class);
60      }
61  
62      public void setUp() throws Exception
63      {
64          ve = new VelocityEngine();
65          ve.setProperty(
66                  Velocity.FILE_RESOURCE_LOADER_PATH, "test/includeerror");
67  
68          ve.init();
69      }
70  
71  
72  
73      public void testMissingParseError() throws Exception
74      {
75          checkException("missingparse.vm",ResourceNotFoundException.class);
76      }
77  
78      public void testMissingIncludeError() throws Exception
79      {
80          checkException("missinginclude.vm",ResourceNotFoundException.class);
81      }
82  
83      public void testParseError() throws Exception
84      {
85          checkException("parsemain.vm",ParseErrorException.class);
86      }
87  
88      public void testParseError2() throws Exception
89      {
90          checkException("parsemain2.vm",ParseErrorException.class);
91      }
92  
93  
94      /**
95       * Check that an exception is thrown for the given template
96       * @param templateName
97       * @param exceptionClass
98       * @throws Exception
99       */
100     private void checkException(String templateName,Class exceptionClass)
101     throws Exception
102     {
103         Context context = new VelocityContext();
104         StringWriter writer = new StringWriter();
105         Template template = ve.getTemplate(templateName, "UTF-8");
106 
107         try
108         {
109             template.merge(context, writer);
110             writer.flush();
111             fail("File should have thrown an exception");
112         }
113         catch (Exception E)
114         {
115             assertTrue(exceptionClass.isAssignableFrom(E.getClass()));
116         }
117         finally
118         {
119             writer.close();
120         }
121 
122     }
123 
124 }