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.test.misc.UberspectTestException;
33  import org.apache.velocity.util.introspection.Info;
34  
35  
36  
37  
38  /**
39   * Test that the Info class in the Introspector holds the correct information.
40   *
41   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
42   * @author <a href="mailto:isidore@setgame.com">Llewellyn Falco</a>
43   * @version $Id: InfoTestCase.java 463298 2006-10-12 16:10:32Z henning $
44   */
45  public class InfoTestCase extends BaseTestCase implements TemplateTestBase
46  {
47      VelocityEngine ve;
48  
49      /**
50       * Default constructor.
51       */
52      public InfoTestCase(String name)
53      {
54          super(name);
55      }
56  
57      public static Test suite ()
58      {
59          return new TestSuite(InfoTestCase.class);
60      }
61  
62      public void setUp() throws Exception
63      {
64          ve = new VelocityEngine();
65          ve.setProperty(
66                  "runtime.introspector.uberspect", "org.apache.velocity.test.misc.UberspectTestImpl");
67  
68          ve.setProperty(
69                  Velocity.FILE_RESOURCE_LOADER_PATH, "test/info");
70  
71          ve.init();
72      }
73  
74  
75  
76      public void testInfoProperty() throws Exception
77      {
78          // check property
79          checkInfo("info1.vm", 1, 7);
80      }
81  
82      public void testInfoMethod() throws Exception
83      {
84          // check method
85          checkInfo("info2.vm", 1, 7);
86      }
87  
88      public void checkInfo(String templateName,
89              int expectedLine, int expectedCol) throws Exception
90      {
91          Context context = new VelocityContext();
92          StringWriter writer = new StringWriter();
93          Template template = ve.getTemplate(templateName, "UTF-8");
94          Info info = null;
95  
96          context.put("main", this);
97  
98          try
99          {
100             template.merge(context, writer);
101             writer.flush();
102             fail("Uberspect should have thrown an exception");
103         }
104         catch (UberspectTestException E)
105         {
106             info = E.getInfo();
107         }
108         finally
109         {
110             writer.close();
111         }
112         assertInfoEqual(info, templateName, expectedLine, expectedCol);
113 
114     }
115 
116     private void assertInfoEqual(Info i, String name, int line, int column)
117     {
118         assertEquals("Template Name", name, i.getTemplateName());
119         assertEquals("Template Line", line, i.getLine());
120         assertEquals("Template Column", column, i.getColumn());
121     }
122 
123 }