From: Teemu Suo-Anttila Date: Fri, 20 Feb 2015 13:00:17 +0000 (+0200) Subject: Migrate TB2 tests from package applicationcontext to TB4 X-Git-Tag: 7.5.0.alpha1~26 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c08b0238e0aebcd167c15624c9f1188c871139b5;p=vaadin-framework.git Migrate TB2 tests from package applicationcontext to TB4 ChangeSessionIdTest CloseSessionTest RpcForClosedUITest Change-Id: I539d7194e67a839ac8d7bd4dbb81c1e4ddc3c1b7 --- diff --git a/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionIdTest.java b/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionIdTest.java new file mode 100644 index 0000000000..ec1479ce15 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationcontext/ChangeSessionIdTest.java @@ -0,0 +1,27 @@ +package com.vaadin.tests.applicationcontext; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class ChangeSessionIdTest extends MultiBrowserTest { + + @Test + public void testSessionIdChange() throws Exception { + openTestURL(); + checkLogMatches("1. Session id: .*"); + $(ButtonElement.class).first().click(); + checkLogMatches("2. Session id changed successfully from .* to .*"); + $(ButtonElement.class).get(1).click(); + checkLogMatches("3. Session id: .*"); + } + + private void checkLogMatches(String expected) { + String actual = getLogRow(0); + Assert.assertTrue(String.format( + "Unexpected log row.\n expected format: '%s'\n was: '%s'", + expected, actual), actual.matches(expected)); + } +} diff --git a/uitest/src/com/vaadin/tests/applicationcontext/CloseSessionTest.java b/uitest/src/com/vaadin/tests/applicationcontext/CloseSessionTest.java new file mode 100644 index 0000000000..1d6bf3f9bf --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationcontext/CloseSessionTest.java @@ -0,0 +1,103 @@ +package com.vaadin.tests.applicationcontext; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.By; +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.MultiBrowserTest; +import com.vaadin.tests.tb3.newelements.FixedNotificationElement; + +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"); + 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"); + 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"); + Assert.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) { + Assert.assertEquals("Unexpected log text,", expected, getLogRow(index)); + } + + private void assertSessionExpired() { + String expected = "Session Expired"; + String actual = $(FixedNotificationElement.class).first().getCaption(); + Assert.assertEquals("Unexpected notification,", actual, expected); + } + + public void clickButton(String caption) { + $(ButtonElement.class).caption(caption).first().click(); + } +} diff --git a/uitest/src/com/vaadin/tests/applicationcontext/CloseUI.java b/uitest/src/com/vaadin/tests/applicationcontext/CloseUI.java index 9bfef35d8c..8f2cddd529 100644 --- a/uitest/src/com/vaadin/tests/applicationcontext/CloseUI.java +++ b/uitest/src/com/vaadin/tests/applicationcontext/CloseUI.java @@ -20,18 +20,15 @@ import com.vaadin.server.Page; import com.vaadin.server.VaadinRequest; import com.vaadin.server.VaadinService; import com.vaadin.server.VaadinSession; -import com.vaadin.tests.components.AbstractTestUI; -import com.vaadin.tests.util.Log; +import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.UI; -public class CloseUI extends AbstractTestUI { +public class CloseUI extends AbstractTestUIWithLog { private static final String OLD_HASH_PARAM = "oldHash"; private static final String OLD_SESSION_ID_PARAM = "oldSessionId"; - private final Log log = new Log(6); - @Override protected void setup(VaadinRequest request) { System.out.println("UI " + getUIId() + " inited"); @@ -39,59 +36,56 @@ public class CloseUI extends AbstractTestUI { final int sessionHash = getSession().hashCode(); final String sessionId = request.getWrappedSession().getId(); - log.log("Current session hashcode: " + sessionHash); - log.log("Current WrappedSession id: " + sessionId); + log("Current session hashcode: " + sessionHash); + log("Current WrappedSession id: " + sessionId); // Log previous values to make it easier to see what has changed String oldHashValue = request.getParameter(OLD_HASH_PARAM); if (oldHashValue != null) { - log.log("Old session hashcode: " + oldHashValue); - log.log("Same hash as current? " + log("Old session hashcode: " + oldHashValue); + log("Same hash as current? " + oldHashValue.equals(Integer.toString(sessionHash))); } String oldSessionId = request.getParameter(OLD_SESSION_ID_PARAM); if (oldSessionId != null) { - log.log("Old WrappedSession id: " + oldSessionId); - log.log("Same WrappedSession id? " + oldSessionId.equals(sessionId)); + log("Old WrappedSession id: " + oldSessionId); + log("Same WrappedSession id? " + oldSessionId.equals(sessionId)); } - addComponent(log); - addComponent(new Button("Log 'hello'", new Button.ClickListener() { + addButton("Log 'hello'", new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { - log.log("Hello"); + log("Hello"); } - })); - addComponent(new Button("Close UI", new Button.ClickListener() { + }); + addButton("Close UI", new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { close(); } - })); + }); - addComponent(new Button("Close UI (background)", - new Button.ClickListener() { + addButton("Close UI (background)", new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + new UIRunSafelyThread(CloseUI.this) { @Override - public void buttonClick(ClickEvent event) { - new UIRunSafelyThread(CloseUI.this) { - @Override - protected void runSafely() { - close(); - } - }.start(); + protected void runSafely() { + close(); } - })); - addComponent(new Button( - "Close UI and redirect to /statictestfiles/static.html", + }.start(); + } + }); + addButton("Close UI and redirect to /statictestfiles/static.html", new Button.ClickListener() { @Override public void buttonClick(ClickEvent event) { getPage().setLocation("/statictestfiles/static.html"); close(); } - })); - addComponent(new Button( + }); + addButton( "Close UI and redirect to /statictestfiles/static.html (background)", new Button.ClickListener() { @Override @@ -106,29 +100,8 @@ public class CloseUI extends AbstractTestUI { } }.start(); } - })); - - } - - private abstract class RunSafelyThread extends Thread { - private UI ui; - - public RunSafelyThread(UI ui) { - this.ui = ui; - } - - @Override - public void run() { - ui.accessSynchronously(new Runnable() { - - @Override - public void run() { - runSafely(); - } - }); - } + }); - protected abstract void runSafely(); } @Override @@ -144,17 +117,15 @@ public class CloseUI extends AbstractTestUI { @Override public void detach() { super.detach(); - log.log("Detach of " + this + " (" + getUIId() + ")"); + log("Detach of " + this + " (" + getUIId() + ")"); boolean correctUI = (UI.getCurrent() == this); boolean correctPage = (Page.getCurrent() == getPage()); boolean correctVaadinSession = (VaadinSession.getCurrent() == getSession()); boolean correctVaadinService = (VaadinService.getCurrent() == getSession() .getService()); - log.log("UI.current correct in detach: " + correctUI); - log.log("Page.current correct in detach: " + correctPage); - log.log("VaadinSession.current correct in detach: " - + correctVaadinSession); - log.log("VaadinService.current correct in detach: " - + correctVaadinService); + log("UI.current correct in detach: " + correctUI); + log("Page.current correct in detach: " + correctPage); + log("VaadinSession.current correct in detach: " + correctVaadinSession); + log("VaadinService.current correct in detach: " + correctVaadinService); } } diff --git a/uitest/src/com/vaadin/tests/applicationcontext/RpcForClosedUITest.java b/uitest/src/com/vaadin/tests/applicationcontext/RpcForClosedUITest.java new file mode 100644 index 0000000000..bb9d2a9294 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationcontext/RpcForClosedUITest.java @@ -0,0 +1,38 @@ +package com.vaadin.tests.applicationcontext; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class RpcForClosedUITest extends MultiBrowserTest { + @Override + protected Class getUIClass() { + return CloseUI.class; + } + + @Test + public void testRpcForUIClosedInBackground() throws Exception { + openTestURL(); + /* Close the UI in a background thread */ + clickButton("Close UI (background)"); + /* Try to log 'hello' */ + clickButton("Log 'hello'"); + /* Ensure 'hello' was not logged */ + checkLogMatches("2. Current WrappedSession id: .*"); + Assert.assertFalse("Page contains word 'Hello'", driver.getPageSource() + .contains("Hello")); + } + + private void clickButton(String caption) { + $(ButtonElement.class).caption(caption).first().click(); + } + + private void checkLogMatches(String expected) { + String actual = getLogRow(0); + Assert.assertTrue(String.format( + "Unexpected log row.\n expected format: '%s'\n was: '%s'", + expected, actual), actual.matches(expected)); + } +} diff --git a/uitest/tb2/com/vaadin/tests/applicationcontext/ChangeSessionId.html b/uitest/tb2/com/vaadin/tests/applicationcontext/ChangeSessionId.html deleted file mode 100644 index 6bf4041a93..0000000000 --- a/uitest/tb2/com/vaadin/tests/applicationcontext/ChangeSessionId.html +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - -ChangeSessionId - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ChangeSessionId
open/run/com.vaadin.tests.applicationcontext.ChangeSessionId?restartApplication
assertTextvaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_01. Session id: *
clickvaadin=runcomvaadintestsapplicationcontextChangeSessionId::/VVerticalLayout[0]/ChildComponentContainer[1]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_02. Session id changed successfully from * to *
clickvaadin=runcomvaadintestsapplicationcontextChangeSessionId::/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_03. Session id: *
- - diff --git a/uitest/tb2/com/vaadin/tests/applicationcontext/CloseSession.html b/uitest/tb2/com/vaadin/tests/applicationcontext/CloseSession.html deleted file mode 100644 index eb6e7681e6..0000000000 --- a/uitest/tb2/com/vaadin/tests/applicationcontext/CloseSession.html +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - -New Test - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
New Test
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[2]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2exact:4. Same hash as current? false
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0exact:6. Same WrappedSession id? true
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[5]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2exact:4. Same hash as current? false
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0exact:6. Same WrappedSession id? false
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertText//h1This is a static file
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]Session Expired
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[4]/VButton[0]/domChild[0]/domChild[0]
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[4]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]Session Expired
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[7]/VButton[0]/domChild[0]/domChild[0]
pause20002000
clickvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[7]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]Session Expired
open/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication
clickAndWaitvaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertText//h1This is a static file
- - diff --git a/uitest/tb2/com/vaadin/tests/applicationcontext/RpcForClosedUI.html b/uitest/tb2/com/vaadin/tests/applicationcontext/RpcForClosedUI.html deleted file mode 100644 index 642e31b22c..0000000000 --- a/uitest/tb2/com/vaadin/tests/applicationcontext/RpcForClosedUI.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - -New Test - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
New Test
open/run/CloseUI?restartApplication
clickvaadin=runCloseUI::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]
clickvaadin=runCloseUI::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[1]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runCloseUI::PID_SLog_row_02. Current WrappedSession id: *
assertTextNotPresentHello
- -