Search in sources :

Example 11 with LengthOf

use of org.cactoos.scalar.LengthOf in project cactoos by yegor256.

the class TeeInputFromReaderTest method copiesFromReaderWithCharsetByNameAndSizeToPath.

@Test
public void copiesFromReaderWithCharsetByNameAndSizeToPath() throws Exception {
    final String input = "Hello, товарищ path #6 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(new TeeInput(new ReaderOf(input), output.toPath(), StandardCharsets.UTF_8.name(), input.length())).value();
    new Assertion<>("Must copy from reader with charset by name and size to path.", new InputOf(output), new HasContent(input)).affirm();
}
Also used : HasContent(org.llorllale.cactoos.matchers.HasContent) LengthOf(org.cactoos.scalar.LengthOf) File(java.io.File) Test(org.junit.Test)

Example 12 with LengthOf

use of org.cactoos.scalar.LengthOf in project cactoos by yegor256.

the class LoggingOutputTest method logWriteZero.

@Test
public void logWriteZero() throws Exception {
    final Logger logger = new FakeLogger();
    new LengthOf(new TeeInput(new InputOf(""), new LoggingOutput(() -> new ByteArrayOutputStream(), "memory", logger))).value();
    new Assertion<>("Must log write zero byte written to memory", logger.toString(), new StringContains("Written 0 byte(s) to memory in ")).affirm();
}
Also used : LengthOf(org.cactoos.scalar.LengthOf) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 13 with LengthOf

use of org.cactoos.scalar.LengthOf in project cactoos by yegor256.

the class LoggingOutputTest method logAllWriteToLargeTextFile.

@Test
@SuppressWarnings("unchecked")
public void logAllWriteToLargeTextFile() throws Exception {
    final Logger logger = new FakeLogger(Level.WARNING);
    final Path temp = this.folder.newFolder("ccts-2").toPath();
    final Path path = temp.resolve("a/b/c/file.txt");
    try (OutputStream output = new LoggingOutput(new OutputTo(path), "text file", logger).stream()) {
        new LengthOf(new TeeInput(new ResourceOf("org/cactoos/large-text.txt"), new OutputTo(output))).value();
    }
    new Assertion<>("Must log all write and close operations to text file", logger.toString(), new AllOf<>(new StringContains("Written 16384 byte(s) to text file"), new StringContains("Written 32768 byte(s) to text file"), new StringContains("Written 49152 byte(s) to text file"), new StringContains("Written 65536 byte(s) to text file"), new StringContains("Written 74536 byte(s) to text file"), new StringContains("Closed output stream from text file"))).affirm();
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LengthOf(org.cactoos.scalar.LengthOf) Logger(java.util.logging.Logger) AllOf(org.hamcrest.core.AllOf) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 14 with LengthOf

use of org.cactoos.scalar.LengthOf in project cactoos by yegor256.

the class LoggingOutputTest method logWriteToLargeTextFile.

@Test
@SuppressWarnings("unchecked")
public void logWriteToLargeTextFile() throws Exception {
    final Logger logger = new FakeLogger();
    final Path temp = this.folder.newFolder("ccts-1").toPath();
    final Path path = temp.resolve("x/y/z/file.txt");
    try (OutputStream output = new LoggingOutput(new OutputTo(path), "text file", logger).stream()) {
        new LengthOf(new TeeInput(new ResourceOf("org/cactoos/large-text.txt"), new OutputTo(output))).value();
    }
    new Assertion<>("Must log write and close operations to text file", logger.toString(), new AllOf<>(new IsNot<String>(new StringContains("Written 16384 byte(s) to text file")), new StringContains("Written 74536 byte(s) to text file"), new StringContains("Closed output stream from text file"))).affirm();
}
Also used : Path(java.nio.file.Path) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) LengthOf(org.cactoos.scalar.LengthOf) AllOf(org.hamcrest.core.AllOf) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 15 with LengthOf

use of org.cactoos.scalar.LengthOf in project cactoos by yegor256.

the class InputStreamOfTest method readsFileContent.

@Test
public void readsFileContent() throws Exception {
    final File file = this.folder.newFile("readFileContent.txt-2");
    final String content = "Content in a file";
    new LengthOf(new TeeInput(content, file)).value();
    new Assertion<>("Must read from file", new TextOf(new InputStreamOf(file)), new IsText(content)).affirm();
}
Also used : LengthOf(org.cactoos.scalar.LengthOf) IsText(org.llorllale.cactoos.matchers.IsText) TextOf(org.cactoos.text.TextOf) File(java.io.File) Test(org.junit.Test)

Aggregations

LengthOf (org.cactoos.scalar.LengthOf)103 Test (org.junit.Test)92 HasContent (org.llorllale.cactoos.matchers.HasContent)80 File (java.io.File)75 TextOf (org.cactoos.text.TextOf)20 Test (org.junit.jupiter.api.Test)11 Logger (java.util.logging.Logger)9 StringWriter (java.io.StringWriter)7 HasString (org.llorllale.cactoos.matchers.HasString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Path (java.nio.file.Path)5 OutputStream (java.io.OutputStream)4 AllOf (org.hamcrest.core.AllOf)4 IsEqual (org.hamcrest.core.IsEqual)4 IsText (org.llorllale.cactoos.matchers.IsText)4 BytesOf (org.cactoos.bytes.BytesOf)3 StringContains (org.hamcrest.core.StringContains)3 OutputStreamWriter (java.io.OutputStreamWriter)2 GZIPOutputStream (java.util.zip.GZIPOutputStream)2 BufferedWriter (java.io.BufferedWriter)1