You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DetachOldUIOnReload.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.application;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import com.vaadin.server.VaadinRequest;
  20. import com.vaadin.tests.components.AbstractTestUIWithLog;
  21. import com.vaadin.ui.Button;
  22. import com.vaadin.ui.Button.ClickEvent;
  23. import com.vaadin.ui.Label;
  24. public class DetachOldUIOnReload extends AbstractTestUIWithLog {
  25. private static final String PERSISTENT_MESSAGES_ATTRIBUTE = DetachOldUIOnReload.class
  26. .getName() + ".sessionMessages";
  27. @Override
  28. protected void setup(VaadinRequest request) {
  29. addComponent(new Label("This is UI " + getUIId()));
  30. addComponent(new Button("Reload page", new Button.ClickListener() {
  31. @Override
  32. public void buttonClick(ClickEvent event) {
  33. getPage().reload();
  34. }
  35. }));
  36. addComponent(new Button("Read log messages from session",
  37. new Button.ClickListener() {
  38. @Override
  39. public void buttonClick(ClickEvent event) {
  40. for (String message : getSessionMessages(false)) {
  41. log(message);
  42. }
  43. }
  44. }));
  45. }
  46. private List<String> getSessionMessages(boolean storeIfNeeded) {
  47. List<String> messages = (List<String>) getSession().getAttribute(
  48. PERSISTENT_MESSAGES_ATTRIBUTE);
  49. if (messages == null) {
  50. messages = new ArrayList<String>();
  51. if (storeIfNeeded) {
  52. getSession().setAttribute(PERSISTENT_MESSAGES_ATTRIBUTE,
  53. messages);
  54. }
  55. }
  56. return messages;
  57. }
  58. private void logToSession(String message) {
  59. getSessionMessages(true).add(message);
  60. }
  61. @Override
  62. public void detach() {
  63. super.detach();
  64. logToSession("UI " + getUIId() + " has been detached");
  65. }
  66. @Override
  67. protected String getTestDescription() {
  68. return "Tests that the previous UI gets cleaned immediately when refreshing.";
  69. }
  70. @Override
  71. protected Integer getTicketNumber() {
  72. return Integer.valueOf(10338);
  73. }
  74. }