FileDocCategorySizeDatePackage
TestFileSystem.javaAPI DocphoneME MR2 API (J2ME)10320Wed May 02 18:00:14 BST 2007com.sun.midp.io.j2me.storage

TestFileSystem

public class TestFileSystem extends TestCase
Unit test for File.java.

Fields Summary
static final String
EMPTY_STR
static final String
TEST_FILE_A
static final String
TEST_FILE_B
File
fs
RandomAccessStream
ras
Constructors Summary
Methods Summary
public voidassertNotEquals(java.lang.Object expected, java.lang.Object actual)
Asserts that two objects are equal according to the equals() method.

param
message the message to be emitted if the assertion fails
param
expected an object containing the expected value
param
actual an object containing the actual value

	if (expected != actual) {
            if (!expected.equals(actual)) {
                return;
            }

            fail("expected NOT equals actual");
	    return;
        }
    
voidcreateTestFile(java.lang.String name, int size)

	ras.connect(name, Connector.WRITE);
	ras.writeBytes(new byte[size], 0, size);
        ras.commitWrite();
	ras.disconnect();
    
public voidrunTests()

	setUp();

        declare("testGetStorageRoot");
        testGetStorageRoot();

        declare("testGetConfigRoot");
        testGetConfigRoot();

        declare("testUnicodeToAsciiFilenameAndReverse");
        testUnicodeToAsciiFilenameAndReverse();

        declare("testRename");
        testRename();

        declare("testExists");
	testExists();

	declare("testDelete");
	testDelete();

	declare("testGetBytesAvailableForFiles");
	testAvailable();

	tearDown();
    
voidsetUp()


      
	fs = new File();
	ras = new RandomAccessStream();
    
voidtearDown()

    
voidtestAvailable()
- Call and store the size as initialSpace, expect > 0 - Create a file "A" and write 1KB data, and call size again as lessSpace expect: 0 < diff - 1KB < 512B - Delete file "A" and call size again as moreSpace expect moreSpace - lessSpace > 1KB expect abs(moreSpace - lessSpace) < 512B - Repeat from step 2 for 50 times


	int SIZE = 2048; // bytes
	int OVERHEAD = 512; // bytes

	long initialSpace = fs.getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);
	assertTrue("initialSpace="+initialSpace, initialSpace > 0);

	try {
	    // Loop many times to ensure no space leak
	    for (int i = 0; i < 50; i++) {
		// Create a file with certain size
		// expect overhead be less than allowed number 
		createTestFile(TEST_FILE_A, SIZE);
		long lessSpace = fs.getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);
		
		long delta = initialSpace - lessSpace;
		
		assertTrue("space used="+delta, delta >= SIZE);

		assertTrue("create overhead="+(delta - SIZE),
			   delta < SIZE + OVERHEAD);
	     
		// expect delete a file will give back some space
		// expect less than overhead bytes will be kept
		fs.delete(TEST_FILE_A);
		long moreSpace = fs.getBytesAvailableForFiles(Constants.INTERNAL_STORAGE_ID);

		delta = moreSpace - lessSpace;

		assertTrue("freeup space="+delta, delta >= SIZE);

		if (moreSpace < initialSpace) {
		    assertTrue("delete overhead="+(initialSpace - moreSpace),
			       initialSpace - moreSpace < OVERHEAD);
		} else {
		    assertTrue("delete overhead="+(moreSpace - initialSpace),
				moreSpace - initialSpace < OVERHEAD);
		}
	    }
	} catch (IOException ioe) {
	    ioe.printStackTrace();
	}
    
voidtestDelete()
- delete("B") and check for non-exists("B") - delete("DontExist") expects IOException - delete(EMPTY) expects IllegalArgumentException

	try {
	    // Create B first
	    createTestFile(TEST_FILE_B, 1024);

	    // Delete
	    fs.delete(TEST_FILE_B);

	    // Test for existence
	    assertTrue(fs.exists(TEST_FILE_B) == false);

	} catch (Throwable t) {
	    fail("Unexpected Exception while deleting:");
	    t.printStackTrace();
	}

	// Delete a non-existing file
	boolean ioeThrowed = false;
	try {
	    fs.delete("DontExist");
	} catch (IOException ioe) {
	    ioeThrowed = true;
	}

	assertTrue(ioeThrowed);
    
voidtestExists()
exists(EMPTY) return false.

	assertTrue(!fs.exists(EMPTY_STR));
    
public voidtestGetConfigRoot()

        assertTrue("getConfigRoot != null", File.getConfigRoot(Constants.INTERNAL_STORAGE_ID) != null);
    
public voidtestGetStorageRoot()


	// Storage root should not be null
        assertTrue("getStorageRoot == null", File.getStorageRoot(Constants.INTERNAL_STORAGE_ID) != null);

	// Storage root should be writable
        try {
            createTestFile(TEST_FILE_A, 1024);
	    assertTrue(TEST_FILE_A + " exists", fs.exists(TEST_FILE_A));
	    // Leave "A" on FS, so a later test can use it
        } catch (IOException ioe) {
            fail("Could not create a file in storage root");
        }
    
public voidtestRename()


	// Make sure B does not exist
        try {
            fs.delete(TEST_FILE_B);
        } catch (IOException e) {
            // This will happen most of the time since "B" should not exist
        }

	// Rename A to B
	try {
	    fs.rename(TEST_FILE_A, TEST_FILE_B);
	    assertTrue("A does not exist", !fs.exists(TEST_FILE_A));
	    assertTrue("B does exist",      fs.exists(TEST_FILE_B));
        
	    try {
		fs.rename(TEST_FILE_B, EMPTY_STR);
		fail("rename to empty string");
	    } catch (IOException ioe) {
		assertTrue(true);
	    }
	} catch (Throwable t) {
	    fail("Exception when renaming: ");
	    t.printStackTrace();

	    // Clean-up by deleting A
	    try {
		fs.delete(TEST_FILE_A);
	    } catch (IOException ioe) {
		// ignore
	    }
	}
    
public voidtestUnicodeToAsciiFilenameAndReverse()

        String unicode;
        String ascii;
        String result;
        char[] buf = new char[1];

        for (char c = 'a"; c <= 'z"; c++) {
            buf[0] = c;
            unicode = new String(buf);
            ascii = File.unicodeToAsciiFilename(unicode);
            assertEquals(unicode, ascii);
            result = File.asciiFilenameToUnicode(ascii);
            assertEquals(unicode, result);
        }

        for (char c = '0"; c <= '9"; c++) {
            buf[0] = c;
            unicode = new String(buf);
            ascii = File.unicodeToAsciiFilename(unicode);
            assertEquals(unicode, ascii);
            result = File.asciiFilenameToUnicode(ascii);
            assertEquals(unicode, result);
        }

        for (char c = 'A"; c <= 'Z"; c++) {
            buf[0] = c;
            unicode = new String(buf);
            ascii = File.unicodeToAsciiFilename(unicode);
            assertEquals('#", ascii.charAt(0));
            assertEquals(c, ascii.charAt(1));
            result = File.asciiFilenameToUnicode(ascii);
            assertEquals(unicode, result);
        }

        unicode = EMPTY_STR;
        ascii = File.unicodeToAsciiFilename(unicode);
        assertEquals(unicode, ascii);

        buf[0] = 'a" - 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = 'z" + 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = '0" - 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = '9" + 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = 'A" - 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = 'Z" + 1;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = 0xFFFF;
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertNotEquals(unicode, ascii);

        buf[0] = 0x100;
        result = "%0100";
        unicode = new String(buf);
        ascii = File.unicodeToAsciiFilename(unicode);
        assertEquals(result, ascii);
        result = File.asciiFilenameToUnicode(ascii);
        assertEquals(unicode, result);