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

import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.internal.net4j.protocol.CommitTransactionRequest;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSession.Options;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.config.IModelConfig;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.tests.model1.legacy.Model1Factory;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOAdapterPolicy;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.ecore.resource.Resource;

import java.util.Collections;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

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

  @Override
  protected void doSetUp() throws Exception
  {
    super.doSetUp();
  }

  @Requires(IModelConfig.CAPABILITY_LEGACY)
  public void testDeltaNotification() throws Exception
  {
    CDOSession session = openSession();
    CDONet4jSession.Options options = (Options)session.options();
    options.setCommitTimeout(10 * CommitTransactionRequest.DEFAULT_MONITOR_TIMEOUT_SECONDS);

    CDOTransaction transaction1 = session.openTransaction();
    transaction1.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);

    CDOResource resource1 = transaction1.createResource(getResourcePath(RESOURCE_PATH));

    // 1. Create a example model
    Supplier supplier = initializeModel(resource1);

    resource1.getContents().add(supplier);
    resource1.save(Collections.emptyMap());

    transaction1.close();
    session.close();

    session = openSession();
    transaction1 = session.openTransaction();
    transaction1.options().addChangeSubscriptionPolicy(CDOAdapterPolicy.ALL);

    resource1 = transaction1.getResource(getResourcePath(RESOURCE_PATH));
    supplier = (Supplier)resource1.getContents().get(0);

    CountDownLatch latch = new CountDownLatch(1);
    TestAdapter adapter = new TestAdapter(latch);
    supplier.eAdapters().add(adapter);

    doClient2();
    latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);

    assertEquals(true, adapter.notified());
    assertEquals(adapter.getFailureMessage(), true, adapter.assertCorrectNotification());
  }

  private Supplier initializeModel(CDOResource resource1)
  {
    Supplier supplier = Model1Factory.eINSTANCE.createSupplier();
    resource1.getContents().add(supplier);

    return supplier;
  }

  private void doClient2() throws CommitException
  {
    CDOSession session = openSession();
    CDONet4jSession.Options options = (Options)session.options();
    options.setCommitTimeout(10 * CommitTransactionRequest.DEFAULT_MONITOR_TIMEOUT_SECONDS);
    CDOTransaction transaction2 = session.openTransaction();

    Resource resource2 = transaction2.getResource(getResourcePath(RESOURCE_PATH));
    Supplier supplier = (Supplier)resource2.getContents().get(0);

    supplier.setPreferred(false);
    transaction2.commit();
  }

  /**
   * @author Martin Fluegge
   */
  private class TestAdapter extends AdapterImpl
  {
    private CountDownLatch latch;

    private boolean assertCorrectNotification;

    private boolean notified;

    private String failureMessage;

    public TestAdapter(CountDownLatch latch)
    {
      this.latch = latch;
    }

    @Override
    public void notifyChanged(Notification notification)
    {
      // If a previous received notification was incorrect we doesn't checks anymore
      if (!notified || notified && assertCorrectNotification)
      {
        try
        {
          Object notifier = notification.getNotifier();
          boolean newBooleanValue = notification.getNewBooleanValue();
          assertCorrectNotification = notifier instanceof Supplier && !newBooleanValue;
          if (!assertCorrectNotification)
          {
            failureMessage = "Notifier is not the expected type : " + notifier.getClass().getName();
          }

          notified = true;
        }
        finally
        {
          latch.countDown();
        }
      }
    }

    public boolean notified()
    {
      return notified;
    }

    public boolean assertCorrectNotification()
    {
      return assertCorrectNotification;
    }

    public String getFailureMessage()
    {
      return failureMessage;
    }
  }
}

Back to the top