Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd32afecf48b2429475a31e83d109898ba61ae77 (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
/*
 * Copyright (c) 2012 Esteban Dugueperoux 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.CDOResourceFactory;
import org.eclipse.emf.cdo.tests.AbstractCDOTest;

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

import java.util.Collections;

/**
 * ResourceSet.getResource(URI,true) fails when called several times. See bug 395999.
 *
 * @author Esteban Dugueperoux
 */
public class Bugzilla_395999_Test extends AbstractCDOTest
{
  public void testTwiceGetCDOResourceOnResourceSetImpl() throws Exception
  {
    Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE;
    Object oldFactory = registry.getProtocolToFactoryMap().put(getURIProtocol(), CDOResourceFactory.INSTANCE);

    try
    {
      URI uri = URI.createURI(getURIPrefix() + "/" + getRepository().getName() + getResourcePath("/res1")
          + "?transactional=true");

      ResourceSet resourceSet = new ResourceSetImpl();
      Resource resource = resourceSet.createResource(uri);
      resource.save(Collections.emptyMap());

      loadTwiceAndSaveResource(uri);
    }
    finally
    {
      if (oldFactory == null)
      {
        registry.getExtensionToFactoryMap().remove(getURIProtocol());
      }
      else
      {
        registry.getExtensionToFactoryMap().put(getURIProtocol(), oldFactory);
      }
    }
  }

  public void testTwiceGetXMIResourceOnResourceSetImpl() throws Exception
  {
    Resource.Factory.Registry registry = Resource.Factory.Registry.INSTANCE;
    Object oldFactory = registry.getExtensionToFactoryMap().put("model1", new XMIResourceFactoryImpl());

    try
    {
      ResourceSet resourceSet = new ResourceSetImpl();

      URI uri = URI.createFileURI(createTempFile("resource", ".model1").getCanonicalPath());
      Resource resource = resourceSet.createResource(uri);
      resource.save(Collections.emptyMap());

      loadTwiceAndSaveResource(uri);
    }
    finally
    {
      if (oldFactory == null)
      {
        registry.getExtensionToFactoryMap().remove("model1");
      }
      else
      {
        registry.getExtensionToFactoryMap().put("model1", oldFactory);
      }
    }
  }

  private void loadTwiceAndSaveResource(URI resourceURI) throws Exception
  {
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.getResource(resourceURI, true);

    assertEquals("The ResourceSetImpl should returns the same Resource as in the first call", resource,
        resourceSet.getResource(resourceURI, true));

    resource.save(Collections.emptyMap());
  }
}

Back to the top