Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0045f6aa56013eb40010a775e46decd7c7ebd7b0 (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
/*
 * Copyright (c) 2014, 2016 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.eresource.EresourcePackage;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;
import org.eclipse.emf.cdo.tests.util.TestAdapter;
import org.eclipse.emf.cdo.transaction.CDOTransaction;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;

import java.io.File;

/**
 * Test that {@link CDOResource#setURI(org.eclipse.emf.common.util.URI)} notify adapters of Set {@link Notification} on Resource.RESOURCE__URI.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_443281_Test extends AbstractCDOTest
{
  private Object oldResourceFactory;

  private ResourceSetImpl resourceSet;

  @Override
  public void setUp() throws Exception
  {
    super.setUp();
    oldResourceFactory = Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", new XMIResourceFactoryImpl());
    resourceSet = new ResourceSetImpl();
  }

  public void testCDOResource_setURI() throws Exception
  {
    CDOSession session = openSession();
    CDOTransaction tx = session.openTransaction(resourceSet);
    String resourceName = "resource1.model1";
    CDOResource resource1 = tx.createResource(getResourcePath(resourceName));

    // Test
    TestAdapter testAdapter = new TestAdapter();
    resource1.eAdapters().add(testAdapter);
    URI uri = resource1.getURI();
    String newResourceName = "resource2.model1";
    URI newURI = uri.trimSegments(1).appendSegment(newResourceName);
    resource1.setURI(newURI);
    assertEquals(2, testAdapter.getNotifications().length);

    Notification notification1 = testAdapter.getNotifications()[0];
    assertEquals(resource1, notification1.getNotifier());
    assertEquals(EresourcePackage.Literals.CDO_RESOURCE_NODE__NAME, notification1.getFeature());
    assertEquals(Notification.SET, notification1.getEventType());
    assertEquals(resourceName, notification1.getOldValue());
    assertEquals(newResourceName, notification1.getNewValue());

    Notification notification2 = testAdapter.getNotifications()[1];
    assertEquals(resource1, notification2.getNotifier());
    assertEquals(Resource.RESOURCE__URI, notification2.getFeatureID(null));
    assertEquals(Notification.SET, notification2.getEventType());
    assertEquals(uri, notification2.getOldValue());
    assertEquals(newURI, notification2.getNewValue());

  }

  public void testXMIResource_setURI() throws Exception
  {
    String path = new File("./localResource.xmi").getCanonicalPath();
    URI localResourceURI = URI.createFileURI(path);
    Resource resource1 = resourceSet.createResource(localResourceURI);

    // Test
    TestAdapter testAdapter = new TestAdapter();
    resource1.eAdapters().add(testAdapter);
    URI uri = resource1.getURI();
    URI newURI = uri.trimSegments(1).appendSegment("resource2.model1");
    resource1.setURI(newURI);
    assertEquals(1, testAdapter.getNotifications().length);
    Notification notification = testAdapter.getNotifications()[0];
    assertEquals(resource1, notification.getNotifier());
    assertEquals(Resource.RESOURCE__URI, notification.getFeatureID(null));
    assertEquals(Notification.SET, notification.getEventType());
    assertEquals(uri, notification.getOldValue());
    assertEquals(newURI, notification.getNewValue());
  }

  @Override
  public void tearDown() throws Exception
  {
    resourceSet = null;
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("*", oldResourceFactory);
    oldResourceFactory = null;
    resourceSet = null;
    super.tearDown();
  }
}

Back to the top