org.cactoos.io.OutputTo Java Examples

The following examples show how to use org.cactoos.io.OutputTo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CompileMojoTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Main can print a simple text.
 * @throws Exception If some problem inside
 */
public void testSimpleCompilation() throws Exception {
    final CompileMojo mojo = new CompileMojo();
    this.temp.create();
    final File src = this.temp.newFolder();
    this.setVariableValueToObject(mojo, "sourceDirectory", src);
    new LengthOf(
        new TeeInput(
            new InputOf(
                "type Pixel:\n  Pixel moveTo(Integer x, Integer y)"
            ),
            new OutputTo(new File(src, "main.eo"))
        )
    ).value();
    final File target = this.temp.newFolder();
    this.setVariableValueToObject(mojo, "targetDirectory", target);
    this.setVariableValueToObject(mojo, "project", new MavenProjectStub());
    mojo.execute();
    MatcherAssert.assertThat(
        new File(target, "Pixel.java").exists(),
        Matchers.is(true)
    );
}
 
Example #2
Source File: BodyUrlTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * Body.URL can provide the expected input.
 * @throws Exception If there is some problem inside.
 */
@Test
public void returnsCorrectInputWithUrl() throws Exception {
    try (TempFile file = new TempFile()) {
        final Bytes body = new BytesOf("URL returnsCorrectInput!");
        try (OutputStream outStream =
            new OutputTo(file.value()).stream()) {
            new LengthOf(
                new TeeInput(body, new OutputTo(outStream))
            ).intValue();
        }
        final Body.Url input = new Body.Url(file.value().toUri().toURL());
        MatcherAssert.assertThat(
            "Body content of Body.Url doesn't provide the correct bytes",
            new BytesOf(input).asBytes(),
            new IsEqual<>(body.asBytes())
        );
    }
}
 
Example #3
Source File: BodyUrlTest.java    From takes with MIT License 6 votes vote down vote up
/**
 * Body.URL can provide the expected length.
 * @throws Exception If there is some problem inside.
 */
@Test
public void returnsCorrectLengthWithUrl() throws Exception {
    try (TempFile file = new TempFile()) {
        final Bytes body = new BytesOf("URL returnsCorrectLength!");
        try (OutputStream outStream =
            new OutputTo(file.value()).stream()) {
            new LengthOf(
                new TeeInput(body, new OutputTo(outStream))
            ).intValue();
        }
        MatcherAssert.assertThat(
            "Body content of Body.Url doesn't have the correct length",
            new LengthOf(
                new Body.Url(file.value().toUri().toURL())
            ).intValue(),
            new IsEqual<>(body.asBytes().length)
        );
    }
}
 
Example #4
Source File: Program.java    From eo with MIT License 5 votes vote down vote up
/**
 * Ctor.
 *
 * @param ipt Input text
 * @param dir Directory to write to
 */
public Program(final Input ipt, final Path dir) {
    this(
        ipt,
        path -> new OutputTo(
            new File(dir.toFile(), path)
        )
    );
}
 
Example #5
Source File: ProgramTest.java    From eo with MIT License 5 votes vote down vote up
/**
 * Creates Java code that really works.
 * @throws Exception If some problem inside
 */
@Test
public void compilesExecutableJava() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final Program program = new Program(
        new InputOf(
            new JoinedText(
                "\n",
                "object car as Serializable:",
                "  Integer @vin",
                "  String name():",
                "    \"Mercedes-Benz\""
            ).asString()
        ),
        s -> new OutputTo(baos)
    );
    program.compile();
    try (final GroovyClassLoader loader = new GroovyClassLoader()) {
        final Class<?> type = loader.parseClass(
            new GroovyCodeSource(
                new TextOf(baos.toByteArray()).asString(),
                "car", GroovyShell.DEFAULT_CODE_BASE
            )
        );
        MatcherAssert.assertThat(
            type.getMethod("name").invoke(
                type.getConstructor(Integer.class).newInstance(0)
            ),
            Matchers.equalTo("Mercedes-Benz")
        );
    }
}
 
Example #6
Source File: HttpServletResponseFake.java    From takes with MIT License 5 votes vote down vote up
@Override
public ServletOutputStream getOutputStream() throws IOException {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new LengthOf(
        new TeeInput(
            new InputOf(this.response.get().body()),
            new OutputTo(baos)
        )
    ).intValue();
    return new ServletOutputStreamTo(baos);
}