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

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.Company;
import org.eclipse.emf.cdo.tests.model1.PurchaseOrder;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.transaction.CDOSavepoint;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.emf.internal.cdo.object.CDOLegacyWrapper;

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.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 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.IStatus;
import org.eclipse.core.runtime.Status;

import java.io.File;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Esteban Dugueperoux
 */
public class Bugzilla_362270c_Test extends AbstractCDOTest
{
  private static final String RESOURCE_PATH = "/test1";

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

    // 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();
    CDOTransaction transaction = session.openTransaction(resourceSet);
    CDOResource resource = transaction.createResource(getResourcePath(RESOURCE_PATH));

    resource.getContents().add(obeoCompany);
    resource.save(Collections.emptyMap());

    // 2. Create the local XMI resource
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put("model1", new XMIResourceFactoryImpl());

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

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

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

    localResource.getContents().add(martinCompany);
    localResource.save(Collections.emptyMap());
    localResource = resourceSet.getResource(localResourceURI, true);

    // 3. Add a reference from the CDOResource to the local resource
    Command addPurchaseOrderCmd = AddCommand.create(domain, martinSupplier,
        getModel1Package().getSupplier_PurchaseOrders(), purchaseOrder);
    domain.getCommandStack().execute(addPurchaseOrderCmd);

    resource.save(Collections.emptyMap());

    // 4. Create the first CDOSavepoint
    final CDOSavepoint firstSavepoint = transaction.setSavepoint();

    Rollbacker rollbacker = new Rollbacker();
    domain.addResourceSetListener(rollbacker);

    // 5. Detach a CDOObject
    Command removeMartinSupplierCmd = RemoveCommand.create(domain, obeoCompany,
        getModel1Package().getCompany_Suppliers(), martinSupplier);
    domain.getCommandStack().execute(removeMartinSupplierCmd);

    // 6. Rollback the previous operation
    Command rollbackSavepointCmd = new RecordingCommand(domain)
    {
      @Override
      protected void doExecute()
      {
        firstSavepoint.rollback();
      }
    };

    NotifierCollector collector = new NotifierCollector();
    martinSupplier.eAdapters().add(collector);

    domain.getCommandStack().execute(rollbackSavepointCmd);
    domain.removeResourceSetListener(rollbacker);

    for (Object notifier : collector.getNotifiers())
    {
      assertNotInstanceOf(CDOLegacyWrapper.class, notifier);
    }
  }

  /**
   * @author Esteban Dugueperoux
   */
  private class Rollbacker extends ResourceSetListenerImpl
  {
    @Override
    public Command transactionAboutToCommit(ResourceSetChangeEvent event) throws RollbackException
    {
      IStatus status = Status.CANCEL_STATUS;
      throw new RollbackException(status);
    }
  }

  /**
   * @author Esteban Dugueperoux
   */
  private class NotifierCollector extends AdapterImpl
  {
    private Set<Object> notifiers = new HashSet<Object>();

    @Override
    public void notifyChanged(Notification msg)
    {
      notifiers.add(msg.getNotifier());
    }

    public Set<Object> getNotifiers()
    {
      return notifiers;
    }
  }
}

Back to the top