use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderTest method ioExceptionWhenFileDoesntExist.
@Test
public void ioExceptionWhenFileDoesntExist() throws Exception {
File file = File.createTempFile("Hello", ".java").getCanonicalFile();
file.delete();
assertFalse("Doesn't exist", file.exists());
Source.Builder<IOException, RuntimeException, RuntimeException> builder = Source.newBuilder(file);
Source s1 = null;
try {
s1 = builder.build();
} catch (IOException e) {
// OK, file doesn't exist
return;
}
fail("No source should be created: " + s1);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderTest method mimeTypeIsDetectedRandomBytes.
@Test
public void mimeTypeIsDetectedRandomBytes() throws IOException {
File file = File.createTempFile("Hello", ".bin").getCanonicalFile();
file.deleteOnExit();
try (FileOutputStream w = new FileOutputStream(file)) {
w.write(0x04);
w.write(0x05);
}
Source source = Source.newBuilder(file).build();
assertEither(source.getMimeType(), "content/unknown", "application/octet-stream", "text/plain", "application/macbinary");
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderTest method assignMimeTypeAndIdentityForReader.
@Test
public void assignMimeTypeAndIdentityForReader() throws IOException {
String text = "// Hello";
Source.Builder<IOException, MissingMIMETypeException, RuntimeException> builder = Source.newBuilder(new StringReader(text)).name("test.txt");
Source s1 = builder.name("Hello").mimeType("text/plain").build();
assertEquals("Base type assigned", "text/plain", s1.getMimeType());
Source s2 = builder.mimeType("text/x-c").build();
assertEquals("They have the same content", s1.getCharacters(), s2.getCharacters());
assertEquals("// Hello", s1.getCharacters());
assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
assertNotEquals("So they are different", s1, s2);
assertNotNull("Every source must have URI", s1.getURI());
assertEquals("Source with different MIME type has the same URI", s1.getURI(), s2.getURI());
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderTest method succeedsWithBothNameAndMIME.
@Test
public void succeedsWithBothNameAndMIME() {
Source src = Source.newBuilder("Hi").mimeType("content/unknown").name("unknown.txt").build();
assertNotNull(src);
}
use of com.oracle.truffle.api.source.Source in project graal by oracle.
the class SourceBuilderTest method assignMimeTypeAndIdentityForURL.
@Test
public void assignMimeTypeAndIdentityForURL() throws IOException {
File file = File.createTempFile("Hello", ".java");
file.deleteOnExit();
String text;
try (FileWriter w = new FileWriter(file)) {
text = "// Hello";
w.write(text);
}
Source.Builder<IOException, RuntimeException, RuntimeException> builder = Source.newBuilder(file.toURI().toURL()).name("Hello.java");
Source s1 = builder.build();
assertEquals("Recognized as Java", "text/x-java", s1.getMimeType());
Source s2 = builder.mimeType("text/x-c").build();
assertEquals("They have the same content", s1.getCharacters(), s2.getCharacters());
assertEquals("// Hello", s1.getCharacters());
assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
assertNotEquals("So they are different", s1, s2);
assertEquals("File URI", file.toURI(), s1.getURI());
assertEquals("Source with different MIME type has the same URI", s1.getURI(), s2.getURI());
}
Aggregations