Skip to content

Commit

Permalink
feat: add download method to AnchorTester (#1789)
Browse files Browse the repository at this point in the history
Adds a download method to AnchorTester that allows testing that downloads for an anchor work properly.
  • Loading branch information
sissbruecker authored Apr 19, 2024
1 parent 483a0e6 commit 67fbea7
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.vaadin.flow.component.html.testbench;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -42,6 +43,15 @@ void anchorClick_navigatesCorrectly() {
"Click anchor did not navigate to AnchorView");
}

@Test
void anchorClick_disabled_throws() {
Anchor anchor = new Anchor("anchor", "Home");
anchor.setEnabled(false);
UI.getCurrent().add(anchor);

assertThrows(IllegalStateException.class, () -> test(anchor).click());
}

@Test
void anchorClick_notARegisteredRoute_throws() {
Anchor anchor = new Anchor("error", "Home");
Expand Down Expand Up @@ -69,4 +79,48 @@ void anchorClick_streamRegistration_throws() {
exception.getMessage());
}

@Test
void anchorDownload_writesResourceToOutputStream() {
StreamResource resource = new StreamResource("filename",
() -> new ByteArrayInputStream(
"Hello world".getBytes(StandardCharsets.UTF_8)));

Anchor anchor = new Anchor(resource, "Download");
UI.getCurrent().add(anchor);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
test(anchor).download(outputStream);

Assertions.assertEquals("Hello world",
outputStream.toString(StandardCharsets.UTF_8));
}

@Test
void anchorDownload_disabled_throws() {
StreamResource resource = new StreamResource("filename",
() -> new ByteArrayInputStream(
"Hello world".getBytes(StandardCharsets.UTF_8)));

Anchor anchor = new Anchor(resource, "Download");
anchor.setEnabled(false);
UI.getCurrent().add(anchor);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
assertThrows(IllegalStateException.class,
() -> test(anchor).download(outputStream));
}

@Test
void anchorDownload_noStreamRegistration_throws() {
Anchor anchor = new Anchor("anchor", "Download");
UI.getCurrent().add(anchor);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final IllegalStateException exception = assertThrows(
IllegalStateException.class,
() -> test(anchor).download(outputStream));

Assertions.assertEquals("Anchor target does not seem to be a resource",
exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@
*/
package com.vaadin.flow.component.html.testbench;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Optional;

import com.vaadin.flow.component.HasElement;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.html.Anchor;
import com.vaadin.flow.router.RouteConfiguration;
import com.vaadin.flow.server.AbstractStreamResource;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.server.StreamResourceRegistry;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.testbench.unit.Tests;

@Tests(Anchor.class)
Expand Down Expand Up @@ -67,4 +76,38 @@ public HasElement click() {
throw new IllegalStateException("Anchor target seems to be a resource");
}

/**
* Download the stream resource linked by the anchor.
*
* @param outputStream
* output stream to write the stream resource to
* @throws IllegalStateException
* if the anchor does not link to a stream resource
*/
public void download(OutputStream outputStream) {
ensureComponentIsUsable();

Anchor anchor = getComponent();
VaadinSession session = VaadinSession.getCurrent();
StreamResourceRegistry registry = session.getResourceRegistry();

Optional<AbstractStreamResource> maybeResource = Optional.empty();
try {
maybeResource = registry.getResource(new URI(anchor.getHref()));
} catch (URISyntaxException e) {
// Ignore, throws below if resource is empty
}

if (maybeResource.isEmpty() || !(maybeResource
.get() instanceof StreamResource streamResource)) {
throw new IllegalStateException(
"Anchor target does not seem to be a resource");
}

try {
streamResource.getWriter().accept(outputStream, session);
} catch (IOException e) {
throw new RuntimeException("Download failed", e);
}
}
}

0 comments on commit 67fbea7

Please sign in to comment.