Skip to main content
summaryrefslogtreecommitdiffstats
blob: 04d591a6be60f7301ddda012c1770cb1aefdff69 (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
/**
 * Copyright (c) 2004 - 2010 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.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.emf.cdo.util.CommitException;

import org.eclipse.emf.ecore.EStructuralFeature;

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

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

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

    flow1(transaction1, transaction2);
  }

  protected void flow1(CDOTransaction transaction1, CDOTransaction transaction2) throws CommitException
  {
    // Client1
    CDOResource resource1 = transaction1.createResource("/test1");
    Company company1 = getModel1Factory().createCompany();
    resource1.getContents().add(company1);
    Category category1 = getModel1Factory().createCategory();
    company1.getCategories().add(category1);
    long commitTime = transaction1.commit().getTimeStamp();

    // Client2
    transaction2.waitForUpdate(commitTime, DEFAULT_TIMEOUT);
    CDOResource resource2 = transaction2.getResource("/test1");
    Company company2 = (Company)resource2.getContents().get(0); // Infrequent exceptions in legacy mode
    Category category2 = company2.getCategories().get(0);
    category2.setName("client2");
    Product1 product2 = getModel1Factory().createProduct1();
    product2.setName("product2");
    category2.getProducts().add(product2);

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

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

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

    commitTime = transaction1.commit().getTimeStamp();

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

    transaction2.waitForUpdate(commitTime, DEFAULT_TIMEOUT);

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

    try
    {
      commitTime = transaction2.commit().getTimeStamp();
      fail("CommitException expected");
    }
    catch (CommitException ex)
    {
      // Commit process should no have changed state of the object
      CDOObject cdoObjectCategory2 = CDOUtil.getCDOObject(category2);
      EStructuralFeature category_Products2 = getModel1Package().getCategory_Products();
      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");

    commitTime = transaction2.commit().getTimeStamp();

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

    transaction1.waitForUpdate(commitTime, DEFAULT_TIMEOUT);

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

  /**
   * Bug 296680.
   */
  public void test_Bugzilla_296680() throws Exception
  {
    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource("/res");
      resource.getContents().add(getModel1Factory().createCompany());
      transaction.commit();
      session.close();
    }

    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.getResource("/res");

    Company company = (Company)resource.getContents().get(0);
    company.setName("MyCompany");

    resource.getContents().add(getModel1Factory().createCompany());
    transaction.setSavepoint();
    resource.getContents().remove(1);

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

Back to the top