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.Velocity;
33  import org.apache.velocity.runtime.RuntimeSingleton;
34  import org.apache.velocity.runtime.log.NullLogChute;
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 477002 2006-11-20 01:07:43Z henning $
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      /**
68       * Default constructor.
69       */
70      public MultipleFileResourcePathTestCase(String name)
71      {
72          super(name);
73      }
74  
75      public static Test suite ()
76      {
77          return new TestSuite(MultipleFileResourcePathTestCase.class);
78      }
79  
80      public void setUp()
81              throws Exception
82      {
83          assureResultsDirectoryExists(RESULTS_DIR);
84  
85          Velocity.addProperty(
86                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH1);
87  
88          Velocity.addProperty(
89                  Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH2);
90  
91          Velocity.setProperty(
92                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
93  
94          Velocity.init();
95      }
96  
97      /**
98       * Runs the test.
99       */
100     public void  testMultipleFileResources ()
101             throws Exception
102     {
103         Template template1 = RuntimeSingleton.getTemplate(
104             getFileName(null, "path1", TMPL_FILE_EXT));
105 
106         Template template2 = RuntimeSingleton.getTemplate(
107             getFileName(null, "path2", TMPL_FILE_EXT));
108 
109         FileOutputStream fos1 =
110             new FileOutputStream (
111                 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
112 
113         FileOutputStream fos2 =
114             new FileOutputStream (
115                 getFileName(RESULTS_DIR, "path2", RESULT_FILE_EXT));
116 
117         Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
118         Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
119 
120         /*
121          *  put the Vector into the context, and merge both
122          */
123 
124         VelocityContext context = new VelocityContext();
125 
126         template1.merge(context, writer1);
127         writer1.flush();
128         writer1.close();
129 
130         template2.merge(context, writer2);
131         writer2.flush();
132         writer2.close();
133 
134         if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
135                 RESULT_FILE_EXT, CMP_FILE_EXT) ||
136             !isMatch(RESULTS_DIR, COMPARE_DIR, "path2",
137                 RESULT_FILE_EXT, CMP_FILE_EXT))
138         {
139             fail("Output incorrect.");
140         }
141     }
142 }