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.

RemoveFromParentLockingTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.server.component.abstractsinglecomponentcontainer;
  17. import java.util.concurrent.locks.Lock;
  18. import java.util.concurrent.locks.ReentrantLock;
  19. import org.junit.Assert;
  20. import org.junit.Test;
  21. import com.vaadin.server.VaadinRequest;
  22. import com.vaadin.server.VaadinSession;
  23. import com.vaadin.ui.UI;
  24. import com.vaadin.ui.VerticalLayout;
  25. public class RemoveFromParentLockingTest {
  26. private static VerticalLayout createTestComponent() {
  27. VaadinSession session = new VaadinSession(null) {
  28. private final ReentrantLock lock = new ReentrantLock();
  29. @Override
  30. public Lock getLockInstance() {
  31. return lock;
  32. }
  33. };
  34. session.getLockInstance().lock();
  35. UI ui = new UI() {
  36. @Override
  37. protected void init(VaadinRequest request) {
  38. }
  39. };
  40. ui.setSession(session);
  41. VerticalLayout layout = new VerticalLayout();
  42. ui.setContent(layout);
  43. session.getLockInstance().unlock();
  44. return layout;
  45. }
  46. @Test
  47. public void attachNoSessionLocked() {
  48. VerticalLayout testComponent = createTestComponent();
  49. VerticalLayout target = new VerticalLayout();
  50. try {
  51. target.addComponent(testComponent);
  52. throw new AssertionError(
  53. "Moving component when not holding its sessions's lock should throw");
  54. } catch (IllegalStateException e) {
  55. Assert.assertEquals(
  56. "Cannot remove from parent when the session is not locked.",
  57. e.getMessage());
  58. }
  59. }
  60. @Test
  61. public void attachSessionLocked() {
  62. VerticalLayout testComponent = createTestComponent();
  63. VerticalLayout target = new VerticalLayout();
  64. testComponent.getUI().getSession().getLockInstance().lock();
  65. target.addComponent(testComponent);
  66. // OK if we get here without any exception
  67. }
  68. @Test
  69. public void crossAttachOtherSessionLocked() {
  70. VerticalLayout notLockedComponent = createTestComponent();
  71. VerticalLayout lockedComponent = createTestComponent();
  72. // Simulate the situation when attaching cross sessions
  73. lockedComponent.getUI().getSession().getLockInstance().lock();
  74. VaadinSession.setCurrent(lockedComponent.getUI().getSession());
  75. try {
  76. lockedComponent.addComponent(notLockedComponent);
  77. throw new AssertionError(
  78. "Moving component when not holding its sessions's lock should throw");
  79. } catch (IllegalStateException e) {
  80. Assert.assertEquals(
  81. "Cannot remove from parent when the session is not locked."
  82. + " Furthermore, there is another locked session, indicating that the component might be about to be moved from one session to another.",
  83. e.getMessage());
  84. } finally {
  85. VaadinSession.setCurrent(null);
  86. }
  87. }
  88. @Test
  89. public void crossAttachThisSessionLocked() {
  90. VerticalLayout notLockedComponent = createTestComponent();
  91. VerticalLayout lockedComponent = createTestComponent();
  92. // Simulate the situation when attaching cross sessions
  93. lockedComponent.getUI().getSession().getLockInstance().lock();
  94. VaadinSession.setCurrent(lockedComponent.getUI().getSession());
  95. try {
  96. notLockedComponent.addComponent(lockedComponent);
  97. } catch (AssertionError e) {
  98. // All is fine, don't care about the exact wording in this case
  99. } finally {
  100. VaadinSession.setCurrent(null);
  101. }
  102. }
  103. }