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.FileWriter;
23  import java.util.Iterator;
24  import java.util.Vector;
25  
26  import junit.framework.TestSuite;
27  
28  import org.apache.commons.collections.ExtendedProperties;
29  
30  
31  /**
32   * Tests for the Commons ExtendedProperties class. This is an identical
33   *  copy of the ConfigurationTestCase, which will disappear when
34   *  the Configuration class does
35   *
36   * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
37   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
38   * @version $Id: CommonsExtPropTestCase.java 463298 2006-10-12 16:10:32Z henning $
39   */
40  public class CommonsExtPropTestCase extends BaseTestCase
41  {
42      /**
43       * Comparison directory.
44       */
45      private static final String COMPARE_DIR =
46          TEST_COMPARE_DIR + "/configuration/compare";
47  
48      /**
49       * Results directory.
50       */
51      private static final String RESULTS_DIR =
52          TEST_RESULT_DIR + "/configuration";
53  
54      /**
55       * Test configuration
56       */
57      private static final String TEST_CONFIG =
58          TEST_COMPARE_DIR + "/configuration/test-config.properties";
59  
60      /**
61       * Creates a new instance.
62       *
63       */
64      public CommonsExtPropTestCase(String name)
65      {
66          super(name);
67      }
68  
69      public static junit.framework.Test suite()
70      {
71          return new TestSuite(CommonsExtPropTestCase.class);
72      }
73  
74      /**
75       * Runs the test.
76       */
77      public void testExtendedProperties ()
78              throws Exception
79      {
80              assureResultsDirectoryExists(RESULTS_DIR);
81  
82              ExtendedProperties c = new ExtendedProperties(TEST_CONFIG);
83  
84              FileWriter result = new FileWriter(
85                  getFileName(RESULTS_DIR, "output", "res"));
86  
87              message(result, "Testing order of keys ...");
88              showIterator(result, c.getKeys());
89  
90              message(result, "Testing retrieval of CSV values ...");
91              showVector(result, c.getVector("resource.loader"));
92  
93              message(result, "Testing subset(prefix).getKeys() ...");
94              ExtendedProperties subset = c.subset("file.resource.loader");
95              showIterator(result, subset.getKeys());
96  
97              message(result, "Testing getVector(prefix) ...");
98              showVector(result, subset.getVector("path"));
99  
100             message(result, "Testing getString(key) ...");
101             result.write(c.getString("config.string.value"));
102             result.write("\n\n");
103 
104             message(result, "Testing getBoolean(key) ...");
105             result.write(new Boolean(c.getBoolean("config.boolean.value")).toString());
106             result.write("\n\n");
107 
108             message(result, "Testing getByte(key) ...");
109             result.write(new Byte(c.getByte("config.byte.value")).toString());
110             result.write("\n\n");
111 
112             message(result, "Testing getShort(key) ...");
113             result.write(new Short(c.getShort("config.short.value")).toString());
114             result.write("\n\n");
115 
116             message(result, "Testing getInt(key) ...");
117             result.write(new Integer(c.getInt("config.int.value")).toString());
118             result.write("\n\n");
119 
120             message(result, "Testing getLong(key) ...");
121             result.write(new Long(c.getLong("config.long.value")).toString());
122             result.write("\n\n");
123 
124             message(result, "Testing getFloat(key) ...");
125             result.write(new Float(c.getFloat("config.float.value")).toString());
126             result.write("\n\n");
127 
128             message(result, "Testing getDouble(key) ...");
129             result.write(new Double(c.getDouble("config.double.value")).toString());
130             result.write("\n\n");
131 
132             message(result, "Testing escaped-comma scalar...");
133             result.write( c.getString("escape.comma1"));
134             result.write("\n\n");
135 
136             message(result, "Testing escaped-comma vector...");
137             showVector(result,  c.getVector("escape.comma2"));
138             result.write("\n\n");
139 
140             result.flush();
141             result.close();
142 
143             if (!isMatch(RESULTS_DIR, COMPARE_DIR, "output","res","cmp"))
144             {
145                 fail("Output incorrect.");
146             }
147     }
148 
149     private void showIterator(FileWriter result, Iterator i)
150         throws Exception
151     {
152         while(i.hasNext())
153         {
154             result.write((String) i.next());
155             result.write("\n");
156         }
157         result.write("\n");
158     }
159 
160     private void showVector(FileWriter result, Vector v)
161         throws Exception
162     {
163         for (int j = 0; j < v.size(); j++)
164         {
165             result.write((String) v.get(j));
166             result.write("\n");
167         }
168         result.write("\n");
169     }
170 
171     private void message(FileWriter result, String message)
172         throws Exception
173     {
174         result.write("--------------------------------------------------\n");
175         result.write(message + "\n");
176         result.write("--------------------------------------------------\n");
177         result.write("\n");
178     }
179 }
180 
181 
182 
183 
184 
185 
186 
187