FileDocCategorySizeDatePackage
FileBridgeTest.javaAPI DocAndroid 5.1 API4667Thu Mar 12 22:22:12 GMT 2015android.os

FileBridgeTest

public class FileBridgeTest extends android.test.AndroidTestCase

Fields Summary
private File
file
private FileOutputStream
fileOs
private FileBridge
bridge
private android.os.FileBridge.FileBridgeOutputStream
client
Constructors Summary
Methods Summary
private voidassertContents(byte[] expected)

        MoreAsserts.assertEquals(expected, Streams.readFully(new FileInputStream(file)));
    
private voidassertOpen()

        assertFalse("expected open", bridge.isClosed());
    
private voidcloseAndAssertClosed()

        client.close();

        // Wait a beat for things to settle down
        SystemClock.sleep(200);
        assertTrue("expected closed", bridge.isClosed());
    
protected voidsetUp()

        super.setUp();

        file = getContext().getFileStreamPath("meow.dat");
        file.delete();

        fileOs = new FileOutputStream(file);

        bridge = new FileBridge();
        bridge.setTargetFile(fileOs.getFD());
        bridge.start();
        client = new FileBridgeOutputStream(bridge.getClientSocket());
    
protected voidtearDown()

        fileOs.close();
        file.delete();
    
public voidtestEmptyWrite()

        assertOpen();
        client.write(new byte[0]);
        closeAndAssertClosed();
        assertContents(new byte[0]);
    
public voidtestGiantWrite()

        final byte[] test = new byte[263401];
        new Random().nextBytes(test);

        assertOpen();
        client.write(test);
        closeAndAssertClosed();
        assertContents(test);
    
public voidtestNoWriteNoSync()

        assertOpen();
        closeAndAssertClosed();
    
public voidtestNoWriteSync()

        assertOpen();
        client.flush();
        closeAndAssertClosed();
    
public voidtestRandomWrite()

        final Random r = new Random();
        final ByteArrayOutputStream result = new ByteArrayOutputStream();

        for (int i = 0; i < 512; i++) {
            final byte[] test = new byte[r.nextInt(24169)];
            r.nextBytes(test);
            result.write(test);
            client.write(test);
            client.flush();
        }

        closeAndAssertClosed();
        assertContents(result.toByteArray());
    
public voidtestWriteAfterClose()

        assertOpen();
        client.write("meow".getBytes(StandardCharsets.UTF_8));
        closeAndAssertClosed();
        try {
            client.write("cake".getBytes(StandardCharsets.UTF_8));
            fail("wrote after close!");
        } catch (IOException expected) {
        }
        assertContents("meow".getBytes(StandardCharsets.UTF_8));
    
public voidtestWriteNoSync()

        assertOpen();
        client.write("meow".getBytes(StandardCharsets.UTF_8));
        closeAndAssertClosed();
        assertContents("meow".getBytes(StandardCharsets.UTF_8));
    
public voidtestWriteSync()

        assertOpen();
        client.write("cake".getBytes(StandardCharsets.UTF_8));
        client.flush();
        closeAndAssertClosed();
        assertContents("cake".getBytes(StandardCharsets.UTF_8));
    
public voidtestWriteSyncWrite()

        assertOpen();
        client.write("meow".getBytes(StandardCharsets.UTF_8));
        client.flush();
        client.write("cake".getBytes(StandardCharsets.UTF_8));
        closeAndAssertClosed();
        assertContents("meowcake".getBytes(StandardCharsets.UTF_8));