summaryrefslogtreecommitdiffstats
blob: 3e06646bdf4043af25a6adcad43ccdc1c64e33d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *
 *  Acquisition and use of this software and related materials for any
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.filetransfer;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import com.isode.stroke.base.ByteArray;
import com.isode.stroke.signals.Slot1;

/**
 * Tests for {@link FileWriteBytestream}
 *
 */
public class FileWriteBytestreamTest {

    private boolean onWriteWasCalled = false;
    
    @Test
    public void testSuccessfulWrite() {
        File tempfile = null;
        String filename = null;
        try {
            try {
                tempfile = File.createTempFile("write_file_bytestream_test_", ".tmp");
                filename = tempfile.getAbsolutePath();
            } catch (IOException e) {
                // Unable to create file exit test
                return;
            }
            WriteBytestream writeBytestream = new FileWriteBytestream(filename);
            writeBytestream.onWrite.connect(new Slot1<ByteArray>() {
                
                @Override
                public void call(ByteArray data) {
                    handleOnWrite(data);
                }
                
            });
            
            assertTrue(writeBytestream.write(new ByteArray("Some data.")));
            assertTrue(onWriteWasCalled);
        }
        finally {
            if (tempfile != null && tempfile.exists()) {
                tempfile.delete();
            }
        }
    }
    
    @Test
    public void testFailingWrite() {
        WriteBytestream writeBytestream = new FileWriteBytestream("");
        writeBytestream.onWrite.connect(new Slot1<ByteArray>() {
            
            @Override
            public void call(ByteArray data) {
                handleOnWrite(data);
            }
            
        });
        
        assertFalse(writeBytestream.write(new ByteArray("Some data.")));
        assertFalse(onWriteWasCalled);
    }
    
    private void handleOnWrite(ByteArray data) {
        onWriteWasCalled = true;
    }
    
}