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  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.velocity.Template;
31  import org.apache.velocity.VelocityContext;
32  import org.apache.velocity.app.VelocityEngine;
33  import org.apache.velocity.context.Context;
34  import org.apache.velocity.runtime.RuntimeConstants;
35  
36  /**
37   * Test that an instance of a ResourceLoader can be successfully passed in.
38   *
39   * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
40   * @version $Id: SetTestCase.java 463298 2006-10-12 16:10:32Z henning $
41   */
42  public class SetTestCase extends BaseTestCase
43  {
44      /**
45       * VTL file extension.
46       */
47      private static final String TMPL_FILE_EXT = "vm";
48  
49      /**
50       * Comparison file extension.
51       */
52      private static final String CMP_FILE_EXT = "cmp";
53  
54      /**
55       * Comparison file extension.
56       */
57      private static final String RESULT_FILE_EXT = "res";
58  
59      /**
60       * Path for templates. This property will override the
61       * value in the default velocity properties file.
62       */
63      private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/set";
64  
65      /**
66       * Results relative to the build directory.
67       */
68      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/set";
69  
70      /**
71       * Results relative to the build directory.
72       */
73      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/set/compare";
74  
75      /**
76       * Default constructor.
77       */
78      public SetTestCase(String name)
79      {
80          super(name);
81      }
82  
83      public void setUp()
84              throws Exception
85      {
86          assureResultsDirectoryExists(RESULTS_DIR);
87      }
88  
89      public static Test suite ()
90      {
91          return new TestSuite(SetTestCase.class);
92      }
93  
94      /**
95       * Runs the test.
96       */
97      public void testSetNull()
98              throws Exception
99      {
100         /**
101          * Check that #set does not accept nulls
102          */
103 
104         VelocityEngine ve = new VelocityEngine();
105         ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
106         ve.init();
107 
108         checkTemplate(ve,"set1");
109 
110         /**
111          * Check that setting the property is the same as the default
112          */
113         ve = new VelocityEngine();
114         ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "false");
115         ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
116         ve.init();
117 
118         checkTemplate(ve,"set1");
119 
120         /**
121          * Check that #set can accept nulls
122          */
123         ve = new VelocityEngine();
124         ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "true");
125         ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
126         ve.init();
127 
128         checkTemplate(ve,"set2");
129     }
130 
131     public void checkTemplate(VelocityEngine ve, String templateName)
132     throws Exception
133     {
134         Template template;
135         FileOutputStream fos;
136         Writer fwriter;
137         Context context;
138 
139         template = ve.getTemplate( getFileName(null, templateName, TMPL_FILE_EXT) );
140 
141         fos = new FileOutputStream (
142                 getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
143 
144         fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
145 
146         context = new VelocityContext();
147         template.merge(context, fwriter);
148         fwriter.flush();
149         fwriter.close();
150 
151         if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
152         {
153             fail("Output incorrect.");
154         }
155     }
156 
157 }
158