Skip to content

Commit

Permalink
#33 "source code style rule definition"
Browse files Browse the repository at this point in the history
- code was already cycle-free on package level
- fixed 'layering' (no dependencies between a package and any of its (sub-)sub-packages)
-- all test gap analysis related classes are now in analysis package (or a sub-package)
-- created a new top-level package for DebugCoverageResolution
  • Loading branch information
janScheible committed Jun 24, 2022
1 parent 93a58cc commit 880ab72
Show file tree
Hide file tree
Showing 16 changed files with 139 additions and 26 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,8 @@ Unresolvable methods (no coverage information available):
1. exception: completely stateless `*Utils` classes with static methods only
1. utility classes must be `abstract` and have a private default constructor
1. exception: real constants with names in upper case delimited by underscores
1. `logger` has to be protected final but not static: `protected final Logger logger = LoggerFactory.getLogger(getClass());`
1. `logger` has to be protected final but not static: `protected final Logger logger = LoggerFactory.getLogger(getClass());` (see https://www.slf4j.org/faq.html#declared_static)
1. restrict file, method and lambda lengths to reasonable values
1. code dependency
1. no code cycles on package level
1. no dependencies between a package and any of its (sub-)sub-packages (only the other way around)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.scheible.testgapanalysis.maven;

import com.scheible.testgapanalysis.DebugCoverageResolution;
import com.scheible.testgapanalysis.DebugCoverageResolutionReport;
import com.scheible.testgapanalysis.debug.DebugCoverageResolution;
import com.scheible.testgapanalysis.debug.DebugCoverageResolutionReport;
import com.scheible.testgapanalysis.jacoco.JaCoCoReportParser;
import com.scheible.testgapanalysis.parser.JavaParser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.scheible.testgapanalysis.TestGapAnalysis;
import com.scheible.testgapanalysis.TestGapReport;
import com.scheible.testgapanalysis.analysis.testgap.TestGapAnalysis;
import com.scheible.testgapanalysis.analysis.testgap.TestGapReport;
import com.scheible.testgapanalysis.analysis.Analysis;
import com.scheible.testgapanalysis.git.GitDiffer;
import com.scheible.testgapanalysis.jacoco.JaCoCoReportParser;
Expand Down
10 changes: 10 additions & 0 deletions test-gap-analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
<artifactId>archunit</artifactId>
<version>${archunit.version}</version>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit4</artifactId>
<version>${archunit.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jgit</groupId>
Expand Down Expand Up @@ -163,6 +168,11 @@
<artifactId>archunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit4</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.scheible.pocketsaw.impl</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.analysis.testgap;

import static java.util.Collections.emptySet;

Expand All @@ -12,12 +12,12 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.scheible.testgapanalysis.TestGapReport.CoverageReportMethod;
import com.scheible.testgapanalysis.TestGapReport.NewOrChangedFile;
import com.scheible.testgapanalysis.TestGapReport.NewOrChangedFile.State;
import com.scheible.testgapanalysis.TestGapReport.TestGapMethod;
import com.scheible.testgapanalysis.analysis.Analysis;
import com.scheible.testgapanalysis.analysis.AnalysisResult;
import com.scheible.testgapanalysis.analysis.testgap.TestGapReport.CoverageReportMethod;
import com.scheible.testgapanalysis.analysis.testgap.TestGapReport.NewOrChangedFile;
import com.scheible.testgapanalysis.analysis.testgap.TestGapReport.NewOrChangedFile.State;
import com.scheible.testgapanalysis.analysis.testgap.TestGapReport.TestGapMethod;
import com.scheible.testgapanalysis.common.FilesUtils;
import com.scheible.testgapanalysis.git.GitDiffer;
import com.scheible.testgapanalysis.git.RepositoryStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.analysis.testgap;

import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.debug;

import java.io.File;
import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.debug;

import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.scheible.testgapanalysis;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;

import org.junit.runner.RunWith;

import com.tngtech.archunit.core.domain.Dependency;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.importer.ImportOption.DoNotIncludeTests;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import com.tngtech.archunit.library.dependencies.SliceAssignment;
import com.tngtech.archunit.library.dependencies.SliceIdentifier;

/**
*
* @author sj
*/
@RunWith(ArchUnitRunner.class)
@AnalyzeClasses(packagesOf = CodeDependenciesTest.class, importOptions = DoNotIncludeTests.class)
public class CodeDependenciesTest {

private static class SlicePerPackage implements SliceAssignment {

@Override
public SliceIdentifier getIdentifierOf(final JavaClass javaClass) {
return SliceIdentifier.of(javaClass.getPackageName());
}

@Override
public String getDescription() {
return "Every package is treated as a slice.";
}
}

@ArchTest
static final ArchRule noPackageCyclesRule = slices().assignedFrom(new SlicePerPackage()).should().beFreeOfCycles();

private static class DependOnDescendantPackagesCondition extends ArchCondition<JavaClass> {

DependOnDescendantPackagesCondition() {
super("depend on descendant packages");
}

@Override
public void check(JavaClass clazz, ConditionEvents events) {
for (Dependency dependency : clazz.getDirectDependenciesFromSelf()) {
boolean dependencyOnDescendantPackage = isDependencyOnDescendantPackage(dependency.getOriginClass(),
dependency.getTargetClass());
events.add(new SimpleConditionEvent(dependency, dependencyOnDescendantPackage,
dependency.getDescription()));
}
}

private boolean isDependencyOnDescendantPackage(JavaClass origin, JavaClass target) {
String originPackageName = origin.getPackageName();
String targetSubPackagePrefix = target.getPackageName();
return targetSubPackagePrefix.contains(originPackageName + ".");
}
}

@ArchTest
static final ArchRule packageLayeringRule = noClasses().should(new DependOnDescendantPackagesCondition())
.because("lower packages shouldn't build on higher packages");
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package com.scheible.testgapanalysis;

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.ExternalFunctionalities.Slf4j;
import com.scheible.testgapanalysis.analysis.AnalysisSubModule;
import com.scheible.testgapanalysis.common.CommonSubModule;
import com.scheible.testgapanalysis.git.GitSubModule;
import com.scheible.testgapanalysis.jacoco.JaCoCoSubModule;
import com.scheible.testgapanalysis.jacoco.resolver.JaCoCoResolverSubModule;
import com.scheible.testgapanalysis.parser.ParserSubModule;

/**
*
* @author sj
*/
@SubModule(includeSubPackages = false, uses = {AnalysisSubModule.class, JaCoCoSubModule.class, ParserSubModule.class,
JaCoCoResolverSubModule.class, Slf4j.class, GitSubModule.class, CommonSubModule.class})
@SubModule(includeSubPackages = false)
public class TestGapAnalysisSubModule {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.ExternalFunctionalities.Slf4j;
import com.scheible.testgapanalysis.common.CommonSubModule;
import com.scheible.testgapanalysis.git.GitSubModule;
import com.scheible.testgapanalysis.jacoco.JaCoCoSubModule;
import com.scheible.testgapanalysis.jacoco.resolver.JaCoCoResolverSubModule;
Expand All @@ -12,7 +13,7 @@
* @author sj
*/
@SubModule(uses = {JaCoCoSubModule.class, JaCoCoResolverSubModule.class, GitSubModule.class, ParserSubModule.class,
Slf4j.class})
Slf4j.class, CommonSubModule.class})
public class AnalysisSubModule {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.scheible.testgapanalysis.analysis.testgap;

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.ExternalFunctionalities.Slf4j;
import com.scheible.testgapanalysis.analysis.*;
import com.scheible.testgapanalysis.common.CommonSubModule;
import com.scheible.testgapanalysis.git.GitSubModule;
import com.scheible.testgapanalysis.jacoco.JaCoCoSubModule;
import com.scheible.testgapanalysis.jacoco.resolver.JaCoCoResolverSubModule;
import com.scheible.testgapanalysis.parser.ParserSubModule;

/**
*
* @author sj
*/
@SubModule(uses = {JaCoCoSubModule.class, JaCoCoResolverSubModule.class, GitSubModule.class, ParserSubModule.class,
Slf4j.class, CommonSubModule.class})
public class AnalysisTestGapSubModule {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.analysis.testgap;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.analysis.testgap;

import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.scheible.testgapanalysis;
package com.scheible.testgapanalysis.debug;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.scheible.testgapanalysis.debug;

import com.scheible.pocketsaw.api.SubModule;
import com.scheible.testgapanalysis.common.CommonSubModule;
import com.scheible.testgapanalysis.jacoco.JaCoCoSubModule;
import com.scheible.testgapanalysis.jacoco.resolver.JaCoCoResolverSubModule;
import com.scheible.testgapanalysis.parser.ParserSubModule;

/**
*
* @author sj
*/
@SubModule(uses = {JaCoCoSubModule.class, ParserSubModule.class, JaCoCoResolverSubModule.class, CommonSubModule.class})
public class DebugSubModule {

}

0 comments on commit 880ab72

Please sign in to comment.