Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: daebd19d1777ba95a5a52cc1870271f65d513533 (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
/*
 * Copyright (c) 2011-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:
 *    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.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.spi.common.revision.BaseCDORevision;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.IModelConfig;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.model1.Model1Factory;
import org.eclipse.emf.cdo.tests.model1.Model1Package;
import org.eclipse.emf.cdo.tests.model1.OrderDetail;
import org.eclipse.emf.cdo.tests.model1.Product1;
import org.eclipse.emf.cdo.tests.model1.PurchaseOrder;
import org.eclipse.emf.cdo.tests.model1.VAT;
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.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.emf.ecore.util.EcoreUtil;

/**
 * @author Caspar De Groot
 */
@Requires(IModelConfig.CAPABILITY_LEGACY)
public class Bugzilla_335675_Test extends AbstractCDOTest
{
  public void test0() throws Exception
  {
    EPackage pkg = null;
    EAttribute attr1 = null;
    EAttribute attr2 = null;
    EAttribute attr3 = null;

    final int TRANSIENT_VALUE = 2;

    EClass classA = null;
    {
      pkg = createUniquePackage();

      classA = EcoreFactory.eINSTANCE.createEClass();
      classA.setName("A");

      attr1 = createAttribute("attr1", EcorePackage.eINSTANCE.getEInt(), false);
      attr2 = createAttribute("attr2", EcorePackage.eINSTANCE.getEInt(), true);
      attr3 = createAttribute("attr3", EcorePackage.eINSTANCE.getEInt(), false);

      classA.getEStructuralFeatures().add(attr1);
      classA.getEStructuralFeatures().add(attr2);
      classA.getEStructuralFeatures().add(attr3);

      pkg.getEClassifiers().add(classA);
    }

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("test"));

      classA = (EClass)pkg.getEClassifier("A");
      EObject instanceA = EcoreUtil.create(classA);
      attr1 = (EAttribute)classA.getEStructuralFeature("attr1");
      attr2 = (EAttribute)classA.getEStructuralFeature("attr2");
      attr3 = (EAttribute)classA.getEStructuralFeature("attr3");
      instanceA.eSet(attr1, 1);
      instanceA.eSet(attr2, TRANSIENT_VALUE);
      instanceA.eSet(attr3, 3);

      resource.getContents().add(instanceA);
      transaction.commit();
      session.close();
    }

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("test"));
      CDOObject cdoObject = CDOUtil.getCDOObject(resource.getContents().get(0));
      classA = cdoObject.eClass();
      attr1 = (EAttribute)classA.getEStructuralFeature("attr1");
      attr2 = (EAttribute)classA.getEStructuralFeature("attr2");
      attr3 = (EAttribute)classA.getEStructuralFeature("attr3");

      BaseCDORevision rev = (BaseCDORevision)cdoObject.cdoRevision();
      // int attr1Val = (Integer)rev.getValue(attr1);
      // int attr3Val = (Integer)rev.getValue(attr3);
      try
      {
        int attr2Val = (Integer)rev.getValue(attr2);
        fail("Should have thrown an exception, but fetched value " + attr2Val);
      }
      catch (IllegalArgumentException ex)
      {
        // Good
      }

      session.close();
    }
  }

  public void test1() throws CommitException
  {
    EAttribute nameAttr = Model1Package.eINSTANCE.getProduct1_Name();
    assertEquals(false, nameAttr.isTransient());

    EAttribute descriptionAttr = Model1Package.eINSTANCE.getProduct1_Description();
    assertEquals(true, descriptionAttr.isTransient());

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.createResource(getResourcePath("test"));

      Product1 product = Model1Factory.eINSTANCE.createProduct1();
      product.setName("name");
      product.setDescription("description");
      product.setVat(VAT.VAT7);
      resource.getContents().add(product);

      PurchaseOrder order = Model1Factory.eINSTANCE.createPurchaseOrder();
      OrderDetail detail = Model1Factory.eINSTANCE.createOrderDetail();
      order.getOrderDetails().add(detail);
      resource.getContents().add(order);

      detail.setProduct(product);

      transaction.commit();
      session.close();
    }

    {
      CDOSession session = openSession();
      CDOTransaction transaction = session.openTransaction();
      CDOResource resource = transaction.getResource(getResourcePath("test"));
      CDOObject cdoObject = CDOUtil.getCDOObject(resource.getContents().get(0));

      BaseCDORevision rev = (BaseCDORevision)cdoObject.cdoRevision();
      // String name = (String)rev.getValue(nameAttr);
      try
      {
        String desc = (String)rev.getValue(descriptionAttr);
        fail("Should have thrown an exception, but fetched value: " + desc);
      }
      catch (Exception ex)
      {
        // Good
      }

      session.close();
    }
  }

  private EAttribute createAttribute(String name, EDataType type, boolean tranzient)
  {
    EAttribute attr = EcoreFactory.eINSTANCE.createEAttribute();
    attr.setName(name);
    attr.setEType(type);
    attr.setTransient(tranzient);
    return attr;
  }
}

Back to the top