Search in sources :

Example 11 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceRegistryTest method unregisterResource_resourceIsRemoved.

@Test
public void unregisterResource_resourceIsRemoved() {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration = registry.registerResource(resource);
    Assert.assertNotNull(registration);
    URI uri = registration.getResourceUri();
    registration.unregister();
    Optional<StreamResource> stored = registry.getResource(StreamResource.class, uri);
    Assert.assertFalse("Unexpected stored resource is found after unregister()", stored.isPresent());
    Assert.assertFalse("Unexpected resource is returned by the registration instance", registration.getResource() != null);
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Example 12 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceRegistryTest method registerResource_registrationResultCanBeFound.

@Test
public void registerResource_registrationResultCanBeFound() {
    StreamResourceRegistry registry = session.getResourceRegistry();
    StreamResource resource = new StreamResource("name", () -> makeEmptyStream());
    StreamRegistration registration = registry.registerResource(resource);
    Assert.assertNotNull(registration);
    URI uri = registration.getResourceUri();
    Optional<StreamResource> stored = registry.getResource(StreamResource.class, uri);
    Assert.assertSame("Unexpected stored resource is returned for registered URI", resource, stored.get());
    Assert.assertSame("Unexpected resource is returned by the registration instance", resource, registration.getResource());
}
Also used : StreamResourceRegistry(com.vaadin.flow.server.StreamResourceRegistry) StreamResource(com.vaadin.flow.server.StreamResource) StreamRegistration(com.vaadin.flow.server.StreamRegistration) URI(java.net.URI) Test(org.junit.Test)

Example 13 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceTest method setContentTypeResolver.

@Test
public void setContentTypeResolver() {
    StreamResource resource = new StreamResource("foo", () -> makeEmptyStream());
    resource.setContentTypeResolver((res, context) -> "bar");
    Assert.assertNotNull(resource.getContentTypeResolver());
    assertContentType(resource, resource.getContentTypeResolver());
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) Test(org.junit.Test)

Example 14 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class StreamResourceTest method getDefaultContentTypeResolver.

@Test
public void getDefaultContentTypeResolver() {
    StreamResource resource = new StreamResource("foo", () -> makeEmptyStream());
    ContentTypeResolver resolver = resource.getContentTypeResolver();
    Assert.assertNotNull(resolver);
    assertContentType(resource, resolver);
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) ContentTypeResolver(com.vaadin.flow.function.ContentTypeResolver) Test(org.junit.Test)

Example 15 with StreamResource

use of com.vaadin.flow.server.StreamResource in project flow by vaadin.

the class ElementTest method setResourceAttribute_elementIsNotAttached_elementHasAttribute.

@Test
public void setResourceAttribute_elementIsNotAttached_elementHasAttribute() {
    UI.setCurrent(createUI());
    Element element = ElementFactory.createDiv();
    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));
}
Also used : StreamResource(com.vaadin.flow.server.StreamResource) Element(com.vaadin.flow.dom.Element) Test(org.junit.Test)

Aggregations

StreamResource (com.vaadin.flow.server.StreamResource)26 Test (org.junit.Test)24 UI (com.vaadin.flow.component.UI)14 Element (com.vaadin.flow.dom.Element)13 URI (java.net.URI)12 WeakReference (java.lang.ref.WeakReference)9 StreamRegistration (com.vaadin.flow.server.StreamRegistration)4 StreamResourceRegistry (com.vaadin.flow.server.StreamResourceRegistry)4 Anchor (com.vaadin.flow.component.html.Anchor)1 ContentTypeResolver (com.vaadin.flow.function.ContentTypeResolver)1 AbstractStreamResource (com.vaadin.flow.server.AbstractStreamResource)1 StreamReceiver (com.vaadin.flow.server.StreamReceiver)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1