Skip to content
Permalink
Browse files
Specifying builder.content(String) prevents any possible I/O errors, …
…as the content isn't read anymore.
  • Loading branch information
Jaroslav Tulach committed Jun 10, 2016
1 parent 10500c2 commit 584606a657d72cdbf1624eb53c0bc55fbf120ad9
Showing 2 changed files with 21 additions and 6 deletions.
@@ -156,8 +156,18 @@ public void assignMimeTypeAndIdentityForVirtualFile() throws IOException {
assertEquals("Source with different MIME type has the same URI", s1.getURI(), s2.getURI());
}

/* Test currently fails on Sparc. */
@Ignore
@Test
public void noIOWhenContentSpecified() {
File file = new File("some.js");

String text = "// Hello";

Source source = Source.newFromFile(file).content(text).build();
assertEquals("The content has been changed", text, source.getCode());
assertEquals("application/javascript", source.getMimeType());
assertEquals("some.js", source.getName());
}

@Test
public void assignMimeTypeAndIdentityForURL() throws IOException {
File file = File.createTempFile("Hello", ".java");
@@ -162,7 +162,7 @@ public static Source find(String name) {
* <p>
* The system tries to deduce appropriate {@link Source#getMimeType()} by consulting registered
* {@link FileTypeDetector file type detectors}.
*
*
* @param file the location of the file to load content from
* @return new instance of builder
* @since 0.15
@@ -1124,12 +1124,14 @@ public Builder<R, E> internal() {
* {@link SourceSnippets#fromURLWithOwnContent}
*
* @param code the code to be available via {@link Source#getCode()}
* @return instance of this builder
* @return instance of this builder - which's {@link #build()} method
* no longer throws an {@link IOException}
* @since 0.15
*/
public Builder<R, E> content(String code) {
@SuppressWarnings("unchecked")
public Builder<R, RuntimeException> content(String code) {
this.content = code;
return this;
return (Builder<R, RuntimeException>) this;
}

Builder<R, E> content(byte[] arr, int offset, int length, Charset encoding) {
@@ -1173,6 +1175,9 @@ public R build() throws E {
if (type == null) {
throw new IllegalStateException("Unknown mime type for " + origin);
}
if (content != null) {
holder.code = content;
}
SourceImpl ret = new SourceImpl(holder, type, name, internal);
return (R) ret;
} catch (IOException ex) {

0 comments on commit 584606a

Please sign in to comment.