Java Developer

ot-clean, Tests refactoring

The final step before the release is to cleanup the test code. Some test cases are really too complex and I want to make them more clear and straightforward. Simpler tests are making adding new features less error prone and more straightforward.

Helper object to encapsulate case structure

There are multiple tests that are checking if the directory is cleaned up and files are removed properly. In every case project structure has to be created and checked again after cleaning process. It turns out that logic can be fairly easy encapsulated.

Before refactoring

@Test
public void deleteGrails2ProjectWithoutAnyConfiguration() {
    final Path root = this.createGrails2();
    MatcherAssert.assertThat(
        root.resolve("target").toFile().isDirectory(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        root.resolve("subdir.log").toFile().exists(),
        Matchers.is(true)
    );
    MatcherAssert.assertThat(
        root.resolve("subdir/target/some.log").toFile().exists(),
        Matchers.is(true)
    );
    final Mode mode = new Mode(Mode.Arg.D.getLabel());
    this.get(Wipe.Type.GRAILS_2).clean(new Delete(mode, new Summary(mode)), root);
    MatcherAssert.assertThat(
        root.resolve("target").toFile().isDirectory(),
        Matchers.is(false)
    );
    MatcherAssert.assertThat(
        root.resolve("subdir.log").toFile().exists(),
        Matchers.is(false)
    );
    MatcherAssert.assertThat(
        root.resolve("subdir/target/some.log").toFile().exists(),
        Matchers.is(false)
    );
}

After refactoring

@Test
public void defaultGrailsVersionTwo() {
    new Check("target", "subdir.log", "subdir/target/some.log")
        .file("application.properties", "app.grails.version")
        .run(Wipe.Type.GRAILS_2);
}

Java 8 utility class

I wanted to increase test coverage for the project. It turns out that it is not possible to reach 100% due to the fact that private constructor of utility class is not testable. Java 8 default interface methods are able to do the trick which makes the code looking even better.

Before refactoring

public final class Res {

    private Res() {
        // Private
    }

    public static String resource(final String path) {
        try {
            final InputStream stream = Res.class.getResourceAsStream(path);
            Validate.isTrue(stream != null, "File not found");
            return IOUtils.toString(stream);
        } catch (final IllegalArgumentException | IOException exc) {
            Logger.error(Res.class, "Unable to read '%s'", path);
            throw new IllegalArgumentException(
                String.format("Unable to read resource: %s", path), exc
            );
        }
    }

}

After refactoring

interface Res {

    static String resource(final String path) {
        try {
            final InputStream stream = Res.class.getResourceAsStream(path);
            Validate.isTrue(stream != null, "File not found");
            return IOUtils.toString(stream);
        } catch (final IllegalArgumentException | IOException exc) {
            Logger.error(Res.class, "Unable to read '%s'", path);
            throw new IllegalArgumentException(
                String.format("Unable to read resource: %s", path), exc
            );
        }
    }

}

Feels better.

The ot-clean series

This blog post is one of the series about the ot-clean. Simple open source tool that maintains your projects directories cleaned from all the garbage. Feel free to look at the source code or readme page for more information. You can also submit pull request or post suggestion. This text participates in open source blogging competition.

Project status

Build Status

All posts from the ot-clean series


Like it? Share it on , , ,

About the author

Grzegorz Gajos, Software Architect with international consulting and programming background. Co-founder of Open Tangerine Software House. Quality evangelist. An experienced entrepreneur, out of the box thinker and problem solver.