singleton-enforcer
Tool to enforce that certain classes are ony ever constructed once
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
SingletonEnforcerinto a JUnit@Rule - Added a method
SingletonEnforcer.duringthat takes aRunnablethat 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
SingletonEnforcermust besetUpbefore use with the package to cover andtearDownmust be called after useSingletonEnforcer.checkSingletonschecks that the given classes were only constructed onceSingletonEnforcer.checkSingletonDependenciesAreNotLeakedchecks that a dependency of a singleton is only used inside that singleton and not passed on to other objects