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.velocity.runtime.configuration.Configuration; 25 26 34 public class ConfigurationTestCase extends BaseTestCase 35 { 36 39 private static final String COMPARE_DIR = 40 "../test/configuration/compare"; 41 42 45 private static final String RESULTS_DIR = 46 "../test/configuration/results"; 47 48 51 private static final String TEST_CONFIG = 52 "../test/configuration/test.config"; 53 54 58 public ConfigurationTestCase() 59 { 60 super("ConfigurationTestCase"); 61 } 62 63 public static junit.framework.Test suite() 64 { 65 return new ConfigurationTestCase(); 66 } 67 68 71 public void runTest () 72 { 73 try 74 { 75 assureResultsDirectoryExists(RESULTS_DIR); 76 77 Configuration c = new Configuration(TEST_CONFIG); 78 79 FileWriter result = new FileWriter ( 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 (c.getBoolean("config.boolean.value")).toString()); 101 result.write("\n\n"); 102 103 message(result, "Testing getByte(key) ..."); 104 result.write(new Byte (c.getByte("config.byte.value")).toString()); 105 result.write("\n\n"); 106 107 message(result, "Testing getShort(key) ..."); 108 result.write(new Short (c.getShort("config.short.value")).toString()); 109 result.write("\n\n"); 110 111 message(result, "Testing getInt(key) ..."); 112 result.write(new Integer (c.getInt("config.int.value")).toString()); 113 result.write("\n\n"); 114 115 message(result, "Testing getLong(key) ..."); 116 result.write(new Long (c.getLong("config.long.value")).toString()); 117 result.write("\n\n"); 118 119 message(result, "Testing getFloat(key) ..."); 120 result.write(new Float (c.getFloat("config.float.value")).toString()); 121 result.write("\n\n"); 122 123 message(result, "Testing getDouble(key) ..."); 124 result.write(new Double (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 e) 144 { 145 System.err.println("Cannot setup ConfigurationTestCase!"); 146 e.printStackTrace(); 147 System.exit(1); 148 } 149 } 150 151 private void showIterator(FileWriter result, Iterator i) 152 throws Exception 153 { 154 while(i.hasNext()) 155 { 156 result.write((String ) i.next()); 157 result.write("\n"); 158 } 159 result.write("\n"); 160 } 161 162 private void showVector(FileWriter result, Vector v) 163 throws Exception 164 { 165 for (int j = 0; j < v.size(); j++) 166 { 167 result.write((String ) v.get(j)); 168 result.write("\n"); 169 } 170 result.write("\n"); 171 } 172 173 private void message(FileWriter result, String message) 174 throws Exception 175 { 176 result.write("--------------------------------------------------\n"); 177 result.write(message + "\n"); 178 result.write("--------------------------------------------------\n"); 179 result.write("\n"); 180 } 181 } 182 | Popular Tags |