1 17 package org.apache.tools.ant.taskdefs.optional; 18 19 import org.apache.tools.ant.BuildFileTest; 20 21 import java.util.Properties ; 22 import java.io.FileOutputStream ; 23 import java.io.FileInputStream ; 24 import java.io.InputStream ; 25 import java.io.File ; 26 27 34 35 public class PropertyFileTest extends BuildFileTest { 36 37 public PropertyFileTest(String name) { 38 super(name); 39 } 40 41 42 45 public void setUp() throws Exception { 46 destroyTempFiles(); 47 initTestPropFile(); 48 initBuildPropFile(); 49 configureProject(projectFilePath); 50 project.setProperty(valueDoesNotGetOverwrittenPropertyFileKey,valueDoesNotGetOverwrittenPropertyFile); 51 } 52 53 54 57 public void tearDown() { 58 destroyTempFiles(); 59 } 60 61 public void testNonExistingFile() { 62 PropertyFile props = new PropertyFile(); 63 props.setProject( getProject() ); 64 File file = new File ("this-file-does-not-exist.properties"); 65 props.setFile(file); 66 assertFalse("Properties file exists before test.", file.exists()); 67 props.execute(); 68 assertTrue("Properties file does not exist after test.", file.exists()); 69 file.delete(); 70 } 71 72 76 public void testUpdatesExistingProperties() throws Exception { 77 Properties beforeUpdate = getTestProperties(); 78 assertEquals(FNAME, beforeUpdate.getProperty(FNAME_KEY)); 79 assertEquals(LNAME, beforeUpdate.getProperty(LNAME_KEY)); 80 assertEquals(EMAIL, beforeUpdate.getProperty(EMAIL_KEY)); 81 assertEquals(null, beforeUpdate.getProperty(PHONE_KEY)); 82 assertEquals(null, beforeUpdate.getProperty(AGE_KEY)); 83 assertEquals(null, beforeUpdate.getProperty(DATE_KEY)); 84 85 executeTarget("update-existing-properties"); 87 88 Properties afterUpdate = getTestProperties(); 89 assertEquals(NEW_FNAME, afterUpdate.getProperty(FNAME_KEY)); 90 assertEquals(NEW_LNAME, afterUpdate.getProperty(LNAME_KEY)); 91 assertEquals(NEW_EMAIL, afterUpdate.getProperty(EMAIL_KEY)); 92 assertEquals(NEW_PHONE, afterUpdate.getProperty(PHONE_KEY)); 93 assertEquals(NEW_AGE, afterUpdate.getProperty(AGE_KEY)); 94 assertEquals(NEW_DATE, afterUpdate.getProperty(DATE_KEY)); 95 } 96 97 public void testExerciseDefaultAndIncrement() throws Exception { 98 executeTarget("exercise"); 99 assertEquals("3",project.getProperty("int.with.default")); 100 assertEquals("1",project.getProperty("int.without.default")); 101 assertEquals("-->",project.getProperty("string.with.default")); 102 assertEquals(".",project.getProperty("string.without.default")); 103 assertEquals("2002/01/21 12:18", project.getProperty("ethans.birth")); 104 assertEquals("2003/01/21", project.getProperty("first.birthday")); 105 assertEquals("0124", project.getProperty("olderThanAWeek")); 106 assertEquals("37", project.getProperty("existing.prop")); 107 assertEquals("6",project.getProperty("int.without.value")); 108 } 109 110 public void testValueDoesNotGetOverwritten() { 111 executeTarget("bugDemo1"); 113 executeTarget("bugDemo2"); 114 assertEquals("5", project.getProperty("foo")); 115 } 116 139 140 private Properties getTestProperties() throws Exception { 141 Properties testProps = new Properties (); 142 FileInputStream propsFile = new FileInputStream (testPropsFilePath); 143 testProps.load(propsFile); 144 propsFile.close(); 145 return testProps; 146 } 147 148 149 private void initTestPropFile() throws Exception { 150 Properties testProps = new Properties (); 151 testProps.put(FNAME_KEY, FNAME); 152 testProps.put(LNAME_KEY, LNAME); 153 testProps.put(EMAIL_KEY, EMAIL); 154 testProps.put("existing.prop", "37"); 155 156 FileOutputStream fos = new FileOutputStream (testPropsFilePath); 157 testProps.store(fos, "defaults"); 158 fos.close(); 159 } 160 161 162 private void initBuildPropFile() throws Exception { 163 Properties buildProps = new Properties (); 164 buildProps.put(testPropertyFileKey, testPropertyFile); 165 buildProps.put(FNAME_KEY, NEW_FNAME); 166 buildProps.put(LNAME_KEY, NEW_LNAME); 167 buildProps.put(EMAIL_KEY, NEW_EMAIL); 168 buildProps.put(PHONE_KEY, NEW_PHONE); 169 buildProps.put(AGE_KEY, NEW_AGE); 170 buildProps.put(DATE_KEY, NEW_DATE); 171 172 FileOutputStream fos = new FileOutputStream (buildPropsFilePath); 173 buildProps.store(fos, null); 174 fos.close(); 175 } 176 177 178 private void destroyTempFiles() { 179 File tempFile = new File (testPropsFilePath); 180 tempFile.delete(); 181 tempFile = null; 182 183 tempFile = new File (buildPropsFilePath); 184 tempFile.delete(); 185 tempFile = null; 186 187 tempFile = new File (valueDoesNotGetOverwrittenPropsFilePath); 188 tempFile.delete(); 189 tempFile = null; 190 } 191 192 193 194 private static final String 195 projectFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.xml", 196 197 testPropertyFile = "propertyfile.test.properties", 198 testPropertyFileKey = "test.propertyfile", 199 testPropsFilePath = "src/etc/testcases/taskdefs/optional/" + testPropertyFile, 200 201 valueDoesNotGetOverwrittenPropertyFile = "overwrite.test.properties", 202 valueDoesNotGetOverwrittenPropertyFileKey = "overwrite.test.propertyfile", 203 valueDoesNotGetOverwrittenPropsFilePath = "src/etc/testcases/taskdefs/optional/" + valueDoesNotGetOverwrittenPropertyFile, 204 205 buildPropsFilePath = "src/etc/testcases/taskdefs/optional/propertyfile.build.properties", 206 207 FNAME = "Bruce", 208 NEW_FNAME = "Clark", 209 FNAME_KEY = "firstname", 210 211 LNAME = "Banner", 212 NEW_LNAME = "Kent", 213 LNAME_KEY = "lastname", 214 215 EMAIL = "incredible@hulk.com", 216 NEW_EMAIL = "kc@superman.com", 217 EMAIL_KEY = "email", 218 219 NEW_PHONE = "(520) 555-1212", 220 PHONE_KEY = "phone", 221 222 NEW_AGE = "30", 223 AGE_KEY = "age", 224 225 NEW_DATE = "2001/01/01 12:45", 226 DATE_KEY = "date"; 227 } 228 | Popular Tags |