Class FreezingArchRule
java.lang.Object
com.tngtech.archunit.library.freeze.FreezingArchRule
- All Implemented Interfaces:
HasDescription,CanOverrideDescription<ArchRule>,ArchRule,CanBeEvaluated
A decorator around an existing
You might consider using this class when introducing a new
ArchRule that "freezes" the state of all violations on the first call instead of failing the test.
This means in particular that the first run of a FreezingArchRule will always pass.
Consecutive calls will only fail if "unknown" violations are introduced (read below for further explanations when a violation is "unknown").
Once resolved, initially "known" violations will fail again if they were re-introduced.
You might consider using this class when introducing a new
ArchRule to an existing project that causes too many violations to solve
at the current time. A typical example is a huge legacy project where a new rule might cause thousands of violations. Even if it is impossible
to fix all those violations at the moment, it is typically a good idea to a) make sure no further violations are introduced and
b) incrementally fix those violations over time one by one.
FreezingArchRule uses two concepts to support this use case:
-
a
ViolationStoreto store the result of the current evaluation and retrieve the result of the previous evaluation of this rule.
The defaultViolationStorestores violations in plain text files within the path specified byfreeze.store.default.pathof "archunit.properties" (default:archunit_store)
A custom implementation can be provided in two ways. Either programmatically viapersistIn(ViolationStore), or by specifying the fully qualified class name within "archunit.properties", e.g.freeze.store=com.fully.qualified.MyViolationStore -
a
ViolationLineMatcherto decide which violations are "known", i.e. have already been present in the previous evaluation.
The defaultViolationLineMatchercompares violations ignoring the line number of their source code location and auto-generated numbers of anonymous classes or lambda expressions.
A custom implementation can be configured in two ways. Again either programmatically viaassociateViolationLinesVia(ViolationLineMatcher), or within "archunit.properties", e.g.freeze.lineMatcher=com.fully.qualified.MyViolationLineMatcher
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.tngtech.archunit.lang.ArchRule
ArchRule.Assertions, ArchRule.Factory, ArchRule.Transformation -
Method Summary
Modifier and TypeMethodDescriptionallowEmptyShould(boolean allowEmptyShould) If set totrueallows the should-clause of this rule to be checked against an empty set of elements.Allows to adjust the description of this object.Allows to reconfigure how thisFreezingArchRulewill decide if an occurring violation is known or not.voidcheck(JavaClasses classes) evaluate(JavaClasses classes) static FreezingArchRulepersistIn(ViolationStore store) Allows to reconfigure theViolationStoreto use.toString()
-
Method Details
-
check
-
because
-
allowEmptyShould
Description copied from interface:ArchRuleIf set totrueallows the should-clause of this rule to be checked against an empty set of elements. Otherwise, the rule will fail with a respective message. This is to prevent possible implementation errors, like filtering for a non-existing package in the that-clause causing an always-passing rule.
Note that this method will override the configuration propertyarchRule.failOnEmptyShould.- Specified by:
allowEmptyShouldin interfaceArchRule- Parameters:
allowEmptyShould- Whether the rule fails if the should-clause is evaluated with an empty set of elements- Returns:
- A (new)
ArchRulewith adjustedallowEmptyShouldbehavior
-
as
Description copied from interface:CanOverrideDescriptionAllows to adjust the description of this object. Note that this method will not modify the current object, but instead return a new object with adjusted description.- Specified by:
asin interfaceCanOverrideDescription<ArchRule>- Parameters:
newDescription- The description the result of this method will hold- Returns:
- A new equivalent object with adjusted description
-
evaluate
- Specified by:
evaluatein interfaceCanBeEvaluated
-
getDescription
- Specified by:
getDescriptionin interfaceHasDescription
-
persistIn
Allows to reconfigure theViolationStoreto use. TheViolationStorewill be used to store the initial state of aFreezingArchRuleand update this state on further evaluation of this rule.- Parameters:
store- TheViolationStoreto use- Returns:
- An adjusted
FreezingArchRulewhich will store violations in the passedViolationStore - See Also:
-
associateViolationLinesVia
@PublicAPI(usage=ACCESS) public FreezingArchRule associateViolationLinesVia(ViolationLineMatcher matcher) Allows to reconfigure how thisFreezingArchRulewill decide if an occurring violation is known or not.- Parameters:
matcher- AViolationLineMatcherthat decides which lines of a violation description are known and which are unknown and should cause a failure of this rule- Returns:
- An adjusted
FreezingArchRulewhich will compare occurring violations to stored ones with the givenViolationLineMatcher - See Also:
-
toString
-
freeze
- Parameters:
rule- AnArchRulethat should be "frozen" on the first call, i.e. all occurring violations will be stored for comparison on consecutive calls.- Returns:
- A
FreezingArchRulewrapping the original rule - See Also:
-