Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0768d7523f2670c3099f01f4bc78ef363af7b4e6 (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
/*
 * 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:
 *    Erdal Karaca - initial API and implementation
 */
package org.eclipse.emf.cdo.tests;

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.model1.Model1Factory;
import org.eclipse.emf.cdo.tests.model1.Order;
import org.eclipse.emf.cdo.tests.model1.OrderDetail;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.spi.cdo.CDOMergingConflictResolver;
import org.eclipse.emf.spi.cdo.DefaultCDOMerger;

/**
 * Test cases for CDOMergingConflictResolver. See bug 396804.
 *
 * @author Erdal Karaca
 */
public class Bugzilla_396804_Test extends AbstractCDOTest
{
  public void testParallelADD() throws Exception
  {
    String path = getResourcePath("/res1");

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      transaction.createResource(path);
      transaction.commit();
      session.close();
    }

    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    transaction1.options().addConflictResolver(createConflictResolver());

    CDOResource resource1 = transaction1.getResource(path);
    transaction1.commit();

    resource1.getContents().add(Model1Factory.eINSTANCE.createSalesOrder());

    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();

    CDOResource resource2 = transaction2.getResource(path);
    OrderDetail orderDetail = Model1Factory.eINSTANCE.createOrderDetail();
    resource2.getContents().add(orderDetail);
    commitAndSync(transaction2, transaction1);

    transaction1.commit();
  }

  public void testParallelRemoveYieldsUnexpectedObjectState() throws Exception
  {
    String path = getResourcePath("/res1");

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();

      EList<EObject> contents = transaction.createResource(path).getContents();
      contents.add(Model1Factory.eINSTANCE.createOrderDetail());
      contents.add(Model1Factory.eINSTANCE.createOrderDetail());

      transaction.commit();
      session.close();
    }

    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    transaction1.options().addConflictResolver(createConflictResolver());

    EList<EObject> contents1 = transaction1.getResource(path).getContents();
    OrderDetail firstOrderDetail1 = (OrderDetail)contents1.get(0);
    Order order = Model1Factory.eINSTANCE.createSalesOrder();
    order.getOrderDetails().add(firstOrderDetail1); // change2: contents1.remove(0)
    contents1.add(order); // change3

    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();

    EList<EObject> contents2 = transaction2.getResource(path).getContents();
    contents2.remove(1); // change1: lastOrderDetail2
    commitAndSync(transaction2, transaction1);

    transaction1.commit();
  }

  public void testParallelRemoveThrowsIOOBE() throws Exception
  {
    String path = getResourcePath("/res1");

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(path);

      resource.getContents().add(Model1Factory.eINSTANCE.createOrderDetail());
      resource.getContents().add(Model1Factory.eINSTANCE.createOrderDetail());

      transaction.commit();
      session.close();
    }

    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    transaction1.options().addConflictResolver(createConflictResolver());

    CDOResource resource1 = transaction1.getResource(path);
    Order order = Model1Factory.eINSTANCE.createSalesOrder();
    resource1.getContents().add(order);

    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();

    CDOResource resource2 = transaction2.getResource(path);
    OrderDetail lastOrderDetail = (OrderDetail)resource2.getContents().get(resource2.getContents().size() - 1);
    resource2.getContents().remove(lastOrderDetail);
    commitAndSync(transaction2, transaction1);

    transaction1.commit();
  }

  private CDOMergingConflictResolver createConflictResolver()
  {
    return new CDOMergingConflictResolver(new DefaultCDOMerger.PerFeature.ManyValued());
  }
}

Back to the top