singleton-enforcer

Enforce singletons really are singletons!

License

License

GroupId

GroupId

io.github.theangrydev
ArtifactId

ArtifactId

singleton-enforcer
Last Version

Last Version

2.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

singleton-enforcer
Enforce singletons really are singletons!
Project URL

Project URL

https://github.com/theangrydev/singleton-enforcer
Source Code Management

Source Code Management

https://github.com/theangrydev/singleton-enforcer.git

Download singleton-enforcer

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.theangrydev/singleton-enforcer/ -->
<dependency>
    <groupId>io.github.theangrydev</groupId>
    <artifactId>singleton-enforcer</artifactId>
    <version>2.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.theangrydev/singleton-enforcer/
implementation 'io.github.theangrydev:singleton-enforcer:2.1.0'
// https://jarcasting.com/artifacts/io.github.theangrydev/singleton-enforcer/
implementation ("io.github.theangrydev:singleton-enforcer:2.1.0")
'io.github.theangrydev:singleton-enforcer:jar:2.1.0'
<dependency org="io.github.theangrydev" name="singleton-enforcer" rev="2.1.0">
  <artifact name="singleton-enforcer" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.theangrydev', module='singleton-enforcer', version='2.1.0')
)
libraryDependencies += "io.github.theangrydev" % "singleton-enforcer" % "2.1.0"
[io.github.theangrydev/singleton-enforcer "2.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
junit : junit jar 4.12
net.bytebuddy : byte-buddy jar 1.4.8
net.bytebuddy : byte-buddy-agent jar 1.4.8

test (1)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.3.0

Project Modules

There are no modules declared in this project.

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

Versions

Version
2.1.0
2.0.1
2.0.0
1.0.1
1.0.0