Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: aa41715a225ff90aaeabf88b2aa56ebd0fdc11cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * Copyright (c) 2004 - 2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Steve Monnier - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOLock;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Customer;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.concurrent.IRWLockManager.LockType;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.impl.XMLResourceFactoryImpl;

import java.util.ArrayList;

/**
 * Bug 385268.
 *
 * @author Steve Monnier
 */
public class Bugzilla_402142_Test extends AbstractCDOTest
{
  /**
   * Scenario #1
   * <ol>
   * <li>User A : Create a CDOResource and add a Company root and commit
   * <li>User B : Lock the Company element
   * <li>User A : Ask if Company element is lockedByOther -> OK, it's locked
   * </ol>
   */
  public void testCheckLockByOther() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    transaction.options().setLockNotificationEnabled(true);

    CDOResource resource = transaction.createResource(getResourcePath("/my/resource"));
    Company company = getModel1Factory().createCompany();
    resource.getContents().add(company);
    transaction.commit();

    final CDOLock writeLock = CDOUtil.getCDOObject(company).cdoWriteLock();
    assertEquals(false, writeLock.isLockedByOthers());

    lockCompanyRemotely();
    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return writeLock.isLockedByOthers();
      }
    }.assertNoTimeOut();

    transaction.commit();
  }

  /**
   * Scenario #2
   * <ol>
   * <li>User A : Create a CDOResource and add a Company root and commit
   * <li>User A : Lock and Unlock the Company element
   * <li>User B : Lock the Company element
   * <li>User A : Ask if Company element is lockedByOther -> OK, it's locked
   * </ol>
   */
  public void testCheckLockByOtherAfterLockUnlock() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    transaction.options().setLockNotificationEnabled(true);

    CDOResource resource = transaction.createResource(getResourcePath("/my/resource"));
    Company company = getModel1Factory().createCompany();
    resource.getContents().add(company);
    transaction.commit();

    final CDOLock writeLock = CDOUtil.getCDOObject(company).cdoWriteLock();
    writeLock.lock(DEFAULT_TIMEOUT);
    writeLock.unlock();
    assertEquals(false, writeLock.isLockedByOthers());

    lockCompanyRemotely();
    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return writeLock.isLockedByOthers();
      }
    }.assertNoTimeOut();

    transaction.commit();
  }

  /**
   * Scenario #3
   * <ol>
   * <li>Create a local resource and a CDOResource
   * <li>Add an element "company1" to CDOResource
   * <li>Add an element "company2" to the local resource
   * <li>Add a Customer to "comapny1" commit
   * <li>Move the customer to the local resource
   * <li>Lock "company1" and customer (to avoid conflict from a distant user)
   * <li>Commit
   * <li>Move back the customer to "company1"
   * <li>Add lock to customer (lock on new object)
   * <li>Commit -> The new object should not have the lockstate of the former one
   * </ol>
   */
  public void testLockUnlockOnElementMovedBetweenResources() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    transaction.options().setLockNotificationEnabled(true);

    CDOResource resource = transaction.createResource(getResourcePath("/my/resource"));
    Company company = getModel1Factory().createCompany();
    resource.getContents().add(company);
    Customer customer = getModel1Factory().createCustomer();
    company.getCustomers().add(customer);
    transaction.commit();

    ResourceSet resourceSet = transaction.getResourceSet();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("model1", new XMLResourceFactoryImpl());
    URI localURI = URI.createFileURI(createTempFile("resource", ".model1").getCanonicalPath());
    Resource resource2 = resourceSet.createResource(localURI);
    Company company2 = getModel1Factory().createCompany();
    resource2.getContents().add(company2);
    company2.getCustomers().add(customer); // Move the customer to the local resource (detach)

    ArrayList<CDOObject> objectsToLock = new ArrayList<CDOObject>();
    objectsToLock.add(CDOUtil.getCDOObject(company));
    objectsToLock.add(CDOUtil.getCDOObject(customer));
    transaction.lockObjects(objectsToLock, LockType.WRITE, DEFAULT_TIMEOUT);

    transaction.commit();

    company.getCustomers().add(customer); // Move the customer to the CDO resource (attach new)
    objectsToLock.add(CDOUtil.getCDOObject(customer)); // New object
    transaction.lockObjects(objectsToLock, LockType.WRITE, DEFAULT_TIMEOUT);

    transaction.commit();
  }

  private void lockCompanyRemotely() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.getResource(getResourcePath("/my/resource"));
    CDOLock writeLock = CDOUtil.getCDOObject(resource.getContents().get(0)).cdoWriteLock();
    writeLock.lock(DEFAULT_TIMEOUT);
  }
}

Back to the top