-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Warranty: Fixes FileBuffer getInputStream is closed Fixes: vaadin/vaadin-upload#334
- Loading branch information
Tulio Garcia
authored
Nov 20, 2020
1 parent
8ba9bdd
commit 21c8d43
Showing
11 changed files
with
353 additions
and
52 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...gration-tests/src/main/java/com/vaadin/flow/component/upload/tests/it/FileBufferView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2000-2020 Vaadin Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.vaadin.flow.component.upload.tests.it; | ||
|
||
import com.vaadin.flow.component.html.Div; | ||
import com.vaadin.flow.component.upload.SucceededEvent; | ||
import com.vaadin.flow.component.upload.Upload; | ||
import com.vaadin.flow.component.upload.receivers.FileBuffer; | ||
import com.vaadin.flow.component.upload.receivers.MultiFileBuffer; | ||
import com.vaadin.flow.router.Route; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* View for {@link Upload} tests using {@link FileBuffer} and {@link MultiFileBuffer}. | ||
* | ||
* @author Vaadin Ltd | ||
*/ | ||
@Route("vaadin-upload/filebuffer") | ||
public class FileBufferView extends Div { | ||
public FileBufferView() { | ||
add(createSingleFileUpload()); | ||
add(createMultiFileUpload()); | ||
} | ||
|
||
private static Div createSingleFileUpload() { | ||
final FileBuffer buffer = new FileBuffer(); | ||
final Upload singleFileUpload = new Upload(buffer); | ||
singleFileUpload.setId("single-upload"); | ||
singleFileUpload.setMaxFiles(1); | ||
return setupUploadSection(singleFileUpload, | ||
e -> buffer.getInputStream()); | ||
|
||
} | ||
|
||
private static Div createMultiFileUpload() { | ||
final MultiFileBuffer buffer = new MultiFileBuffer(); | ||
final Upload multiFileUpload = new Upload(buffer); | ||
multiFileUpload.setId("multi-upload"); | ||
return setupUploadSection(multiFileUpload, | ||
e -> buffer.getInputStream(e.getFileName())); | ||
} | ||
|
||
private static Div setupUploadSection(Upload upload, | ||
Function<SucceededEvent, InputStream> streamProvider) { | ||
final String uploadId = upload.getId().orElse(""); | ||
final Div output = new Div(); | ||
output.setId(uploadId + "-output"); | ||
final Div eventsOutput = new Div(); | ||
eventsOutput.setId(uploadId + "-event-output"); | ||
|
||
upload.addSucceededListener(event -> { | ||
try { | ||
output.add(event.getFileName()); | ||
output.add( | ||
IOUtils.toString(streamProvider.apply(event), "UTF-8")); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
eventsOutput.add("-succeeded"); | ||
}); | ||
|
||
upload.addAllFinishedListener(event -> eventsOutput.add("-finished")); | ||
|
||
return new Div(output, eventsOutput, upload); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...egration-tests/src/test/java/com/vaadin/flow/component/upload/tests/AbstractUploadIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.vaadin.flow.component.upload.tests; | ||
|
||
import com.vaadin.tests.AbstractComponentIT; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebElement; | ||
import org.openqa.selenium.internal.WrapsElement; | ||
import org.openqa.selenium.remote.LocalFileDetector; | ||
import org.openqa.selenium.remote.RemoteWebElement; | ||
|
||
import java.io.BufferedWriter; | ||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
|
||
public abstract class AbstractUploadIT extends AbstractComponentIT { | ||
/** | ||
* @return The generated temp file handle | ||
* @throws IOException | ||
*/ | ||
File createTempFile() throws IOException { | ||
File tempFile = File.createTempFile("TestFileUpload", ".txt"); | ||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); | ||
writer.write(getTempFileContents()); | ||
writer.close(); | ||
tempFile.deleteOnExit(); | ||
return tempFile; | ||
} | ||
|
||
String getTempFileContents() { | ||
return "This is a test file! Row 2 Row3"; | ||
} | ||
|
||
void fillPathToUploadInput(WebElement input, String... tempFileNames) | ||
throws Exception { | ||
// create a valid path in upload input element. Instead of selecting a | ||
// file by some file browsing dialog, we use the local path directly. | ||
setLocalFileDetector(input); | ||
input.sendKeys(String.join(System.lineSeparator(), tempFileNames)); | ||
} | ||
|
||
private void setLocalFileDetector(WebElement element) throws Exception { | ||
if (getRunLocallyBrowser() != null) { | ||
return; | ||
} | ||
|
||
if (element instanceof WrapsElement) { | ||
element = ((WrapsElement) element).getWrappedElement(); | ||
} | ||
if (element instanceof RemoteWebElement) { | ||
((RemoteWebElement) element) | ||
.setFileDetector(new LocalFileDetector()); | ||
} else { | ||
throw new IllegalArgumentException( | ||
"Expected argument of type RemoteWebElement, received " | ||
+ element.getClass().getName()); | ||
} | ||
} | ||
|
||
WebElement getInput(WebElement upload) { | ||
return getInShadowRoot(upload, By.id("fileInput")); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/FileBufferIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.vaadin.flow.component.upload.tests; | ||
|
||
import com.vaadin.flow.component.upload.testbench.UploadElement; | ||
import com.vaadin.flow.testutil.TestPath; | ||
import org.junit.Assert; | ||
import org.junit.AssumptionViolatedException; | ||
import org.junit.Test; | ||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Tests for Upload using FileBuffer and MultiFileBuffer. | ||
*/ | ||
@TestPath("vaadin-upload/filebuffer") | ||
public class FileBufferIT extends AbstractUploadIT { | ||
@Test | ||
public void testUploadAnyFile() throws Exception { | ||
open(); | ||
final UploadElement upload = $(UploadElement.class).id("single-upload"); | ||
waitUntil(driver -> upload.isDisplayed()); | ||
|
||
File tempFile = createTempFile(); | ||
fillPathToUploadInput(getInput(upload),tempFile.getPath()); | ||
|
||
WebElement uploadOutput = getDriver().findElement(By.id("single-upload-output")); | ||
|
||
String content = uploadOutput.getText(); | ||
|
||
String expectedContent = tempFile.getName() + getTempFileContents(); | ||
|
||
Assert.assertEquals("Upload content does not match expected", | ||
expectedContent, content); | ||
} | ||
|
||
@Test | ||
public void testUploadMultipleEventOrder() throws Exception { | ||
if (getRunLocallyBrowser() == null) { | ||
// Multiple file upload does not work with Remotewebdriver | ||
// https://github.com/SeleniumHQ/selenium/issues/7408 | ||
throw new AssumptionViolatedException( | ||
"Skipped <Multiple file upload does not work with Remotewebdriver>"); | ||
} | ||
open(); | ||
|
||
final UploadElement upload = $(UploadElement.class).id("multi-upload"); | ||
waitUntil(driver -> upload.isDisplayed()); | ||
|
||
File tempFile = createTempFile(); | ||
|
||
fillPathToUploadInput(getInput(upload),tempFile.getPath(), tempFile.getPath(), | ||
tempFile.getPath()); | ||
|
||
WebElement eventsOutput = getDriver() | ||
.findElement(By.id("multi-upload-event-output")); | ||
|
||
Assert.assertEquals("Upload event order does not match expected", | ||
"-succeeded-succeeded-succeeded-finished", | ||
eventsOutput.getText()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.