com.mucommander.file
Class AbstractFileTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by com.mucommander.file.AbstractFileTestCase
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
FTPFileTestCase, SFTPFileTestCase, SMBFileTestCase

public abstract class AbstractFileTestCase
extends junit.framework.TestCase

A generic JUnit test case for the AbstractFile class. This class is abstract and must be extended by file implementations test classes. The tests performed by this class are generic and should validate on any proper file implementation, but they may not test the implementation's specifics. It is recommended the test case implementation provides additional test methods to complete those tests.

This test case is a WORK-IN-PROGRESS and by no means complete.

Author:
Maxence Bernard

Field Summary
protected  java.util.Vector filesToDelete
          AbstractFile instances to be deleted if they exist when tearDown() is called.
protected  java.util.Random random
          Random instance initialized with a static seed so that the values it generates are reproducible.
protected  AbstractFile tempFile
          A temporary file instance automatically instanciated by setUp() when a test is started.
 
Constructor Summary
AbstractFileTestCase()
           
 
Method Summary
protected  void assertContentsEquals(AbstractFile file1, AbstractFile file2)
          Asserts that both files contain the same data, by calculating their checksum and comparing them.
protected  void assertEquals(java.io.InputStream in1, java.io.InputStream in2)
          Asserts that both InputStream contain the same data, by calculating their checksum and comparing them.
protected  boolean byteArraysEqual(byte[] b1, byte[] b2)
          Returns true if both byte arrays are equal.
protected  java.lang.String calculateMd5(AbstractFile file)
          Calculates and returns the md5 checksum of the given AbstractFile's contents.
protected  java.lang.String calculateMd5(java.io.InputStream in)
          Calculates and returns the md5 checksum of the given InputStream's contents.
protected  java.lang.String createFile(AbstractFile file, long length)
          Creates a regular file and fills it with length random bytes.
protected  void deleteWhenFinished(AbstractFile fileToDelete)
          Adds the specified file to the list of files to be deleted by tearDown() when the test is finished.
 ChecksumOutputStream getMd5OutputStream(java.io.OutputStream out)
          Creates and returns a ChecksumOutputStream that generates an md5 checksum as data is written to it.
protected  java.lang.String getPseudoUniqueFilename(java.lang.String prefix)
          Generates and returns a pseudo unique filename, prepended by the given prefix.
abstract  AbstractFile getTemporaryFile()
          Returns a temporary file that can be used for testing purposes.
protected  void setUp()
          Initializes test variables before each test execution.
protected  void sleep(long timeMs)
          Sleeps for the given number of milliseconds.
protected  void tearDown()
          Cleans up test files after each test execution so as to leave the filesystem in the same state as it was before the test.
 void testAbsolutePath()
          Tests AbstractFile.getAbsolutePath() by asserting that it returns a non-null value, that the file can be resolved again using this path, and that the resolved file is the same as the orginal file.
 void testCanonicalPath()
          Tests AbstractFile.getCanonicalPath() by asserting that it returns a non-null value, that the file can be resolved again using this path, and that the resolved file is the same as the orginal file.
 void testCopyTo()
          Tests AbstractFile.getCopyToHint(AbstractFile) and AbstractFile.copyTo(AbstractFile).
 void testDate()
          Tests AbstractFile.getDate(), AbstractFile.canChangeDate() and AbstractFile.changeDate(long), no matter if dates can be changed or not.
 void testDelete()
          Tests the AbstractFile.delete() method in various situations.
 void testDigest()
          Tests AbstractFile.calculateChecksum(java.security.MessageDigest) and ByteUtils.toHexString(byte[]) by computing file digests using different algorithms (MD5, SHA-1, ...) and comparing them against known values.
 void testExists()
          Tests AbstractFile.exists() in various situations.
 void testFileURL()
          Tests AbstractFile.getURL() by asserting that it returns a non-null value, that the file can be resolved again using its string representation (with credentials), and that the resolved file is the same as the orginal file.
 void testFreeSpace()
          Tests AbstractFile.getFreeSpace() by asserting that the returned value is either -1 (not available), or a positive (potentially null) value.
 void testIcon()
          Tests AbstractFile.getIcon() and AbstractFile.getIcon(java.awt.Dimension).
 void testInputStream()
          Tests AbstractFile.getInputStream().
 void testIsDirectory()
          Tests the AbstractFile.isDirectory() method in various situations.
 void testJavaNetURL()
          Tests the java.net.URL returned by AbstractFile.getJavaNetURL() and its associated java.net.URLConnection.
 void testLs()
          Tests AbstractFile.ls().
 void testMkdir()
          Tests the AbstractFile.mkdir() method in various situations.
 void testMkdirs()
          Tests the AbstractFile.mkdirs() method in various situations.
 void testMkfile()
          Tests the AbstractFile.mkfile() method in various situations.
 void testMoveTo()
          Tests AbstractFile.getMoveToHint(AbstractFile) and AbstractFile.moveTo(AbstractFile).
 void testOutputStream()
          Tests AbstractFile.getOutputStream(boolean).
 void testParent()
          Tests AbstractFile.getParent() and AbstractFile.isParentOf(AbstractFile) methods.
protected  void testPathResolution(AbstractFile file, java.lang.String path)
          Verifies the given path is not null, that it can be resolved by FileFactory.getFile(String) into a file, and that this file is equal to the given one.
 void testPermissions()
          Tests AbstractFile permission methods.
 void testRandomAccessInputStream()
          Tests AbstractFile.hasRandomAccessInputStream() and AbstractFile.getRandomAccessInputStream().
 void testRandomAccessOutputStream()
          Tests AbstractFile.hasRandomAccessOutputStream() and AbstractFile.getRandomAccessOutputStream().
 void testRoot()
          Tests AbstractFile.getRoot() and AbstractFile.isRoot() methods.
 void testSeparator()
          Tests AbstractFile.getSeparator() by simply asserting that the return value is not null.
 void testTotalSpace()
          Tests AbstractFile.getTotalSpace() by asserting that the returned value is either -1 (not available), or a positive (potentially null) value.
 void testUnicodeFilenames()
          Verifies that the file implementation handles unicode/non-ascii filenames properly.
protected  java.lang.String writeRandomData(AbstractFile file, long length, int maxChunkSize, boolean append)
          Fills the given file with a total of length bytes of random data.
protected  void writeRandomData(java.io.OutputStream out, long length, int maxChunkSize)
          Fills the given OutputStream with a total of length bytes of random data.
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

filesToDelete

protected java.util.Vector filesToDelete
AbstractFile instances to be deleted if they exist when tearDown() is called.


tempFile

protected AbstractFile tempFile
A temporary file instance automatically instanciated by setUp() when a test is started. The file is not physically created.


random

protected java.util.Random random
Random instance initialized with a static seed so that the values it generates are reproducible. This makes it possible to reproduce and fix a failed test case.

Constructor Detail

AbstractFileTestCase

public AbstractFileTestCase()
Method Detail

setUp

protected void setUp()
              throws java.io.IOException
Initializes test variables before each test execution.

In particular, the tempFile file is created and ready for use by test methods. Note that this AbstractFile instance is created, but the file is not physically created.

Overrides:
setUp in class junit.framework.TestCase
Throws:
java.io.IOException - if an error occurred while creating test variables

tearDown

protected void tearDown()
                 throws java.io.IOException
Cleans up test files after each test execution so as to leave the filesystem in the same state as it was before the test. In particular, all files registered with deleteWhenFinished(AbstractFile) are deleted if they exist.

Overrides:
tearDown in class junit.framework.TestCase
Throws:
java.io.IOException - if an error occurred while delete files registered with deleteWhenFinished(AbstractFile)

deleteWhenFinished

protected void deleteWhenFinished(AbstractFile fileToDelete)
Adds the specified file to the list of files to be deleted by tearDown() when the test is finished. This file will be deleted only if it exists, and any children file it contains will also be deleted.

Parameters:
fileToDelete - a file to be deleted when the test is finished

writeRandomData

protected java.lang.String writeRandomData(AbstractFile file,
                                           long length,
                                           int maxChunkSize,
                                           boolean append)
                                    throws java.io.IOException,
                                           java.security.NoSuchAlgorithmException
Fills the given file with a total of length bytes of random data. The data is generated and written chunk by chunk, where each chunk has a random length comprised between 1 and maxChunkSize bytes. This method returns the md5 checksum of the data written to the file, allowing to later on test the integrity of the file. Before returning, this method asserts that the file exists (as reported by AbstractFile.exists()) and that its size (as returned by AbstractFile.getSize()) matches the specified length argument.

The OutputStream used for writing data is retrieved from AbstractFile.getOutputStream(boolean), passing the specified append argument. This method uses writeRandomData(java.io.OutputStream, long, int) to write the file, see this method's documentation for more information about how the random data is generated and written.

Parameters:
file - the file to write the data to
length - the number of random bytes to fill the file with
maxChunkSize - maximum size of a data chunk written to the file. Size of chunks is comprised between 1 and this value (inclusive).
append - if true, data written to the OutputStream will be appended to the end of this file. If false, any existing data this file contains will be discarded and overwritten.
Returns:
the md5 checksum of the data written to the file
Throws:
java.io.IOException - if an error occurred while retrieving the file's OutputStream or writing to it
java.security.NoSuchAlgorithmException - should not happen

writeRandomData

protected void writeRandomData(java.io.OutputStream out,
                               long length,
                               int maxChunkSize)
                        throws java.io.IOException,
                               java.security.NoSuchAlgorithmException
Fills the given OutputStream with a total of length bytes of random data. The data is generated and written chunk by chunk, where each chunk has a random length comprised between 1 and maxChunkSize bytes.

The random data is generated with a java.util.Random instance initialized with a static seed, so the data generated by this method will remain the same if the series of prior calls to the random instance haven't changed. This makes it possible to reproduce and fix a failed test case.

Parameters:
out - the OutputStream to use for writing the data
length - the number of random bytes to fill the file with
maxChunkSize - maximum size of a data chunk written to the file. Size of chunks is comprised between 1 and this value (inclusive).
Throws:
java.io.IOException - if an error occurred while writing to the OutputStream
java.security.NoSuchAlgorithmException - should not happen

createFile

protected java.lang.String createFile(AbstractFile file,
                                      long length)
                               throws java.io.IOException,
                                      java.security.NoSuchAlgorithmException
Creates a regular file and fills it with length random bytes. The file will be overwritten if it already exists. Before returning, this method asserts that the file exists and that its size by AbstractFile.getSize() matches the specified length argument.

Parameters:
file - the file to create or overwrite
length - the number of random bytes to fill the file with
Returns:
the md5 checksum of the data written to the file
Throws:
java.io.IOException - if the file already exists or if an error occurred while writing to it
java.security.NoSuchAlgorithmException - should not happen

sleep

protected void sleep(long timeMs)
Sleeps for the given number of milliseconds.

Parameters:
timeMs - number of milliseconds to sleep

getPseudoUniqueFilename

protected java.lang.String getPseudoUniqueFilename(java.lang.String prefix)
Generates and returns a pseudo unique filename, prepended by the given prefix.

Parameters:
prefix - the string to prepend to the filename, can be null.
Returns:
a pseudo unique filename

byteArraysEqual

protected boolean byteArraysEqual(byte[] b1,
                                  byte[] b2)
Returns true if both byte arrays are equal.

Parameters:
b1 - the first byte array to test
b2 - the second byte array to test
Returns:
true if both byte arrays are equal

getMd5OutputStream

public ChecksumOutputStream getMd5OutputStream(java.io.OutputStream out)
                                        throws java.security.NoSuchAlgorithmException
Creates and returns a ChecksumOutputStream that generates an md5 checksum as data is written to it.

Parameters:
out - the underlying OutputStream used by the DigestOutputStream
Returns:
a ChecksumOutputStream that generates an md5 checksum as data is written to it
Throws:
java.security.NoSuchAlgorithmException - should not happen

calculateMd5

protected java.lang.String calculateMd5(java.io.InputStream in)
                                 throws java.io.IOException,
                                        java.security.NoSuchAlgorithmException
Calculates and returns the md5 checksum of the given InputStream's contents. The provided stream is read completely (until EOF) but is not closed.

Parameters:
in - the InputStream to digest
Returns:
the md5 checksum of the given InputStream's contents
Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

calculateMd5

protected java.lang.String calculateMd5(AbstractFile file)
                                 throws java.io.IOException,
                                        java.security.NoSuchAlgorithmException
Calculates and returns the md5 checksum of the given AbstractFile's contents.

Parameters:
file - the file to digest
Returns:
the md5 checksum of the given InputStream's contents
Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

assertEquals

protected void assertEquals(java.io.InputStream in1,
                            java.io.InputStream in2)
                     throws java.io.IOException,
                            java.security.NoSuchAlgorithmException
Asserts that both InputStream contain the same data, by calculating their checksum and comparing them. Both streams are read completely (until EOF) but are not closed.

Parameters:
in1 - the first InputStream to compare
in2 - the second InputStream to compare
Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

assertContentsEquals

protected void assertContentsEquals(AbstractFile file1,
                                    AbstractFile file2)
                             throws java.io.IOException,
                                    java.security.NoSuchAlgorithmException
Asserts that both files contain the same data, by calculating their checksum and comparing them.

Parameters:
file1 - the first file to compare
file2 - the second file to compare
Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testPathResolution

protected void testPathResolution(AbstractFile file,
                                  java.lang.String path)
                           throws java.io.IOException,
                                  java.security.NoSuchAlgorithmException
Verifies the given path is not null, that it can be resolved by FileFactory.getFile(String) into a file, and that this file is equal to the given one. If the given file is not a directory, the contents of both file instances are compared to make sure they are equal.

Parameters:
file - the file instance that corresponds to the given path
path - the path that should be resolved into the specified file
Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testDigest

public void testDigest()
                throws java.io.IOException,
                       java.security.NoSuchAlgorithmException
Tests AbstractFile.calculateChecksum(java.security.MessageDigest) and ByteUtils.toHexString(byte[]) by computing file digests using different algorithms (MD5, SHA-1, ...) and comparing them against known values.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testSeparator

public void testSeparator()
Tests AbstractFile.getSeparator() by simply asserting that the return value is not null.


testAbsolutePath

public void testAbsolutePath()
                      throws java.io.IOException,
                             java.security.NoSuchAlgorithmException
Tests AbstractFile.getAbsolutePath() by asserting that it returns a non-null value, that the file can be resolved again using this path, and that the resolved file is the same as the orginal file. The tests are performed on a regular file and a directory file.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testCanonicalPath

public void testCanonicalPath()
                       throws java.io.IOException,
                              java.security.NoSuchAlgorithmException
Tests AbstractFile.getCanonicalPath() by asserting that it returns a non-null value, that the file can be resolved again using this path, and that the resolved file is the same as the orginal file. The tests are performed on a regular file and a directory file.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testFileURL

public void testFileURL()
                 throws java.io.IOException,
                        java.security.NoSuchAlgorithmException
Tests AbstractFile.getURL() by asserting that it returns a non-null value, that the file can be resolved again using its string representation (with credentials), and that the resolved file is the same as the orginal file. The tests are performed on a regular file and a directory file.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testJavaNetURL

public void testJavaNetURL()
                    throws java.io.IOException,
                           java.security.NoSuchAlgorithmException
Tests the java.net.URL returned by AbstractFile.getJavaNetURL() and its associated java.net.URLConnection.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testRoot

public void testRoot()
              throws java.io.IOException
Tests AbstractFile.getRoot() and AbstractFile.isRoot() methods.

Throws:
java.io.IOException - should not happen

testParent

public void testParent()
                throws java.io.IOException
Tests AbstractFile.getParent() and AbstractFile.isParentOf(AbstractFile) methods.

Throws:
java.io.IOException - should not happen

testExists

public void testExists()
                throws java.io.IOException
Tests AbstractFile.exists() in various situations.

Throws:
java.io.IOException - should not happen

testDelete

public void testDelete()
                throws java.io.IOException
Tests the AbstractFile.delete() method in various situations.

Throws:
java.io.IOException - should not happen

testMkdir

public void testMkdir()
               throws java.io.IOException
Tests the AbstractFile.mkdir() method in various situations.

Throws:
java.io.IOException - should not happen

testMkdirs

public void testMkdirs()
                throws java.io.IOException
Tests the AbstractFile.mkdirs() method in various situations.

Throws:
java.io.IOException - should not happen

testMkfile

public void testMkfile()
                throws java.io.IOException
Tests the AbstractFile.mkfile() method in various situations.

Throws:
java.io.IOException - should not happen

testIsDirectory

public void testIsDirectory()
                     throws java.io.IOException
Tests the AbstractFile.isDirectory() method in various situations.

Throws:
java.io.IOException - should not happen

testPermissions

public void testPermissions()
                     throws java.io.IOException,
                            java.security.NoSuchAlgorithmException
Tests AbstractFile permission methods.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testDate

public void testDate()
              throws java.io.IOException,
                     java.security.NoSuchAlgorithmException
Tests AbstractFile.getDate(), AbstractFile.canChangeDate() and AbstractFile.changeDate(long), no matter if dates can be changed or not.

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testInputStream

public void testInputStream()
                     throws java.io.IOException,
                            java.security.NoSuchAlgorithmException
Tests AbstractFile.getInputStream().

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testRandomAccessInputStream

public void testRandomAccessInputStream()
                                 throws java.io.IOException,
                                        java.security.NoSuchAlgorithmException
Tests AbstractFile.hasRandomAccessInputStream() and AbstractFile.getRandomAccessInputStream().

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testOutputStream

public void testOutputStream()
                      throws java.io.IOException,
                             java.security.NoSuchAlgorithmException
Tests AbstractFile.getOutputStream(boolean).

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testRandomAccessOutputStream

public void testRandomAccessOutputStream()
                                  throws java.io.IOException,
                                         java.security.NoSuchAlgorithmException
Tests AbstractFile.hasRandomAccessOutputStream() and AbstractFile.getRandomAccessOutputStream().

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testLs

public void testLs()
            throws java.io.IOException
Tests AbstractFile.ls().

Throws:
java.io.IOException - should not happen

testFreeSpace

public void testFreeSpace()
Tests AbstractFile.getFreeSpace() by asserting that the returned value is either -1 (not available), or a positive (potentially null) value.


testTotalSpace

public void testTotalSpace()
Tests AbstractFile.getTotalSpace() by asserting that the returned value is either -1 (not available), or a positive (potentially null) value.


testCopyTo

public void testCopyTo()
                throws java.io.IOException,
                       java.security.NoSuchAlgorithmException
Tests AbstractFile.getCopyToHint(AbstractFile) and AbstractFile.copyTo(AbstractFile).

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happen

testMoveTo

public void testMoveTo()
                throws java.io.IOException,
                       java.security.NoSuchAlgorithmException
Tests AbstractFile.getMoveToHint(AbstractFile) and AbstractFile.moveTo(AbstractFile).

Throws:
java.io.IOException - should not happen
java.security.NoSuchAlgorithmException - should not happens

testIcon

public void testIcon()
              throws java.io.IOException
Tests AbstractFile.getIcon() and AbstractFile.getIcon(java.awt.Dimension).

Throws:
java.io.IOException - should not happen

testUnicodeFilenames

public void testUnicodeFilenames()
                          throws java.io.IOException
Verifies that the file implementation handles unicode/non-ascii filenames properly.

Throws:
java.io.IOException - should not happen

getTemporaryFile

public abstract AbstractFile getTemporaryFile()
                                       throws java.io.IOException
Returns a temporary file that can be used for testing purposes. The implementation should return a file that does not exist, i.e. for which AbstractFile.exists() returns false.

Returns:
a temporary file that does not physically exist
Throws:
java.io.IOException - if an error occurred while creating a temporary file


This file is part of muCommander - Copyright (C) 2002-2008 Maxence Bernard