Skip to main content
summaryrefslogtreecommitdiffstats
blob: f0000904a22ecc1cfbc2a4037b34cfd530b0c8c2 (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
186
187
/*
 * Copyright (c) 2004 - 2012 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.Category;
import org.eclipse.emf.cdo.tests.model1.Company;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;

/**
 * IndexOutOfBoundsException in buildNotification.
 * <p>
 * See bug 306710
 */
public class Bugzilla_306710_Test extends AbstractCDOTest
{
  public void testBugzilla_306710_remove() throws Exception
  {
    final NotifyCounter counter = new NotifyCounter();

    // setup connection1.
    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath("/test1"));

    // add initial model.
    Company company1 = getModel1Factory().createCompany();
    resource1.getContents().add(company1);
    Category category1a = getModel1Factory().createCategory();
    company1.getCategories().add(category1a);
    transaction1.commit();

    // sleep(100);

    // setup connection2.
    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();
    transaction2.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
    CDOResource resource2 = transaction2.getOrCreateResource(getResourcePath("/test1"));

    // add adapter to company2 to have sendDeltaNotification being called.
    Company company2 = (Company)resource2.getContents().get(0);
    company2.eAdapters().add(new AdapterImpl()
    {
      @Override
      public void notifyChanged(Notification msg)
      {
        if (msg.getEventType() == Notification.ADD)
        {
          counter.incAdds();
        }
        else if (msg.getEventType() == Notification.REMOVE)
        {
          counter.incRemoves();
        }
      }
    });

    // add and remove an object from category list of company to have the CDONotificationBuilder call remove with an
    // index not known to the oldRevision.
    Category category1b = getModel1Factory().createCategory();
    company1.getCategories().add(0, category1b);
    company1.getCategories().remove(1);

    // commit the changes.
    transaction1.commit();

    // wait for the invalidation to arrive on transaction2.
    transaction2.waitForUpdate(transaction1.getLastCommitTime(), DEFAULT_TIMEOUT);

    // cleanup.
    session1.close();
    session2.close();

    // check if the notifications arrived (which is not the case because of the exception).
    assertEquals(1, counter.getAdds());
    assertEquals(1, counter.getRemoves());
  }

  /**
   * @since 4.0
   */
  public void testBugzilla_306710_addRemove() throws Exception
  {
    final NotifyCounter counter = new NotifyCounter();

    // setup connection1.
    CDOSession session1 = openSession();
    CDOTransaction transaction1 = session1.openTransaction();
    CDOResource resource1 = transaction1.createResource(getResourcePath("/test1"));

    // add initial model.
    Company company1 = getModel1Factory().createCompany();
    resource1.getContents().add(company1);
    transaction1.commit();

    // sleep(100);

    // setup connection2.
    CDOSession session2 = openSession();
    CDOTransaction transaction2 = session2.openTransaction();
    transaction2.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);
    CDOResource resource2 = transaction2.getOrCreateResource(getResourcePath("/test1"));

    // add adapter to company2 to have sendDeltaNotification being called.
    Company company2 = (Company)resource2.getContents().get(0);
    company2.eAdapters().add(new AdapterImpl()
    {
      @Override
      public void notifyChanged(Notification msg)
      {
        if (msg.getEventType() == Notification.ADD)
        {
          counter.incAdds();
        }
        else if (msg.getEventType() == Notification.REMOVE)
        {
          counter.incRemoves();
        }
      }
    });

    // add and remove an object from category list of company to have the CDONotificationBuilder call remove with an
    // index not known to the oldRevision.
    Category category1a = getModel1Factory().createCategory();
    company1.getCategories().add(0, category1a);
    company1.getCategories().remove(0);

    // commit the changes.
    transaction1.commit();

    // wait for the invalidation to arrive on transaction2.
    transaction2.waitForUpdate(transaction1.getLastCommitTime(), DEFAULT_TIMEOUT);

    // cleanup.
    session1.close();
    session2.close();

    // check if the notifications arrived (which is not the case because of the exception).
    assertEquals(0, counter.getAdds());
    assertEquals(0, counter.getRemoves());
  }

  /**
   * Helper class to count the notifications.
   */
  public static class NotifyCounter
  {
    int adds;

    int removes;

    public int getAdds()
    {
      return adds;
    }

    public int getRemoves()
    {
      return removes;
    }

    public void incAdds()
    {
      adds++;
    }

    public void incRemoves()
    {
      removes++;
    }
  }
}

Back to the top