downloadedFile = null; } /** * @When user :user downloads zip file for entries :entries in folder :folder */ public function userDownloadsZipFileForEntriesInFolder($user, $entries, $folder) { $folder = trim($folder, '/'); $this->asAn($user); $this->sendingToDirectUrl('GET', "/remote.php/dav/files/$user/$folder?accept=zip&files=[" . $entries . ']'); $this->theHTTPStatusCodeShouldBe('200'); } private function getDownloadedFile() { $this->downloadedFile = ''; /** @var StreamInterface */ $body = $this->response->getBody(); while (!$body->eof()) { $this->downloadedFile .= $body->read(8192); } $body->close(); } /** * @Then the downloaded file is a zip file */ public function theDownloadedFileIsAZipFile() { $this->getDownloadedFile(); Assert::assertTrue( strpos($this->downloadedFile, "\x50\x4B\x01\x02") !== false, 'File does not contain the central directory file header' ); } /** * @Then the downloaded zip file is a zip32 file */ public function theDownloadedZipFileIsAZip32File() { $this->theDownloadedFileIsAZipFile(); // assertNotContains is not used to prevent the whole file from being // printed in case of error. Assert::assertTrue( strpos($this->downloadedFile, "\x50\x4B\x06\x06") === false, 'File contains the zip64 end of central dir signature' ); } /** * @Then the downloaded zip file is a zip64 file */ public function theDownloadedZipFileIsAZip64File() { $this->theDownloadedFileIsAZipFile(); // assertNotContains is not used to prevent the whole file from being // printed in case of error. Assert::assertTrue( strpos($this->downloadedFile, "\x50\x4B\x06\x06") !== false, 'File does not contain the zip64 end of central dir signature' ); } /** * @Then the downloaded zip file contains a file named :fileName with the contents of :sourceFileName from :user data */ public function theDownloadedZipFileContainsAFileNamedWithTheContentsOfFromData($fileName, $sourceFileName, $user) { $fileHeaderRegExp = '/'; $fileHeaderRegExp .= "\x50\x4B\x03\x04"; // Local file header signature $fileHeaderRegExp .= '.{22,22}'; // Ignore from "version needed to extract" to "uncompressed size" $fileHeaderRegExp .= preg_quote(pack('v', strlen($fileName)), '/'); // File name length $fileHeaderRegExp .= '(.{2,2})'; // Get "extra field length" $fileHeaderRegExp .= preg_quote($fileName, '/'); // File name $fileHeaderRegExp .= '/s'; // PCRE_DOTALL, so all characters (including bytes that happen to be new line characters) match // assertRegExp is not used to prevent the whole file from being printed // in case of error and to be able to get the extra field length. Assert::assertEquals( 1, preg_match($fileHeaderRegExp, $this->downloadedFile, $matches), 'Local header for file did not appear once in zip file' ); $extraFieldLength = unpack('vextraFieldLength', $matches[1])['extraFieldLength']; $expectedFileContents = file_get_contents($this->getDataDirectory() . "/$user/files" . $sourceFileName); $fileHeaderAndContentRegExp = '/'; $fileHeaderAndContentRegExp .= "\x50\x4B\x03\x04"; // Local file header signature $fileHeaderAndContentRegExp .= '.{22,22}'; // Ignore from "version needed to extract" to "uncompressed size" $fileHeaderAndContentRegExp .= preg_quote(pack('v', strlen($fileName)), '/'); // File name length $fileHeaderAndContentRegExp .= '.{2,2}'; // Ignore "extra field length" $fileHeaderAndContentRegExp .= preg_quote($fileName, '/'); // File name $fileHeaderAndContentRegExp .= '.{' . $extraFieldLength . ',' . $extraFieldLength . '}'; // Ignore "extra field" $fileHeaderAndContentRegExp .= preg_quote($expectedFileContents, '/'); // File contents $fileHeaderAndContentRegExp .= '/s'; // PCRE_DOTALL, so all characters (including bytes that happen to be new line characters) match // assertRegExp is not used to prevent the whole file from being printed // in case of error. Assert::assertEquals( 1, preg_match($fileHeaderAndContentRegExp, $this->downloadedFile), 'Local header and contents for file did not appear once in zip file' ); } /** * @Then the downloaded zip file contains a folder named :folderName */ public function theDownloadedZipFileContainsAFolderNamed($folderName) { $folderHeaderRegExp = '/'; $folderHeaderRegExp .= "\x50\x4B\x03\x04"; // Local file header signature $folderHeaderRegExp .= '.{22,22}'; // Ignore from "version needed to extract" to "uncompressed size" $folderHeaderRegExp .= preg_quote(pack('v', strlen($folderName)), '/'); // File name length $folderHeaderRegExp .= '.{2,2}'; // Ignore "extra field length" $folderHeaderRegExp .= preg_quote($folderName, '/'); // File name $folderHeaderRegExp .= '/s'; // PCRE_DOTALL, so all characters (including bytes that happen to be new line characters) match // assertRegExp is not used to prevent the whole file from being printed // in case of error. Assert::assertEquals( 1, preg_match($folderHeaderRegExp, $this->downloadedFile), 'Local header for folder did not appear once in zip file' ); } /** * @Then the downloaded file has the content of :sourceFilename from :user data */ public function theDownloadedFileHasContentOfUserFile($sourceFilename, $user) { $this->getDownloadedFile(); $expectedFileContents = file_get_contents($this->getDataDirectory() . "/$user/files" . $sourceFilename); // prevent the whole file from being printed in case of error. Assert::assertEquals( 0, strcmp($expectedFileContents, $this->downloadedFile), 'Downloaded file content does not match local file content' ); } } elect> Vaadin 6, 7, 8 is a Java framework for modern Java web applications: https://github.com/vaadin/frameworkwww-data
summaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/UsingObjectsInSelect.java
blob: 54664c04d0e2d8b9e6cd4f211b2809be5b3815f5 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.vaadin.tests;

import java.util.LinkedList;
import java.util.Random;

import com.vaadin.shared.ui.ContentMode;
import com.vaadin.ui.Label;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.v7.data.Property.ValueChangeEvent;
import com.vaadin.v7.data.Property.ValueChangeListener;
import com.vaadin.v7.ui.Select;

public class UsingObjectsInSelect extends com.vaadin.server.LegacyApplication
        implements ValueChangeListener {

    private final Select select = new Select();
    private final Label selectedTask = new Label("Selected task",
            ContentMode.HTML);

    public LinkedList<?> exampleTasks = new LinkedList<>();

    public static Random random = new Random(1);

    @Override
    public void init() {
        final LegacyWindow main = new LegacyWindow("Select demo");
        setMainWindow(main);

        VerticalLayout panelLayout = new VerticalLayout();
        panelLayout.setMargin(true);
        final Panel panel = new Panel("Select demo", panelLayout);
        panelLayout.addComponent(select);
        VerticalLayout panel2Layout = new VerticalLayout();
        panel2Layout.setMargin(true);
        final Panel panel2 = new Panel("Selection", panel2Layout);
        panel2Layout.addComponent(selectedTask);

        select.setCaption("Select component");
        select.addListener(this);
        select.setImmediate(true);

        main.addComponent(panel);
        main.addComponent(panel2);

        createExampleTasks();
    }

    public void createExampleTasks() {
        final String[] assignedTo = { "John", "Mary", "Joe", "Sarah", "Jeff",
                "Jane", "Peter", "Marc", "Josie", "Linus" };
        final String[] type = { "Enhancement", "Bugfix", "Testing", "Task" };
        for (int j = 0; j < 100; j++) {
            final Task task = new Task(
                    type[(int) (random.nextDouble() * (type.length - 1))],
                    assignedTo[(int) (random.nextDouble()
                            * (assignedTo.length - 1))],
                    random.nextInt(100));
            select.addItem(task);
        }
    }

    @Override
    public void valueChange(ValueChangeEvent event) {
        final Task task = (Task) select.getValue();
        selectedTask.setValue("<b>Type:</b> " + task.getType()
                + "<br /><b>Assigned to:</b> " + task.getAssignedTo()
                + "<br /><b>Estimated hours: </b>" + task.getEstimatedHours());
    }

    /**
     * Sample class which is bound to Vaadin components
     *
     */
    public class Task {

        private String type;
        private String assignedTo;
        private int estimatedHours;

        public Task(String type, String assignedTo, int estimatedHours) {
            this.type = type;
            this.assignedTo = assignedTo;
            this.estimatedHours = estimatedHours;
        }

        @Override
        public String toString() {
            return type + ", " + assignedTo;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getAssignedTo() {
            return assignedTo;
        }

        public void setAssignedTo(String assignedTo) {
            this.assignedTo = assignedTo;
        }

        public float getEstimatedHours() {
            return estimatedHours;
        }

        public void setEstimatedHours(int estimatedHours) {
            this.estimatedHours = estimatedHours;
        }
    }

}