https://github.com/theangrydev/singleton-enforcer
Tool to enforce that certain classes are only ever constructed once
https://github.com/theangrydev/singleton-enforcer
enforce scope singleton static-analysis
Last synced: 8 months ago
JSON representation
Tool to enforce that certain classes are only ever constructed once
- Host: GitHub
- URL: https://github.com/theangrydev/singleton-enforcer
- Owner: theangrydev
- License: apache-2.0
- Created: 2016-07-08T21:45:23.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-16T20:11:13.000Z (over 7 years ago)
- Last Synced: 2025-02-09T16:42:51.888Z (about 1 year ago)
- Topics: enforce, scope, singleton, static-analysis
- Language: Java
- Homepage:
- Size: 82 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://mvnrepository.com/artifact/io.github.theangrydev/singleton-enforcer)
[](http://javadoc-badge.appspot.com/io.github.theangrydev/singleton-enforcer)
[](https://gitter.im/singleton-enforcer/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[](https://travis-ci.org/theangrydev/singleton-enforcer)
[](https://codecov.io/gh/theangrydev/singleton-enforcer)
[](https://www.codacy.com/app/liam-williams/singleton-enforcer?utm_source=github.com&utm_medium=referral&utm_content=theangrydev/singleton-enforcer&utm_campaign=Badge_Grade)
[](https://codebeat.co/projects/github-com-theangrydev-singleton-enforcer)
[](https://sonarcloud.io/dashboard?id=io.github.theangrydev:singleton-enforcer)
# singleton-enforcer
Tool to enforce that certain classes are ony ever constructed once
[Example:](https://github.com/theangrydev/singleton-enforcer/blob/master/src/test/java/acceptance/ExampleTest.java)
```java
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:
```xml
io.github.theangrydev
singleton-enforcer
2.1.0
```
## 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