Search in sources :

Example 26 with StreamResource

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

the class ElementTest method setResourceAttribute_elementIsAttached_removeAttribute.

@Test
public void setResourceAttribute_elementIsAttached_removeAttribute() throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource");
    ui.getElement().setAttribute("foo", resource);
    String uri = ui.getElement().getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
    res = null;
    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;
    ui.getElement().removeAttribute("foo");
    TestUtil.isGarbageCollected(ref);
    res = ui.getSession().getResourceRegistry().getResource(StreamResource.class, new URI(uri));
    Assert.assertFalse(res.isPresent());
    Assert.assertFalse(ui.getElement().hasAttribute("foo"));
    Assert.assertNull(ui.getElement().getAttribute("foo"));
}
Also used : UI(com.vaadin.flow.component.UI) StreamResource(com.vaadin.flow.server.StreamResource) WeakReference(java.lang.ref.WeakReference) URI(java.net.URI) 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