Saffik
Saffik

Reputation: 1013

Java code reuse in two or more methods with same code

I have the following code in Java to query a database:

public interface MapReduceDAO {

    String host = "mysql";
    int port = 3306;
    String user = "root";
    String password = "root";
    String dbName = "customers";

    default String customersMysqlUrl(String name) {
        return getDocker().containers().container(name).port(port).inFormat("$HOST:$EXTERNAL_PORT");
    }

    default void checkTableHasData(Duration atMost, String tableName) throws Exception {
        try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

            await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
            () -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
            is(Matchers.greaterThan(0)));
        }
    }

    default void checkExistsQuery(Duration atMost, String tableName, int countValueExpected) throws Exception {
    try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

        await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
        () -> mysqlQuery.count("SELECT COUNT(*) FROM " + tableName),
        is(Matchers.equalTo(countValueExpected)));
    }
}

    DockerComposeRule getDocker();
}

How to avoid using repeated code. In method checkTableHasData and checkExistsQuery, I have mostly repeated code.

Edit: Forgot to mention, they may have different assert at the end, e.g:

is(Matchers.greaterThan(0)));

is(Matchers.equalTo(countValueExpected)));

Upvotes: 1

Views: 250

Answers (2)

assylias
assylias

Reputation: 328913

You can simply extract the common behaviour in a separate method:

default void checkTableHasData(Duration atMost, String tableName) throws Exception {
    checkExistsQuery("SELECT COUNT(*) FROM " + tableName),
}

default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
                                              String columnValue) throws Exception {
    checkExistsQuery("SELECT COUNT(*) FROM " + tableName + " where " + columnName +
                               " = " + columnValue),
}

private static void checkExistsQuery(Duration atMost, String query) {
    try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

        await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
        () -> mysqlQuery.count(query),
        is(Matchers.greaterThan(0)));
    }
}

Upvotes: 1

André Stannek
André Stannek

Reputation: 7863

If I see it correctly, those methods only differ in the parameter you give to count(). Just introduce a method that takes this as a parameter and call it with the different values.

default void checkTableHasData(Duration atMost, String tableName) throws Exception {
    check(atMost, "SELECT COUNT(*) FROM " + tableName);
}

default void checkTableRowExistSearchOnColumn(Duration atMost, String tableName, String columnName,
                                              String columnValue) throws Exception {
    check(atMost, "SELECT COUNT(*) FROM " + tableName + " where " + columnName +
                               " = " + columnValue);
}

private void check(Duration atMost, String countStatement) throws Exception {
    try (MysqlQuery mysqlQuery = new MysqlQuery(customersMysqlUrl(host), dbName, user, password)) {

        await().atMost(atMost).pollDelay(Duration.ONE_SECOND).ignoreExceptions().until(
        () -> mysqlQuery.count(countStatement),
        is(Matchers.greaterThan(0)));
    }
}

Upvotes: 2

Related Questions