org.cactoos.text.TextOf Java Examples

The following examples show how to use org.cactoos.text.TextOf. 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: MetricsTest.java    From jpeek with 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 File: XslReportTest.java    From jpeek with 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 #3
Source File: Get.java    From cactoos-http with 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 #4
Source File: TeeOutputTest.java    From cactoos with 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 #5
Source File: HtHeadTest.java    From cactoos-http with 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 File: ReaderOfTest.java    From cactoos with 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 #7
Source File: HtHeadTest.java    From cactoos-http with 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 #8
Source File: TeeOutputTest.java    From cactoos with 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 #9
Source File: StickyTest.java    From cactoos with 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 #10
Source File: TeeOutputTest.java    From cactoos with 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 #11
Source File: InputOfTest.java    From cactoos with 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 #12
Source File: HtUpgradeWireTest.java    From cactoos-http with 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 #13
Source File: XslReportTest.java    From jpeek with 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 #14
Source File: WriterAsOutputTest.java    From cactoos with 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 #15
Source File: Lcom4.java    From jpeek with 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 #16
Source File: InputOfTest.java    From cactoos with 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 #17
Source File: HtBodyTest.java    From cactoos-http with 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 #18
Source File: HtSecureWireTest.java    From cactoos-http with 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 #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: TeeInputFromTextTest.java    From cactoos with 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 #21
Source File: IteratorOfBytesTest.java    From cactoos with 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 #22
Source File: SkipInputTest.java    From cactoos-http with 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 #23
Source File: BytesOfTest.java    From cactoos with 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 #24
Source File: SkipInputTest.java    From cactoos-http with 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 #25
Source File: TempFolder.java    From cactoos with 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 #26
Source File: TeeOutputTest.java    From cactoos with 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 #27
Source File: InputAsBytesTest.java    From cactoos with 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 #28
Source File: AppTest.java    From jpeek with 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 #29
Source File: ExpectedStatusTest.java    From verano-http with 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")
        )
    );
}
 
Example #30
Source File: MappedTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test
public void transformsList() throws Exception {
    MatcherAssert.assertThat(
        "Can't transform an iterable",
        new Mapped<String, Text>(
            input -> new Upper(new TextOf(input)),
            new IterableOf<>(
                "hello", "world", "друг"
            )
        ).iterator().next().asString(),
        Matchers.equalTo("HELLO")
    );
}