Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 249375d68e663340daf2b4cd803aadf3d18dafb8 (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
/***************************************************************************
 * Copyright (c) 2004, 2005, 2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, 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.model1;


import org.eclipse.emf.cdo.client.CDOResource;
import org.eclipse.emf.cdo.client.PausableChangeRecorder;
import org.eclipse.emf.cdo.client.ResourceManager;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;

import testmodel1.TreeNode;

import java.util.Iterator;


public class AdapterTest extends AbstractModel1Test
{
  public void testFiveLevels() throws Exception
  {
    final String RESOURCE = "/test/res";
    final String ROOT = "root";
    final String[] CHILDREN = { "a", "b", "c"};

    TreeNode root = createNode(ROOT);
    TreeNode[] children = createChildren(CHILDREN, root);
    for (int i = 0; i < children.length; i++)
      createChildren(new String[] { "a" + i, "b" + i, "c" + i}, children[i]);
    CDOResource resource = saveRoot(root, RESOURCE);

    ResourceManager resourceManager = resource.getResourceManager();
    ResourceSet resourceSet = resourceManager.getResourceSet();
    PausableChangeRecorder transaction = resourceManager.getTransaction();
    assertAdapter(resourceSet, transaction);
  }

  protected void assertAdapter(Notifier notifier, Adapter adapter)
  {
    EList adapters = notifier.eAdapters();
    assertNotNull(notifier.toString(), adapters);

    int count = 0;
    for (Iterator it = adapters.iterator(); it.hasNext();)
    {
      Adapter element = (Adapter) it.next();
      count += element == adapter ? 1 : 0;
    }
    assertEquals(notifier.toString(), 1, count);

    if (notifier instanceof ResourceSet)
    {
      ResourceSet resourceSet = (ResourceSet) notifier;
      EList resources = resourceSet.getResources();
      for (Iterator it = resources.iterator(); it.hasNext();)
      {
        Resource resource = (Resource) it.next();
        assertAdapter(resource, adapter);
      }
    }
    else if (notifier instanceof Resource)
    {
      Resource resource = (Resource) notifier;
      EList contents = resource.getContents();
      for (Iterator it = contents.iterator(); it.hasNext();)
      {
        EObject object = (EObject) it.next();
        assertAdapter(object, adapter);
      }
    }
    else if (notifier instanceof EObject)
    {
      EObject object = (EObject) notifier;
      EList contents = object.eContents();
      for (Iterator it = contents.iterator(); it.hasNext();)
      {
        EObject child = (EObject) it.next();
        assertAdapter(child, adapter);
      }
    }
  }
}

Back to the top