Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0564640107c1c155f89a83ea10af8a069073841f (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
/*
 * Copyright (c) 2012, 2013 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.hibernate;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.CDOObjectHistory;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
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 Hibernate_Missing_Version_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();
    Customer lastCustomer = null;
    {
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/res1"));
      Customer customer = getModel1Factory().createCustomer();
      customer.setName("Martin");
      resource.getContents().add(customer);
      transaction.commit();
      lastCustomer = customer;
    }
    {
      CDOTransaction transaction = session.openTransaction();
      Customer customer = transaction.getObject(lastCustomer);// (Customer)resource.getContents().get(0);
      customer.setName("Eike");
      assertEquals(1, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      transaction.commit();
      assertEquals(2, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      lastCustomer = customer;
    }
    {
      CDOTransaction transaction = session.openTransaction();
      Customer customer = lastCustomer; // transaction.getObject(lastCustomer);//
                                        // (Customer)resource.getContents().get(0);
      customer.setName("Peter");
      assertEquals(2, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      transaction.commit();
      // assertEquals(3, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      lastCustomer = customer;
    }
    getRepository().getRevisionManager().getCache().clear();

    // now go to another repo
    InternalRepository repo2 = getRepository("repo2", true);
    repo2.getRevisionManager().getCache().clear();
    {
      CDOSession session2 = openSession("repo2");
      CDOTransaction transaction = session2.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/res1"));
      Customer customer = (Customer)resource.getContents().get(0);
      customer.setName("John");
      assertEquals(3, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      transaction.commit();
      assertEquals(4, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      session2.close();
    }
    {
      CDOSession session2 = openSession("repo2");
      CDOTransaction transaction = session2.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("/res1"));
      Customer customer = (Customer)resource.getContents().get(0);
      customer.setName("Mike");
      assertEquals(4, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      transaction.commit();
      assertEquals(5, CDOUtil.getCDOObject(customer).cdoRevision().getVersion());
      session2.close();
    }
    {
      CDOSession session2 = openSession("repo2");
      CDOView view = session2.openView();
      CDOResource resource = view.getResource(getResourcePath("/res1"));
      Customer customer = (Customer)resource.getContents().get(0);

      assertNotNull(CDOUtil.getRevisionByVersion(CDOUtil.getCDOObject(customer), 4));
      assertNotNull(CDOUtil.getRevisionByVersion(CDOUtil.getCDOObject(customer), 3));
      assertNotNull(CDOUtil.getRevisionByVersion(CDOUtil.getCDOObject(customer), 2));
      assertNotNull(CDOUtil.getRevisionByVersion(CDOUtil.getCDOObject(customer), 1));

      CDOObjectHistory history = getCDOObjectHistory(view, customer);
      assertEquals(5, history.getElements().length);
      // for (CDOCommitInfo cdoCommitInfo : history.getElements())
      // {
      //
      // }
      view.close();
      session2.close();
    }
  }

  private synchronized CDOObjectHistory getCDOObjectHistory(CDOView audit, Object object)
  {
    CDOObjectHistory cdoObjectHistory = audit.getHistory((CDOObject)object);
    cdoObjectHistory.triggerLoad();
    long startTime = System.currentTimeMillis();
    while (cdoObjectHistory.isLoading())
    {
      ConcurrencyUtil.sleep(10);

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

Back to the top