Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 03a53e6be81f07c15888137154faecc314880813 (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
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (c) 2016 Eike Stepper (Loehne, 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.common.CDOCommonSession.Options.LockNotificationMode;
import org.eclipse.emf.cdo.common.lock.CDOLockState;
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.Category;
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.util.concurrent.IRWLockManager.LockType;
import org.eclipse.net4j.util.io.IOUtil;

import java.util.Collections;
import java.util.List;

/**
 * @author Esteban Dugueperoux
 */
public class Bugzilla_387563_Test extends AbstractCDOTest
{
  private CDOTransaction transaction;

  private Company company;

  private Category category1;

  private Category category2;

  private Category category3;

  private Category category4;

  private Category category5;

  @Override
  protected void doSetUp() throws Exception
  {
    super.doSetUp();

    CDOSession session = openSession();
    session.options().setLockNotificationMode(LockNotificationMode.ALWAYS);

    transaction = session.openTransaction();
    // transaction.options().setAutoReleaseLocksEnabled(false);

    CDOResource resource = transaction.createResource(getResourcePath("test1"));

    category1 = getModel1Factory().createCategory();
    category1.setName("category1");

    category2 = getModel1Factory().createCategory();
    category2.setName("category2");

    category3 = getModel1Factory().createCategory();
    category3.setName("category3");

    category4 = getModel1Factory().createCategory();
    category4.setName("category4");

    category5 = getModel1Factory().createCategory();
    category5.setName("category5");

    Category category6 = getModel1Factory().createCategory();
    category6.setName("category6");

    Category category7 = getModel1Factory().createCategory();
    category7.setName("category7");

    Category category8 = getModel1Factory().createCategory();
    category8.setName("category8");

    Category category9 = getModel1Factory().createCategory();
    category9.setName("category9");

    Category category10 = getModel1Factory().createCategory();
    category10.setName("category10");

    Category category11 = getModel1Factory().createCategory();
    category11.setName("category11");

    category1.getCategories().add(category2);
    category1.getCategories().add(category3);
    category2.getCategories().add(category4);
    category2.getCategories().add(category5);
    category3.getCategories().add(category8);
    category3.getCategories().add(category9);
    category4.getCategories().add(category6);
    category5.getCategories().add(category7);
    category8.getCategories().add(category10);
    category9.getCategories().add(category11);

    company = getModel1Factory().createCompany();
    company.getCategories().add(category1);

    resource.getContents().add(company);

    log("", Collections.singletonList(category1));
  }

  public void testPartialLockOnCommit() throws Exception
  {
    transaction.lockObjects(CDOUtil.getCDOObjects(category1), LockType.WRITE, DEFAULT_TIMEOUT, true);

    // Mark all objects to stay locked on commit
    transaction.options().addAutoReleaseLocksExemptions(true, category1);
    assertLockStatus(category1, true, true);

    // Mark category2 to be unlocked on commit
    transaction.options().removeAutoReleaseLocksExemptions(false, category2);
    assertLockStatus(category1, true, true);

    // Mark category3 and its descendants to be unlocked on commit
    transaction.options().removeAutoReleaseLocksExemptions(true, category3);
    assertLockStatus(category1, true, true);

    transaction.commit();
    assertLockStatus(category1, true, false);
    assertLockStatus(category2, false, false);
    assertLockStatus(category3, false, true);
    assertLockStatus(category4, true, true);
    assertLockStatus(category5, true, true);
  }

  public void testPartialUnlockOnCommit() throws Exception
  {
    transaction.lockObjects(CDOUtil.getCDOObjects(category1), LockType.WRITE, DEFAULT_TIMEOUT, true);

    // Mark category1 and its descendants to stay locked on commit
    transaction.options().addAutoReleaseLocksExemptions(true, category1);
    assertLockStatus(category1, true, true);

    transaction.unlockObjects(CDOUtil.getCDOObjects(category3), LockType.WRITE, true);
    assertLockStatus(category3, false, true);

    transaction.commit();
    assertLockStatus(category1, true, false);
    assertLockStatus(category2, true, true);
    assertLockStatus(category3, false, true);
  }

  private static void assertLockStatus(Category category, boolean expected, boolean recursive)
  {
    CDOObject cdoObject = CDOUtil.getCDOObject(category);
    CDOLockState lockState = cdoObject.cdoLockState();

    assertEquals("new object " + category.getName() + (expected ? " should be locally locked" : " shouldn't be locally locked."), expected,
        lockState.isLocked(LockType.WRITE, cdoObject.cdoView(), false));

    if (recursive)
    {
      for (Category subCategory : category.getCategories())
      {
        assertLockStatus(subCategory, expected, recursive);
      }
    }
  }

  private static void log(String indent, List<Category> categories)
  {
    for (Category category : categories)
    {
      IOUtil.OUT().println(indent + category.getName() + " --> " + CDOUtil.getCDOObject(category).cdoID());
      log(indent + "   ", category.getCategories());
    }
  }
}

Back to the top