001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.FileWriter;
023: import java.util.Iterator;
024: import java.util.Vector;
025:
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.collections.ExtendedProperties;
029:
030: /**
031: * Tests for the Commons ExtendedProperties class. This is an identical
032: * copy of the ConfigurationTestCase, which will disappear when
033: * the Configuration class does
034: *
035: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
036: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
037: * @version $Id: CommonsExtPropTestCase.java 463298 2006-10-12 16:10:32Z henning $
038: */
039: public class CommonsExtPropTestCase extends BaseTestCase {
040: /**
041: * Comparison directory.
042: */
043: private static final String COMPARE_DIR = TEST_COMPARE_DIR
044: + "/configuration/compare";
045:
046: /**
047: * Results directory.
048: */
049: private static final String RESULTS_DIR = TEST_RESULT_DIR
050: + "/configuration";
051:
052: /**
053: * Test configuration
054: */
055: private static final String TEST_CONFIG = TEST_COMPARE_DIR
056: + "/configuration/test-config.properties";
057:
058: /**
059: * Creates a new instance.
060: *
061: */
062: public CommonsExtPropTestCase(String name) {
063: super (name);
064: }
065:
066: public static junit.framework.Test suite() {
067: return new TestSuite(CommonsExtPropTestCase.class);
068: }
069:
070: /**
071: * Runs the test.
072: */
073: public void testExtendedProperties() throws Exception {
074: assureResultsDirectoryExists(RESULTS_DIR);
075:
076: ExtendedProperties c = new ExtendedProperties(TEST_CONFIG);
077:
078: FileWriter result = new FileWriter(getFileName(RESULTS_DIR,
079: "output", "res"));
080:
081: message(result, "Testing order of keys ...");
082: showIterator(result, c.getKeys());
083:
084: message(result, "Testing retrieval of CSV values ...");
085: showVector(result, c.getVector("resource.loader"));
086:
087: message(result, "Testing subset(prefix).getKeys() ...");
088: ExtendedProperties subset = c.subset("file.resource.loader");
089: showIterator(result, subset.getKeys());
090:
091: message(result, "Testing getVector(prefix) ...");
092: showVector(result, subset.getVector("path"));
093:
094: message(result, "Testing getString(key) ...");
095: result.write(c.getString("config.string.value"));
096: result.write("\n\n");
097:
098: message(result, "Testing getBoolean(key) ...");
099: result.write(new Boolean(c.getBoolean("config.boolean.value"))
100: .toString());
101: result.write("\n\n");
102:
103: message(result, "Testing getByte(key) ...");
104: result.write(new Byte(c.getByte("config.byte.value"))
105: .toString());
106: result.write("\n\n");
107:
108: message(result, "Testing getShort(key) ...");
109: result.write(new Short(c.getShort("config.short.value"))
110: .toString());
111: result.write("\n\n");
112:
113: message(result, "Testing getInt(key) ...");
114: result.write(new Integer(c.getInt("config.int.value"))
115: .toString());
116: result.write("\n\n");
117:
118: message(result, "Testing getLong(key) ...");
119: result.write(new Long(c.getLong("config.long.value"))
120: .toString());
121: result.write("\n\n");
122:
123: message(result, "Testing getFloat(key) ...");
124: result.write(new Float(c.getFloat("config.float.value"))
125: .toString());
126: result.write("\n\n");
127:
128: message(result, "Testing getDouble(key) ...");
129: result.write(new Double(c.getDouble("config.double.value"))
130: .toString());
131: result.write("\n\n");
132:
133: message(result, "Testing escaped-comma scalar...");
134: result.write(c.getString("escape.comma1"));
135: result.write("\n\n");
136:
137: message(result, "Testing escaped-comma vector...");
138: showVector(result, c.getVector("escape.comma2"));
139: result.write("\n\n");
140:
141: result.flush();
142: result.close();
143:
144: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "output", "res", "cmp")) {
145: fail("Output incorrect.");
146: }
147: }
148:
149: private void showIterator(FileWriter result, Iterator i)
150: throws Exception {
151: while (i.hasNext()) {
152: result.write((String) i.next());
153: result.write("\n");
154: }
155: result.write("\n");
156: }
157:
158: private void showVector(FileWriter result, Vector v)
159: throws Exception {
160: for (int j = 0; j < v.size(); j++) {
161: result.write((String) v.get(j));
162: result.write("\n");
163: }
164: result.write("\n");
165: }
166:
167: private void message(FileWriter result, String message)
168: throws Exception {
169: result
170: .write("--------------------------------------------------\n");
171: result.write(message + "\n");
172: result
173: .write("--------------------------------------------------\n");
174: result.write("\n");
175: }
176: }
|