Skip to main content
summaryrefslogtreecommitdiffstats
blob: 36c9056348df657beaa66836a659fe161c83002f (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
/***************************************************************************
 * Copyright (c) 2004 - 2009 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;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.model.CDOFeature;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.model1.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.Product1;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.net4j.util.transaction.TransactionException;

/**
 * @author Eike Stepper
 */
public class RollbackTest extends AbstractCDOTest
{
  public void testRollbackSameSession() throws Exception
  {
    CDOSession session = openModel1Session();
    CDOTransaction transaction1 = session.openTransaction();
    CDOTransaction transaction2 = session.openTransaction();
    flow1(transaction1, transaction2);
  }

  public void testRollbackSeparateSession() throws Exception
  {
    // Client1
    CDOSession session1 = openModel1Session();
    CDOTransaction transaction1 = session1.openTransaction();

    // Client2
    CDOSession session2 = openModel1Session();
    CDOTransaction transaction2 = session2.openTransaction();

    flow1(transaction1, transaction2);
  }

  protected void flow1(CDOTransaction transaction1, CDOTransaction transaction2)
  {
    CDOFeature category_Products1 = transaction1.getSession().getPackageManager().convert(
        getModel1Package().getCategory_Products());

    CDOFeature category_Products2 = transaction2.getSession().getPackageManager().convert(
        getModel1Package().getCategory_Products());

    // Client1
    CDOResource resource1 = transaction1.createResource("/test1");
    Company company1 = getModel1Factory().createCompany();
    resource1.getContents().add(company1);
    Category category1 = getModel1Factory().createCategory();
    company1.getCategories().add(category1);
    transaction1.commit();

    // Client2
    CDOResource resource2 = transaction2.getResource("/test1");
    Company company2 = (Company)resource2.getContents().get(0);
    Category category2 = company2.getCategories().get(0);
    category2.setName("client2");
    Product1 product2 = getModel1Factory().createProduct1();
    category2.getProducts().add(product2);

    // Client1
    category1.setName("client1");
    Product1 product1 = getModel1Factory().createProduct1();
    category1.getProducts().add(product1);

    msg("Checking state of category");
    CDOObject cdoObjectCategory1 = CDOUtil.getCDOObject(category1);
    CDOObject cdoObjectProduct1 = CDOUtil.getCDOObject(product1);

    msg("Object should contains internalEObject");
    Object testObject = cdoObjectCategory1.cdoRevision().data().get(category_Products1, 0);
    assertEquals(product1, testObject);

    transaction1.commit();

    msg("Object should contains CDOID");
    testObject = cdoObjectCategory1.cdoRevision().data().get(category_Products1, 0);
    assertEquals(cdoObjectProduct1.cdoID(), testObject);

    sleep(500);

    // Client2
    assertEquals(true, transaction2.isDirty());
    assertEquals(true, transaction2.hasConflict());

    try
    {
      transaction2.commit();
      fail("Commit on transaction2 should fail");
    }
    catch (TransactionException ex)
    {
      // Commit process should no have changed state of the object
      CDOObject cdoObjectCategory2 = CDOUtil.getCDOObject(category2);
      testObject = cdoObjectCategory2.cdoRevision().data().get(category_Products2, 0);
      assertEquals(product2, testObject);
      transaction2.rollback();
    }

    assertEquals(false, transaction2.isDirty());
    assertEquals(false, transaction2.hasConflict());

    assertEquals("client1", category2.getName());
    category2.setName("client2");

    transaction2.commit();

    assertEquals(false, transaction2.isDirty());
    assertEquals(false, transaction2.hasConflict());
    assertEquals("client2", category2.getName());

    sleep(500);

    // Client1
    assertEquals(false, transaction1.isDirty());
    assertEquals(false, transaction1.hasConflict());
    assertEquals("client2", category1.getName());
  }
}

Back to the top