Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b2c4f3eb7d221ef7798696b0f4ca2d3a6ebc6b63 (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
/*
 * Copyright (c) 2014, 2015, 2022 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.bugzilla;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
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.spi.common.revision.InternalCDORevision;
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.Category;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;

/**
 * See bug 439173.
 *
 * @author Leonid Ripeynih
 */
@Requires(IRepositoryConfig.CAPABILITY_BRANCHING)
public class Bugzilla_439173_Test extends AbstractCDOTest
{
  public void testSwitchTargetVersion() throws Exception
  {
    CDOSession session = openSession();

    // Create an object in main branch
    //
    CDOTransaction mainTx = session.openTransaction();
    String path = getResourcePath("test");
    CDOResource resource = mainTx.createResource(path);
    resource.getContents().add(createCategory("cat1"));
    mainTx.commit();
    mainTx.close();

    // Create a branch and change object
    //
    CDOBranch branch = session.getBranchManager().getMainBranch().createBranch(getBranchName("branch"));
    CDOTransaction branchTx = session.openTransaction(branch);
    resource = branchTx.getResource(path);

    // Change resource in branch twice (so it's version becomes 2)
    //
    resource.getContents().add(createCategory("cat2"));
    branchTx.commit();
    resource.getContents().add(createCategory("cat3"));
    branchTx.commit();
    branchTx.close();

    // Open another session to invalidate cache
    //
    session.close();
    CDOSession cleanSession = openSession();

    // Open view on new session and switch target
    //
    CDOView view = cleanSession.openView();
    resource = view.getResource(path);
    resource.getContents().size();

    assertEquals(1, resource.cdoRevision().getVersion());
    view.setBranch(branch);
    assertEquals(2, resource.cdoRevision().getVersion());
  }

  public void testSwitchTargetVersionResourceID() throws Exception
  {
    CDOSession session = openSession();

    // Create an object in main branch
    //
    CDOTransaction mainTx = session.openTransaction();
    String sourcePath = getResourcePath("source");
    String targetPath = getResourcePath("target");
    CDOResource resource = mainTx.createResource(sourcePath);
    Category category = createCategory("cat1");
    resource.getContents().add(category);

    mainTx.commit();
    CDOID sourceResourceID = resource.cdoID();
    mainTx.close();

    // Create a branch and change object
    //
    CDOBranch branch = session.getBranchManager().getMainBranch().createBranch(getBranchName("branch"));
    CDOTransaction branchTx = session.openTransaction(branch);
    resource = branchTx.getResource(sourcePath);

    // Change category resource
    //
    CDOResource targetResource = branchTx.createResource(targetPath);
    targetResource.getContents().addAll(resource.getContents());

    branchTx.commit();
    CDOID targetResourceID = targetResource.cdoID();
    branchTx.close();

    // Open another session to invalidate cache
    //
    session.close();
    CDOSession cleanSession = openSession();

    // Open view on new session and switch target
    //
    CDOView view = cleanSession.openView();
    resource = view.getResource(sourcePath);
    category = (Category)resource.getContents().get(0);
    category.getName();

    assertEquals(sourceResourceID, ((InternalCDORevision)CDOUtil.getCDOObject(category).cdoRevision()).getResourceID());
    view.setBranch(branch);
    assertEquals(targetResourceID, ((InternalCDORevision)CDOUtil.getCDOObject(category).cdoRevision()).getResourceID());
  }

  private Category createCategory(String name)
  {
    Category category = getModel1Factory().createCategory();
    category.setName(name);
    return category;
  }
}

Back to the top