diff --git a/org.eclipse.xtext.tests/src/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.xtend b/org.eclipse.xtext.tests/src/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.xtend new file mode 100644 index 000000000..dcb341e66 --- /dev/null +++ b/org.eclipse.xtext.tests/src/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.xtend @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) 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 + *******************************************************************************/ +package org.eclipse.xtext.filesystem + +import com.google.common.base.StandardSystemProperty +import com.google.inject.Inject +import java.nio.file.Files +import java.nio.file.Paths +import java.util.UUID +import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl +import org.eclipse.xtext.generator.IFileSystemAccess +import org.eclipse.xtext.generator.IOutputConfigurationProvider +import org.eclipse.xtext.generator.URIBasedFileSystemAccess +import org.eclipse.xtext.testing.InjectWith +import org.eclipse.xtext.testing.XtextRunner +import org.eclipse.xtext.tests.XtextInjectorProvider +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith + +import static org.junit.Assert.* + +/** + * @author akos.kitta - Initial contribution and API + * + * @see https://github.com/eclipse/xtext-core/issues/180 + */ +@RunWith(XtextRunner) +@InjectWith(XtextInjectorProvider) +class URIBasedFileSystemAccessTest { + + static val MISSING_RESOURCE_NAME = 'someMissingResource'; + static val EXISTING_RESOURCE_NAME = 'someExistingResource'; + + @Inject + URIBasedFileSystemAccess fsa; + + @Inject + IOutputConfigurationProvider configProvider; + + @Inject + ExtensibleURIConverterImpl uriConverter; + + @Before + def void before() { + val tmpPath = Paths.get(StandardSystemProperty.JAVA_IO_TMPDIR.value); + val output = Files.createTempDirectory(tmpPath, '''tempFolder_«UUID.randomUUID»'''); + val resource = Files.createFile(output.resolve(EXISTING_RESOURCE_NAME)); + + resource.toFile.deleteOnExit; + output.toFile.deleteOnExit; + + val config = configProvider.outputConfigurations.head; + config.outputDirectory = output.toString; + fsa.outputConfigurations = #{IFileSystemAccess.DEFAULT_OUTPUT -> config}; + fsa.converter = uriConverter; + } + + @Test + def void testFalseOnAbsent() { + assertFalse(fsa.isFile(MISSING_RESOURCE_NAME)); + } + + @Test + def void testTrueOnPresent() { + assertTrue(fsa.isFile(EXISTING_RESOURCE_NAME)); + } + +} diff --git a/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.java b/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.java new file mode 100644 index 000000000..c56f2d893 --- /dev/null +++ b/org.eclipse.xtext.tests/xtend-gen/org/eclipse/xtext/filesystem/URIBasedFileSystemAccessTest.java @@ -0,0 +1,98 @@ +/** + * Copyright (c) 2016 TypeFox GmbH (http://www.typefox.io) 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 + */ +package org.eclipse.xtext.filesystem; + +import com.google.common.base.StandardSystemProperty; +import com.google.inject.Inject; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Collections; +import java.util.Set; +import java.util.UUID; +import org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl; +import org.eclipse.xtend2.lib.StringConcatenation; +import org.eclipse.xtext.generator.IFileSystemAccess; +import org.eclipse.xtext.generator.IOutputConfigurationProvider; +import org.eclipse.xtext.generator.OutputConfiguration; +import org.eclipse.xtext.generator.URIBasedFileSystemAccess; +import org.eclipse.xtext.testing.InjectWith; +import org.eclipse.xtext.testing.XtextRunner; +import org.eclipse.xtext.tests.XtextInjectorProvider; +import org.eclipse.xtext.xbase.lib.CollectionLiterals; +import org.eclipse.xtext.xbase.lib.Exceptions; +import org.eclipse.xtext.xbase.lib.IterableExtensions; +import org.eclipse.xtext.xbase.lib.Pair; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +/** + * @author akos.kitta - Initial contribution and API + * + * @see https://github.com/eclipse/xtext-core/issues/180 + */ +@RunWith(XtextRunner.class) +@InjectWith(XtextInjectorProvider.class) +@SuppressWarnings("all") +public class URIBasedFileSystemAccessTest { + private final static String MISSING_RESOURCE_NAME = "someMissingResource"; + + private final static String EXISTING_RESOURCE_NAME = "someExistingResource"; + + @Inject + private URIBasedFileSystemAccess fsa; + + @Inject + private IOutputConfigurationProvider configProvider; + + @Inject + private ExtensibleURIConverterImpl uriConverter; + + @Before + public void before() { + try { + String _value = StandardSystemProperty.JAVA_IO_TMPDIR.value(); + final Path tmpPath = Paths.get(_value); + StringConcatenation _builder = new StringConcatenation(); + _builder.append("tempFolder_"); + UUID _randomUUID = UUID.randomUUID(); + _builder.append(_randomUUID, ""); + final Path output = Files.createTempDirectory(tmpPath, _builder.toString()); + Path _resolve = output.resolve(URIBasedFileSystemAccessTest.EXISTING_RESOURCE_NAME); + final Path resource = Files.createFile(_resolve); + File _file = resource.toFile(); + _file.deleteOnExit(); + File _file_1 = output.toFile(); + _file_1.deleteOnExit(); + Set _outputConfigurations = this.configProvider.getOutputConfigurations(); + final OutputConfiguration config = IterableExtensions.head(_outputConfigurations); + String _string = output.toString(); + config.setOutputDirectory(_string); + Pair _mappedTo = Pair.of(IFileSystemAccess.DEFAULT_OUTPUT, config); + this.fsa.setOutputConfigurations(Collections.unmodifiableMap(CollectionLiterals.newHashMap(_mappedTo))); + this.fsa.setConverter(this.uriConverter); + } catch (Throwable _e) { + throw Exceptions.sneakyThrow(_e); + } + } + + @Test + public void testFalseOnAbsent() { + boolean _isFile = this.fsa.isFile(URIBasedFileSystemAccessTest.MISSING_RESOURCE_NAME); + Assert.assertFalse(_isFile); + } + + @Test + public void testTrueOnPresent() { + boolean _isFile = this.fsa.isFile(URIBasedFileSystemAccessTest.EXISTING_RESOURCE_NAME); + Assert.assertTrue(_isFile); + } +} diff --git a/org.eclipse.xtext/src/org/eclipse/xtext/generator/URIBasedFileSystemAccess.xtend b/org.eclipse.xtext/src/org/eclipse/xtext/generator/URIBasedFileSystemAccess.xtend index 90caa0213..1ad075fd4 100644 --- a/org.eclipse.xtext/src/org/eclipse/xtext/generator/URIBasedFileSystemAccess.xtend +++ b/org.eclipse.xtext/src/org/eclipse/xtext/generator/URIBasedFileSystemAccess.xtend @@ -23,6 +23,7 @@ import org.eclipse.xtext.parser.IEncodingProvider import org.eclipse.xtext.util.RuntimeIOException import java.io.ByteArrayOutputStream import java.io.File +import java.io.FileNotFoundException /** * A file system access implementation that is based on EMF URIs and URIConverter @@ -110,9 +111,13 @@ class URIBasedFileSystemAccess extends AbstractFileSystemAccess2 { } override readBinaryFile(String fileName, String outputCfgName) throws RuntimeIOException { - val uri = getURI(fileName, outputCfgName) - val input = converter.createInputStream(uri) - return beforeRead.beforeRead(uri, input) + try { + val uri = getURI(fileName, outputCfgName) + val input = converter.createInputStream(uri) + return beforeRead.beforeRead(uri, input) + } catch (FileNotFoundException e) { + throw new RuntimeIOException(e); + } } override readTextFile(String fileName, String outputCfgName) throws RuntimeIOException { diff --git a/org.eclipse.xtext/xtend-gen/org/eclipse/xtext/generator/URIBasedFileSystemAccess.java b/org.eclipse.xtext/xtend-gen/org/eclipse/xtext/generator/URIBasedFileSystemAccess.java index e7a362781..4b20a8a73 100644 --- a/org.eclipse.xtext/xtend-gen/org/eclipse/xtext/generator/URIBasedFileSystemAccess.java +++ b/org.eclipse.xtext/xtend-gen/org/eclipse/xtext/generator/URIBasedFileSystemAccess.java @@ -12,6 +12,7 @@ import com.google.common.io.CharStreams; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileNotFoundException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; @@ -164,9 +165,18 @@ public class URIBasedFileSystemAccess extends AbstractFileSystemAccess2 { @Override public InputStream readBinaryFile(final String fileName, final String outputCfgName) throws RuntimeIOException { try { - final URI uri = this.getURI(fileName, outputCfgName); - final InputStream input = this.converter.createInputStream(uri); - return this.beforeRead.beforeRead(uri, input); + try { + final URI uri = this.getURI(fileName, outputCfgName); + final InputStream input = this.converter.createInputStream(uri); + return this.beforeRead.beforeRead(uri, input); + } catch (final Throwable _t) { + if (_t instanceof FileNotFoundException) { + final FileNotFoundException e = (FileNotFoundException)_t; + throw new RuntimeIOException(e); + } else { + throw Exceptions.sneakyThrow(_t); + } + } } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); }