KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > MultipleFileResourcePathTest


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.File JavaDoc;
24
25 import java.util.Properties JavaDoc;
26
27 import org.apache.velocity.VelocityContext;
28
29 import org.apache.velocity.Template;
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.runtime.RuntimeSingleton;
32 import org.apache.velocity.test.provider.TestProvider;
33 import org.apache.velocity.util.StringUtils;
34 import org.apache.velocity.runtime.VelocimacroFactory;
35
36 import junit.framework.TestCase;
37
38 /**
39  * Multiple paths in the file resource loader.
40  *
41  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
42  * @version $Id: MultipleFileResourcePathTest.java,v 1.8.8.1 2004/03/03 23:23:04 geirm Exp $
43  */

44 public class MultipleFileResourcePathTest extends BaseTestCase
45 {
46      /**
47      * VTL file extension.
48      */

49     private static final String JavaDoc TMPL_FILE_EXT = "vm";
50
51     /**
52      * Comparison file extension.
53      */

54     private static final String JavaDoc CMP_FILE_EXT = "cmp";
55
56     /**
57      * Comparison file extension.
58      */

59     private static final String JavaDoc RESULT_FILE_EXT = "res";
60
61     /**
62      * Path for templates. This property will override the
63      * value in the default velocity properties file.
64      */

65     private final static String JavaDoc FILE_RESOURCE_LOADER_PATH1 = "../test/multi/path1";
66
67     /**
68      * Path for templates. This property will override the
69      * value in the default velocity properties file.
70      */

71     private final static String JavaDoc FILE_RESOURCE_LOADER_PATH2 = "../test/multi/path2";
72
73     /**
74      * Results relative to the build directory.
75      */

76     private static final String JavaDoc RESULTS_DIR = "../test/multi/results";
77
78     /**
79      * Results relative to the build directory.
80      */

81     private static final String JavaDoc COMPARE_DIR = "../test/multi/compare";
82
83     /**
84      * Default constructor.
85      */

86     MultipleFileResourcePathTest()
87     {
88         super("MultipleFileResourcePathTest");
89
90         try
91         {
92             assureResultsDirectoryExists(RESULTS_DIR);
93
94             Velocity.addProperty(
95                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH1);
96
97             Velocity.addProperty(
98                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH2);
99
100             Velocity.init();
101         }
102         catch (Exception JavaDoc e)
103         {
104             System.err.println("Cannot setup MultipleFileResourcePathTest!");
105             e.printStackTrace();
106             System.exit(1);
107         }
108     }
109
110     public static junit.framework.Test suite ()
111     {
112         return new MultipleFileResourcePathTest();
113     }
114
115     /**
116      * Runs the test.
117      */

118     public void runTest ()
119     {
120         try
121         {
122             Template template1 = RuntimeSingleton.getTemplate(
123                 getFileName(null, "path1", TMPL_FILE_EXT));
124             
125             Template template2 = RuntimeSingleton.getTemplate(
126                 getFileName(null, "path2", TMPL_FILE_EXT));
127            
128             FileOutputStream JavaDoc fos1 =
129                 new FileOutputStream JavaDoc (
130                     getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
131
132             FileOutputStream JavaDoc fos2 =
133                 new FileOutputStream JavaDoc (
134                     getFileName(RESULTS_DIR, "path2", RESULT_FILE_EXT));
135
136             Writer JavaDoc writer1 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos1));
137             Writer JavaDoc writer2 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos2));
138             
139             /*
140              * put the Vector into the context, and merge both
141              */

142
143             VelocityContext context = new VelocityContext();
144
145             template1.merge(context, writer1);
146             writer1.flush();
147             writer1.close();
148             
149             template2.merge(context, writer2);
150             writer2.flush();
151             writer2.close();
152
153             if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
154                     RESULT_FILE_EXT, CMP_FILE_EXT) ||
155                 !isMatch(RESULTS_DIR, COMPARE_DIR, "path2",
156                     RESULT_FILE_EXT, CMP_FILE_EXT))
157             {
158                 fail("Output incorrect.");
159             }
160         }
161         catch (Exception JavaDoc e)
162         {
163             fail(e.getMessage());
164         }
165     }
166 }
167
Popular Tags