Skip to main content
summaryrefslogtreecommitdiffstats
blob: f09fbf2dea7277591865b2e36834615a6b2b2376 (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
/*
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

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.CommitException;

import org.eclipse.emf.common.util.EList;

/**
 * @author Eike Stepper
 */
public class Bugzilla_409284_Test extends AbstractCDOTest
{
  @SuppressWarnings("unused")
  public void testContainmentCycle() throws Exception
  {
    // Client1 - Init
    Category a1 = createCategory("A");
    Category b1 = createCategory("B");
    Category c1 = createCategory("C");
    Category d1 = createCategory("D");
    Category e1 = createCategory("E");

    Company r1 = getModel1Factory().createCompany();
    r1.setName("R");

    r1.getCategories().add(a1);
    a1.getCategories().add(b1);
    b1.getCategories().add(c1);

    r1.getCategories().add(d1);
    d1.getCategories().add(e1);

    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath("res"));

    resource1.getContents().add(r1);
    transaction1.commit();

    // Client2 - Load
    CDOSession session2 = openSession();
    session2.options().setPassiveUpdateEnabled(false); // Important!
    CDOTransaction transaction2 = session2.openTransaction();
    CDOResource resource2 = transaction2.getResource(getResourcePath("res"));

    Company r2 = (Company)resource2.getContents().get(0);

    Category a2 = loadCategory(r2.getCategories(), 0, "A");
    Category b2 = loadCategory(a2.getCategories(), 0, "B");
    Category c2 = loadCategory(b2.getCategories(), 0, "C");

    Category d2 = loadCategory(r2.getCategories(), 1, "D");
    Category e2 = loadCategory(d2.getCategories(), 0, "E");

    // Client1 - First tree move (element B from A to E)
    e1.getCategories().add(b1);
    transaction1.commit();

    // Client2 - Second tree move (element D from R to C)
    c2.getCategories().add(d2);

    try
    {
      transaction2.commit();
      fail("CommitException expected");
    }
    catch (CommitException expected)
    {
      String message = expected.getMessage();
      assertEquals(true, message.contains("ContainmentCycleDetectedException"));
    }
  }

  private Category createCategory(String name)
  {
    Category category = getModel1Factory().createCategory();
    category.setName(name);
    return category;
  }

  private Category loadCategory(EList<Category> categories, int i, String name)
  {
    Category category = categories.get(i);
    assertEquals(name, category.getName());
    return category;
  }
}

Back to the top