Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9e33d9709b51f1abb413e6a138d9413e819f2c57 (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
/*
 * Copyright (c) 2011-2013, 2016 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.CDODeltaNotification;
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.config.IRepositoryConfig;
import org.eclipse.emf.cdo.tests.config.impl.ConfigTest.Requires;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.tests.model1.PurchaseOrder;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
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.XMIResource;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.edit.command.AddCommand;
import org.eclipse.emf.edit.command.RemoveCommand;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.ResourceSetListenerImpl;
import org.eclipse.emf.transaction.RollbackException;
import org.eclipse.emf.transaction.TransactionalEditingDomain;

import org.eclipse.core.runtime.Status;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

/**
 * Test case for {@link CDODeltaNotification#getNewValue()} which must returns a local {@link EObject} contained in a
 * {@link XMIResource} because a {@link CDOObject} stored in a {@link CDOResource} references the local {@link EObject}.
 *
 * @author Esteban Dugueperoux
 */
@Requires(IRepositoryConfig.CAPABILITY_EXTERNAL_REFS)
public class Bugzilla_362270_Test extends AbstractCDOTest
{
  private final EReference SUPPLIERS = getModel1Package().getCompany_Suppliers();

  private final EReference PURCHASE_ORDERS = getModel1Package().getSupplier_PurchaseOrders();

  public void testNotifierNotACDOLegacyAdapter() throws Exception
  {
    TransactionalEditingDomain domain = TransactionalEditingDomain.Factory.INSTANCE.createEditingDomain();
    ResourceSet resourceSet = domain.getResourceSet();
    registerXMIFactory(resourceSet);

    // 1. Create the CDOResource
    Company obeoCompany = getModel1Factory().createCompany();
    obeoCompany.setName("OBEO");
    obeoCompany.setCity("Nantes");

    Supplier martinSupplier = getModel1Factory().createSupplier();
    obeoCompany.getSuppliers().add(martinSupplier);

    CDOSession session = openSession();
    final CDOTransaction cdoTransaction = session.openTransaction(resourceSet);

    CDOResource cdoResource = cdoTransaction.createResource(getResourcePath("/test1"));
    cdoResource.getContents().add(obeoCompany);
    cdoTransaction.commit();

    // 2. Create the local XMI resource
    URI localResourceURI = createXMIResource();

    // Reload the local resource through the CDOTransaction!!!
    Resource localResource = resourceSet.getResource(localResourceURI, true);
    Company martinCompany = (Company)localResource.getContents().get(0);
    PurchaseOrder purchaseOrder = martinCompany.getPurchaseOrders().get(0);

    Command addPurchaseOrderCmd = AddCommand.create(domain, martinSupplier, PURCHASE_ORDERS, purchaseOrder);
    domain.getCommandStack().execute(addPurchaseOrderCmd);

    // 4. Commit
    cdoTransaction.commit();

    domain.addResourceSetListener(new ResourceSetListenerImpl()
    {
      @Override
      public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException
      {
        throw new RollbackException(Status.CANCEL_STATUS);
      }
    });

    Command removeMartinSupplierCmd = RemoveCommand.create(domain, obeoCompany, SUPPLIERS, martinSupplier);
    domain.getCommandStack().execute(removeMartinSupplierCmd);

    AssertAdapter assertAdapter = new AssertAdapter(purchaseOrder);
    martinSupplier.eAdapters().add(assertAdapter);

    // 6. rollback the previous operation
    domain.getCommandStack().execute(new RecordingCommand(domain)
    {
      @Override
      protected void doExecute()
      {
        cdoTransaction.rollback();
      }
    });
  }

  private URI createXMIResource() throws IOException
  {
    ResourceSet localResourceSet = new ResourceSetImpl();
    registerXMIFactory(localResourceSet);

    File localResourceFile = createTempFile(getName(), ".model1").getAbsoluteFile();
    URI localResourceURI = URI.createFileURI(localResourceFile.getAbsolutePath());
    Resource localResource = localResourceSet.createResource(localResourceURI);

    Company martinCompany = getModel1Factory().createCompany();
    martinCompany.setName("Martin");
    martinCompany.setCity("Berlin");

    PurchaseOrder purchaseOrder = getModel1Factory().createPurchaseOrder();
    martinCompany.getPurchaseOrders().add(purchaseOrder);

    localResource.getContents().add(martinCompany);
    localResource.save(Collections.emptyMap());

    return localResourceURI;
  }

  private void registerXMIFactory(ResourceSet resourceSet)
  {
    Map<String, Object> map = resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap();
    map.put("model1", new XMIResourceFactoryImpl());
  }

  private final class AssertAdapter extends AdapterImpl
  {
    private EObject eObject;

    private int notifyCounter;

    public AssertAdapter(EObject eObject)
    {
      this.eObject = eObject;
    }

    @Override
    public void notifyChanged(Notification msg)
    {
      if (getModel1Package().getSupplier_PurchaseOrders().equals(msg.getFeature()))
      {
        switch (++notifyCounter)
        {
        case 1:
          assertEquals(null, msg.getNewValue());
          break;

        case 2:
          assertEquals(eObject, msg.getNewValue());
          break;

        default:
          fail("Only 0 or 2 calls are expected");
        }
      }
    }
  }
}

Back to the top