Skip to main content
summaryrefslogtreecommitdiffstats
blob: 839e9b9c31c69feec399b8faad33c89aa7daf35f (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Copyright (c) 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:
 *    Esteban Dugueperoux - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.CDOCommonSession.Options.PassiveUpdateMode;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
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.tests.model3.NodeA;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

/**
 * Bug 415415: Stale reference not removed between locally detached object and remotely changed ones
 * 
 * @author Esteban Dugueperoux
 */
public class Bugzilla_415415_Test extends AbstractCDOTest
{
  private static final String RESOURCE_NAME = "res1";

  private CDOTransaction transaction;

  private NodeA root;

  private NodeA child1;

  private NodeA child2;

  private CDOObject rootCDO;

  private CDOObject child1CDO;

  private CDOObject child2CDO;

  private CDOID rootID;

  private CDOID child1ID;

  private CDOID child2ID;

  private RemoteUser remoteUser;

  @Override
  public void setUp() throws Exception
  {
    super.setUp();

    root = getModel3Factory().createNodeA();
    root.setName("root");

    child1 = getModel3Factory().createNodeA();
    child1.setName("child1");
    root.getChildren().add(child1);

    child2 = getModel3Factory().createNodeA();
    child2.setName("child2");
    root.getChildren().add(child2);

    CDOSession session = openSession();
    session.options().setPassiveUpdateEnabled(true);
    session.options().setPassiveUpdateMode(PassiveUpdateMode.ADDITIONS);

    transaction = session.openTransaction();
    CDOResource resource = transaction.createResource(getResourcePath(RESOURCE_NAME));
    resource.getContents().add(root);
    transaction.commit();

    rootCDO = CDOUtil.getCDOObject(root);
    rootID = rootCDO.cdoID();

    child1CDO = CDOUtil.getCDOObject(child1);
    child1ID = child1CDO.cdoID();

    child2CDO = CDOUtil.getCDOObject(child2);
    child2ID = child2CDO.cdoID();

    remoteUser = new RemoteUser();
  }

  @Override
  public void tearDown() throws Exception
  {
    root = null;
    child1 = null;
    child2 = null;

    rootCDO = null;
    child1CDO = null;
    child2CDO = null;

    rootID = null;
    child1ID = null;
    child2ID = null;

    remoteUser = null;
    super.tearDown();
  }

  public void testStaleReferenceRemove_FromLocallyDirtyObjectToRemotelyDetachedObject() throws Exception
  {
    child2.getOtherNodes().add(child1);
    remoteUser.removeNodeA1ChildToNodeAroot();
    CDOCommitInfo commit = remoteUser.commit();
    transaction.waitForUpdate(commit.getTimeStamp(), DEFAULT_TIMEOUT);

    assertEquals("As child1 is remotelly detached, the stale reference child2.otherNodes:child1 should be removed", 0,
        child2.getOtherNodes().size());

    String assertMessage = "As the stale reference child2.otherNodes:child1 has been removed and child2 is already dirty child2 should be the only dirty object";
    assertEquals(assertMessage, 1, transaction.getDirtyObjects().size());
    assertEquals(assertMessage, child2CDO, transaction.getDirtyObjects().get(child2ID));

    transaction.commit();
    remoteUser.assertNotStaleReference();
  }

  public void testStaleReferenceRemove_FromRemotelyDirtyObjectToLocallyDetachedObject() throws Exception
  {
    root.getChildren().remove(child1);
    remoteUser.createReferenceFromNodeA2ToNodeA1();
    CDOCommitInfo commit = remoteUser.commit();
    transaction.waitForUpdate(commit.getTimeStamp(), DEFAULT_TIMEOUT);

    assertEquals(1, transaction.getDetachedObjects().size());
    assertEquals(child1CDO, transaction.getDetachedObjects().get(child1ID));
    assertEquals("As child1 is locally detached, the stale reference child2.otherNodes:child1 should be removed", 0,
        child2.getOtherNodes().size());

    String assertMessage = "As the stale reference child2.otherNodes:child1 has been removed, child2 should become the only dirty object with the nodeARoot";
    assertEquals(assertMessage, 2, transaction.getDirtyObjects().size());
    assertEquals(assertMessage, child2CDO, transaction.getDirtyObjects().get(child2ID));
    assertEquals(assertMessage, rootCDO, transaction.getDirtyObjects().get(rootID));

    commit = transaction.commit();
    remoteUser.waitForUpdate(commit.getTimeStamp(), DEFAULT_TIMEOUT);
    remoteUser.assertNotStaleReference();
  }

  /**
   * @author Esteban Dugueperoux
   */
  private final class RemoteUser
  {
    private CDOTransaction transaction;

    private NodeA root;

    private NodeA child1;

    private NodeA child2;

    public RemoteUser()
    {
      CDOSession session = openSession();
      session.options().setPassiveUpdateEnabled(true);
      session.options().setPassiveUpdateMode(PassiveUpdateMode.ADDITIONS);

      transaction = session.openTransaction();

      CDOResource resource = transaction.getResource(getResourcePath(RESOURCE_NAME));

      root = (NodeA)resource.getContents().get(0);
      child1 = root.getChildren().get(0);
      child2 = root.getChildren().get(1);
    }

    public void removeNodeA1ChildToNodeAroot()
    {
      root.getChildren().remove(child1);
    }

    public void createReferenceFromNodeA2ToNodeA1()
    {
      child2.getOtherNodes().add(child1);
    }

    public CDOCommitInfo commit() throws Exception
    {
      return transaction.commit();
    }

    public void waitForUpdate(long timeStamp, long defaultTimeout)
    {
      transaction.waitForUpdate(timeStamp, defaultTimeout);
    }

    public void assertNotStaleReference()
    {
      assertEquals(0, transaction.getDetachedObjects().size());
      assertEquals(0, transaction.getDirtyObjects().size());
      assertEquals(0, child2.getOtherNodes().size());
    }
  }
}

Back to the top