org.cactoos.scalar.LengthOf Java Examples

The following examples show how to use org.cactoos.scalar.LengthOf. 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: LoggingInputTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void logReadFromDeadInput() throws IOException {
    final Logger logger = new FakeLogger();
    new LengthOf(
        new LoggingInput(
            new DeadInput(),
            "dead input",
            logger
        )
    ).intValue();
    new Assertion<>(
        "Can't log zero byte read from dead input",
        logger.toString(),
        Matchers.containsString("Read 0 byte(s) from dead input in")
    ).affirm();
}
 
Example #2
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withIterable() throws Exception {
    new Assertion<>(
        "Must work with iterable",
        new LengthOf(
            new Filtered<>(
                input -> !input.startsWith("st"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        new IterableOf<Func<String, String>>(
                            input -> input.concat("2"),
                            input -> input.replaceAll("a", "b")
                        ),
                        String::trim
                    ),
                    new IterableOf<>("private", "static", "String")
                )
            )
        ).intValue(),
        new IsEqual<>(2)
    ).affirm();
}
 
Example #3
Source File: ChainedTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void withoutIterable() throws Exception {
    new Assertion<>(
        "Must work without iterable",
        new LengthOf(
            new Filtered<>(
                input -> input.endsWith("12"),
                new Mapped<>(
                    new Chained<>(
                        input -> input.concat("1"),
                        input -> input.concat("2")
                    ),
                    new IterableOf<>("public", "final", "class")
                )
            )
        ).intValue(),
        new IsEqual<>(3)
    ).affirm();
}
 
Example #4
Source File: TeeInputFromTextTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromTextWithCharsetToOutput() throws IOException {
    final String input =
        "Hello, товарищ output #2 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new TextOf(input),
            new OutputTo(output),
            StandardCharsets.UTF_8
        )
    ).intValue();
    new Assertion<>(
        "text must be copied to the output with UTF_8 charset",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #5
Source File: TeeInputFromFileTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromFileToPath() throws IOException {
    final String message =
        "Hello, товарищ path #1 äÄ üÜ öÖ and ß";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            input,
            output.toPath()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from input file to output path",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #6
Source File: TeeInputFromUrlTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromUrlToOutput() throws IOException {
    final String message =
        "Hello, товарищ output #1 äÄ üÜ öÖ and ß";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(input.toURI().toURL(), new OutputTo(output))
    ).intValue();
    new Assertion<>(
        "Must copy from URL to output.",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #7
Source File: TeeInputFromUrlTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromUrlToFile() throws IOException {
    final String message =
        "Hello, товарищ file #1 äÄ üÜ öÖ and ß";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(input.toURI().toURL(), output)
    ).intValue();
    new Assertion<>(
        "Must copy from URL to file.",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #8
Source File: TeeInputFromUrlTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromUrlToPath() throws IOException {
    final String message =
        "Hello, товарищ path #1 äÄ üÜ öÖ and ß";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(input.toURI().toURL(), output.toPath())
    ).intValue();
    new Assertion<>(
        "Must copy from URL to path.",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #9
Source File: TeeInputFromTextTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromTextWithCharsetByNameToPath() throws IOException {
    final String input =
        "Hello, товарищ path #3 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new TextOf(input),
            output.toPath(),
            StandardCharsets.UTF_8.name()
        )
    ).intValue();
    new Assertion<>(
        "text must be copied to the path with UTF_8 charset's name",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #10
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetAndSizeToPath() throws IOException {
    final String input =
        "Hello, товарищ path #4 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output.toPath(),
            StandardCharsets.UTF_8,
            input.length()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset and size to path.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #11
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetToPath() throws IOException {
    final String input =
        "Hello, товарищ path #3 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output.toPath(),
            StandardCharsets.UTF_8
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset to path.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #12
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithSizeToPath() throws IOException {
    final String input =
        "Hello, товарищ path #2 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output.toPath(),
            input.length()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with size to path",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #13
Source File: InputStreamOfTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void readsFromTextWithCharset() throws Exception {
    final File file = this.folder.newFile("readTextWithCharset.txt-5");
    final String content = "Content for reading text with charset";
    new LengthOf(
        new TeeInput(content, file)
    ).intValue();
    new Assertion<>(
        "Can't read from text with charset",
        new TextOf(
            new InputStreamOf(
                new TextOf(file),
                StandardCharsets.UTF_8.name()
            )
        ).asString(),
        Matchers.equalTo(content)
    );
}
 
Example #14
Source File: OutputToTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void writesIntoWriterWithDecoderAndSize() {
    final String txt = "Hello, writer with decoder and size!";
    final StringWriter output = new StringWriter();
    new LengthOf(
        new TeeInput(
            txt,
            new OutputTo(output, StandardCharsets.UTF_8.newDecoder(), 1)
        )
    ).intValue();
    new Assertion<>(
        "Must write into writer with decoder and size",
        new InputOf(output.getBuffer()),
        new InputHasContent(txt)
    ).affirm();
}
 
Example #15
Source File: TeeInputFromByteArrayTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromByteArrayToOutput() throws Exception {
    final String message =
        "Hello, товарищ output äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            message.getBytes(StandardCharsets.UTF_8),
            new OutputTo(output)
        )
    ).intValue();
    new Assertion<>(
        "Must copy bytes to output",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #16
Source File: TeeInputStreamTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void leftInputClosed() {
    try (StringWriterMock write = new StringWriterMock()) {
        new LengthOf(
            new TeeInput(
                "foo",
                new OutputTo(write)
            )
        ).intValue();
        new Assertion<>(
            "Must use output after usage from TeeInput",
            write.isClosed(),
            new IsEqual<>(true)
        ).affirm();
    }
}
 
Example #17
Source File: GzipOutputTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test(expected = IOException.class)
public void writeToClosedGzipOutput() throws Exception {
    final OutputStream stream =
        Files.newOutputStream(
            Paths.get(
                this.folder.newFile("cactoos.txt").getPath()
            )
        );
    stream.close();
    new LengthOf(
        new TeeInput(
            "Hello!",
            new GzipOutput(new OutputTo(stream))
        )
    ).value();
}
 
Example #18
Source File: LoggingInputTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void logReadFromText() throws IOException {
    final Logger logger = new FakeLogger();
    new LengthOf(
        new LoggingInput(
            new InputOf("Hello, товарищ!"),
            "memory",
            logger
        )
    ).intValue();
    new Assertion<>(
        "Can't log 22 bytes read from memory",
        logger.toString(),
        Matchers.containsString("Read 22 byte(s) from memory in")
    ).affirm();
}
 
Example #19
Source File: TeeInputFromTextTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromTextWithCharsetByNameToFile() throws IOException {
    final String input =
        "Hello, товарищ file #3 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new TextOf(input),
            output,
            StandardCharsets.UTF_8.name()
        )
    ).intValue();
    new Assertion<>(
        "text must be copied to the file with UTF_8 charset's name",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #20
Source File: LoggingInputTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void logAllFromLargeTextFile() throws IOException {
    final Logger logger = new FakeLogger(Level.WARNING);
    new LengthOf(
        new LoggingInput(
            new ResourceOf("org/cactoos/large-text.txt"),
            "text file",
            logger
        )
    ).intValue();
    new Assertion<>(
        "Can't log all read and close operations from text file",
        logger.toString(),
        Matchers.allOf(
            Matchers.containsString("Read 16384 byte(s) from text file"),
            Matchers.containsString("Read 32768 byte(s) from text file"),
            Matchers.containsString("Read 49152 byte(s) from text file"),
            Matchers.containsString("Read 65536 byte(s) from text file"),
            Matchers.containsString("Read 74536 byte(s) from text file"),
            Matchers.containsString("Closed input stream from text file")
        )
    ).affirm();
}
 
Example #21
Source File: LoggingInputTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void logIntoCreatedLogger() {
    final FakeHandler handler = new FakeHandler();
    final String src = "my source";
    final Logger logger = Logger.getLogger(src);
    logger.addHandler(handler);
    try {
        new LengthOf(
            new LoggingInput(
                new InputOf("Hi there"),
                src
            )
        ).intValue();
        new Assertion<>(
            "",
            handler.toString(),
            Matchers.containsString("Read 8 byte(s)")
        ).affirm();
    } finally {
        logger.removeHandler(handler);
    }
}
 
Example #22
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetByNameAndSizeToFile()
    throws IOException {
    final String input =
        "Hello, товарищ file #6 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output,
            StandardCharsets.UTF_8.name(),
            input.length()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset by name and size to file.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #23
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetByNameToFile() throws IOException {
    final String input =
        "Hello, товарищ file #5 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output,
            StandardCharsets.UTF_8.name()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset by name to file.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #24
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetAndSizeToFile() throws IOException {
    final String input =
        "Hello, товарищ file #4 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output,
            StandardCharsets.UTF_8,
            input.length()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset and size to file.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #25
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithCharsetToFile() throws IOException {
    final String input =
        "Hello, товарищ file #3 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output,
            StandardCharsets.UTF_8
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with charset to file.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #26
Source File: TeeInputFromReaderTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromReaderWithSizeToFile() throws IOException {
    final String input =
        "Hello, товарищ file #2 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new ReaderOf(input),
            output,
            input.length()
        )
    ).intValue();
    new Assertion<>(
        "Must copy from reader with size to file.",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example #27
Source File: OutputToTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void writesIntoWriterWithCharsetByNameAndSize() {
    final String txt = "Hello, writer with charset by name and size!";
    final StringWriter output = new StringWriter();
    new LengthOf(
        new TeeInput(
            txt,
            new OutputTo(output, StandardCharsets.UTF_8.name(), 1)
        )
    ).intValue();
    new Assertion<>(
        "Must write into writer with charset by name and size",
        new InputOf(output.getBuffer()),
        new InputHasContent(txt)
    ).affirm();
}
 
Example #28
Source File: TeeInputFromPathTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromPathToOutput() throws IOException {
    final String message =
        "Hello, товарищ output #1 äÄ üÜ öÖ and ß";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(input.toPath(), new OutputTo(output))
    ).intValue();
    new Assertion<>(
        "Must copy from input path to output",
        new InputOf(output),
        new InputHasContent(message)
    ).affirm();
}
 
Example #29
Source File: Reports.java    From jpeek with MIT License 6 votes vote down vote up
/**
 * Extract classes from passed Jar file.
 * @param path Jar file path
 * @throws IOException If fails
 */
private static void extractClasses(final Path path) throws IOException {
    try (JarFile jar = new JarFile(path.toFile())) {
        final Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            final JarEntry entry = entries.nextElement();
            final Path item = path.getParent().resolve(entry.getName());
            if (entry.isDirectory()) {
                item.toFile().mkdir();
                continue;
            }
            final Path parent = item.getParent();
            if (!parent.toFile().exists()) {
                parent.toFile().mkdirs();
            }
            new LengthOf(
                new TeeInput(
                    new InputOf(jar.getInputStream(entry)),
                    item
                )
            ).intValue();
        }
    }
}
 
Example #30
Source File: TeeInputFromTextTest.java    From cactoos with MIT License 6 votes vote down vote up
@Test
public void copiesFromTextWithCharsetByNameToOutput() throws IOException {
    final String input =
        "Hello, товарищ output #3 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new TextOf(input),
            new OutputTo(output),
            StandardCharsets.UTF_8.name()
        )
    ).intValue();
    new Assertion<>(
        "text must be copied to the output with UTF_8 charset's name",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}