FileDocCategorySizeDatePackage
PropertyFileTest.javaAPI DocApache Ant 1.708296Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional

PropertyFileTest

public class PropertyFileTest extends org.apache.tools.ant.BuildFileTest
JUnit testcase that excercises the optional PropertyFile task in ant. (this is really more of a functional test so far.., but it's enough to let me start refactoring...)
created
October 2, 2001

Fields Summary
private static final String
projectFilePath
private static final String
testPropertyFile
private static final String
testPropertyFileKey
private static final String
testPropsFilePath
private static final String
valueDoesNotGetOverwrittenPropertyFile
private static final String
valueDoesNotGetOverwrittenPropertyFileKey
private static final String
valueDoesNotGetOverwrittenPropsFilePath
private static final String
buildPropsFilePath
private static final String
FNAME
private static final String
NEW_FNAME
private static final String
FNAME_KEY
private static final String
LNAME
private static final String
NEW_LNAME
private static final String
LNAME_KEY
private static final String
EMAIL
private static final String
NEW_EMAIL
private static final String
EMAIL_KEY
private static final String
NEW_PHONE
private static final String
PHONE_KEY
private static final String
NEW_AGE
private static final String
AGE_KEY
private static final String
NEW_DATE
private static final String
DATE_KEY
Constructors Summary
public PropertyFileTest(String name)

        super(name);
    
Methods Summary
private voiddestroyTempFiles()

        File tempFile = new File(System.getProperty("root"), testPropsFilePath);
        tempFile.delete();
        tempFile = null;

        tempFile = new File(System.getProperty("root"), buildPropsFilePath);
        tempFile.delete();
        tempFile = null;

        tempFile = new File(System.getProperty("root"), valueDoesNotGetOverwrittenPropsFilePath);
        tempFile.delete();
        tempFile = null;
    
private java.util.PropertiesgetTestProperties()

        Properties testProps = new Properties();
        FileInputStream propsFile = new FileInputStream(new File(System.getProperty("root"), testPropsFilePath));
        testProps.load(propsFile);
        propsFile.close();
        return testProps;
    
private voidinitBuildPropFile()

        Properties buildProps = new Properties();
        buildProps.put(testPropertyFileKey, testPropertyFile);
        buildProps.put(FNAME_KEY, NEW_FNAME);
        buildProps.put(LNAME_KEY, NEW_LNAME);
        buildProps.put(EMAIL_KEY, NEW_EMAIL);
        buildProps.put(PHONE_KEY, NEW_PHONE);
        buildProps.put(AGE_KEY, NEW_AGE);
        buildProps.put(DATE_KEY, NEW_DATE);

        FileOutputStream fos = new FileOutputStream(new File(System.getProperty("root"), buildPropsFilePath));
        buildProps.store(fos, null);
        fos.close();
    
private voidinitTestPropFile()

        Properties testProps = new Properties();
        testProps.put(FNAME_KEY, FNAME);
        testProps.put(LNAME_KEY, LNAME);
        testProps.put(EMAIL_KEY, EMAIL);
        testProps.put("existing.prop", "37");

        FileOutputStream fos = new FileOutputStream(new File(System.getProperty("root"), testPropsFilePath));
        testProps.store(fos, "defaults");
        fos.close();
    
public voidsetUp()
The JUnit setup method

        destroyTempFiles();
        initTestPropFile();
        initBuildPropFile();
        configureProject(projectFilePath);
        project.setProperty(valueDoesNotGetOverwrittenPropertyFileKey,valueDoesNotGetOverwrittenPropertyFile);
    
public voidtearDown()
The JUnit tearDown method

        destroyTempFiles();
    
public voidtestExerciseDefaultAndIncrement()

        executeTarget("exercise");
        assertEquals("3",project.getProperty("int.with.default"));
        assertEquals("1",project.getProperty("int.without.default"));
        assertEquals("-->",project.getProperty("string.with.default"));
        assertEquals(".",project.getProperty("string.without.default"));
        assertEquals("2002/01/21 12:18", project.getProperty("ethans.birth"));
        assertEquals("2003/01/21", project.getProperty("first.birthday"));
        assertEquals("0124", project.getProperty("olderThanAWeek"));
        assertEquals("37", project.getProperty("existing.prop"));
        assertEquals("6",project.getProperty("int.without.value"));
    
public voidtestNonExistingFile()

        PropertyFile props = new PropertyFile();
        props.setProject( getProject() );
        File file = new File("this-file-does-not-exist.properties");
        props.setFile(file);
        assertFalse("Properties file exists before test.", file.exists());
        props.execute();
        assertTrue("Properties file does not exist after test.", file.exists());
        file.delete();
    
public voidtestUpdatesExistingProperties()
A unit test for JUnit- Excercises the propertyfile tasks ability to update properties that are already defined-

        Properties beforeUpdate = getTestProperties();
        assertEquals(FNAME, beforeUpdate.getProperty(FNAME_KEY));
        assertEquals(LNAME, beforeUpdate.getProperty(LNAME_KEY));
        assertEquals(EMAIL, beforeUpdate.getProperty(EMAIL_KEY));
        assertEquals(null, beforeUpdate.getProperty(PHONE_KEY));
        assertEquals(null, beforeUpdate.getProperty(AGE_KEY));
        assertEquals(null, beforeUpdate.getProperty(DATE_KEY));

        // ask ant to update the properties...
        executeTarget("update-existing-properties");

        Properties afterUpdate = getTestProperties();
        assertEquals(NEW_FNAME, afterUpdate.getProperty(FNAME_KEY));
        assertEquals(NEW_LNAME, afterUpdate.getProperty(LNAME_KEY));
        assertEquals(NEW_EMAIL, afterUpdate.getProperty(EMAIL_KEY));
        assertEquals(NEW_PHONE, afterUpdate.getProperty(PHONE_KEY));
        assertEquals(NEW_AGE, afterUpdate.getProperty(AGE_KEY));
        assertEquals(NEW_DATE, afterUpdate.getProperty(DATE_KEY));
    
public voidtestValueDoesNotGetOverwritten()

        // this test shows that the bug report 21505 is fixed
        executeTarget("bugDemo1");
        executeTarget("bugDemo2");
        assertEquals("5", project.getProperty("foo"));