Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 432cbe0b5592c217481e5a8ac9ddccaa1cec6260 (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
/*
 * Copyright (c) 2012, 2013 Eike Stepper (Loehne, 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.hibernate;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.commit.CDOCommitHistory;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.revision.CDOIDAndVersion;
import org.eclipse.emf.cdo.common.revision.CDORevision;
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.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.model1.Customer;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.net4j.util.concurrent.ConcurrencyUtil;

/**
 * @author Martin Taal
 */
@Requires(IRepositoryConfig.CAPABILITY_AUDITING)
public class HibernateBugzilla_395684_Test extends AbstractCDOTest
{
  @Override
  protected void doSetUp() throws Exception
  {
    disableConsole();
    super.doSetUp();
    skipStoreWithoutRawAccess();
  }

  @Override
  protected void doTearDown() throws Exception
  {
    disableConsole();
    super.doTearDown();
  }

  public void testVersionChange() throws Exception
  {
    CDOSession session = openSession();
    {
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/res1"));
      Customer customer = getModel1Factory().createCustomer();
      customer.setName("Martin");
      resource.getContents().add(customer);
      transaction.commit();
    }
    {
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/res1"));
      Customer customer = (Customer)resource.getContents().get(0);
      customer.setName("Eike");
      transaction.commit();
    }
    {
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/res1"));
      resource.getContents().remove(0);
      transaction.commit();
    }

    getRepository().getRevisionManager().getCache().clear();

    {
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/res1"));
      assertEquals(0, resource.getContents().size());
      transaction.commit();
    }

    {
      CDOView view = session.openView();

      int i = 0;
      for (CDOCommitInfo cdoCommitInfo : getCDOCommitHistory(view).getElements())
      {
        if (i == 0)
        {
          assertEquals(1, cdoCommitInfo.getDetachedObjects().size());
          assertEquals(1, cdoCommitInfo.getChangedObjects().size());
          assertEquals(0, cdoCommitInfo.getNewObjects().size());
          CDOIDAndVersion cdoIDAndVersion = cdoCommitInfo.getDetachedObjects().get(0);
          // get the view before the delete
          CDOView otherView = session.openView(cdoCommitInfo.getTimeStamp() - 1);
          CDOObject obj = otherView.getObject(cdoIDAndVersion.getID());
          {
            final Customer oldCustomer = (Customer)CDOUtil.getEObject(obj);
            assertEquals("Eike", oldCustomer.getName());
          }
          {
            final CDORevision cdoRevision = CDOUtil.getRevisionByVersion(obj, cdoIDAndVersion.getVersion());
            // get the detached entry
            final CDORevision cdoRevisionDetached = CDOUtil.getRevisionByVersion(obj, 1 + cdoIDAndVersion.getVersion());
            assertEquals(cdoRevision.getRevised() + 1, cdoRevisionDetached.getTimeStamp());
          }

        }
        else if (i == 1)
        {
          assertEquals(0, cdoCommitInfo.getDetachedObjects().size());
          assertEquals(1, cdoCommitInfo.getChangedObjects().size());
          assertEquals(0, cdoCommitInfo.getNewObjects().size());
        }
        else
        {
          break;
        }
        i++;
      }
      view.close();
    }
  }

  private synchronized CDOCommitHistory getCDOCommitHistory(CDOView audit)
  {
    CDOCommitHistory history = audit.getHistory();
    history.triggerLoad();
    long startTime = System.currentTimeMillis();
    while (history.isLoading())
    {
      ConcurrencyUtil.sleep(10);

      // waited too long
      if (System.currentTimeMillis() - startTime > 5000)
      {
        throw new IllegalStateException("commit info could not be loaded");
      }
    }
    return history;
  }
}

Back to the top