Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15287a5f43e27fb69c703ed024ca4dafb83cabcf (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
/*
 * Copyright (c) 2009-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:
 *    Simon McDuff - initial API and implementation
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDOObject;
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.model1.Order;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * Bug 258933 - CDORevisionImpl.eIsSet() works incorrectly.
 *
 * @author Simon McDuff
 */
public class Bugzilla_258933_Test extends AbstractCDOTest
{
  private static final Object NIL = new Object();

  public void testBugzilla_258933() throws Exception
  {
    // Do not set any value to level. Because 'level' is NOT unsettable we expect isSet==false
    testWithValue("level", NIL, false);
  }

  public void testBugzilla_258933_SetToDefaultValue() throws Exception
  {
    // Set level to it's default (10). Because 'level' is NOT unsettable we expect isSet==false
    testWithValue("level", new Integer(10), false);
  }

  public void testBugzilla_258933_String_SetToDefaultValue() throws Exception
  {
    // Set level to it's default (Simon). Because 'settable' is NOT unsettable we expect isSet==false
    testWithValue("settable", "Simon", false);
  }

  public void testBugzilla_258933_String_SetToNull() throws Exception
  {
    // Set level to null. Because 'settable' is NOT unsettable and null is not the default we expect isSet==true
    testWithValue("settable", null, true);
  }

  public void testBugzilla_258933_String_unsettable() throws Exception
  {
    // Do not set any value to settable. Because 'settable' is NOT unsettable we expect isSet==false
    testWithValue("settable", NIL, false);
  }

  public void testBugzilla_258933_String_SetToDefaultValue_unsettable() throws Exception
  {
    // Set level to it's default (Simon). Because 'unsettable' is unsettable we expect isSet==true
    testWithValue("unsettable", "Simon", true);
  }

  public void testBugzilla_258933_String_SetToNull_unsettable() throws Exception
  {
    // Set level to null. Because 'unsettable' is unsettable we expect isSet==true
    testWithValue("unsettable", null, true);
  }

  public void testBugzilla_258933_String() throws Exception
  {
    // Do not set any value to unsettable. Because 'unsettable' is unsettable we expect isSet==false
    testWithValue("unsettable", NIL, false);
  }

  private void testWithValue(String featureName, Object initializeValue, boolean isSet) throws Exception
  {
    EPackage topPackage = createDynamicEPackage();

    {
      EPackage subpackage1 = topPackage.getESubpackages().get(0);
      EClass class1Class = (EClass)subpackage1.getEClassifier("class1");
      EStructuralFeature feature = class1Class.getEStructuralFeature(featureName);

      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(topPackage);

      CDOTransaction transaction = session.openTransaction();
      EObject instance = EcoreUtil.create(class1Class);
      if (initializeValue != NIL)
      {
        instance.eSet(feature, initializeValue);
        // instance.eUnset(feature);
      }

      assertEquals(isSet, instance.eIsSet(feature));

      CDOResource resource = transaction.createResource(getResourcePath("/test1"));
      resource.getContents().add(instance);
      assertEquals(isSet, instance.eIsSet(feature));

      transaction.commit();
      assertEquals(isSet, instance.eIsSet(feature));

      session.close();
    }

    clearCache(getRepository().getRevisionManager());

    CDOSession session = openSession();
    if (session instanceof org.eclipse.emf.cdo.net4j.CDONet4jSession)
    {
      ((org.eclipse.emf.cdo.net4j.CDONet4jSession)session).options().getNet4jProtocol().setTimeout(2000L);
    }

    EPackage subpackage1 = session.getPackageRegistry().getEPackage(topPackage.getESubpackages().get(0).getNsURI());
    EClass class1Class = (EClass)subpackage1.getEClassifier("class1");
    EStructuralFeature feature = class1Class.getEStructuralFeature(featureName);

    CDOTransaction transaction = session.openTransaction();
    CDOResource resource = transaction.getResource(getResourcePath("/test1"));
    CDOObject instance = CDOUtil.getCDOObject(resource.getContents().get(0));
    assertEquals(isSet, instance.eIsSet(feature));

    resource.getContents().remove(0);
    assertEquals(isSet, instance.eIsSet(feature));

    if (feature.isUnsettable())
    {
      instance.eUnset(feature);
      assertEquals(false, instance.eIsSet(feature));
    }
  }

  public void testBugzilla_258278_List() throws Exception
  {
    {
      Order order = getModel1Factory().createPurchaseOrder();
      assertEquals(false, order.eIsSet(getModel1Package().getOrder_OrderDetails()));

      CDOSession session = openSession();
      session.getPackageRegistry().putEPackage(getModel1Package());

      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("/test1"));
      resource.getContents().add(order);
      assertEquals(false, order.eIsSet(getModel1Package().getOrder_OrderDetails()));

      transaction.commit();
      assertEquals(false, order.eIsSet(getModel1Package().getOrder_OrderDetails()));

      session.close();
    }

    clearCache(getRepository().getRevisionManager());
    CDOSession session = openSession();

    CDOTransaction transaction = session.openTransaction();
    Order instance = (Order)transaction.getResource(getResourcePath("/test1")).getContents().get(0);
    assertEquals(false, instance.eIsSet(getModel1Package().getOrder_OrderDetails()));

    transaction.getResource(getResourcePath("/test1")).getContents().remove(0);
    assertEquals(false, instance.eIsSet(getModel1Package().getOrder_OrderDetails()));

    instance.getOrderDetails().add(getModel1Factory().createOrderDetail());
  }

  private EPackage createDynamicEPackage()
  {
    final EcoreFactory efactory = EcoreFactory.eINSTANCE;
    final EcorePackage epackage = EcorePackage.eINSTANCE;

    // Create a new EPackage and add the new EClasses
    EPackage topPackage = createUniquePackage("top");
    EPackage subPackage1 = createUniquePackage("sub");

    {
      EClass schoolBookEClass = efactory.createEClass();
      schoolBookEClass.setName("class1");
      // create a new attribute for this EClass
      EAttribute level = efactory.createEAttribute();
      level.setName("level");
      level.setEType(epackage.getEInt());
      level.setUnsettable(false);
      level.setDefaultValue(new Integer(10));
      schoolBookEClass.getEStructuralFeatures().add(level);

      EAttribute settable = efactory.createEAttribute();
      settable.setName("settable");
      settable.setEType(epackage.getEString());
      level.setUnsettable(false);
      settable.setDefaultValue(new String("Simon"));
      schoolBookEClass.getEStructuralFeatures().add(settable);

      EAttribute unsettable = efactory.createEAttribute();
      unsettable.setName("unsettable");
      unsettable.setEType(epackage.getEString());
      unsettable.setUnsettable(true);
      unsettable.setDefaultValue(new String("Simon"));
      schoolBookEClass.getEStructuralFeatures().add(unsettable);

      subPackage1.getEClassifiers().add(schoolBookEClass);
    }

    topPackage.getESubpackages().add(subPackage1);
    return topPackage;
  }
}

Back to the top