KickJava   Java API By Example, From Geeks To Geeks.

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


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.commons.collections.ExtendedProperties;
25
26
27 /**
28  * Tests for the Commons ExtendedProperties class. This is an identical
29  * copy of the ConfigurationTestCase, which will disappear when
30  * the Configuration class does
31  *
32  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
33  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34  * @version $Id: CommonsExtPropTestCase.java,v 1.2.4.1 2004/03/03 23:23:04 geirm Exp $
35  */

36 public class CommonsExtPropTestCase extends BaseTestCase
37 {
38     /**
39      * Comparison directory.
40      */

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

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

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

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

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