Example usage for javax.tools JavaFileManager getJavaFileForOutput

List of usage examples for javax.tools JavaFileManager getJavaFileForOutput

Introduction

In this page you can find the example usage for javax.tools JavaFileManager getJavaFileForOutput.

Prototype

JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling)
        throws IOException;

Source Link

Document

Returns a JavaFileObject file object for output representing the specified class of the specified kind in the given package-oriented location.

Usage

From source file:org.cloudfoundry.tools.compiler.CloudFoundryJavaCompilerTest.java

@Test
public void shouldCompileVirtualFile() throws Exception {
    JavaFileObject sourceFile = mock(JavaFileObject.class);
    given(sourceFile.getKind()).willReturn(Kind.SOURCE);
    given(sourceFile.getName()).willReturn("Example.java");
    given(sourceFile.getCharContent(anyBoolean())).willReturn(getExampleJavaContent());
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    JavaFileObject classFile = mock(JavaFileObject.class);
    given(classFile.openOutputStream()).willReturn(outputStream);

    JavaFileManager fileManager = mock(JavaFileManager.class);
    given(fileManager.getJavaFileForOutput(Matchers.any(Location.class), anyString(), eq(Kind.CLASS),
            eq(sourceFile))).willReturn(classFile);
    Iterable<? extends JavaFileObject> compilationUnits = Collections.singleton(sourceFile);
    CompilationTask task = this.javaCompiler.getTask(null, fileManager, null, standardCompilerOptions(), null,
            compilationUnits);/*  ww w .ja  v  a2 s  . c om*/
    assertThat(task.call(), is(Boolean.TRUE));
    assertThat(outputStream.toByteArray().length, is(greaterThan(0)));
}