2 * Copyright 2000-2014 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.Assert;
22 import org.junit.Test;
24 import com.vaadin.server.VaadinRequest;
25 import com.vaadin.server.VaadinSession;
26 import com.vaadin.ui.UI;
27 import com.vaadin.ui.VerticalLayout;
29 public class RemoveFromParentLockingTest {
31 private static VerticalLayout createTestComponent() {
32 VaadinSession session = new VaadinSession(null) {
33 private final ReentrantLock lock = new ReentrantLock();
36 public Lock getLockInstance() {
41 session.getLockInstance().lock();
45 protected void init(VaadinRequest request) {
48 ui.setSession(session);
50 VerticalLayout layout = new VerticalLayout();
51 ui.setContent(layout);
53 session.getLockInstance().unlock();
58 public void attachNoSessionLocked() {
59 VerticalLayout testComponent = createTestComponent();
61 VerticalLayout target = new VerticalLayout();
64 target.addComponent(testComponent);
65 throw new AssertionError(
66 "Moving component when not holding its sessions's lock should throw");
67 } catch (IllegalStateException e) {
69 "Cannot remove from parent when the session is not locked.",
75 public void attachSessionLocked() {
76 VerticalLayout testComponent = createTestComponent();
78 VerticalLayout target = new VerticalLayout();
80 testComponent.getUI().getSession().getLockInstance().lock();
82 target.addComponent(testComponent);
83 // OK if we get here without any exception
87 public void crossAttachOtherSessionLocked() {
88 VerticalLayout notLockedComponent = createTestComponent();
90 VerticalLayout lockedComponent = createTestComponent();
92 // Simulate the situation when attaching cross sessions
93 lockedComponent.getUI().getSession().getLockInstance().lock();
94 VaadinSession.setCurrent(lockedComponent.getUI().getSession());
97 lockedComponent.addComponent(notLockedComponent);
98 throw new AssertionError(
99 "Moving component when not holding its sessions's lock should throw");
100 } catch (IllegalStateException e) {
102 "Cannot remove from parent when the session is not locked."
103 + " Furthermore, there is another locked session, indicating that the component might be about to be moved from one session to another.",
109 public void crossAttachThisSessionLocked() {
110 VerticalLayout notLockedComponent = createTestComponent();
112 VerticalLayout lockedComponent = createTestComponent();
114 // Simulate the situation when attaching cross sessions
115 lockedComponent.getUI().getSession().getLockInstance().lock();
116 VaadinSession.setCurrent(lockedComponent.getUI().getSession());
119 notLockedComponent.addComponent(lockedComponent);
120 } catch (AssertionError e) {
121 // All is fine, don't care about the exact wording in this case