Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2a2a3127933124643e6a430cea5669ca521efe9d (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
/**
 * Copyright (c) 2004 - 2009 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:
 *    Victor Roldan Betancort - initial API and implementation
 *    Simon McDuff - maintenance
 *    Eike Stepper - maintenance
 */
package org.eclipse.emf.cdo.tests.bugzilla;

import org.eclipse.emf.cdo.common.id.CDOID;
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.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.spi.cdo.InternalCDOTransaction;

import java.util.ArrayList;
import java.util.List;

/**
 * NPE in ChangeSubscriptionManager.isPending() while subscribing a pending TRANSIENT-by-removal object
 * <p>
 * See https://bugs.eclipse.org/251087
 * 
 * @author Victor Roldan Betancort
 */
public class Bugzilla_251087_Test extends AbstractCDOTest
{
  public void testSubscription() throws Exception
  {
    CDOSession session = openModel1Session();
    CDOTransaction transaction1 = session.openTransaction();

    transaction1.options().setInvalidationNotificationEnabled(true);
    transaction1.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);

    String resourcePath = "/test1";
    CDOResource res = transaction1.createResource(resourcePath);

    Company obj2 = getModel1Factory().createCompany();

    TestAdapter testAdapter = new TestAdapter();

    obj2.eAdapters().add(testAdapter);
    res.getContents().add(obj2);
    transaction1.commit();

    res.getContents().remove(obj2);
    res.getContents().add(obj2);
    res.getContents().remove(obj2);
    transaction1.commit();
  }

  public void testSubscriptionDetached() throws Exception
  {
    CDOSession sessionA = openModel1Session();
    CDOSession sessionB = openModel1Session();
    CDOTransaction transaction1 = sessionA.openTransaction();

    transaction1.options().setInvalidationNotificationEnabled(true);
    transaction1.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);

    String resourcePath = "/test1";
    CDOResource res = transaction1.createResource(resourcePath);
    Company obj2 = getModel1Factory().createCompany();
    res.getContents().add(obj2);
    transaction1.commit();

    CDOTransaction transB1 = sessionB.openTransaction();
    CDOID companyID = CDOUtil.getCDOObject(obj2).cdoID();
    Company companyB = (Company)transB1.getObject(companyID);
    sessionB.options().setPassiveUpdateEnabled(false);
    transB1.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
    final TestAdapter testAdapter = new TestAdapter();
    companyB.eAdapters().add(testAdapter);
    assertEquals(true, ((InternalCDOTransaction)transB1).hasSubscription(companyID));

    res.getContents().remove(obj2);
    transaction1.commit();

    msg("Checking after commit");
    new PollingTimeOuter()
    {
      @Override
      protected boolean successful()
      {
        return testAdapter.getNotifications().size() == 1;
      }
    }.assertNoTimeOut();

    assertEquals(false, ((InternalCDOTransaction)transB1).hasSubscription(companyID));
  }

  /**
   * @author Simon McDuff
   */
  private static class TestAdapter implements Adapter
  {
    private List<Notification> notifications = new ArrayList<Notification>();

    private Notifier notifier;

    public TestAdapter()
    {
    }

    public Notifier getTarget()
    {
      return notifier;
    }

    public List<Notification> getNotifications()
    {
      return notifications;
    }

    public boolean isAdapterForType(Object type)
    {
      return false;
    }

    public void notifyChanged(Notification notification)
    {
      notifications.add(notification);
    }

    public void setTarget(Notifier newTarget)
    {
      notifier = newTarget;
    }
  }
}

Back to the top