Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 545aeefb7293772580c3f94baca69689c3e6f38f (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
/*
 * Copyright (c) 2011, 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:
 *    Caspar De Groot - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
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.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;

import java.util.Map.Entry;

/**
 * Bug 334995 - CDOTransaction corrupted by persisted + new resource with same URI
 *
 * @author Caspar De Groot
 */
public class Bugzilla_334995_Test extends AbstractCDOTest
{
  public void test() throws CommitException
  {
    CDOID[] resourceIDs = persistResources("/res1");

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

      CDOResource resource = transaction.createResource(getResourcePath("/res1"));
      msg("New resource: " + resource);
      msg("newObjects:");

      for (Entry<CDOID, CDOObject> entry : transaction.getNewObjects().entrySet())
      {
        msg("    " + entry + ", state: " + entry.getValue().cdoState());
        assertNew(entry.getValue(), transaction);
      }

      // Fetch the persisted resource that has the same URI
      CDOResource resource1 = (CDOResource)transaction.getObject(resourceIDs[0]);
      msg("Persisted resource: " + resource1);

      msg("newObjects:");
      for (Entry<CDOID, CDOObject> entry : transaction.getNewObjects().entrySet())
      {
        msg("    " + entry + ", state: " + entry.getValue().cdoState());
        assertNew(entry.getValue(), transaction);
      }

      transaction.commit();
    }
  }

  public void testRename() throws CommitException
  {
    CDOID[] resourceIDs = persistResources("/res1", "/res2");

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

      CDOResource resource1 = transaction.getResource(getResourcePath("/res1"));
      resource1.setPath("/res2");

      CDOResource resource2 = (CDOResource)transaction.getObject(resourceIDs[1]);

      resource1.getContents().add(getModel1Factory().createAddress());
      resource2.getContents().add(getModel1Factory().createAddress());
      transaction.commit();

      session.close();
    }
  }

  private CDOID[] persistResources(String... resourceNames) throws CommitException
  {
    CDOSession session = openSession();
    CDOTransaction transaction = session.openTransaction();
    int i = 0;
    CDOResource[] resources = new CDOResource[resourceNames.length];
    for (String resourceName : resourceNames)
    {
      resources[i++] = transaction.createResource(getResourcePath(resourceName));
    }
    transaction.commit();

    CDOID[] resourceIDs = new CDOID[resourceNames.length];
    for (i = 0; i < resources.length; i++)
    {
      resourceIDs[i] = resources[i].cdoID();
    }

    session.close();
    return resourceIDs;
  }
}

Back to the top