2 * Copyright 2000-2016 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.tests.server.component.abstractsinglecomponentcontainer;
18 import java.util.concurrent.locks.Lock;
19 import java.util.concurrent.locks.ReentrantLock;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
26 import com.vaadin.server.VaadinRequest;
27 import com.vaadin.server.VaadinSession;
28 import com.vaadin.ui.UI;
29 import com.vaadin.ui.VerticalLayout;
31 public class RemoveFromParentLockingTest {
35 public void cleanCurrentSession() {
36 VaadinSession.setCurrent(null);
39 private static VerticalLayout createTestComponent() {
40 VaadinSession session = new VaadinSession(null) {
41 private final ReentrantLock lock = new ReentrantLock();
44 public Lock getLockInstance() {
49 session.getLockInstance().lock();
53 protected void init(VaadinRequest request) {
56 ui.setSession(session);
58 VerticalLayout layout = new VerticalLayout();
59 ui.setContent(layout);
61 session.getLockInstance().unlock();
66 public void attachNoSessionLocked() {
67 VerticalLayout testComponent = createTestComponent();
69 VerticalLayout target = new VerticalLayout();
72 target.addComponent(testComponent);
73 throw new AssertionError(
74 "Moving component when not holding its sessions's lock should throw");
75 } catch (IllegalStateException e) {
77 "Cannot remove from parent when the session is not locked.",
83 public void attachSessionLocked() {
84 VerticalLayout testComponent = createTestComponent();
86 VerticalLayout target = new VerticalLayout();
88 testComponent.getUI().getSession().getLockInstance().lock();
90 target.addComponent(testComponent);
91 // OK if we get here without any exception
95 public void crossAttachOtherSessionLocked() {
96 VerticalLayout notLockedComponent = createTestComponent();
98 VerticalLayout lockedComponent = createTestComponent();
100 // Simulate the situation when attaching cross sessions
101 lockedComponent.getUI().getSession().getLockInstance().lock();
102 VaadinSession.setCurrent(lockedComponent.getUI().getSession());
105 lockedComponent.addComponent(notLockedComponent);
106 throw new AssertionError(
107 "Moving component when not holding its sessions's lock should throw");
108 } catch (IllegalStateException e) {
110 "Cannot remove from parent when the session is not locked."
111 + " Furthermore, there is another locked session, indicating that the component might be about to be moved from one session to another.",
117 public void crossAttachThisSessionLocked() {
118 VerticalLayout notLockedComponent = createTestComponent();
120 VerticalLayout lockedComponent = createTestComponent();
122 // Simulate the situation when attaching cross sessions
123 lockedComponent.getUI().getSession().getLockInstance().lock();
124 VaadinSession.setCurrent(lockedComponent.getUI().getSession());
127 notLockedComponent.addComponent(lockedComponent);
128 } catch (AssertionError e) {
129 // All is fine, don't care about the exact wording in this case