summaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/resources/DownloadLargeFileResource.java
blob: d799e1a8d4397528046131f37c37364dc9611857 (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
package com.vaadin.tests.resources;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.vaadin.server.FileResource;
import com.vaadin.tests.components.TestBase;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;

public class DownloadLargeFileResource extends TestBase {

    private FileResource hugeFileResource = null;
    private long fileSize = (long) (1233.2 * 1024.0 * 1024.0);

    @Override
    protected void setup() {
        Button b = new Button(
                "Download a "
                        + String.format("%.1f", fileSize / 1024.0 / 1024.0)
                        + "MB file", new Button.ClickListener() {

                    @Override
                    public void buttonClick(ClickEvent event) {
                        download();
                    }
                });
        addComponent(b);
    }

    protected void download() {
        if (hugeFileResource == null) {
            createFile();
        }

        getMainWindow().open(hugeFileResource);

    }

    private void createFile() {
        try {
            File hugeFile = File.createTempFile("huge", ".txt");
            hugeFile.deleteOnExit();
            BufferedOutputStream os = new BufferedOutputStream(
                    new FileOutputStream(hugeFile));
            int writeAtOnce = 1024 * 1024;
            byte[] b = new byte[writeAtOnce];
            for (int i = 0; i < fileSize; i += writeAtOnce) {
                os.write(b);
            }
            os.close();
            hugeFileResource = new FileResource(hugeFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    protected String getDescription() {
        return "Click the button to download huge-file.txt. The file is generated on the first download.";
    }

    @Override
    protected Integer getTicketNumber() {
        return 5356;
    }

}