aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/applicationcontext/CloseSessionTest.java
blob: c58f51373bf207ee58a2935dd4ad350036c52aeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.vaadin.tests.applicationcontext;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.vaadin.testbench.By;
import com.vaadin.testbench.elements.ButtonElement;
import com.vaadin.testbench.elements.NotificationElement;
import com.vaadin.testbench.elements.UIElement;
import com.vaadin.tests.tb3.MultiBrowserTest;

public class CloseSessionTest extends MultiBrowserTest {

    @Override
    public void setup() throws Exception {
        super.setup();
        openTestURL();
    }

    /**
     * Close, reload and assert there's a new VaadinServiceSession in the old
     * HttpSession.
     */
    @Test
    public void testCloseAndReopen() {
        clickButton("Close VaadinServiceSession and reopen page");
        waitUntil(driver -> isElementPresent(UIElement.class));
        assertLogText(2, "4. Same hash as current? false");
        assertLogText(0, "6. Same WrappedSession id? true");
    }

    /**
     * Invalidate, reload and assert there's a new VaadinServiceSession in a new
     * HttpSession.
     */
    @Test
    public void testInvalidateHttpSessionAndReopen() {
        clickButton("Invalidate HttpSession and reopen page");
        waitUntil(driver -> isElementPresent(UIElement.class));
        assertLogText(2, "4. Same hash as current? false");
        assertLogText(0, "6. Same WrappedSession id? false");
    }

    /**
     * Test closing session and redirecting to another page.
     */
    @Test
    public void testCloseVaadinServiceAndRedirect() {
        clickButton("Close VaadinServiceSession and redirect elsewhere");
        assertEquals("Unexpected page contents,", "This is a static file",
                findElement(By.xpath("//h1")).getText());
    }

    /**
     * Verify we get a Session Expired error if doing something after closing
     * the VaadinSession.
     */
    @Test
    public void testCloseVaadinSession() {
        String caption = "Just close VaadinSession";
        clickButton(caption);
        clickButton(caption);
        assertSessionExpired();
    }

    /**
     * Verify we get a Session Expired error if doing something after closing
     * the HttpSession.
     */
    @Test
    public void testCloseHttpSession() {
        String caption = "Just close HttpSession";
        clickButton(caption);
        clickButton(caption);
        assertSessionExpired();
    }

    /**
     * Verify we get a Session Expired error if closing HttpSession in a
     * background thread.
     */
    @Test
    public void testBackgroundThreadHttpSessionInvalidation()
            throws InterruptedException {
        String caption = "Invalidate HttpSession in a background thread";
        clickButton(caption);
        sleep(2000);
        clickButton(caption);
        assertSessionExpired();
    }

    private void assertLogText(int index, String expected) {
        assertEquals("Unexpected log text,", expected, getLogRow(index));
    }

    private void assertSessionExpired() {
        String expected = "Session Expired";
        String actual = $(NotificationElement.class).first().getCaption();
        assertEquals("Unexpected notification,", actual, expected);
    }

    public void clickButton(String caption) {
        $(ButtonElement.class).caption(caption).first().click();
    }
}