[#476] Support EMF's ResourceLocators in XtextResourceSet.

Signed-off-by: Arne Deutsch <Arne.Deutsch@itemis.de>
This commit is contained in:
Arne Deutsch 2018-03-21 15:40:22 +01:00
parent f4280fc031
commit e90230f1b1
8 changed files with 75 additions and 9 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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
@ -17,6 +17,7 @@ import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.junit.Test
import static org.junit.Assert.*
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.ResourceLocator
/**
* @author Sven Efftinge - Initial contribution and API
@ -35,7 +36,7 @@ abstract class AbstractResourceSetTest {
]
rs.resourceFactoryRegistry.extensionToFactoryMap.put('xmi', nullFactory)
assertEquals(0, rs.URIResourceMap.size)
val uri = URI::createURI('file:/does/not/exist.xmi')
val uri = URI.createURI('file:/does/not/exist.xmi')
val demandLoaded = rs.getResource(uri, true)
assertNotNull(demandLoaded)
val second = rs.getResource(uri, true)
@ -44,6 +45,34 @@ abstract class AbstractResourceSetTest {
assertEquals(1, rs.URIResourceMap.size)
}
@Test
def void testResourceLocatorIsUsed() {
val rs = createEmptyResourceSet
val resource = new XtextResource
resource.URI = URI.createFileURI(new File('foo').absolutePath)
// resource locators register themselves on the resource set as a side effect in their constructor
new ResourceLocator(rs) {
override getResource(URI uri, boolean loadOnDemand) {
if (uri == resource.URI) {
return resource;
}
throw new IllegalArgumentException(uri.toString)
}
}
assertSame(resource, rs.getResource(resource.URI, true))
assertTrue(rs.resources.isEmpty)
assertNull(resource.resourceSet)
try {
rs.getResource(resource.URI.appendSegment("doesNotExist"), true)
fail()
} catch(IllegalArgumentException e) {
assertTrue(e.message.endsWith("doesNotExist"))
}
}
}
abstract class AbstractXtextResourceSetTest extends AbstractResourceSetTest {

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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
@ -7,10 +7,14 @@
*/
package org.eclipse.xtext.resource;
import com.google.common.base.Objects;
import java.io.File;
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.xtext.resource.NullResource;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.junit.Assert;
import org.junit.Test;
@ -43,4 +47,37 @@ public abstract class AbstractResourceSetTest {
Assert.assertSame(demandLoaded, second);
Assert.assertEquals(1, rs.getURIResourceMap().size());
}
@Test
public void testResourceLocatorIsUsed() {
final ResourceSetImpl rs = this.createEmptyResourceSet();
final XtextResource resource = new XtextResource();
resource.setURI(URI.createFileURI(new File("foo").getAbsolutePath()));
new ResourceSetImpl.ResourceLocator(rs) {
@Override
public Resource getResource(final URI uri, final boolean loadOnDemand) {
URI _uRI = resource.getURI();
boolean _equals = Objects.equal(uri, _uRI);
if (_equals) {
return resource;
}
String _string = uri.toString();
throw new IllegalArgumentException(_string);
}
};
Assert.assertSame(resource, rs.getResource(resource.getURI(), true));
Assert.assertTrue(rs.getResources().isEmpty());
Assert.assertNull(resource.getResourceSet());
try {
rs.getResource(resource.getURI().appendSegment("doesNotExist"), true);
Assert.fail();
} catch (final Throwable _t) {
if (_t instanceof IllegalArgumentException) {
final IllegalArgumentException e = (IllegalArgumentException)_t;
Assert.assertTrue(e.getMessage().endsWith("doesNotExist"));
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
}

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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

View file

@ -1,5 +1,5 @@
/**
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2012, 2018 itemis AG (http://www.itemis.eu) 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

View file

@ -232,7 +232,7 @@ public class XtextResourceSet extends ResourceSetImpl {
@Override
public Resource getResource(URI uri, boolean loadOnDemand) {
Map<URI, Resource> map = getURIResourceMap();
if (map == null)
if (map == null || resourceLocator != null)
return super.getResource(uri, loadOnDemand);
Resource resource = map.get(uri);
if (resource == null) {