KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileWriter JavaDoc;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.velocity.runtime.configuration.Configuration;
25
26 /**
27  * Tests for the Configuration class.
28  *
29  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
30  * @version $Id: ConfigurationTestCase.java,v 1.5.4.1 2004/03/03 23:23:04 geirm Exp $
31  *
32  * @deprecated Will be removed when Configuration class is removed
33  */

34 public class ConfigurationTestCase extends BaseTestCase
35 {
36     /**
37      * Comparison directory.
38      */

39     private static final String JavaDoc COMPARE_DIR =
40         "../test/configuration/compare";
41     
42     /**
43      * Results directory.
44      */

45     private static final String JavaDoc RESULTS_DIR =
46         "../test/configuration/results";
47
48     /**
49      * Test configuration
50      */

51     private static final String JavaDoc TEST_CONFIG =
52         "../test/configuration/test.config";
53
54     /**
55      * Creates a new instance.
56      *
57      */

58     public ConfigurationTestCase()
59     {
60         super("ConfigurationTestCase");
61     }
62
63     public static junit.framework.Test suite()
64     {
65         return new ConfigurationTestCase();
66     }
67
68     /**
69      * Runs the test.
70      */

71     public void runTest ()
72     {
73         try
74         {
75             assureResultsDirectoryExists(RESULTS_DIR);
76             
77             Configuration c = new Configuration(TEST_CONFIG);
78             
79             FileWriter JavaDoc result = new FileWriter JavaDoc(
80                 getFileName(RESULTS_DIR, "output", "res"));
81             
82             message(result, "Testing order of keys ...");
83             showIterator(result, c.getKeys());
84             
85             message(result, "Testing retrieval of CSV values ...");
86             showVector(result, c.getVector("resource.loader"));
87
88             message(result, "Testing subset(prefix).getKeys() ...");
89             Configuration subset = c.subset("file.resource.loader");
90             showIterator(result, subset.getKeys());
91
92             message(result, "Testing getVector(prefix) ...");
93             showVector(result, subset.getVector("path"));
94
95             message(result, "Testing getString(key) ...");
96             result.write(c.getString("config.string.value"));
97             result.write("\n\n");
98
99             message(result, "Testing getBoolean(key) ...");
100             result.write(new Boolean JavaDoc(c.getBoolean("config.boolean.value")).toString());
101             result.write("\n\n");
102
103             message(result, "Testing getByte(key) ...");
104             result.write(new Byte JavaDoc(c.getByte("config.byte.value")).toString());
105             result.write("\n\n");
106
107             message(result, "Testing getShort(key) ...");
108             result.write(new Short JavaDoc(c.getShort("config.short.value")).toString());
109             result.write("\n\n");
110
111             message(result, "Testing getInt(key) ...");
112             result.write(new Integer JavaDoc(c.getInt("config.int.value")).toString());
113             result.write("\n\n");
114
115             message(result, "Testing getLong(key) ...");
116             result.write(new Long JavaDoc(c.getLong("config.long.value")).toString());
117             result.write("\n\n");
118
119             message(result, "Testing getFloat(key) ...");
120             result.write(new Float JavaDoc(c.getFloat("config.float.value")).toString());
121             result.write("\n\n");
122
123             message(result, "Testing getDouble(key) ...");
124             result.write(new Double JavaDoc(c.getDouble("config.double.value")).toString());
125             result.write("\n\n");
126
127             message(result, "Testing escaped-comma scalar...");
128             result.write( c.getString("escape.comma1"));
129             result.write("\n\n");
130
131             message(result, "Testing escaped-comma vector...");
132             showVector(result, c.getVector("escape.comma2"));
133             result.write("\n\n");
134
135             result.flush();
136             result.close();
137             
138             if (!isMatch(RESULTS_DIR, COMPARE_DIR, "output","res","cmp"))
139             {
140                 fail("Output incorrect.");
141             }
142         }
143         catch (Exception JavaDoc e)
144         {
145             System.err.println("Cannot setup ConfigurationTestCase!");
146             e.printStackTrace();
147             System.exit(1);
148         }
149     }
150
151     private void showIterator(FileWriter JavaDoc result, Iterator JavaDoc i)
152         throws Exception JavaDoc
153     {
154         while(i.hasNext())
155         {
156             result.write((String JavaDoc) i.next());
157             result.write("\n");
158         }
159         result.write("\n");
160     }
161
162     private void showVector(FileWriter JavaDoc result, Vector JavaDoc v)
163         throws Exception JavaDoc
164     {
165         for (int j = 0; j < v.size(); j++)
166         {
167             result.write((String JavaDoc) v.get(j));
168             result.write("\n");
169         }
170         result.write("\n");
171     }
172
173     private void message(FileWriter JavaDoc result, String JavaDoc message)
174         throws Exception JavaDoc
175     {
176         result.write("--------------------------------------------------\n");
177         result.write(message + "\n");
178         result.write("--------------------------------------------------\n");
179         result.write("\n");
180     }
181 }
182
Popular Tags