-
Notifications
You must be signed in to change notification settings - Fork 356
Reimplemented dependency tests using ArchUnit. #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright 2017-2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc; | ||
|
||
import org.assertj.core.api.SoftAssertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.data.auditing.config.AuditingHandlerBeanDefinitionParser; | ||
|
||
import com.tngtech.archunit.base.DescribedPredicate; | ||
import com.tngtech.archunit.core.domain.JavaClass; | ||
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.dependencies.SliceAssignment; | ||
import com.tngtech.archunit.library.dependencies.SliceIdentifier; | ||
import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition; | ||
|
||
/** | ||
* Test package dependencies for violations. | ||
* | ||
* @author Jens Schauder | ||
*/ | ||
public class DependencyTests { | ||
|
||
@Test | ||
void cycleFree() { | ||
|
||
JavaClasses importedClasses = new ClassFileImporter() // | ||
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) // | ||
.withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS) // we just analyze the code of this module. | ||
.importPackages("org.springframework.data.jdbc"); | ||
|
||
final JavaClasses relevantClasses = importedClasses.that( // | ||
onlySpringData() // | ||
); | ||
|
||
ArchRule rule = SlicesRuleDefinition.slices() // | ||
.matching("org.springframework.data.jdbc.(**)") // | ||
.should() // | ||
.beFreeOfCycles(); | ||
|
||
rule.check(relevantClasses); | ||
} | ||
|
||
@Test | ||
void acrossModules() { | ||
|
||
JavaClasses importedClasses = new ClassFileImporter().withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) | ||
.importPackages( // | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this consistent with the previous test. |
||
"org.springframework.data.jdbc", // Spring Data Relational | ||
"org.springframework.data.relational", // Spring Data Relational | ||
"org.springframework.data" // Spring Data Commons | ||
).that(onlySpringData() // | ||
.and(ignore(AuditingHandlerBeanDefinitionParser.class))); | ||
|
||
ArchRule rule = SlicesRuleDefinition.slices() // | ||
.assignedFrom(subModuleSlicing()) // | ||
.should().beFreeOfCycles(); | ||
|
||
rule.check(importedClasses); | ||
} | ||
|
||
@Test // GH-1058 | ||
void testGetFirstPackagePart() { | ||
|
||
SoftAssertions.assertSoftly(softly -> { | ||
softly.assertThat(getFirstPackagePart("a.b.c")).isEqualTo("a"); | ||
softly.assertThat(getFirstPackagePart("a")).isEqualTo("a"); | ||
}); | ||
} | ||
|
||
@Test // GH-1058 | ||
void testSubModule() { | ||
|
||
SoftAssertions.assertSoftly(softly -> { | ||
softly.assertThat(subModule("a.b", "a.b.c.d")).isEqualTo("c"); | ||
softly.assertThat(subModule("a.b", "a.b.c")).isEqualTo("c"); | ||
softly.assertThat(subModule("a.b", "a.b")).isEqualTo(""); | ||
}); | ||
} | ||
|
||
private DescribedPredicate<JavaClass> onlySpringData() { | ||
|
||
return new DescribedPredicate<>("Spring Data Classes") { | ||
@Override | ||
public boolean apply(JavaClass input) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need the blank line if there is only one line in the method? |
||
return input.getPackageName().startsWith("org.springframework.data"); | ||
} | ||
}; | ||
} | ||
|
||
private DescribedPredicate<JavaClass> ignore(Class<?> type) { | ||
|
||
return new DescribedPredicate<>("ignored class " + type.getName()) { | ||
@Override | ||
public boolean apply(JavaClass input) { | ||
return !input.getFullName().startsWith(type.getName()); | ||
} | ||
}; | ||
} | ||
|
||
private String getFirstPackagePart(String subpackage) { | ||
|
||
int index = subpackage.indexOf("."); | ||
if (index < 0) { | ||
return subpackage; | ||
} | ||
return subpackage.substring(0, index); | ||
} | ||
|
||
private String subModule(String basePackage, String packageName) { | ||
|
||
if (packageName.startsWith(basePackage) && packageName.length() > basePackage.length()) { | ||
final int index = basePackage.length() + 1; | ||
String subpackage = packageName.substring(index); | ||
return getFirstPackagePart(subpackage); | ||
} | ||
return ""; | ||
} | ||
|
||
private SliceAssignment subModuleSlicing() { | ||
return new SliceAssignment() { | ||
|
||
@Override | ||
public SliceIdentifier getIdentifierOf(JavaClass javaClass) { | ||
|
||
String packageName = javaClass.getPackageName(); | ||
|
||
String subModule = subModule("org.springframework.data.jdbc", packageName); | ||
if (!subModule.isEmpty()) { | ||
return SliceIdentifier.of(subModule); | ||
} | ||
|
||
subModule = subModule("org.springframework.data.relational", packageName); | ||
if (!subModule.isEmpty()) { | ||
return SliceIdentifier.of(subModule); | ||
} | ||
|
||
subModule = subModule("org.springframework.data", packageName); | ||
if (!subModule.isEmpty()) { | ||
return SliceIdentifier.of(subModule); | ||
} | ||
|
||
return SliceIdentifier.ignore(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Submodule"; | ||
} | ||
}; | ||
} | ||
|
||
} |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you prefer keeping all the versions as property settings at the top?