diff options
author | Teemu Suo-Anttila <teemusa@vaadin.com> | 2015-02-20 15:00:17 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2015-03-12 14:56:21 +0000 |
commit | c08b0238e0aebcd167c15624c9f1188c871139b5 (patch) | |
tree | 6d59e20ac4428ded665dc70ca8f570d9df4d5df6 | |
parent | 7bcd8c29d80843336875f931c3b41f8f44f1d833 (diff) | |
download | vaadin-framework-c08b0238e0aebcd167c15624c9f1188c871139b5.tar.gz vaadin-framework-c08b0238e0aebcd167c15624c9f1188c871139b5.zip |
Migrate TB2 tests from package applicationcontext to TB4
ChangeSessionIdTest
CloseSessionTest
RpcForClosedUITest
Change-Id: I539d7194e67a839ac8d7bd4dbb81c1e4ddc3c1b7
7 files changed, 199 insertions, 303 deletions
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 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head profile="http://selenium-ide.openqa.org/profiles/test-case"> -<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> -<link rel="selenium.base" href="http://arturwin.office.itmill.com:8888/" /> -<title>ChangeSessionId</title> -</head> -<body> -<table cellpadding="1" cellspacing="1" border="1"> -<thead> -<tr><td rowspan="1" colspan="3">ChangeSessionId</td></tr> -</thead><tbody> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.ChangeSessionId?restartApplication</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_0</td> - <td>1. Session id: *</td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextChangeSessionId::/VVerticalLayout[0]/ChildComponentContainer[1]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_0</td> - <td>2. Session id changed successfully from * to *</td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextChangeSessionId::/VVerticalLayout[0]/ChildComponentContainer[2]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextChangeSessionId::PID_SLog_row_0</td> - <td>3. Session id: *</td> -</tr> -</tbody></table> -</body> -</html> 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 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head profile="http://selenium-ide.openqa.org/profiles/test-case"> -<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> -<link rel="selenium.base" href="" /> -<title>New Test</title> -</head> -<body> -<table cellpadding="1" cellspacing="1" border="1"> -<thead> -<tr><td rowspan="1" colspan="3">New Test</td></tr> -</thead><tbody> -<!--Close, reload and assert there's a new VaadinServiceSession in the old HttpSession--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>clickAndWait</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[2]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2</td> - <td>exact:4. Same hash as current? false</td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0</td> - <td>exact:6. Same WrappedSession id? true</td> -</tr> -<!--invalidate reload and assert there's a new VaadinServiceSession in a new HttpSession--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>clickAndWait</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[5]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_2</td> - <td>exact:4. Same hash as current? false</td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::PID_SLog_row_0</td> - <td>exact:6. Same WrappedSession id? false</td> -</tr> -<!--Test closing session and redirecting to another page--> -<tr> - <td>clickAndWait</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>//h1</td> - <td>This is a static file</td> -</tr> -<!--Open again and verify we get a Session Expired error if doing something after closing the VaadinSession--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[3]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]</td> - <td>Session Expired</td> -</tr> -<!--Open again and verify we get a Session Expired error if doing something after closing the HttpSession--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[4]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[4]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]</td> - <td>Session Expired</td> -</tr> -<!--Open again and verify we get a Session Expired error if closing HttpSession in a background thread--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[7]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>pause</td> - <td>2000</td> - <td>2000</td> -</tr> -<tr> - <td>click</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[7]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::Root/VNotification[0]/HTML[0]/domChild[0]</td> - <td>Session Expired</td> -</tr> -<!--Open again and test closing session and redirecting to another page--> -<tr> - <td>open</td> - <td>/run/com.vaadin.tests.applicationcontext.CloseSession?restartApplication</td> - <td></td> -</tr> -<tr> - <td>clickAndWait</td> - <td>vaadin=runcomvaadintestsapplicationcontextCloseSession::/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VVerticalLayout[0]/VOrderedLayout$Slot[1]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<tr> - <td>assertText</td> - <td>//h1</td> - <td>This is a static file</td> -</tr> -</tbody></table> -</body> -</html> 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 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> -<head profile="http://selenium-ide.openqa.org/profiles/test-case"> -<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> -<link rel="selenium.base" href="http://192.168.2.75:8888/" /> -<title>New Test</title> -</head> -<body> -<table cellpadding="1" cellspacing="1" border="1"> -<thead> -<tr><td rowspan="1" colspan="3">New Test</td></tr> -</thead><tbody> -<tr> - <td>open</td> - <td>/run/CloseUI?restartApplication</td> - <td></td> -</tr> -<!--Close the UI in a background thread--> -<tr> - <td>click</td> - <td>vaadin=runCloseUI::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<!--Try to log 'hello'--> -<tr> - <td>click</td> - <td>vaadin=runCloseUI::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[1]/VButton[0]/domChild[0]/domChild[0]</td> - <td></td> -</tr> -<!--Ensure 'hello' was not logged--> -<tr> - <td>assertText</td> - <td>vaadin=runCloseUI::PID_SLog_row_0</td> - <td>2. Current WrappedSession id: *</td> -</tr> -<tr> - <td>assertTextNotPresent</td> - <td>Hello</td> - <td></td> -</tr> -</tbody></table> -</body> -</html> |