Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4da25294e025839831a2581d3dad7130ca89389f (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/**
 * Copyright (c) 2004 - 2010 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.bugzilla;

import org.eclipse.emf.cdo.CDOState;
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.model4.ContainedElementNoOpposite;
import org.eclipse.emf.cdo.tests.model4.RefMultiNonContainedNPL;
import org.eclipse.emf.cdo.tests.model4.RefSingleNonContainedNPL;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.util.ObjectNotFoundException;

import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * See bug 298561
 * 
 * @author Eike Stepper
 */
public class Bugzilla_298561_Test extends AbstractCDOTest
{
  private static String RESOURCENAME = "/r1";

  public void testNew() throws CommitException
  {
    CDOSession session = openSession();
    session.options().setPassiveUpdateEnabled(false);
    session.getPackageRegistry().putEPackage(getModel4Package());

    CDOTransaction tx = session.openTransaction();
    CDOResource r1 = tx.createResource(RESOURCENAME);

    // Create referencee and store it
    ContainedElementNoOpposite referencee = getModel4Factory().createContainedElementNoOpposite();
    r1.getContents().add(referencee);
    tx.commit();

    // Create referencer, don't store it -- keep it as NEW
    RefSingleNonContainedNPL referencer = getModel4Factory().createRefSingleNonContainedNPL();
    r1.getContents().add(referencer);
    referencer.setElement(referencee);
    assertEquals(CDOState.NEW, CDOUtil.getCDOObject(referencer).cdoState());

    // Delete the referencee in 2nd session
    doSecondSession();

    // Refresh
    session.refresh();

    //
    try
    {
      EReference ref = getModel4Package().getRefSingleNonContainedNPL_Element();
      boolean isSet = referencer.eIsSet(ref);
      if (isSet)
      {
        assertNull(referencer.getElement());
      }
    }
    catch (ObjectNotFoundException e)
    {
      fail("Should not have thrown ObjectNotFoundException");
    }

    tx.close();
    session.close();
  }

  public void testDirty() throws CommitException
  {
    CDOSession session = openSession();
    session.options().setPassiveUpdateEnabled(false);
    session.getPackageRegistry().putEPackage(getModel4Package());

    CDOTransaction tx = session.openTransaction();
    CDOResource r1 = tx.createResource(RESOURCENAME);

    // Create referencee and referencer (but no reference yet), and store them
    ContainedElementNoOpposite referencee = getModel4Factory().createContainedElementNoOpposite();
    r1.getContents().add(referencee);
    RefSingleNonContainedNPL referencer = getModel4Factory().createRefSingleNonContainedNPL();
    r1.getContents().add(referencer);
    tx.commit();

    // Create the reference, making the referencer dirty
    referencer.setElement(referencee);
    assertEquals(CDOState.DIRTY, CDOUtil.getCDOObject(referencer).cdoState());

    // Delete the referencee in 2nd session
    doSecondSession();

    // Refresh
    session.refresh();

    try
    {
      boolean isSet = referencer.eIsSet(getModel4Package().getRefSingleNonContainedNPL_Element());
      if (isSet)
      {
        assertNull(referencer.getElement());
      }
    }
    catch (ObjectNotFoundException e)
    {
      fail("Should not have thrown ObjectNotFoundException");
    }

    tx.close();
    session.close();
  }

  public void testNewMulti() throws CommitException
  {
    CDOSession session = openSession();
    session.options().setPassiveUpdateEnabled(false);
    session.getPackageRegistry().putEPackage(getModel4Package());

    CDOTransaction tx = session.openTransaction();
    CDOResource r1 = tx.createResource(RESOURCENAME);

    // Create referencee and store it
    ContainedElementNoOpposite referencee = getModel4Factory().createContainedElementNoOpposite();
    r1.getContents().add(referencee);
    tx.commit();

    // Create referencer, don't store it -- keep it as NEW
    RefMultiNonContainedNPL referencer = getModel4Factory().createRefMultiNonContainedNPL();
    r1.getContents().add(referencer);
    referencer.getElements().add(referencee);
    assertEquals(CDOState.NEW, CDOUtil.getCDOObject(referencer).cdoState());

    // Delete the referencee in 2nd session
    doSecondSession();

    // Refresh
    session.refresh();

    //
    try
    {
      boolean isSet = referencer.eIsSet(getModel4Package().getRefMultiNonContainedNPL_Elements());
      if (isSet && referencer.getElements().size() > 0)
      {
        assertNull(referencer.getElements().get(0));
      }
    }
    catch (ObjectNotFoundException e)
    {
      fail("Should not have thrown ObjectNotFoundException");
    }

    tx.close();
    session.close();
  }

  public void testDirtyMulti() throws CommitException
  {
    CDOSession session = openSession();
    session.options().setPassiveUpdateEnabled(false);
    session.getPackageRegistry().putEPackage(getModel4Package());

    CDOTransaction tx = session.openTransaction();
    CDOResource r1 = tx.createResource(RESOURCENAME);

    // Create referencee and referencer (but no reference yet), and store them
    ContainedElementNoOpposite referencee = getModel4Factory().createContainedElementNoOpposite();
    r1.getContents().add(referencee);
    RefMultiNonContainedNPL referencer = getModel4Factory().createRefMultiNonContainedNPL();
    r1.getContents().add(referencer);
    tx.commit();

    // Create the reference, making the referencer dirty
    referencer.getElements().add(referencee);
    assertEquals(CDOState.DIRTY, CDOUtil.getCDOObject(referencer).cdoState());

    // Delete the referencee in 2nd session
    doSecondSession();

    // Refresh
    session.refresh();

    //
    try
    {
      boolean isSet = referencer.eIsSet(getModel4Package().getRefMultiNonContainedNPL_Elements());
      if (isSet && referencer.getElements().size() > 0)
      {
        assertNull(referencer.getElements().get(0));
      }
    }
    catch (ObjectNotFoundException e)
    {
      fail("Should not have thrown ObjectNotFoundException");
    }

    tx.close();
    session.close();
  }

  private void doSecondSession() throws CommitException
  {
    CDOSession session = openSession();
    CDOTransaction tx = session.openTransaction();
    CDOResource r1 = tx.getResource(RESOURCENAME);
    ContainedElementNoOpposite referencee = (ContainedElementNoOpposite)r1.getContents().get(0);
    EcoreUtil.delete(referencee);
    tx.commit();
    session.close();
  }
}

Back to the top