alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 * Copyright 2003,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.io;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.testtools.*;

/**
 * This is used to test FileUtils for correctness.
 *
 * @author Alban Peignier
 */
public class FileUtilsFileNewerTestCase extends FileBasedTestCase {

    // Test data
    private static final int FILE1_SIZE = 1;
    private static final int FILE2_SIZE = 1024 * 4 + 1;

    private File m_testFile1;
    private File m_testFile2;

    public FileUtilsFileNewerTestCase(String name) {
        super(name);
        
        m_testFile1 = new File(getTestDirectory(), "file1-test.txt");
        m_testFile2 = new File(getTestDirectory(), "file2-test.txt");
    }

    /** @see junit.framework.TestCase#setUp() */
    protected void setUp() throws Exception {
        getTestDirectory().mkdirs();
        createFile(m_testFile1, FILE1_SIZE);
        createFile(m_testFile2, FILE2_SIZE);
    }

    /** @see junit.framework.TestCase#tearDown() */
    protected void tearDown() throws Exception {
        m_testFile1.delete();
        m_testFile2.delete();
    }

    /**
     * Tests the isFileNewer(File, *) methods which a "normal" file.
     *
     * @see FileUtils#isFileNewer(File, long)
     * @see FileUtils#isFileNewer(File, Date)
     * @see FileUtils#isFileNewer(File, File)
     */
    public void testIsFileNewer() {
        if (!m_testFile1.exists())
            throw new IllegalStateException("The m_testFile1 should exist");

        long fileLastModified = m_testFile1.lastModified();
        final long ONE_SECOND = 1000;

        testIsFileNewer("one second earlier is not newer" , m_testFile1, fileLastModified + ONE_SECOND, false);
        testIsFileNewer("same time is not newer" , m_testFile1, fileLastModified, false);
        testIsFileNewer("one second later is newer" , m_testFile1, fileLastModified - ONE_SECOND, true);
    }

    /**
     * Tests the isFileNewer(File, *) methods which a not existing file.
     *
     * @see FileUtils#isFileNewer(File, long)
     * @see FileUtils#isFileNewer(File, Date)
     * @see FileUtils#isFileNewer(File, File)
     */
    public void testIsFileNewerImaginaryFile() {
        File imaginaryFile = new File(getTestDirectory(), "imaginaryFile");
        if (imaginaryFile.exists())
            throw new IllegalStateException("The imaginary File exists");

        testIsFileNewer("imaginary file can be newer" , imaginaryFile, 0, false);
    }

    /**
     * Tests the isFileNewer(File, *) methods which the specified conditions.
     * 

* Creates : *

    *
  • a Date which represents the time reference
  • *
  • a temporary file with the same last modification date than the time reference
  • *
* Then compares (with the needed isFileNewer method) the last modification date of * the specified file with the specified time reference, the created Date and the temporary * file. *
* The test is successfull if the three comparaisons return the specified wanted result. * * @param description describes the tested situation * @param file the file of which the last modification date is compared * @param timeMillis the time reference measured in milliseconds since the epoch * * @see FileUtils#isFileNewer(File, long) * @see FileUtils#isFileNewer(File, Date) * @see FileUtils#isFileNewer(File, File) */ protected void testIsFileNewer(String description, File file, long time, boolean wantedResult) { assertEquals(description + " - time", wantedResult, FileUtils.isFileNewer(file, time)); assertEquals(description + " - date", wantedResult, FileUtils.isFileNewer(file, new Date(time))); File temporaryFile = m_testFile2; temporaryFile.setLastModified(time); if (temporaryFile.lastModified() != time) throw new IllegalStateException("The temporary file hasn't the right last modification date"); assertEquals(description + " - file", wantedResult, FileUtils.isFileNewer(file, temporaryFile)); } /** * Tests the isFileNewer(File, long) method without specifying a File. *
* The test is successfull if the method throws an IllegalArgumentException. */ public void testIsFileNewerNoFile() { try { FileUtils.isFileNewer(null,0); fail("File not specified"); } catch (IllegalArgumentException e) {} } /** * Tests the isFileNewer(File, Date) method without specifying a Date. *
* The test is successfull if the method throws an IllegalArgumentException. */ public void testIsFileNewerNoDate() { try { FileUtils.isFileNewer(m_testFile1, (Date) null); fail("Date not specified"); } catch (IllegalArgumentException e) {} } /** * Tests the isFileNewer(File, File) method without specifying a reference File. *
* The test is successfull if the method throws an IllegalArgumentException. */ public void testIsFileNewerNoFileReference() { try { FileUtils.isFileNewer(m_testFile1, (File) null); fail("Reference file not specified"); } catch (IllegalArgumentException e) {} } }
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.