Java Code Examples for com.vaadin.flow.component.UI#setCurrent()

The following examples show how to use com.vaadin.flow.component.UI#setCurrent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ElementTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void setResourceAttribute_attachElement_removeAttribute()
        throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);

    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);

    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;

    element.removeAttribute("foo");

    ui.getElement().appendChild(element);

    TestUtil.isGarbageCollected(ref);

    Assert.assertFalse(element.hasAttribute("foo"));

    Assert.assertNull(element.getAttribute("foo"));
}
 
Example 2
Source File: ElementTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void setResourceAttribute_attachElement_setRawAttribute()
        throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);

    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);

    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;

    element.setAttribute("foo", "bar");

    TestUtil.isGarbageCollected(ref);

    ui.getElement().appendChild(element);

    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertEquals("bar", element.getAttribute("foo"));
}
 
Example 3
Source File: FlowClassesSerializableTest.java    From flow with Apache License 2.0 6 votes vote down vote up
/**
 * Tests a serialization bug (probably located in JVM ) when serialized
 * {@link Command} is deserialized as some internal lambda and produces
 * {@link ClassCastException}
 *
 * see the workaround in ElementAttributeMap#deferRegistration
 */
@Test
public void streamResource() throws Throwable {
    UI ui = new UI();
    UI.setCurrent(ui);
    try {
        Element element = new Element("dummy-element");
        StreamReceiver streamReceiver = new StreamReceiver(
                element.getNode(), "upload", new MyStreamVariable());
        Assert.assertEquals(ui, UI.getCurrent());
        element.setAttribute("target", streamReceiver);
        serializeAndDeserialize(element);
        assertTrue("Basic smoke test with ",
                element.getAttribute("target").length() > 10);

    } finally {
        UI.setCurrent(null);
    }
}
 
Example 4
Source File: ElementTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void setResourceAttribute_attachElement_resourceIsRegistered()
        throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);

    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);

    ui.getElement().appendChild(element);

    Assert.assertTrue(element.hasAttribute("foo"));

    String uri = element.getAttribute("foo");
    Optional<StreamResource> res = ui.getSession().getResourceRegistry()
            .getResource(StreamResource.class, new URI(uri));
    Assert.assertTrue(res.isPresent());
}
 
Example 5
Source File: ElementTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void setResourceAttributeSeveralTimes_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"));

    resName = "resource1";
    resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);

    Assert.assertTrue(element.hasAttribute("foo"));

    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));
}
 
Example 6
Source File: ElementTest.java    From flow with Apache License 2.0 6 votes vote down vote up
@Test
public void setResourceAttribute_elementIsAttached_setAnotherResource()
        throws URISyntaxException {
    UI ui = createUI();
    UI.setCurrent(ui);
    StreamResource resource = createEmptyResource("resource1");
    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());

    String resName = "resource2";
    ui.getElement().setAttribute("foo", createEmptyResource(resName));
    res = ui.getSession().getResourceRegistry()
            .getResource(StreamResource.class, new URI(uri));
    Assert.assertFalse(res.isPresent());

    Assert.assertTrue(ui.getElement().hasAttribute("foo"));
    Assert.assertTrue(
            ui.getElement().getAttribute("foo").endsWith(resName));
}
 
Example 7
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 5 votes vote down vote up
@Test
void testSameSessionDifferentUIReturnsSameSessionEventBus() {
    EventBus.SessionEventBus sessionBus = applicationContext.getBean(EventBus.SessionEventBus.class);
    UI.setCurrent(createMockUI());
    EventBus.SessionEventBus sessionBus2 = applicationContext.getBean(EventBus.SessionEventBus.class);
    assertEquals(sessionBus, sessionBus2, "Same session different UIs should return same SessionEventBus");
}
 
Example 8
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 5 votes vote down vote up
@Test
void testDifferentUIReturnsDifferentUIEventBus() {
    EventBus.UIEventBus uiBus = applicationContext.getBean(EventBus.UIEventBus.class);
    UI.setCurrent(createMockUI());
    EventBus.UIEventBus uiBus2 = applicationContext.getBean(EventBus.UIEventBus.class);
    assertNotEquals(uiBus, uiBus2, "Different UIs should return different UIEventBuses");
}
 
Example 9
Source File: ElementTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void setResourceAttribute_detachElement_resourceIsUnregistered()
        throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);
    Element element = ElementFactory.createDiv();
    ui.getElement().appendChild(element);

    String resName = "resource";
    StreamResource resource = createEmptyResource(resName);
    element.setAttribute("foo", resource);
    String attribute = element.getAttribute("foo");

    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;

    URI uri = new URI(attribute);
    Optional<StreamResource> res = ui.getSession().getResourceRegistry()
            .getResource(StreamResource.class, uri);
    Assert.assertTrue(res.isPresent());

    ui.getElement().removeAllChildren();

    res = ui.getSession().getResourceRegistry()
            .getResource(StreamResource.class, uri);
    Assert.assertFalse(res.isPresent());

    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertNotNull(element.getAttribute("foo"));
    Assert.assertTrue(element.getAttribute("foo").endsWith(resName));

    element.setAttribute("foo", "bar");
    Assert.assertTrue(element.hasAttribute("foo"));
    Assert.assertEquals("bar", element.getAttribute("foo"));

    TestUtil.isGarbageCollected(ref);
}
 
Example 10
Source File: BeanBinderTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    ui = new UI();
    ui.setLocale(Locale.ENGLISH);
    UI.setCurrent(ui);

    binder = new BeanValidationBinder<>(BeanToValidate.class);
    item = new BeanToValidate();
    item.setFirstname("Johannes");
    item.setAge(32);
}
 
Example 11
Source File: EventUtilTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    VaadinSession session = Mockito.mock(VaadinSession.class);
    UI ui = new UI() {
        @Override
        public VaadinSession getSession() {
            return session;
        }
    };
    VaadinService service = Mockito.mock(VaadinService.class);
    when(session.getService()).thenReturn(service);
    DefaultInstantiator instantiator = new DefaultInstantiator(service);
    when(service.getInstantiator()).thenReturn(instantiator);
    UI.setCurrent(ui);
}
 
Example 12
Source File: ValueContextTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    setLocale(UI_LOCALE);
    UI.setCurrent(this);
    textField = new TestTextField();
    add(textField);
}
 
Example 13
Source File: ValueContextTest.java    From flow with Apache License 2.0 5 votes vote down vote up
@Test
public void getLocale_localeComesFromComponentUI() {
    UI.setCurrent(null);

    UI ui = new UI();
    ui.setLocale(Locale.GERMAN);

    Text text = new Text("");
    ui.add(text);
    ValueContext context = new ValueContext(text);

    Assert.assertEquals(Locale.GERMAN, context.getLocale().get());
}
 
Example 14
Source File: StreamReceiverHandler.java    From vertx-vaadin with MIT License 5 votes vote down vote up
/**
 * Handle reception of incoming stream from the client.
 *
 * @param session        The session for the request
 * @param request        The request to handle
 * @param response       The response object to which a response can be written.
 * @param streamReceiver the receiver containing the destination stream variable
 * @param uiId           id of the targeted ui
 * @param securityKey    security from the request that should match registered stream
 *                       receiver id
 * @throws IOException if an IO error occurred
 */
public void handleRequest(VaadinSession session, VaadinRequest request,
                          VaadinResponse response, StreamReceiver streamReceiver, String uiId,
                          String securityKey) throws IOException {
    StateNode source;

    session.lock();
    try {
        String secKey = streamReceiver.getId();
        if (secKey == null || !secKey.equals(securityKey)) {
            getLogger().warn(
                "Received incoming stream with faulty security key.");
            return;
        }

        UI ui = session.getUIById(Integer.parseInt(uiId));
        UI.setCurrent(ui);

        source = streamReceiver.getNode();

    } finally {
        session.unlock();
    }

    try {
        Set<FileUpload> fileUploads = ((VertxVaadinRequest) request).getRoutingContext().fileUploads();
        if (!fileUploads.isEmpty()) {
            doHandleMultipartFileUpload(session, request, response, fileUploads, streamReceiver, source);
        } else {
            // if boundary string does not exist, the posted file is from
            // XHR2.post(File)
            doHandleXhrFilePost(session, request, response, streamReceiver,
                source, getContentLength(request));
        }
    } finally {
        UI.setCurrent(null);
    }
}
 
Example 15
Source File: ScopedEventBusIntegrationTest.java    From vaadin4spring with Apache License 2.0 4 votes vote down vote up
@Test
void testNoUIThrowsBeanCreationException() {
    UI.setCurrent(null);
    assertThrows(BeanCreationException.class, () -> applicationContext.getBean(EventBus.UIEventBus.class));
}
 
Example 16
Source File: PageTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    UI.setCurrent(null);
}
 
Example 17
Source File: BeanValidatorTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    UI.setCurrent(null);
}
 
Example 18
Source File: BeanBinderTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    UI.setCurrent(null);
}
 
Example 19
Source File: ElementTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@Test
public void setResourceAttribute_attachElement_setRawAttributeAfterAttaching()
        throws URISyntaxException, InterruptedException {
    UI ui = createUI();
    UI.setCurrent(ui);

    StreamResource resource = createEmptyResource("resource");
    Element element = ElementFactory.createDiv();
    element.setAttribute("foo", resource);

    WeakReference<StreamResource> ref = new WeakReference<>(resource);
    resource = null;

    ui.getElement().appendChild(element);

    element.setAttribute("foo", "bar");

    TestUtil.isGarbageCollected(ref);

    Assert.assertNull(ref.get());

    Assert.assertTrue(element.hasAttribute("foo"));

    Assert.assertEquals("bar", element.getAttribute("foo"));
}
 
Example 20
Source File: PolymerTemplateTest.java    From flow with Apache License 2.0 4 votes vote down vote up
@After
public void tearDown() {
    UI.setCurrent(null);
}