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.runtime.RuntimeConstants;
34  import org.apache.velocity.test.misc.TestLogChute;
35  
36  /**
37   * Multiple paths in the file resource loader.
38   *
39   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
40   * @version $Id: MultipleFileResourcePathTestCase.java 832247 2009-11-03 01:29:30Z wglass $
41   */
42  public class MultipleFileResourcePathTestCase extends BaseTestCase
43  {
44  
45      /**
46       * Path for templates. This property will override the
47       * value in the default velocity properties file.
48       */
49      private final static String FILE_RESOURCE_LOADER_PATH1 = TEST_COMPARE_DIR + "/multi/path1";
50  
51      /**
52       * Path for templates. This property will override the
53       * value in the default velocity properties file.
54       */
55      private final static String FILE_RESOURCE_LOADER_PATH2 = TEST_COMPARE_DIR + "/multi/path2";
56  
57      /**
58       * Results relative to the build directory.
59       */
60      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/multi";
61  
62      /**
63       * Results relative to the build directory.
64       */
65      private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/multi/compare";
66  
67      VelocityEngine engine;
68      
69      /**
70       * Default constructor.
71       */
72      public MultipleFileResourcePathTestCase(String name)
73      {
74          super(name);
75      }
76  
77      public static Test suite ()
78      {
79          return new TestSuite(MultipleFileResourcePathTestCase.class);
80      }
81  
82      public void setUp()
83              throws Exception
84      {
85          assureResultsDirectoryExists(RESULTS_DIR);
86  
87          engine = new VelocityEngine();
88          
89          engine.addProperty(
90                  RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH1);
91  
92          engine.addProperty(
93                  RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH2);
94  
95          engine.setProperty(
96                  RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
97  
98          engine.init();
99      }
100 
101     /**
102      * Runs the test.
103      */
104     public void  testMultipleFileResources ()
105             throws Exception
106     {
107         Template template1 = engine.getTemplate(
108             getFileName(null, "path1", TMPL_FILE_EXT));
109 
110         Template template2 = engine.getTemplate(
111             getFileName(null, "path2", TMPL_FILE_EXT));
112 
113         FileOutputStream fos1 =
114             new FileOutputStream (
115                 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
116 
117         FileOutputStream fos2 =
118             new FileOutputStream (
119                 getFileName(RESULTS_DIR, "path2", RESULT_FILE_EXT));
120 
121         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
122         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
123 
124         /*
125          *  put the Vector into the context, and merge both
126          */
127 
128         VelocityContext context = new VelocityContext();
129 
130         template1.merge(context, writer1);
131         writer1.flush();
132         writer1.close();
133 
134         template2.merge(context, writer2);
135         writer2.flush();
136         writer2.close();
137 
138         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
139                 RESULT_FILE_EXT, CMP_FILE_EXT) ||
140             !isMatch(RESULTS_DIR, COMPARE_DIR, "path2",
141                 RESULT_FILE_EXT, CMP_FILE_EXT))
142         {
143             fail("Output incorrect.");
144         }
145     }
146 }