Skip to content

Commit

Permalink
Issue #12099: Add ArchUnit test to detect cycles in packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyom-Yadav committed Sep 11, 2022
1 parent b61f11a commit c7ee98b
Show file tree
Hide file tree
Showing 8 changed files with 2,848 additions and 1 deletion.
1 change: 1 addition & 0 deletions .ci/jsoref-spellchecker/exclude.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/openjdk17-excluded\.files$
^cdg-pitest-licence.txt$
^.teamcity/
^config/archunit-store/
);
my $exclude = join "|", @excludes;
while (<>) {
Expand Down
2,776 changes: 2,776 additions & 0 deletions config/archunit-store/7999d2d1-606c-4e14-a852-2cb0fa661f48

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions config/archunit-store/stored.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#
#Sat Sep 10 22:27:26 IST 2022
slices\ matching\ 'com.puppycrawl.tools.checkstyle.(**)'\ should\ be\ free\ of\ cycles=7999d2d1-606c-4e14-a852-2cb0fa661f48
3 changes: 3 additions & 0 deletions config/checkstyle_non_main_files_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
<!-- this file cannot be line wrapped, it contains cdg pitest licence -->
<suppress id="lineLength" files="cdg-pitest-licence.txt"/>

<!-- files in this directory cannot be line wrapped as they are generated by ArchUnit -->
<suppress id="lineLength" files="config[\\/]archunit-store.*"/>

<!-- apply check numberOfTestCasesInXpath only for files in suppressionxpathfilter directory -->
<suppress id="numberOfTestCasesInXpath"
files="src[\\/]it[\\/]java[\\/]com[\\/].*" />
Expand Down
5 changes: 4 additions & 1 deletion config/pmd-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@
from the test method.
In XdocsPagesTest PMD does not find asserts in lambdas.
All test classes which starts with XpathRegression have asserts inside parent's method.
In ArchUnitTest assertion calls are not required as they are called by the library -->
In ArchUnitTest, ArchUnitCyclesCheckTest assertion calls are not required as they are
called by the library -->
<property name="violationSuppressXPath"
value="//ClassOrInterfaceDeclaration[@SimpleName='AllChecksTest'
or @SimpleName='AstRegressionTest'
Expand All @@ -133,6 +134,8 @@
| //ClassOrInterfaceDeclaration[starts-with(@SimpleName,'XpathRegression')]
//MethodDeclaration
| //ClassOrInterfaceDeclaration[@SimpleName='ArchUnitTest']
//MethodDeclaration
| //ClassOrInterfaceDeclaration[@SimpleName='ArchUnitCyclesCheckTest']
//MethodDeclaration"/>
</properties>
</rule>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,7 @@
<exclude>markdownlint.rb</exclude>
<exclude>signatures.txt</exclude>
<exclude>signatures-test.txt</exclude>
<exclude>archunit-store/**</exclude>
</excludes>
</validationSet>
<validationSet>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
///////////////////////////////////////////////////////////////////////////////////////////////

package com.puppycrawl.tools.checkstyle.internal;

import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;

import org.junit.jupiter.api.Test;

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.library.freeze.FreezingArchRule;

public class ArchUnitCyclesCheckTest {

@Test
public void testSlicesShouldBeFreeOfCycle() {
final JavaClasses importedClasses = new ClassFileImporter()
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS)
.importPackages("com.puppycrawl.tools.checkstyle");

final ArchRule slicesShouldBeFreeOfCycle = FreezingArchRule.freeze(
slices()
.matching("com.puppycrawl.tools.checkstyle.(**)")
.should().beFreeOfCycles());

slicesShouldBeFreeOfCycle.check(importedClasses);
}
}
13 changes: 13 additions & 0 deletions src/test/resources/archunit.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This will limit the maximum number of dependencies to report per cycle edge.
# Note that ArchUnit will regardless always analyze all dependencies to detect cycles,
# so this purely affects how many dependencies will be printed in the report.
# Also note that this number will quickly affect the required heap since it scales with number.
# of edges and number of cycles
# default is 20
cycles.maxNumberOfDependenciesPerEdge=550

# This will limit the maximum number of cycles to detect and thus required CPU and heap.
# default is 100
cycles.maxNumberToDetect=200

freeze.store.default.path=config/archunit-store

0 comments on commit c7ee98b

Please sign in to comment.