Skip to main content
summaryrefslogtreecommitdiffstats
blob: ad5538ad4855148ffe026902b06c6cd7c4c7b136 (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
/*
 * Copyright (c) 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:
 *    Steve Monnier - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.common.id.CDOID;
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.model6.A;
import org.eclipse.emf.cdo.tests.model6.B;
import org.eclipse.emf.cdo.tests.model6.C;
import org.eclipse.emf.cdo.tests.model6.D;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

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

/**
 * @author Steve Monnier
 */
public class Bugzilla_372307_Test extends AbstractCDOTest
{
  public void testMoveElementToOtherContainer() throws Exception
  {
    A session1a1 = getModel6Factory().createA();
    A session1a2 = getModel6Factory().createA();
    D session1d = getModel6Factory().createD();

    session1a1.getOwnedDs().add(session1d);

    // Create model using session1
    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath("myResource"));
    resource1.getContents().add(session1a1);
    resource1.getContents().add(session1a2);
    transaction1.commit();

    System.out.println("a1: " + CDOUtil.getCDOObject(session1a1));
    System.out.println("a2: " + CDOUtil.getCDOObject(session1a2));
    System.out.println("d:  " + CDOUtil.getCDOObject(session1d));

    // Open a second session on the shared resource
    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();
    CDOResource resource2 = transaction2.getResource(getResourcePath("myResource"), true);
    A session2a1 = (A)resource2.getContents().get(0);
    A session2a2 = (A)resource2.getContents().get(1);
    D session2d = session2a1.getOwnedDs().get(0);

    // Move the D element from parent A1 to A2 in session1 and commit
    EList<D> ownedDs = session1a2.getOwnedDs();
    ownedDs.add(session1d);
    commitAndSync(transaction1, transaction2);

    // Validate that A1 has no content in both sessions
    assertEquals("A1 in session1 is expected to be empty", 0, session1a1.eContents().size());
    assertEquals("A1 in session2 is expected to be empty", 0, session2a1.eContents().size());

    // Validate that A2 contains D in both sessions
    assertEquals("A2 in session1 is expected to have contents", 1, session1a2.eContents().size());
    assertEquals("A2 in session2 is expected to have contents", 1, session2a2.eContents().size());

    CDOID id1 = CDOUtil.getCDOObject(session1a2.eContents().get(0)).cdoID();
    CDOID id2 = CDOUtil.getCDOObject(session2a2.eContents().get(0)).cdoID();
    assertEquals("Element contained in A1 should have the same CDOID in both sessions", id1, id2);

    // Validate that the container of the D element is the same in both sessions
    id1 = CDOUtil.getCDOObject(session1d.eContainer()).cdoID();
    id2 = CDOUtil.getCDOObject(session2d.eContainer()).cdoID();
    assertEquals("Container of D should have the same CDOID in both sessions", id1, id2);

    // Validate that the containment feature of D on both sessions are not null, and equal
    EReference ownedDs_Feature = getModel6Package().getA_OwnedDs();
    assertEquals(ownedDs_Feature, session1d.eContainmentFeature());
    assertEquals(ownedDs_Feature, session2d.eContainmentFeature());
  }

  public void testMoveElementToOtherContainerSingleValued() throws Exception
  {
    B session1a1 = getModel6Factory().createB();
    B session1a2 = getModel6Factory().createB();
    C session1d = getModel6Factory().createC();

    session1a1.setOwnedC(session1d);

    // Create model using session1
    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath("myResource"));
    resource1.getContents().add(session1a1);
    resource1.getContents().add(session1a2);
    transaction1.commit();

    System.out.println("a1: " + CDOUtil.getCDOObject(session1a1));
    System.out.println("a2: " + CDOUtil.getCDOObject(session1a2));
    System.out.println("d:  " + CDOUtil.getCDOObject(session1d));

    // Move the D element from parent A1 to A2 in session1 and commit
    session1a2.setOwnedC(session1d);
    transaction1.commit();

    // Open a second session on the shared resource
    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();
    C session2d = transaction2.getObject(session1d);

    assertEquals(CDOUtil.getCDOObject(session1a2).cdoID(), CDOUtil.getCDOObject(session2d.eContainer()).cdoID());

    // Validate that the containment feature of D on both sessions are not null, and equal
    EReference ownedC_Feature = getModel6Package().getB_OwnedC();
    assertEquals(ownedC_Feature, session1d.eContainmentFeature());
    assertEquals(ownedC_Feature, session2d.eContainmentFeature());
  }
}

Back to the top