Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bc3bc6db61e3e8e1947ecaf0c14043e52664b9b1 (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
/*
 * Copyright (c) 2015, 2016 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:
 *    Esteban Dugueperoux - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOState;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.lock.CDOLockState;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.net4j.protocol.LockStateRequest;
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.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.signal.ISignalProtocol;
import org.eclipse.net4j.signal.SignalCounter;
import org.eclipse.net4j.util.concurrent.IRWLockManager.LockType;

import org.eclipse.emf.ecore.util.EcoreUtil;

import java.util.Collections;

/**
 * Bug 466951 to avoid {@link LockStateRequest} for {@link CDOObject} in {@link CDOState#NEW} state.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_466951_Test extends AbstractCDOTest
{
  private static final String RESOURCE_NAME = "test1.model1";

  /**
   * Test that no {@link LockStateRequest} is sent to the server when calling {@link CDOObject#cdoLockState()} for {@link CDOObject} in {@link CDOState#NEW} state.
   */
  public void testCDOObjectCDOLockState() throws Exception
  {
    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    transaction1.options().setAutoReleaseLocksEnabled(false);
    transaction1.options().setLockNotificationEnabled(true);
    CDOResource resource1 = transaction1.createResource(getResourcePath(RESOURCE_NAME));
    Company company = getModel1Factory().createCompany();
    resource1.getContents().add(company);

    ISignalProtocol<?> protocol = ((org.eclipse.emf.cdo.net4j.CDONet4jSession)session1).options().getNet4jProtocol();
    SignalCounter signalCounter = new SignalCounter(protocol);

    try
    {
      assertEquals(0, signalCounter.removeCountFor(LockStateRequest.class));

      CDOObject companyCDOObject = CDOUtil.getCDOObject(company);
      CDOLockState lockState = companyCDOObject.cdoLockState();
      Object expectedLockedObject = companyCDOObject.cdoID();
      if (session1.getRepositoryInfo().isSupportingBranches())
      {
        expectedLockedObject = CDOIDUtil.createIDAndBranch(companyCDOObject.cdoID(), transaction1.getBranch());
      }

      assertEquals(expectedLockedObject, lockState.getLockedObject());
      assertTrue(lockState.getReadLockOwners().isEmpty());
      assertNull(lockState.getWriteLockOwner());
      assertNull(lockState.getWriteOptionOwner());

      transaction1.lockObjects(Collections.singleton(companyCDOObject), LockType.WRITE, -1);

      lockState = companyCDOObject.cdoLockState();
      expectedLockedObject = companyCDOObject.cdoID();
      if (session1.getRepositoryInfo().isSupportingBranches())
      {
        expectedLockedObject = CDOIDUtil.createIDAndBranch(companyCDOObject.cdoID(), transaction1.getBranch());
      }

      assertEquals(expectedLockedObject, lockState.getLockedObject());
      assertTrue(lockState.getReadLockOwners().isEmpty());
      assertEquals(transaction1, lockState.getWriteLockOwner());
      assertNull(lockState.getWriteOptionOwner());

      EcoreUtil.delete(company);
      assertEquals(CDOState.TRANSIENT, companyCDOObject.cdoState());
      lockState = companyCDOObject.cdoLockState();
      assertNull(lockState);

      Company company2 = getModel1Factory().createCompany();
      resource1.getContents().add(company2);

      CDOObject companyCDOObject2 = CDOUtil.getCDOObject(company2);
      lockState = companyCDOObject2.cdoLockState();
      expectedLockedObject = companyCDOObject2.cdoID();
      if (session1.getRepositoryInfo().isSupportingBranches())
      {
        expectedLockedObject = CDOIDUtil.createIDAndBranch(companyCDOObject2.cdoID(), transaction1.getBranch());
      }

      assertEquals(expectedLockedObject, lockState.getLockedObject());
      assertTrue(lockState.getReadLockOwners().isEmpty());
      assertNull(lockState.getWriteLockOwner());
      assertNull(lockState.getWriteOptionOwner());
      assertEquals(0, signalCounter.removeCountFor(LockStateRequest.class));

      transaction1.commit();

      lockState = companyCDOObject2.cdoLockState();
      expectedLockedObject = companyCDOObject2.cdoID();
      if (session1.getRepositoryInfo().isSupportingBranches())
      {
        expectedLockedObject = CDOIDUtil.createIDAndBranch(companyCDOObject2.cdoID(), transaction1.getBranch());
      }

      assertEquals(expectedLockedObject, lockState.getLockedObject());
      assertTrue(lockState.getReadLockOwners().isEmpty());
      assertNull(lockState.getWriteLockOwner());
      assertNull(lockState.getWriteOptionOwner());
      assertEquals(0, signalCounter.removeCountFor(LockStateRequest.class));
    }
    finally
    {
      signalCounter.dispose();
    }
  }
}

Back to the top