Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: fad9c66cd34dcd4b50d18d5dee7c84d7580da81a (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (c) 2004 - 2012 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.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.junit.Assert;

import java.util.Collections;

/**
 * @author Esteban Dugueperoux
 */
public class Bugzilla_355045_Test extends AbstractCDOTest
{
  private static final String RESOURCE_PATH = "/test1";

  private CDOTransaction transaction;

  private Company company;

  private Category category1;

  @Override
  public void setUp() throws Exception
  {
    super.setUp();
    CDOSession cdoSession = openSession();
    cdoSession.options().setLockNotificationMode(LockNotificationMode.ALWAYS);
    transaction = cdoSession.openTransaction();
    // cdoTransaction.options().setAutoReleaseLocksEnabled(false);
    CDOResource cdoResource = transaction.createResource(getResourcePath(RESOURCE_PATH));

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

    cdoResource.getContents().add(company);
    cdoResource.save(Collections.emptyMap());
  }

  public void testLockOnCommitOfSingleNewObject() throws Exception
  {
    Category category2 = getModel1Factory().createCategory();
    category2.setName("category2");
    category1.getCategories().add(category2);

    transaction.lockObjects(CDOUtil.getCDOObjects(category2), LockType.WRITE, DEFAULT_TIMEOUT);
    transaction.options().addAutoReleaseLocksExemptions(true, category2);
    assertLockStatus(category2, true, false);

    transaction.commit();
    assertEquals(CDOState.CLEAN, CDOUtil.getCDOObject(category2).cdoState());
    assertLockStatus(category2, true, false);
  }

  public void testRecursiveLockOnCommitOfNewObjectsTree() throws Exception
  {
    Category category2 = getModel1Factory().createCategory();
    category2.setName("category2");
    Category category4 = getModel1Factory().createCategory();
    category4.setName("category4");
    Category category5 = getModel1Factory().createCategory();
    category5.setName("category5");
    Category category6 = getModel1Factory().createCategory();
    category6.setName("category6");
    Category category7 = getModel1Factory().createCategory();
    category7.setName("category7");

    category2.getCategories().add(category4);
    category2.getCategories().add(category5);
    category4.getCategories().add(category6);
    category5.getCategories().add(category7);
    category1.getCategories().add(category2);

    Category category3 = getModel1Factory().createCategory();
    category3.setName("category3");
    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");

    category3.getCategories().add(category8);
    category3.getCategories().add(category9);
    category8.getCategories().add(category10);
    category9.getCategories().add(category11);
    category1.getCategories().add(category3);

    transaction.lockObjects(CDOUtil.getCDOObjects(category2), LockType.WRITE, DEFAULT_TIMEOUT, true);
    transaction.options().addAutoReleaseLocksExemptions(true, category2);

    assertLockStatus(category1, false, false);
    assertLockStatus(category2, true, true);
    assertLockStatus(category3, false, true);

    transaction.lockObjects(CDOUtil.getCDOObjects(category3), LockType.WRITE, DEFAULT_TIMEOUT, true);
    transaction.options().addAutoReleaseLocksExemptions(true, category3);

    assertLockStatus(category1, false, false);
    assertLockStatus(category2, true, true);
    assertLockStatus(category3, true, true);

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

    assertLockStatus(category1, false, false);
    assertLockStatus(category2, true, true);
    assertLockStatus(category3, false, false);
    assertLockStatus(category8, true, true);
    assertLockStatus(category9, true, true);

    transaction.commit();

    assertLockStatus(category1, false, false);
    assertLockStatus(category2, true, true);
    assertLockStatus(category3, false, false);
    assertLockStatus(category8, true, true);
    assertLockStatus(category9, true, true);
  }

  public void testRecursiveLockOfObjectsTreeContainingASubTreeOfNewObjects() throws Exception
  {
    Category category2 = getModel1Factory().createCategory();
    category2.setName("category2");
    Category category4 = getModel1Factory().createCategory();
    category4.setName("category4");
    Category category5 = getModel1Factory().createCategory();
    category5.setName("category5");
    Category category6 = getModel1Factory().createCategory();
    category6.setName("category6");
    Category category7 = getModel1Factory().createCategory();
    category7.setName("category7");

    category2.getCategories().add(category4);
    category2.getCategories().add(category5);
    category4.getCategories().add(category6);
    category5.getCategories().add(category7);
    category1.getCategories().add(category2);

    Category category3 = getModel1Factory().createCategory();
    category3.setName("category3");
    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");

    category3.getCategories().add(category8);
    category3.getCategories().add(category9);
    category8.getCategories().add(category10);
    category9.getCategories().add(category11);
    category1.getCategories().add(category3);

    transaction.lockObjects(CDOUtil.getCDOObjects(category1), LockType.WRITE, DEFAULT_TIMEOUT, true);
    transaction.options().addAutoReleaseLocksExemptions(false, category1);
    assertLockStatus(category1, true, false);

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

  private void assertLockStatus(Category category, boolean lockedByMe, boolean recursive)
  {
    CDOObject categoryCDOObject = CDOUtil.getCDOObject(category);
    CDOLockState cdoLockState = categoryCDOObject.cdoLockState();
    Assert.assertEquals(
        "new object " + category.getName()
            + (lockedByMe ? " should be locally locked" : " shouldn't be locally locked"),
        lockedByMe, cdoLockState.isLocked(LockType.WRITE, transaction, false));

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

Back to the top