/singleton-enforcer

Tool to enforce that certain classes are only ever constructed once

Primary LanguageJavaApache License 2.0Apache-2.0

Maven Central Javadoc Gitter

Build Status codecov Codacy Badge codebeat badge

Quality Gate

singleton-enforcer

Tool to enforce that certain classes are ony ever constructed once

Example:

public class ExampleTest {

    @Rule
    public SingletonEnforcer singletonEnforcer = SingletonEnforcer.enforcePackage("example");

    @Test(expected = AssertionError.class)
    public void singletonConstructedTwiceWillThrowException() {
        singletonEnforcer.during(() -> {
            new Singleton();
            new Singleton();
        }).checkSingletonsAreConstructedOnce(Singleton.class);
    }

    @Test(expected = AssertionError.class)
    public void leakedDependencyWillThrowAssertionError() {
        singletonEnforcer.during(() -> {
            LeakedDependencyInterface leakedDependency = new LeakedDependency();
            new SingletonWithDependency(leakedDependency, new Object());
            new ClassWithLeakedDependency(leakedDependency);
        }).checkDependencyIsNotLeaked(SingletonWithDependency.class, LeakedDependencyInterface.class);
    }
}

Dependency:

<dependency>
    <groupId>io.github.theangrydev</groupId>
    <artifactId>singleton-enforcer</artifactId>
    <version>2.1.0</version>
</dependency>

Releases

2.1.0

  • Made the public API thread safe
  • Added documentation for the public API

2.0.1

  • Turned SingletonEnforcer into a JUnit @Rule
  • Added a method SingletonEnforcer.during that takes a Runnable that should execute some code that the assertions will be made about
  • Added a checks to make sure that the classes in the package to enforce are instrumented before use
  • Added a check to make sure that instrumented classes are not used outside of SingletonEnforcer.during

1.0.1

  • Tool to enforce that certain classes are ony ever constructed once
  • SingletonEnforcer must be setUp before use with the package to cover and tearDown must be called after use
  • SingletonEnforcer.checkSingletons checks that the given classes were only constructed once
  • SingletonEnforcer.checkSingletonDependenciesAreNotLeaked checks that a dependency of a singleton is only used inside that singleton and not passed on to other objects