org.cactoos.text.Joined Java Examples

The following examples show how to use org.cactoos.text.Joined. 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: 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 #2
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void setBigDecimal(
    final int index,
    final BigDecimal value
) throws SQLException {
    this.origin.setBigDecimal(index, value);
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] changed at",
                    "parameter[#%d] with '%s' value."
                ),
                this.source,
                this.id,
                index,
                value.toString()
            )
        ).asString()
    );
}
 
Example #3
Source File: PhoneSql.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void update(final Map<String, String> properties) throws Exception {
    new StatementUpdate(
        this.session,
        new QuerySimple(
            new Joined(
                " ",
                "UPDATE phone SET number = :number, carrier = :carrier",
                "WHERE (contact_id = :contact_id) AND (number = :number)"
            ),
            new ParamText("number", properties.get("number")),
            new ParamText("carrier", properties.get("carrier")),
            new ParamUuid("contact_id", this.id),
            new ParamText("number", this.num)
        )
    ).result();
}
 
Example #4
Source File: ContactsSqlFiltered.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * @param sssn A Session.
 * @param name Contact's name to be filtered.
 */
public ContactsSqlFiltered(final Session sssn, final String name) {
    this.session = sssn;
    this.ids = new ResultSetAsValues<>(
        new StatementSelect(
            sssn,
            new QuerySimple(
                new Joined(
                    " ",
                    "SELECT id FROM contact WHERE LOWER(name) LIKE",
                    "'%' || :name || '%'"
                ),
                new ParamText("name", new Lowered(name))
            )
        )
    );
}
 
Example #5
Source File: PhonesSql.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public int count() throws Exception {
    return new ResultSetAsValue<Integer>(
        new StatementSelect(
            this.session,
            new QuerySimple(
                new Joined(
                    " ",
                    "SELECT COUNT(number) FROM phone WHERE",
                    "contact_id = :contact_id"
                ),
                new ParamUuid("contact_id", this.id)
            )
        )
    ).value();
}
 
Example #6
Source File: PhonesSql.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public Phone get(final int index) throws Exception {
    final Scalar<String> number = new ResultSetAsValue<>(
        new StatementSelect(
            this.session,
            new QuerySimple(
                new FormattedText(
                    new Joined(
                        " ",
                        "SELECT number FROM phone WHERE",
                        "contact_id = :contact_id",
                        "FETCH FIRST %d ROWS ONLY"
                    ),
                    index
                )
            )
        )
    );
    return new PhoneSql(this.session, this.id, number.value());
}
 
Example #7
Source File: PhonesSql.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void add(final Map<String, String> properties) throws Exception {
    new StatementInsert(
        this.session,
        new QuerySimple(
            new Joined(
                " ",
                "INSERT INTO phone (contact_id, number, carrier)",
                "VALUES (:contact_id, :number, :carrier)"
            ),
            new ParamUuid("contact_id", this.id),
            new ParamText("number", properties.get("number")),
            new ParamText("carrier", properties.get("carrier"))
        )
    ).result();
}
 
Example #8
Source File: XmlMethodCallTest.java    From jpeek with MIT License 6 votes vote down vote up
@Test
void hasClassMethodAndArgs() throws IOException {
    new Assertion<>(
        "Must have class name, method name and args.",
        new XmlMethodCall(
            new XMLDocument(
                new Joined(
                    "",
                    "<op code=\"call\">",
                    "  <name>OverloadMethods.methodOne</name>",
                    "  <args>",
                    "    <arg type=\"Ljava/lang/String\">?</arg>",
                    "    <arg type=\"Z\">?</arg>",
                    "  </args>",
                    "</op>"
                ).asString()
            ).nodes("//op").get(0)
        ).asString(),
        new IsEqual<>(
            "OverloadMethods.methodOne.Ljava/lang/String:Z"
        )
    ).affirm();
}
 
Example #9
Source File: ServerPgsql.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public void start() throws Exception {
    new StatementUpdate(
        new SessionAuth(
            new SourcePgsql(
                this.host,
                this.port,
                ""
            ),
            this.username,
            this.password
        ),
        new QuerySimple(
            new FormattedText(
                new Joined(
                    " ",
                    "CREATE DATABASE %s WITH OWNER %s",
                    "ENCODING utf8 TEMPLATE template1;"
                ),
                this.dbname.value(),
                this.username
            )
        )
    ).result();
    this.script.run(this.session());
}
 
Example #10
Source File: ServerPgsqlTest.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Test
public void startAndStop() throws Exception {
    try (
        Server pgsql = new ServerPgsql(
            new ScriptSql(
                new ResourceOf(
                    new Joined(
                        "/",
                        "com/github/fabriciofx/cactoos/jdbc/phonebook",
                        "phonebook-pgsql.sql"
                    )
                )
            )
        )
    ) {
        pgsql.start();
    }
}
 
Example #11
Source File: ServerMysqlTest.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Test
public void startAndStop() throws Exception {
    try (
        Server mysql = new ServerMysql(
            new ScriptSql(
                new ResourceOf(
                    new Joined(
                        "/",
                        "com/github/fabriciofx/cactoos/jdbc/phonebook",
                        "phonebook-mysql.sql"
                    )
                )
            )
        )
    ) {
        mysql.start();
    }
}
 
Example #12
Source File: SourceH2.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
/**
 * Public ctor.
 * @param drvr H2 Driver
 * @param dbname DB name
 */
public SourceH2(final Driver drvr, final String dbname) {
    this.driver = drvr;
    this.url = new Unchecked<>(
        new Sticky<>(
            () -> new FormattedText(
                new Joined(
                    "",
                    "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;",
                    "INIT=CREATE SCHEMA IF NOT EXISTS %s\\;SET SCHEMA %s"
                ),
                dbname,
                dbname,
                dbname
            ).asString()
        )
    );
}
 
Example #13
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 #14
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 #15
Source File: HtHeadTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void emptyHeadOfHttpResponse()  {
    new Assertion<>(
        "Text does not have an empty string",
        new TextOf(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "",
                        "",
                        "Body"
                    )
                )
            )
        ),
        new TextHasString("")
    ).affirm();
}
 
Example #16
Source File: Logged.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Override
public int executeUpdate() throws SQLException {
    final Instant start = Instant.now();
    final int updated = this.origin.executeUpdate();
    final Instant end = Instant.now();
    final long millis = Duration.between(start, end).toMillis();
    this.logger.log(
        this.level,
        new UncheckedText(
            new FormattedText(
                new Joined(
                    " ",
                    "[%s] PreparedStatement[#%d] updated a source and",
                    "returned '%d' in %dms."
                ),
                this.source,
                this.id,
                updated,
                millis
            )
        ).asString()
    );
    return updated;
}
 
Example #17
Source File: HtCookiesTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void takesCookiesOfHttpResponse() {
    MatcherAssert.assertThat(
        new HtCookies(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "Set-Cookie: path=/; domain=.google.com",
                        "",
                        "Hello, dude!",
                        "How are you?"
                    )
                )
            )
        ),
        new IsMapContaining<>(
            new IsEqual<>("domain"),
            new IsEqual<>(new ListOf<>(".google.com"))
        )
    );
}
 
Example #18
Source File: HtCookiesTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void incorrectHttpResponseCookie() {
    MatcherAssert.assertThat(
        new HtCookies(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "Set-Cookie: path=/; 123; domain=.google.com",
                        "",
                        "Hello, dude!",
                        "How are you?"
                    )
                )
            )
        ),
        new IsMapContaining<>(
            new IsEqual<>("domain"),
            new IsEqual<>(".google.com")
        )
    );
}
 
Example #19
Source File: HtHeadersTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void takesHeadersOutOfHttpResponse() throws IOException {
    MatcherAssert.assertThat(
        new HtHeaders(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        "Hello, dude!",
                        "How are you?"
                    )
                )
            )
        ),
        new IsMapContaining<>(
            new IsEqual<>("content-type"),
            new IsEqual<>(new ListOf<>("text/plain"))
        )
    );
}
 
Example #20
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 #21
Source File: HtSecureWireTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Override
public InputStream stream() throws Exception {
    final String delimiter = "\r\n";
    return new InputOf(
        new Joined(
            delimiter,
            "GET / HTTP/1.1",
            new FormattedText(
                "Host: %s",
                this.host
            ).asString(),
            "Connection: close",
            delimiter
        )
    ).stream();
}
 
Example #22
Source File: HtStatusTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void takesStatusOutOfHttpResponse() throws IOException {
    MatcherAssert.assertThat(
        new HtStatus(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        "Hello, dude!",
                        "How are you?"
                    )
                )
            )
        ).intValue(),
        Matchers.equalTo(HttpURLConnection.HTTP_OK)
    );
}
 
Example #23
Source File: HtContentTypeTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void takesContentTypeOutOfHttpResponse() {
    MatcherAssert.assertThat(
        new HtContentType(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "Content-type: text/plain",
                        "",
                        "Hello, dude!"
                    )
                )
            )
        ).value(),
        new IsEqual<>(new ListOf<>("text/plain"))
    );
}
 
Example #24
Source File: HtContentTypeTest.java    From cactoos-http with MIT License 6 votes vote down vote up
@Test
public void takesDefaultContentType() {
    MatcherAssert.assertThat(
        new HtContentType(
            new HtHead(
                new InputOf(
                    new Joined(
                        "\r\n",
                        "HTTP/1.1 200 OK",
                        "",
                        "Hello, dude!"
                    )
                )
            )
        ).value(),
        new IsEqual<>(new ListOf<>("application/octet-stream"))
    );
}
 
Example #25
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 #26
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 #27
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 #28
Source File: TempFolder.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ctor.
 * Creates new folder in temporary directory.
 * @param path Relative path to new directory.
 * @since 1.0
 */
public TempFolder(final Text path) {
    this(
        new Sticky<>(
            () -> Files.createDirectory(
                Paths.get(
                    new Joined(
                        File.separator,
                        System.getProperty("java.io.tmpdir"),
                        path.asString()
                    ).asString()
                )
            )
        )
    );
}
 
Example #29
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 #30
Source File: AppendToTest.java    From cactoos with MIT License 6 votes vote down vote up
/**
 * Ensures that AppendTo is appending unicode text to a given file.
 * @throws Exception if fails
 */
@Test
public void appendsUnicodeToFile() throws Exception {
    final File source = this.folder.newFile();
    final String first = "Hello, товарищ output #3 äÄ ";
    new OutputTo(source)
        .stream()
        .write(first.getBytes(StandardCharsets.UTF_8));
    final String second = "#4 äÄ üÜ öÖ and ß";
    new AppendTo(source)
        .stream()
        .write(second.getBytes(StandardCharsets.UTF_8));
    new Assertion<>(
        "Can't find expected unicode text content",
        new InputOf(source),
        new InputHasContent(new Joined("", first, second))
    ).affirm();
}