Java Code Examples for org.cactoos.text.TextOf

The following examples show how to use org.cactoos.text.TextOf. These examples are extracted from open source projects. 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 Project: jpeek   Source File: MetricsTest.java    License: MIT License 7 votes vote down vote up
@ParameterizedTest
@CsvFileSource(resources = "/org/jpeek/metricstest-params.csv")
public void testsTarget(final String target, final String metric, final double value,
    @TempDir final Path output)
    throws Exception {
    new XslReport(
        new Skeleton(new FakeBase(target)).xml(), new XslCalculus(),
        new ReportData(metric)
    ).save(output);
    final String xpath;
    if (Double.isNaN(value)) {
        xpath = "//class[@id='%s' and @value='NaN']";
    } else {
        xpath = "//class[@id='%s' and number(@value)=%.4f]";
    }
    new Assertion<>(
        new FormattedText(
            "Must exists with target '%s' and value '%s'",
            target, value
        ).asString(),
        XhtmlMatchers.xhtml(
            new TextOf(
                output.resolve(String.format("%s.xml", metric))
            ).asString()
        ),
        XhtmlMatchers.hasXPaths(
            String.format(
                xpath,
                target, value
            )
        )
    ).affirm();
}
 
Example 2
Source Project: cactoos   Source File: InputOfTest.java    License: MIT License 6 votes vote down vote up
@Test
public void readsEncodedStringFromReader() throws Exception {
    final String source = "hello, друг!";
    new Assertion<>(
        "must read encoded string through a reader",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new StringReader(source),
                    StandardCharsets.UTF_8
                )
            )
        ).asString(),
        new IsEqual<>(source)
    ).affirm();
}
 
Example 3
Source Project: cactoos   Source File: TeeOutputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void copiesContent() {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new Assertion<>(
        "Can't copy Output to Output and return Input",
        new TeeInput(
            new InputOf("Hello, товарищ!"),
                new TeeOutput(
                    new OutputTo(baos),
                    new OutputTo(new ByteArrayOutputStream())
                )
        ),
        new InputHasContent(
            new TextOf(baos::toByteArray, StandardCharsets.UTF_8)
        )
    ).affirm();
}
 
Example 4
Source Project: cactoos-http   Source File: Get.java    License: MIT License 6 votes vote down vote up
/**
 * Ctor.
 *
 * @param url Url to GET.
 */
public Get(final URI url) {
    super(new InputOf(
        new Joined(
            new TextOf("\r\n"),
            new FormattedText(
                "GET %s HTTP/1.1",
                url.getPath()
            ),
            new FormattedText(
                "Host: %s",
                url.getHost()
            )
        )
    ));
}
 
Example 5
Source Project: cactoos-http   Source File: HtHeadTest.java    License: MIT License 6 votes vote down vote up
@Test
public void takesHeadOutOfHttpResponse() {
    new Assertion<>(
        "Header does not have 'text/plain'",
        new TextOf(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        "Hello, dude!"
                    )
                )
            )
        ),
        new EndsWith("text/plain")
    ).affirm();
}
 
Example 6
Source Project: cactoos-http   Source File: HtHeadTest.java    License: MIT License 6 votes vote down vote up
@Test
public void largeText() throws Exception {
    //@checkstyle MagicNumberCheck (1 lines)
    final byte[] bytes = new byte[18000];
    new Random().nextBytes(bytes);
    new Assertion<>(
        "Header does not have text/plain header",
        new TextOf(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        new TextOf(new BytesOf(bytes)).asString()
                    )
                )
            )
        ),
        new EndsWith("text/plain")
    ).affirm();
}
 
Example 7
Source Project: cactoos-http   Source File: HtUpgradeWireTest.java    License: MIT License 6 votes vote down vote up
/**
 * Test of {@link HtUpgradeWire} just to suit coverage standards.
 * @throws Exception If something goes wrong.
 */
@Test
public void testHtUpgrade() throws Exception {
    new FtRemote(new TkText("Upgraded wire")).exec(
        home -> MatcherAssert.assertThat(
            "Upgrade wire not found",
            new TextOf(
                new HtResponse(
                    new HtUpgradeWire(
                        new HtWire(
                            home.getHost(),
                            home.getPort()
                        )
                    ),
                    home.getHost()
                )
            ).asString(),
            Matchers.containsString("HTTP/1.1 200")
        )
    );
}
 
Example 8
Source Project: jpeek   Source File: AppTest.java    License: MIT License 6 votes vote down vote up
@Test
public void canIncludePrivateMethods(@TempDir final Path output) throws IOException {
    final Path input = Paths.get(".");
    final Map<String, Object> args = new HashMap<>();
    args.put("include-private-methods", 1);
    new App(input, output, args).analyze();
    new Assertion<>(
        "Must contain private method",
        XhtmlMatchers.xhtml(
            new TextOf(output.resolve("skeleton.xml")).asString()
        ),
        XhtmlMatchers.hasXPaths(
            "//method[@visibility='private']"
        )
    ).affirm();
}
 
Example 9
Source Project: cactoos-http   Source File: HtBodyTest.java    License: MIT License 6 votes vote down vote up
@Test
public void takesBodyOutOfHttpResponse() {
    MatcherAssert.assertThat(
        new TextOf(
            new HtBody(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        "Hello, dude!",
                        "How are you?"
                    )
                )
            )
        ),
        new TextHasString("Hello, dude!\r\nHow are you?")
    );
}
 
Example 10
Source Project: cactoos-http   Source File: HtSecureWireTest.java    License: MIT License 6 votes vote down vote up
@Test
public void worksFineThroughSsl() throws Exception {
    HtSecureWireTest.secure(new TkText("Hello, world!"), 0).exec(
        home -> MatcherAssert.assertThat(
            "Basic ssl request doesn't work for specified host",
            new TextOf(
                new HtResponse(
                    new HtSecureWire(
                        home.getHost(), home.getPort()
                    ),
                    new HtSecureWireTest.Request(home.getHost())
                )
            ).asString(),
            Matchers.containsString("HTTP/1.1 200")
        )
    );
}
 
Example 11
Source Project: cactoos   Source File: TeeInputFromTextTest.java    License: 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 12
Source Project: cactoos-http   Source File: SkipInputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void skipsSomeBytes() throws Exception {
    final String prefix = "Hello dude!";
    final String suffix = "How are you?";
    final String delimiter = "\r";
    MatcherAssert.assertThat(
        new TextOf(
            new SkipInput(
                new InputOf(
                    new Joined(
                        delimiter,
                        prefix,
                        suffix
                    )
                ),
                new BytesOf(delimiter)
            )
        ),
        new TextHasString(suffix)
    );
}
 
Example 13
Source Project: cactoos   Source File: BytesOfTest.java    License: MIT License 6 votes vote down vote up
@Test
public void printsStackTrace() {
    new Assertion<>(
        "Can't print exception stacktrace",
        new TextOf(
            new BytesOf(
                new IOException(
                    "It doesn't work at all"
                )
            )
        ),
        new TextHasString(
            new Joined(
                System.lineSeparator(),
                "java.io.IOException: It doesn't work at all",
                "\tat org.cactoos.io.BytesOfTest"
            )
        )
    ).affirm();
}
 
Example 14
Source Project: cactoos-http   Source File: SkipInputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void skipsEverythingWhenEndingWithDelimiter() throws Exception {
    final String delimiter = "\r\n";
    MatcherAssert.assertThat(
        new TextOf(
            new SkipInput(
                new InputOf(
                    new Joined(
                        "",
                        "Hello dude! How are you?",
                        delimiter
                    )
                ),
                new BytesOf(delimiter)
            )
        ),
        new TextHasString("")
    );
}
 
Example 15
Source Project: cactoos   Source File: InputAsBytesTest.java    License: MIT License 6 votes vote down vote up
@Test
public void readsInputIntoBytes() throws Exception {
    new Assertion<>(
        "must read bytes from Input",
        new TextOf(
            new InputAsBytes(
                new InputOf(
                    new BytesOf(
                        new TextOf("Hello, друг!")
                    )
                )
            ),
            StandardCharsets.UTF_8
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith("Hello, "),
                new EndsWith("друг!")
            )
        )
    ).affirm();
}
 
Example 16
Source Project: cactoos   Source File: TeeOutputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void copiesWithPath() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final File file = this.folder.newFile();
    new Assertion<>(
        "Must copy Output with path",
        new TextOf(
            new TeeInput(
                new InputOf("Hello, товарищ! with path"),
                new TeeOutput(
                    new OutputTo(baos),
                    file.toPath()
                )
            )
        ),
        new TextIs(
            new TextOf(file.toPath())
        )
    ).affirm();
}
 
Example 17
Source Project: cactoos   Source File: TempFolder.java    License: MIT License 6 votes vote down vote up
/**
 * Ctor.
 * Creates new folder in temporary directory
 * with a random name.
 * @since 1.0
 */
public TempFolder() {
    this(
        new Joined(
            new TextOf(""),
            new TextOf("tmp"),
            new Randomized(
                // @checkstyle MagicNumber (1 line)
                5,
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
                'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                'y', 'z'
            )
        )
    );
}
 
Example 18
Source Project: cactoos   Source File: TeeInputFromTextTest.java    License: MIT License 6 votes vote down vote up
@Test
public void copiesFromTextWithCharsetToPath() throws IOException {
    final String input =
        "Hello, товарищ path #2 äÄ üÜ öÖ and ß";
    final File output = this.folder.newFile();
    new LengthOf(
        new TeeInput(
            new TextOf(input),
            output.toPath(),
            StandardCharsets.UTF_8
        )
    ).intValue();
    new Assertion<>(
        "text must be copied to the path with UTF_8 charset",
        new InputOf(output),
        new InputHasContent(input)
    ).affirm();
}
 
Example 19
Source Project: cactoos   Source File: InputOfTest.java    License: MIT License 6 votes vote down vote up
@Test
public void readsStringBuffer() throws Exception {
    final String starts = "The future ";
    final String ends = "is now!";
    new Assertion<>(
        "must receive a string buffer",
        new TextOf(
            new BytesOf(
                new InputOf(
                    new StringBuffer(starts)
                        .append(ends)
                )
            )
        ),
        new AllOf<>(
            new IterableOf<>(
                new StartsWith(starts),
                new EndsWith(ends)
            )
        )
    ).affirm();
}
 
Example 20
Source Project: jpeek   Source File: Lcom4.java    License: MIT License 6 votes vote down vote up
@Override
public XML node(final String metric, final Map<String, Object> params,
    final XML skeleton) throws IOException {
    final XML result = new XSLDocument(
        new TextOf(
            new ResourceOf("org/jpeek/metrics/LCOM4.xsl")
        ).asString(),
        Sources.DUMMY,
        params
    ).transform(skeleton);
    final List<XML> packages = result.nodes("//package");
    for (final XML elt : packages) {
        final String pack = elt.xpath("/@id").get(0);
        final List<XML> classes = elt.nodes("//class");
        for (final XML clazz : classes) {
            this.update(skeleton, pack, clazz);
        }
    }
    return result;
}
 
Example 21
Source Project: cactoos   Source File: StickyTest.java    License: MIT License 6 votes vote down vote up
@Test
public void readsRealUrl() throws MalformedURLException {
    new Assertion<>(
        "Can't fetch text page from the URL",
        new TextOf(
            new Sticky(
                new InputOf(
                    new URL(
                        // @checkstyle LineLength (1 line)
                        "file:src/test/resources/org/cactoos/large-text.txt"
                    )
                )
            )
        ),
        new EndsWith("est laborum.\n")
    ).affirm();
}
 
Example 22
Source Project: jpeek   Source File: XslReportTest.java    License: MIT License 6 votes vote down vote up
@Test
public void setsCorrectSchemaLocation(@TempDir final Path output) throws IOException {
    new XslReport(
        new Skeleton(new FakeBase()).xml(), new XslCalculus(), new ReportData("LCOM")
    ).save(output);
    new Assertion<>(
        "Must have correct schema location",
        XhtmlMatchers.xhtml(
            new TextOf(output.resolve("LCOM.xml")).asString()
        ),
        XhtmlMatchers.hasXPaths(
            // @checkstyle LineLength (1 line)
            "/metric[@xsi:noNamespaceSchemaLocation = 'xsd/metric.xsd']"
        )
    ).affirm();
}
 
Example 23
Source Project: cactoos   Source File: ReaderOfTest.java    License: MIT License 6 votes vote down vote up
@Test
public void readsUrl() throws Exception {
    final String message =
        "URL on äÄ üÜ öÖ ß жш";
    final File input = this.folder.newFile();
    Files.write(
        input.toPath(),
        message.getBytes(StandardCharsets.UTF_8)
    );
    new Assertion<>(
        "Must read from url",
        new TextOf(
            new ReaderOf(
                input
                    .toURI()
                    .toURL()
            )
        ),
        new TextIs(message)
    ).affirm();
}
 
Example 24
Source Project: jpeek   Source File: XslReportTest.java    License: MIT License 6 votes vote down vote up
@Test
public void createsXmlReportWithXpaths(@TempDir final Path output) throws IOException {
    new XslReport(
        new Skeleton(
            new FakeBase(
                "NoMethods", "Bar", "OverloadMethods",
                "OnlyOneMethodWithParams", "WithoutAttributes"
            )
        ).xml(), new XslCalculus(), new ReportData("LCOM")
    ).save(output);
    new Assertion<>(
        "Must create LCOM report",
        XhtmlMatchers.xhtml(
            new TextOf(output.resolve("LCOM.xml")).asString()
        ),
        XhtmlMatchers.hasXPaths(
            "/metric/statistics/mean",
            "/metric/bars/bar[@x='0' and .='0' and @color='yellow']"
        )
    ).affirm();
}
 
Example 25
Source Project: cactoos   Source File: IteratorOfBytesTest.java    License: MIT License 6 votes vote down vote up
@Test
public void canBeConstructedFromText() throws Exception {
    final Iterator<Byte> itr = new IteratorOfBytes(
        new TextOf("ABC")
    );
    new Assertion<>(
        "Must have 3 elements",
        new ListOf<>(
            itr.next(),
            itr.next(),
            itr.next(),
            itr.hasNext()
        ),
        new HasValues<Object>(
            (byte) 'A', (byte) 'B', (byte) 'C', false
        )
    ).affirm();
}
 
Example 26
Source Project: cactoos   Source File: WriterAsOutputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void writesLargeContentToFile() throws IOException {
    final Path temp = this.folder.newFile("cactoos-1.txt-1")
        .toPath();
    try (OutputStreamWriter writer = new OutputStreamWriter(
        Files.newOutputStream(temp.toAbsolutePath()),
        StandardCharsets.UTF_8
    )) {
        new Assertion<>(
            "Can't copy Input to Output and return Input",
            new TeeInput(
                new ResourceOf("org/cactoos/large-text.txt"),
                new WriterAsOutput(writer)
            ),
            new InputHasContent(
                new TextOf(temp)
            )
        ).affirm();
    }
}
 
Example 27
Source Project: cactoos   Source File: TeeOutputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void copiesWithOutputStream() {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    new Assertion<>(
        "Can't copy Output with output stream",
        new TeeInput(
            new InputOf(
                "Hello, товарищ! with output stream"
            ),
            new TeeOutput(
                new OutputTo(baos),
                new ByteArrayOutputStream()
            )
        ),
        new InputHasContent(
            new TextOf(baos::toByteArray, StandardCharsets.UTF_8)
        )
    ).affirm();
}
 
Example 28
Source Project: cactoos   Source File: TeeOutputTest.java    License: MIT License 6 votes vote down vote up
@Test
public void copiesWithFile() throws Exception {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final File file = this.folder.newFile();
    new Assertion<>(
        "Must copy Output with file",
        new TextOf(
            new TeeInput(
                new InputOf("Hello, товарищ! with file"),
                new TeeOutput(
                    new OutputTo(baos),
                    file
                )
            )
        ),
        new TextIs(
            new TextOf(file.toPath())
        )
    ).affirm();
}
 
Example 29
Source Project: cactoos   Source File: ReaderOfTest.java    License: MIT License 5 votes vote down vote up
@Test
public void readsInputStreamWithCharsetByName() throws Exception {
    final String input =
        "InputStream with charset by name on äÄ üÜ öÖ ß жш";
    new Assertion<>(
        "Must read from stream with charset by name",
        new TextOf(
            new ReaderOf(
                new InputStreamOf(input),
                StandardCharsets.UTF_8.name()
            )
        ),
        new TextIs(input)
    ).affirm();
}
 
Example 30
Source Project: verano-http   Source File: ExpectedStatusTest.java    License: MIT License 5 votes vote down vote up
@Test
public void statusMatches() {
    new ExpectedStatus(
        200, new TextOf("msg")
    ).verify(
        new HashDict(
            new KvpOf("status", "200")
        )
    );
}