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

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",
                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) {
            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;
    }

}