Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 510294a3616ad744e20070e5775e7def8c5e2f2b (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
/*
 * Copyright (c) 2015, 2016, 2019 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:
 *    Esteban Dugueperoux - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.common.CDOCommonSession.Options.PassiveUpdateMode;
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.Product1;
import org.eclipse.emf.cdo.tests.model1.VAT;
import org.eclipse.emf.cdo.tests.util.TestAdapter;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import java.util.Collections;

/**
 * Bug 450880 about ClassCastException on CDODeltaNotification.getNewValue or getOldValue for Enum-based attribute.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_450880_Test extends AbstractCDOTest
{
  private static final String RESOURCE_NAME = "test1.model1";

  public void testCDODeltaNotificationGetValueWithEnumTypedAttributeOnCDOResource() throws Exception
  {
    CDOSession session1 = openSession();
    session1.options().setPassiveUpdateMode(PassiveUpdateMode.ADDITIONS);
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath(RESOURCE_NAME));
    testCDODeltaNotificationGetValueWithEnumTypedAttribute(resource1);

    CDOSession session2 = openSession();
    session2.options().setPassiveUpdateMode(PassiveUpdateMode.ADDITIONS);
    CDOTransaction transaction2 = session2.openTransaction();
    CDOResource resource1Bis = transaction2.getResource(getResourcePath(RESOURCE_NAME));
    Product1 product1 = (Product1)resource1Bis.getContents().get(0);
    TestAdapter testAdapter = new TestAdapter(product1);
    commitAndSync(transaction1, transaction2);

    Notification notification = testAdapter.assertNotifications(1)[0];
    assertEquals(getModel1Package().getProduct1_OtherVATs().getDefaultValue(), notification.getOldValue());
    assertEquals(VAT.VAT0, notification.getNewValue());
  }

  public void testCDODeltaNotificationGetValueWithEnumTypedAttributeOnXMIResource() throws Exception
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("model1", new XMIResourceFactoryImpl());

    URI localMainResourceURI = URI.createFileURI(createTempFile(getName(), ".model1").getCanonicalPath());
    Resource resource1 = resourceSet.createResource(localMainResourceURI);

    testCDODeltaNotificationGetValueWithEnumTypedAttribute(resource1);
  }

  private void testCDODeltaNotificationGetValueWithEnumTypedAttribute(Resource resource) throws Exception
  {
    Product1 product1 = getModel1Factory().createProduct1();
    resource.getContents().add(product1);
    resource.save(Collections.emptyMap());
    TestAdapter testAdapter = new TestAdapter(product1);
    VAT newValue = VAT.VAT0;
    product1.setVat(newValue);

    Notification notification = testAdapter.assertNotifications(1)[0];
    assertEquals(getModel1Package().getProduct1_OtherVATs().getDefaultValue(), notification.getOldValue());
    assertEquals(newValue, notification.getNewValue());
  }
}

Back to the top