Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e3ac3d8a8baacec3a9335255e1d86d8257c6514b (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
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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;

import org.eclipse.emf.cdo.common.CDOFetchRule;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.session.CDOCollectionLoadingPolicy;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.model1.Supplier;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOFetchRuleManager;

import org.eclipse.emf.internal.cdo.session.CDORevisionManagerImpl;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.util.EContentAdapter;
import org.eclipse.emf.spi.cdo.InternalCDOSession;
import org.eclipse.emf.spi.cdo.InternalCDOTransaction;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public class ContentAdapterTest extends AbstractCDOTest
{
  public void testContentAdapterUsingCDOResource() throws Exception
  {
    final boolean notified[] = new boolean[1];
    EContentAdapter contentAdapter = new EContentAdapter()
    {
      @Override
      public void notifyChanged(Notification notification)
      {
        super.notifyChanged(notification);
        notified[0] = true;
      }

      @Override
      protected boolean resolve()
      {
        return false;
      }
    };

    CDOID supplierID = null;

    {
      CDOSession session = openModel1Session();
      CDOTransaction transaction = session.openTransaction();

      CDOResource resource = transaction.createResource("/test2");
      // resource.eAdapters().add(contentAdapter);

      Supplier supplier = getModel1Factory().createSupplier();
      resource.getContents().add(supplier);
      // notified[0] = false;

      // contentAdapter should receive notification
      supplier.setName("HELLO");

      // assertEquals(true, notified[0]);
      transaction.commit();
      supplierID = CDOUtil.getCDOObject(supplier).cdoID();

      transaction.close();
      session.close();
    }

    {
      InternalCDOSession session = (InternalCDOSession)openModel1Session();

      CDOFetchRuleManagerInfo info = new CDOFetchRuleManagerInfo();
      CDORevisionManagerImpl revisionManager = (CDORevisionManagerImpl)session.getRevisionManager();
      revisionManager.setRuleManager(info);

      InternalCDOTransaction transaction = (InternalCDOTransaction)session.openTransaction();
      CDOResource resource = transaction.getResource("/test2");

      // I don't want to fetch my objects!!
      EList<Adapter> adapters = resource.eAdapters();
      adapters.add(contentAdapter);

      // XXX FAILURE HERE!!!
      // By adding an adapter, we shouldn`t fetch objects
      assertEquals(false, info.getFetchedIDs().contains(supplierID));

      Supplier supplier = (Supplier)transaction.getObject(supplierID);
      notified[0] = false;

      // contentAdapter should receive notification
      supplier.setName("HELLO");

      // Should have been notified!!
      assertEquals(true, notified[0]);

      transaction.commit();
      transaction.close();
    }
  }

  /**
   * @author Eike Stepper
   */
  public class CDOFetchRuleManagerInfo implements CDOFetchRuleManager
  {
    private Set<CDOID> fetchedIDs = new HashSet<CDOID>();

    public CDOFetchRuleManagerInfo()
    {
    }

    public CDOID getContext()
    {
      return CDOID.NULL;
    }

    public void clear()
    {
      fetchedIDs.clear();
    }

    public Set<CDOID> getFetchedIDs()
    {
      return fetchedIDs;
    }

    public List<CDOFetchRule> getFetchRules(Collection<CDOID> ids)
    {
      // accumulate changes
      for (CDOID id : ids)
      {
        fetchedIDs.add(id);
      }

      return null;
    }

    public CDOCollectionLoadingPolicy getCollectionLoadingPolicy()
    {
      return CDOCollectionLoadingPolicy.DEFAULT;
    }
  }
}

Back to the top