1 package org.apache.velocity.test; 2 3 18 19 import java.io.FileWriter ; 20 21 import java.util.Iterator ; 22 import java.util.Vector ; 23 24 import org.apache.commons.collections.ExtendedProperties; 25 26 27 36 public class CommonsExtPropTestCase extends BaseTestCase 37 { 38 41 private static final String COMPARE_DIR = 42 "../test/configuration/compare"; 43 44 47 private static final String RESULTS_DIR = 48 "../test/configuration/results"; 49 50 53 private static final String TEST_CONFIG = 54 "../test/configuration/test.config"; 55 56 60 public CommonsExtPropTestCase() 61 { 62 super("CommonsExtPropTestCase"); 63 } 64 65 public static junit.framework.Test suite() 66 { 67 return new CommonsExtPropTestCase(); 68 } 69 70 73 public void runTest () 74 { 75 try 76 { 77 assureResultsDirectoryExists(RESULTS_DIR); 78 79 ExtendedProperties c = new ExtendedProperties(TEST_CONFIG); 80 81 FileWriter result = new FileWriter ( 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 (c.getBoolean("config.boolean.value")).toString()); 103 result.write("\n\n"); 104 105 message(result, "Testing getByte(key) ..."); 106 result.write(new Byte (c.getByte("config.byte.value")).toString()); 107 result.write("\n\n"); 108 109 message(result, "Testing getShort(key) ..."); 110 result.write(new Short (c.getShort("config.short.value")).toString()); 111 result.write("\n\n"); 112 113 message(result, "Testing getInt(key) ..."); 114 result.write(new Integer (c.getInt("config.int.value")).toString()); 115 result.write("\n\n"); 116 117 message(result, "Testing getLong(key) ..."); 118 result.write(new Long (c.getLong("config.long.value")).toString()); 119 result.write("\n\n"); 120 121 message(result, "Testing getFloat(key) ..."); 122 result.write(new Float (c.getFloat("config.float.value")).toString()); 123 result.write("\n\n"); 124 125 message(result, "Testing getDouble(key) ..."); 126 result.write(new Double (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 e) 146 { 147 System.err.println("Cannot setup CommonsExtPropTestCase!"); 148 e.printStackTrace(); 149 System.exit(1); 150 } 151 } 152 153 private void showIterator(FileWriter result, Iterator i) 154 throws Exception 155 { 156 while(i.hasNext()) 157 { 158 result.write((String ) i.next()); 159 result.write("\n"); 160 } 161 result.write("\n"); 162 } 163 164 private void showVector(FileWriter result, Vector v) 165 throws Exception 166 { 167 for (int j = 0; j < v.size(); j++) 168 { 169 result.write((String ) v.get(j)); 170 result.write("\n"); 171 } 172 result.write("\n"); 173 } 174 175 private void message(FileWriter result, String message) 176 throws Exception 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 |