Search in sources :

Example 1 with NullResource

use of org.eclipse.xtext.resource.NullResource in project xtext-core by eclipse.

the class SynchronizedXtextResourceSetTest method testSynchronization.

@Test
public void testSynchronization() {
    try {
        XtextResourceSet _createEmptyResourceSet = this.createEmptyResourceSet();
        final SynchronizedXtextResourceSet resourceSet = ((SynchronizedXtextResourceSet) _createEmptyResourceSet);
        final Resource.Factory _function = (URI uri) -> {
            NullResource _xblockexpression = null;
            {
                final NullResource result = new NullResource();
                result.setURI(uri);
                _xblockexpression = result;
            }
            return _xblockexpression;
        };
        final Resource.Factory nullFactory = _function;
        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", nullFactory);
        final ArrayList<Thread> threads = CollectionLiterals.<Thread>newArrayList();
        final Consumer<Integer> _function_1 = (Integer i) -> {
            final Runnable _function_2 = () -> {
                final ArrayList<Resource> resources = CollectionLiterals.<Resource>newArrayList();
                for (int j = 0; (j < 5000); j++) {
                    {
                        String _plus = (i + " ");
                        String _plus_1 = (_plus + Integer.valueOf(j));
                        String _plus_2 = (_plus_1 + ".xmi");
                        final Resource resource = resourceSet.createResource(URI.createURI(_plus_2));
                        Assert.assertNotNull(resource);
                        resources.add(resource);
                        URI _uRI = resource.getURI();
                        String _plus_3 = (_uRI + "b");
                        resource.setURI(URI.createURI(_plus_3));
                    }
                }
            };
            Thread _thread = new Thread(_function_2);
            threads.add(_thread);
        };
        new IntegerRange(1, 10).forEach(_function_1);
        for (final Thread thread : threads) {
            thread.start();
        }
        for (final Thread thread_1 : threads) {
            thread_1.join();
        }
        Assert.assertEquals(50000, resourceSet.getResources().size());
        Assert.assertEquals(IterableExtensions.<Resource>toSet(resourceSet.getResources()).size(), IterableExtensions.<Resource>toSet(resourceSet.getURIResourceMap().values()).size());
        final Function1<Resource, List<URI>> _function_2 = (Resource it) -> {
            URI _uRI = it.getURI();
            URI _normalize = resourceSet.getURIConverter().normalize(it.getURI());
            return Collections.<URI>unmodifiableList(CollectionLiterals.<URI>newArrayList(_uRI, _normalize));
        };
        Assert.assertEquals(IterableExtensions.<URI>toSet(Iterables.<URI>concat(ListExtensions.<Resource, List<URI>>map(resourceSet.getResources(), _function_2))), resourceSet.getURIResourceMap().keySet());
        final Function1<Resource, String> _function_3 = (Resource it) -> {
            return it.getURI().toString();
        };
        final Function1<URI, String> _function_4 = (URI it) -> {
            return it.toString();
        };
        Assert.assertEquals(IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<String>toList(ListExtensions.<Resource, String>map(resourceSet.getResources(), _function_3))), "\n"), IterableExtensions.join(IterableExtensions.<String>sort(IterableExtensions.<String>toList(IterableExtensions.<URI, String>map(resourceSet.getNormalizationMap().keySet(), _function_4))), "\n"));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : IntegerRange(org.eclipse.xtext.xbase.lib.IntegerRange) NullResource(org.eclipse.xtext.resource.NullResource) Resource(org.eclipse.emf.ecore.resource.Resource) SynchronizedXtextResourceSet(org.eclipse.xtext.resource.SynchronizedXtextResourceSet) URI(org.eclipse.emf.common.util.URI) NullResource(org.eclipse.xtext.resource.NullResource) SynchronizedXtextResourceSet(org.eclipse.xtext.resource.SynchronizedXtextResourceSet) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) AbstractXtextResourceSetTest(org.eclipse.xtext.resource.AbstractXtextResourceSetTest)

Example 2 with NullResource

use of org.eclipse.xtext.resource.NullResource in project xtext-core by eclipse.

the class AbstractResourceSetTest method testDemandLoadedResourcesAreInMap.

@Test
public void testDemandLoadedResourcesAreInMap() {
    final ResourceSetImpl rs = this.createEmptyResourceSet();
    final Resource.Factory _function = (URI uri) -> {
        NullResource _xblockexpression = null;
        {
            final NullResource result = new NullResource();
            result.setURI(uri);
            _xblockexpression = result;
        }
        return _xblockexpression;
    };
    final Resource.Factory nullFactory = _function;
    rs.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", nullFactory);
    Assert.assertEquals(0, rs.getURIResourceMap().size());
    final URI uri = URI.createURI("file:/does/not/exist.xmi");
    final Resource demandLoaded = rs.getResource(uri, true);
    Assert.assertNotNull(demandLoaded);
    final Resource second = rs.getResource(uri, true);
    Assert.assertSame(demandLoaded, second);
    Assert.assertEquals(1, rs.getURIResourceMap().size());
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) NullResource(org.eclipse.xtext.resource.NullResource) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) NullResource(org.eclipse.xtext.resource.NullResource) URI(org.eclipse.emf.common.util.URI) Test(org.junit.Test)

Aggregations

URI (org.eclipse.emf.common.util.URI)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 NullResource (org.eclipse.xtext.resource.NullResource)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 AbstractXtextResourceSetTest (org.eclipse.xtext.resource.AbstractXtextResourceSetTest)1 SynchronizedXtextResourceSet (org.eclipse.xtext.resource.SynchronizedXtextResourceSet)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)1 IntegerRange (org.eclipse.xtext.xbase.lib.IntegerRange)1