common-utils

Common utils: Guard, GuardCheck, GcUtils, LeakDetector, ErrorInfo, BaseException

License

License

Categories

Categories

Ant Build Tools
GroupId

GroupId

com.antkorwin
ArtifactId

ArtifactId

common-utils
Last Version

Last Version

1.2
Release Date

Release Date

Type

Type

jar
Description

Description

common-utils
Common utils: Guard, GuardCheck, GcUtils, LeakDetector, ErrorInfo, BaseException
Project URL

Project URL

https://github.com/antkorwin/common-utils
Source Code Management

Source Code Management

http://github.com/antkorwin/common-utils

Download common-utils

How to add to project

<!-- https://jarcasting.com/artifacts/com.antkorwin/common-utils/ -->
<dependency>
    <groupId>com.antkorwin</groupId>
    <artifactId>common-utils</artifactId>
    <version>1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.antkorwin/common-utils/
implementation 'com.antkorwin:common-utils:1.2'
// https://jarcasting.com/artifacts/com.antkorwin/common-utils/
implementation ("com.antkorwin:common-utils:1.2")
'com.antkorwin:common-utils:jar:1.2'
<dependency org="com.antkorwin" name="common-utils" rev="1.2">
  <artifact name="common-utils" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.antkorwin', module='common-utils', version='1.2')
)
libraryDependencies += "com.antkorwin" % "common-utils" % "1.2"
[com.antkorwin/common-utils "1.2"]

Dependencies

compile (6)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.16.22
org.assertj : assertj-core jar 3.11.1
junit : junit jar 4.12
org.awaitility : awaitility jar 3.1.0
org.reflections : reflections jar 0.9.11
org.apache.commons : commons-lang3 jar 3.8.1

test (1)

Group / Artifact Type Version
org.mockito : mockito-core jar 2.15.0

Project Modules

There are no modules declared in this project.

Different Java utility

Build Status Maven Central

This is a utility for simplifying the writing of tests.

Getting started

Dependency:

<dependency>
  <groupId>com.antkorwin</groupId>
  <artifactId>common-utils</artifactId>
  <version>1.0</version>
</dependency>

Guard

Guard helps you, when you need to check a some boolean condition and expect a throws Exception if condition is false.

Guard.check( 02 + 010 != 12, CruelJavaWorldException.class, "ooops");

Guard.check( object != null, WrongArgumentException.class, "Object must be not null");

Guard check

You can use this tool while writing a test, to make tests for a checking throw of the exception more readable.

@Test
public void guardWithoutErrorInfo() {

    GuardCheck.check(() -> { throw new IndexOutOfBoundsException(); },
                     IndexOutOfBoundsException.class);
}


@Test
public void testGuardCheck() {

    GuardCheck.check(() -> {
                         throw new NotFoundException(TestErrorInfo.TEST_ERROR);
                     },
                     NotFoundException.class,
                     TestErrorInfo.TEST_ERROR);
}

ErrorInfo

You can use it with an ErrorInfo to accumulate all you domain specific exceptions in an one enum-file:

@ErrorInfoUnique
public enum TestErrorInfo implements ErrorInfo {
    TEST_ERROR("error"),
    TEST_ERROR_ARG("wrong arg");

    private static final int BASE = 1000;
    private final String msg;

    TestErrorInfo(String msg) {
        this.msg = msg;
    }

    @Override
    public String getMessage() {
        return this.msg;
    }

    @Override
    public Integer getCode() {
        return BASE + ordinal();
    }
}

and sample of a test case :

Guard.check(a != b, TestErrorInfo.TEST_ERROR);

Collision Detector for ErrorInfos

In order to ensure the uniqueness of codes in a whole project, you can use enums which implements an ErrorInfo interface.

@Test
public void assertInPackage() {   
    // Act & assert
    ErrorInfoCollisionDetector.assertInPackage("com.demo.project");
}

Utils to work with Garbage Collector

GcUtils

You can use this tool to run the garbage collector and finalize unused memory in your junit tests.

@Test
public void testWeakAfterGC() {
    // Arrange
    String instance = new String("123");
    WeakReference<String> ref = new WeakReference<>(instance);

    // Act
    instance = null;
    GcUtils.fullFinalization();

    // Asserts
    Assertions.assertThat(ref.get()).isNull();
}

Simple memory LeakDetector

You can check a memory leak in your code for a particular object.

@Test
    public void testWithLeak() {
        // Arrange
        Foo foo = new Foo();
        Foo bar = foo;
        LeakDetector leakDetector = new LeakDetector(foo);

        // Act
        foo = null;

        // Asserts
        leakDetector.assertMemoryLeaksExist();
    }

Versions

Version
1.2
1.1
1.0