Class UploadTester<T extends Upload>
- Type Parameters:
T- the component type.
- All Implemented Interfaces:
Clickable<T>
Files handed to upload(File) and friends go through the same
client-side gate the vaadin-upload web component applies before it
sends anything to the server: maxFiles,
maxFileSize and the accepted file types
are checked in that order, and a file failing any of them is not delivered to
the upload handler or receiver. Instead a
FileRejectedEvent is fired, exactly as it would be in a browser.
A refused file is no more of an error in the tester than it is in the
browser, so uploading does not throw for it. Use
getLastUploadStatus() to see what became of each file, or
ensureUploaded() to fail the test unless every file went through.
maxFiles is checked against an emulated file list, so files stay in
it between calls just like the entries the browser shows: uploading two files
to an Upload configured with a plain Receiver rejects the
second one, because such an Upload implicitly sets maxFiles
to one. Use removeFile(String) or Upload.clearFileList() to
make room, as the user would.
The accepted file types are checked the way the web component does, against
the file name or the content type. That is a laxer rule than the server side
validation Flow applies to Upload.setAcceptedMimeTypes(String...) and
Upload.setAcceptedFileExtensions(String...), which the file has to
pass as well before it reaches the upload handler.
- Since:
- 1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOutcome of a single file of an upload simulated by the tester.static enumWhat became of a file handed to the tester for uploading. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidChecks that the upload last simulated on this component delivered every one of its files, and fails otherwise.Returns what happened to each file of the upload last simulated on this component, in the order the files were given, so that a test can check the outcome the same way a user reads the upload file list in the browser.voidremoveFile(File file) Simulates the user removing a file from the upload file list, as if clicking the remove button on the file entry.voidremoveFile(String fileName) Simulates the user removing a file from the upload file list, as if clicking the remove button on the file entry.voidSend the file data to the Upload component as if it is uploaded in the browser.voidSend the file data to the Upload component as if it is uploaded in the browser.voidupload(String fileName, String contentType, InputStream contents) Send the file data to the Upload component as if it is uploaded in the browser.voiduploadAborted(File file) Simulates upload interruption by user on browser.voiduploadAborted(String fileName, String contentType) Simulates upload interruption by user on browser.voidSimulates uploading multiple files at once.voiduploadAll(Collection<File> files) Simulates uploading multiple files at once.voiduploadFailed(File file) Simulates a failure during file upload.voiduploadFailed(String fileName, String contentType) Simulates a failure during file upload.Methods inherited from class com.vaadin.browserless.ComponentTester
canSetValueAsUser, clearAsUser, clickClearButtonAsUser, ensureComponentIsUsable, ensureComponentIsUsable, ensureComponentIsUsableOrDetach, ensureVisible, ensureVisible, find, findAllByQuery, findByQuery, fireDomEvent, fireDomEvent, fireDomEvent, getComponent, getField, getField, getMethod, getMethod, isComponentReadOnly, isUsable, isUsable, notUsableReasons, notUsableReasons, roundTrip, setModal, setPropertyAsUser, setValueAsUser, setValueAsUserMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.browserless.Clickable
click, click, click, middleClick, middleClick, rightClick, rightClick
-
Constructor Details
-
UploadTester
Wrap given component for testing.- Parameters:
component- target component
-
-
Method Details
-
upload
Send the file data to the Upload component as if it is uploaded in the browser.The file is rejected with a
FileRejectedEvent, and never reaches the upload handler, if it violates one of the client-side constraints of the component.- Parameters:
fileName- name of the file to uploadcontentType- content type of the file to uploadcontents- file contents as an array of bytes- Throws:
UncheckedIOException- if the upload component fails to handle file contentsIllegalStateException- if the component is not usable
-
upload
Send the file data to the Upload component as if it is uploaded in the browser.The file is rejected with a
FileRejectedEvent, and never reaches the upload handler, if it violates one of the client-side constraints of the component.- Parameters:
fileName- name of the file to uploadcontentType- content type of the file to uploadcontents- file contents as an array of bytes- Throws:
UncheckedIOException- if the upload component fails to handle file contentsIllegalStateException- if the component is not usable
-
upload
Send the file data to the Upload component as if it is uploaded in the browser. The content type is detected from file name.The file is rejected with a
FileRejectedEvent, and never reaches the upload handler, if it violates one of the client-side constraints of the component.- Parameters:
file- the file to upload- Throws:
UncheckedIOException- if the upload component fails to handle file contentsIllegalStateException- if the component is not usable
-
uploadAll
Simulates uploading multiple files at once.Files violating one of the client-side constraints of the component are rejected with a
FileRejectedEventand never reach the upload handler; the remaining files are uploaded.- Parameters:
files- files to upload- Throws:
IllegalStateException- if the component is not usable
-
uploadAll
Simulates uploading multiple files at once.Files violating one of the client-side constraints of the component are rejected with a
FileRejectedEventand never reach the upload handler; the remaining files are uploaded.- Parameters:
files- files to upload- Throws:
IllegalStateException- if the component is not usable
-
uploadAborted
Simulates upload interruption by user on browser.As in the browser, the interrupted file is dropped from the file list and a
FileRemovedEventis fired, freeing a slot whenUpload.setMaxFiles(int)is in use.- Parameters:
fileName- name of uploading filecontentType- content type of the uploading file
-
uploadAborted
Simulates upload interruption by user on browser.As in the browser, the interrupted file is dropped from the file list and a
FileRemovedEventis fired, freeing a slot whenUpload.setMaxFiles(int)is in use.- Parameters:
file- uploading file
-
uploadFailed
Simulates a failure during file upload.As in the browser, a file whose upload failed stays in the file list.
- Parameters:
file- uploading file
-
uploadFailed
Simulates a failure during file upload.As in the browser, a file whose upload failed stays in the file list.
- Parameters:
fileName- name of uploading filecontentType- content type of the uploading file
-
removeFile
Simulates the user removing a file from the upload file list, as if clicking the remove button on the file entry.A
FileRemovedEventis fired and the file stops counting towardsUpload.setMaxFiles(int).- Parameters:
fileName- name of the file to remove, as given when it was uploaded- Throws:
IllegalArgumentException- if the file is not in the upload file list
-
removeFile
Simulates the user removing a file from the upload file list, as if clicking the remove button on the file entry.A
FileRemovedEventis fired and the file stops counting towardsUpload.setMaxFiles(int).- Parameters:
file- the file to remove- Throws:
IllegalArgumentException- if the file is not in the upload file list
-
getLastUploadStatus
Returns what happened to each file of the upload last simulated on this component, in the order the files were given, so that a test can check the outcome the same way a user reads the upload file list in the browser.Files are reported as
UploadTester.UploadStatus.UPLOADEDonce the upload handler or receiver has consumed them, and asUploadTester.UploadStatus.REJECTEDboth when the client-side gate refused them and when Flow's server-side accepted type validation did, in which case neither shows up as a thrown exception.- Returns:
- the outcome of the last simulated upload, one entry per file, or an empty list if no upload has been simulated yet
-
ensureUploaded
public void ensureUploaded()Checks that the upload last simulated on this component delivered every one of its files, and fails otherwise.Convenience for the common case where a test only wants to be sure the files went through: an upload the browser or the server refused is silent, so without this check it would be noticed only later, as a missing side effect.
- Throws:
IllegalStateException- if no upload has been simulated on the component, or if any file of the last upload was not uploaded- See Also:
-