org.cactoos.io.ResourceOf Java Examples

The following examples show how to use org.cactoos.io.ResourceOf. 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: ScriptSqlTest.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Test
public void mysqlServer() throws Exception {
    final Server server = new ServerMysql(
        new ScriptSql(
            new ResourceOf(
                new Joined(
                    "/",
                    "com/github/fabriciofx/cactoos/jdbc/phonebook",
                    "phonebook-mysql.sql"
                )
            )
        )
    );
    server.start();
    server.stop();
}
 
Example #2
Source File: ProgramTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Program can parse multiple types in one file.
 * This test verifies indentation parsing.
 *
 * @throws Exception If some problem inside
 */
@Test
public void parsesMultipleTypes() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/multitypes.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("Number.java"))).asString(),
        Matchers.allOf(
            Matchers.containsString("interface Number"),
            Matchers.containsString("Decimal decimal()"),
            Matchers.containsString("Integral integral()")
        )
    );
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("Text.java"))).asString(),
        Matchers.allOf(
            Matchers.containsString("interface Text"),
            Matchers.containsString("Number length()"),
            Matchers.containsString("Collection lines()")
        )
    );
}
 
Example #3
Source File: ProgramTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Program can parse a type with multiple methods.
 *
 * @throws Exception If some problem inside
 */
@Test
public void parsesBigType() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/car.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("Car.java"))).asString(),
        Matchers.allOf(
            Matchers.containsString("interface Car"),
            Matchers.containsString("Money cost()"),
            Matchers.containsString("Bytes picture()"),
            Matchers.containsString("Car moveTo(final Coordinates coords)")
        )
    );
}
 
Example #4
Source File: ProgramTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Program can parse a type with one method with parameters.
 *
 * @throws Exception If some problem inside
 */
@Test
public void parsesTypeWithParametrizedMethods() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/pixel.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("Pixel.java"))).asString(),
        Matchers.allOf(
            Matchers.containsString("interface Pixel"),
            Matchers.containsString(
                "Pixel moveTo(final Integer x, final Integer y)"
            )
        )
    );
}
 
Example #5
Source File: ProgramTest.java    From eo with MIT License 6 votes vote down vote up
/**
 * Program can parse a simple type with one method.
 *
 * @throws Exception If some problem inside
 */
@Test
public void parsesSimpleType() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/book.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("Book.java"))).asString(),
        Matchers.allOf(
            Matchers.containsString("interface Book"),
            Matchers.containsString("Text text()")
        )
    );
}
 
Example #6
Source File: Ccm.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
) {
    if (!"ccm".equalsIgnoreCase(metric)) {
        throw new IllegalArgumentException(
            new FormattedText(
                "This metric is CCM, not %s.", metric
            ).toString()
        );
    }
    return Ccm.withFixedNcc(
        new XSLDocument(
            new UncheckedInput(
                new ResourceOf("org/jpeek/metrics/CCM.xsl")
            ).stream()
        ).transform(skeleton),
        skeleton
    );
}
 
Example #7
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 #8
Source File: ScriptSqlTest.java    From cactoos-jdbc with MIT License 6 votes vote down vote up
@Test
public void h2Server() throws Exception {
    final Server server = new ServerH2(
        new ScriptSql(
            new ResourceOf(
                new Joined(
                    "/",
                    "com/github/fabriciofx/cactoos/jdbc/phonebook",
                    "phonebook-h2.sql"
                )
            )
        )
    );
    server.start();
    server.stop();
}
 
Example #9
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 #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: App.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Copy resource.
 * @param name The name of resource
 * @throws IOException If fails
 */
private void copy(final String name) throws IOException {
    new IoChecked<>(
        new LengthOf(
            new TeeInput(
                new ResourceOf(String.format("org/jpeek/%s", name)),
                this.output.resolve(name)
            )
        )
    ).value();
}
 
Example #12
Source File: ProgramTest.java    From eo with MIT License 5 votes vote down vote up
/**
 * Test zero example, this object implements two interfaces.
 *
 * @throws Exception If some problem inside
 */
@Test
public void processZeroExample() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/zero.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("zero.java"))).asString(),
        Matchers.stringContainsInOrder(
            new StickyList<>(
                "public",
                "final",
                "class",
                "zero",
                "implements",
                "Money",
                ",",
                "Int",
                "{",
                "private final Int amount;",
                "private final Text currency;",
                "}"
            )
        )
    );
}
 
Example #13
Source File: ProgramTest.java    From eo with MIT License 5 votes vote down vote up
/**
 * Compiles long EO syntax.
 * @throws Exception If some problem inside
 */
@Test
public void compilesLongSyntax() throws Exception {
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/long-program.eo"),
        s -> new DeadOutput()
    );
    program.compile();
}
 
Example #14
Source File: MetricBase.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Ctor.
 * @param path Path to the xsl
 * @throws Exception If file not found.
 */
public MetricBase(final String path) throws Exception {
    this.xsl = new XSLDocument(
        new ResourceOf(
            path
        ).stream()
    );
}
 
Example #15
Source File: Version.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public String value() throws IOException {
    return new PropertiesOf(
        new ResourceOf(
            "org/jpeek/jpeek.properties"
        )
    ).value().getProperty("org.jpeek.version");
}
 
Example #16
Source File: TkApp.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Main Java entry point.
 * @param args Command line args
 * @throws IOException If fails
 */
public static void main(final String... args) throws IOException {
    Sentry.init(
        new PropertiesOf(
            new ResourceOf(
                "org/jpeek/jpeek.properties"
            )
        ).value().getProperty("org.jpeek.sentry")
    );
    new FtCli(
        new TkApp(Files.createTempDirectory("jpeek")),
        args
    ).start(Exit.NEVER);
}
 
Example #17
Source File: Dynamo.java    From jpeek with MIT License 5 votes vote down vote up
/**
 * Properties.
 * @return Props
 * @throws IOException If fails
 */
private static Properties pros() throws IOException {
    return new PropertiesOf(
        new ResourceOf(
            "org/jpeek/jpeek.properties"
        )
    ).value();
}
 
Example #18
Source File: XslCalculus.java    From jpeek with MIT License 5 votes vote down vote up
@Override
public XML node(final String metric, final Map<String, Object> params,
    final XML skeleton) throws IOException {
    return new XSLDocument(
        new TextOf(
            new ResourceOf(
                new FormattedText("org/jpeek/metrics/%s.xsl", metric)
            )
        ).asString(),
        Sources.DUMMY,
        params
    ).transform(skeleton);
}
 
Example #19
Source File: TkApp.java    From jpeek with MIT License 4 votes vote down vote up
/**
 * Ctor.
 * @param home Home directory
 * @return The take
 * @throws IOException If fails
 */
private static Take make(final Path home) throws IOException {
    final Futures futures = new Futures(
        new Reports(home)
    );
    final BiFunc<String, String, Func<String, Response>> reports =
        new AsyncReports(
            // @checkstyle MagicNumber (1 line)
            new StickyFutures(futures, 100)
        );
    return new TkSslOnly(
        new TkFallback(
            new TkForward(
                new TkFork(
                    new FkRegex("/", new TkIndex()),
                    new FkRegex("/robots.txt", new TkText("")),
                    new FkRegex("/mistakes", new TkMistakes()),
                    new FkRegex(
                        "/flush",
                        (Take) req -> new RsText(
                            String.format("%d flushed", new Results().flush())
                        )
                    ),
                    new FkRegex(
                        "/upload",
                        (Take) req -> new RsPage(req, "upload")
                    ),
                    new FkRegex("/do-upload", new TkUpload(reports)),
                    new FkRegex("/all", new TkAll()),
                    new FkRegex("/queue", new TkQueue(futures)),
                    new FkRegex(
                        ".+\\.xsl",
                        new TkWithType(
                            new TkClasspath(),
                            "text/xsl"
                        )
                    ),
                    new FkRegex(
                        "/jpeek\\.css",
                        new TkWithType(
                            new TkText(
                                new TextOf(
                                    new ResourceOf("org/jpeek/jpeek.css")
                                ).asString()
                            ),
                            "text/css"
                        )
                    ),
                    new FkRegex(
                        "/([^/]+)/([^/]+)(.*)",
                        new TkReport(reports, new Results())
                    )
                )
            ),
            new FbChain(
                new FbStatus(
                    HttpURLConnection.HTTP_NOT_FOUND,
                    (Fallback) req -> new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(req.throwable().getMessage()),
                            req.code()
                        )
                    )
                ),
                req -> {
                    Sentry.capture(req.throwable());
                    return new Opt.Single<>(
                        new RsWithStatus(
                            new RsText(
                                new TextOf(req.throwable()).asString()
                            ),
                            HttpURLConnection.HTTP_INTERNAL_ERROR
                        )
                    );
                }
            )
        )
    );
}
 
Example #20
Source File: StatementTransactionTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void commit() throws Exception {
    final Transacted transacted = new Transacted(
        new SessionNoAuth(
            new SourceH2("safedb")
        )
    );
    new ScriptSql(
        new ResourceOf(
            "com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
        )
    ).run(transacted);
    MatcherAssert.assertThat(
        "Can't perform a transaction commit",
        XhtmlMatchers.xhtml(
            new ResultAsValue<>(
                new StatementTransaction<>(
                    transacted,
                    () -> {
                        final Phonebook phonebook = new PhonebookSql(
                            transacted
                        );
                        final Contact contact = phonebook.contact(
                            new MapOf<String, String>(
                                new MapEntry<>("name", "Albert Einstein")
                            )
                        );
                        contact.phones().add(
                            new MapOf<String, String>(
                                new MapEntry<>("number", "99991234"),
                                new MapEntry<>("carrier", "TIM")
                            )
                        );
                        contact.phones().add(
                            new MapOf<String, String>(
                                new MapEntry<>("number", "98812564"),
                                new MapEntry<>("carrier", "Oi")
                            )
                        );
                        return contact.about();
                    }
                )
            ).value()
        ),
        XhtmlMatchers.hasXPaths(
            "/contact/name[text()='Albert Einstein']",
            "/contact/phones/phone/number[text()='99991234']",
            "/contact/phones/phone/carrier[text()='TIM']",
            "/contact/phones/phone/number[text()='98812564']",
            "/contact/phones/phone/carrier[text()='Oi']"
        )
    );
}
 
Example #21
Source File: FakeBase.java    From jpeek with MIT License 4 votes vote down vote up
@Override
public Iterable<Path> files() throws IOException {
    final Path temp = Files.createTempDirectory("jpeek");
    final Iterable<String> sources = new Mapped<>(
        cls -> String.format("%s.java", cls),
        this.classes
    );
    new IoChecked<>(
        new And(
            java -> {
                new LengthOf(
                    new TeeInput(
                        new ResourceOf(
                            String.format("org/jpeek/samples/%s", java)
                        ),
                        temp.resolve(java)
                    )
                ).value();
            },
            sources
        )
    ).value();
    if (sources.iterator().hasNext()) {
        final int exit;
        try {
            exit = new ProcessBuilder()
                .redirectOutput(ProcessBuilder.Redirect.INHERIT)
                .redirectInput(ProcessBuilder.Redirect.INHERIT)
                .redirectError(ProcessBuilder.Redirect.INHERIT)
                .directory(temp.toFile())
                .command(
                    new ListOf<>(
                        new Joined<String>(
                            new ListOf<>("javac"),
                            sources
                        )
                    )
                )
                .start()
                .waitFor();
        } catch (final InterruptedException ex) {
            Thread.currentThread().interrupt();
            throw new IllegalStateException(ex);
        }
        if (exit != 0) {
            throw new IllegalStateException(
                String.format("javac failed with exit code %d", exit)
            );
        }
    }
    return new DefaultBase(temp).files();
}
 
Example #22
Source File: ProgramTest.java    From eo with MIT License 4 votes vote down vote up
@Test
public void parsesFibonacciExample() throws Exception {
    final Path dir = Files.createTempDirectory("");
    final Program program = new Program(
        new ResourceOf("org/eolang/compiler/fibonacci.eo"), dir
    );
    program.compile();
    MatcherAssert.assertThat(
        new TextOf(dir.resolve(Paths.get("fibonacci.java"))).asString(),
        Matchers.stringContainsInOrder(
            new StickyList<>(
                "public", "final", "class",
                "fibonacci", "implements", "Int", "{",
                "private final Int n;",
                "public fibonacci()", "{",
                "this(1);",
                "}",
                "public fibonacci(final Int n)", "{",
                "this.n = n",
                "}",
                "public", "Int", "int", "()", "{",
                "return", "new", "if", "(",
                "new", "firstIsLess", "(",
                "this.n", ",", "2",
                ")", ",",
                "1", ",",
                "new", "plus", "(",
                "this.n", ",",
                "new", "fibonacci", "(",
                "new", "minus", "(",
                "this.n", ",", "1",
                ")",
                ")",
                ")",
                ");",
                "}",
                "}"
            )
        )
    );
}
 
Example #23
Source File: PhonebookTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void renameContact() throws Exception {
    try (
        Servers servers = new Servers(
            new ServerH2(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-h2.sql"
                        )
                    )
                )
            ),
            new ServerPgsql(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-pgsql.sql"
                        )
                    )
                )
            )
        )
    ) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.filter("maria").iterator()
                .next();
            contact.update(
                new MapOf<String, String>(
                    new MapEntry<>("name", "Maria Lima")
                )
            );
            MatcherAssert.assertThat(
                XhtmlMatchers.xhtml(
                    new PhonebookSql(session)
                        .filter("maria")
                        .iterator()
                        .next()
                        .about()
                ),
                XhtmlMatchers.hasXPaths(
                    "/contact/name[text()='Maria Lima']"
                )
            );
        }
    }
}
 
Example #24
Source File: PhonebookTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void findContact() throws Exception {
    try (
        Servers servers = new Servers(
            new ServerH2(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-h2.sql"
                        )
                    )
                )
            ),
            new ServerPgsql(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-pgsql.sql"
                        )
                    )
                )
            )
        )
    ) {
        for (final Session session : servers.sessions()) {
            MatcherAssert.assertThat(
                XhtmlMatchers.xhtml(
                    new PhonebookSql(session)
                        .filter("maria")
                        .iterator()
                        .next()
                        .about()
                ),
                XhtmlMatchers.hasXPaths(
                    "/contact/name[text()='Maria Souza']"
                )
            );
        }
    }
}
 
Example #25
Source File: PhonebookTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void addContact() throws Exception {
    try (
        Servers servers = new Servers(
            new ServerH2(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-h2.sql"
                        )
                    )
                )
            ),
            new ServerPgsql(
                new ScriptSql(
                    new ResourceOf(
                        new Joined(
                            "/",
                            "com/github/fabriciofx/cactoos/jdbc/phonebook",
                            "phonebook-pgsql.sql"
                        )
                    )
                )
            )
        )
    ) {
        for (final Session session : servers.sessions()) {
            final Phonebook phonebook = new PhonebookSql(session);
            final Contact contact = phonebook.contact(
                new MapOf<String, String>(
                    new MapEntry<>("name", "Donald Knuth")
                )
            );
            contact.phones().add(
                new MapOf<String, String>(
                    new MapEntry<>("number", "99991234"),
                    new MapEntry<>("carrier", "TIM")
                )
            );
            contact.phones().add(
                new MapOf<String, String>(
                    new MapEntry<>("number", "98812564"),
                    new MapEntry<>("carrier", "Oi")
                )
            );
            MatcherAssert.assertThat(
                XhtmlMatchers.xhtml(
                    contact.about()
                ),
                XhtmlMatchers.hasXPaths(
                    "/contact/name[text()='Donald Knuth']",
                    "/contact/phones/phone/number[text()='99991234']",
                    "/contact/phones/phone/carrier[text()='TIM']",
                    "/contact/phones/phone/number[text()='98812564']",
                    "/contact/phones/phone/carrier[text()='Oi']"
                )
            );
        }
    }
}
 
Example #26
Source File: StatementTransactionTest.java    From cactoos-jdbc with MIT License 4 votes vote down vote up
@Test
public void rollback() throws Exception {
    final Transacted transacted = new Transacted(
        new SessionNoAuth(
            new SourceH2("unsafedb")
        )
    );
    new ScriptSql(
        new ResourceOf(
            "com/github/fabriciofx/cactoos/jdbc/phonebook/phonebook-h2.sql"
        )
    ).run(transacted);
    final Phonebook phonebook = new PhonebookSql(transacted);
    final String name = "Frank Miller";
    try {
        new StatementTransaction<>(
            transacted,
            () -> {
                final Contact contact = phonebook.contact(
                    new MapOf<String, String>(
                        new MapEntry<>("name", name)
                    )
                );
                contact.phones().add(
                    new MapOf<String, String>(
                        new MapEntry<>("number", "99991234"),
                        new MapEntry<>("carrier", "TIM")
                    )
                );
                throw new IllegalStateException("Rollback");
            }
        ).result();
    } catch (final IllegalStateException ex) {
    }
    MatcherAssert.assertThat(
        "Can't perform a transaction rollback",
        StreamSupport.stream(
            phonebook.filter(name).spliterator(),
            false
        ).count(),
        Matchers.equalTo(0L)
    );
}