|
15 | 15 | */ |
16 | 16 | package org.springframework.data.r2dbc; |
17 | 17 |
|
18 | | -import static de.schauderhaft.degraph.check.JCheck.*; |
19 | | -import static org.junit.Assert.*; |
20 | | - |
21 | | -import de.schauderhaft.degraph.check.JCheck; |
22 | | -import de.schauderhaft.degraph.configuration.NamedPattern; |
23 | | -import scala.runtime.AbstractFunction1; |
24 | | - |
25 | | -import org.junit.Assume; |
| 18 | +import org.assertj.core.api.SoftAssertions; |
26 | 19 | import org.junit.jupiter.api.Disabled; |
27 | 20 | import org.junit.jupiter.api.Test; |
28 | 21 |
|
| 22 | +import com.tngtech.archunit.base.DescribedPredicate; |
| 23 | +import com.tngtech.archunit.core.domain.JavaClass; |
| 24 | +import com.tngtech.archunit.core.domain.JavaClasses; |
| 25 | +import com.tngtech.archunit.core.importer.ClassFileImporter; |
| 26 | +import com.tngtech.archunit.core.importer.ImportOption; |
| 27 | +import com.tngtech.archunit.lang.ArchRule; |
| 28 | +import com.tngtech.archunit.library.dependencies.SliceAssignment; |
| 29 | +import com.tngtech.archunit.library.dependencies.SliceIdentifier; |
| 30 | +import com.tngtech.archunit.library.dependencies.SlicesRuleDefinition; |
| 31 | + |
29 | 32 | /** |
30 | 33 | * Test package dependencies for violations. |
31 | 34 | * |
|
35 | 38 | public class DependencyTests { |
36 | 39 |
|
37 | 40 | @Test // DATAJDBC-114 |
38 | | - public void cycleFree() { |
39 | | - |
40 | | - Assume.assumeThat( // |
41 | | - classpath() // |
42 | | - .noJars() // |
43 | | - .including("org.springframework.data.jdbc.**") // |
44 | | - .including("org.springframework.data.relational.**") // |
45 | | - .including("org.springframework.data.r2dbc.**") // |
46 | | - .filterClasspath("*target/classes") // exclude test code |
47 | | - .withSlicing("modules", "org.springframework.data.(*).**").printOnFailure("degraph.graphml"), |
48 | | - JCheck.violationFree()); |
| 41 | + void cycleFree() { |
| 42 | + |
| 43 | + JavaClasses importedClasses = new ClassFileImporter() // |
| 44 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) // |
| 45 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS) // we just analyze the code of this module. |
| 46 | + .importPackages("org.springframework.data.r2dbc").that( // |
| 47 | + onlySpringData() // |
| 48 | + ); |
| 49 | + |
| 50 | + ArchRule rule = SlicesRuleDefinition.slices() // |
| 51 | + .matching("org.springframework.data.r2dbc.(**)") // |
| 52 | + .should() // |
| 53 | + .beFreeOfCycles(); |
| 54 | + |
| 55 | + rule.check(importedClasses); |
49 | 56 | } |
50 | 57 |
|
51 | 58 | @Test // DATAJDBC-220 |
52 | | - public void acrossModules() { |
53 | | - |
54 | | - assertThat( // |
55 | | - classpath() // |
56 | | - // include only Spring Data related classes (for example no JDK code) |
57 | | - .including("org.springframework.data.**") // |
58 | | - .filterClasspath(new AbstractFunction1<String, Object>() { |
59 | | - @Override |
60 | | - public Object apply(String s) { // |
61 | | - // only the current module + commons |
62 | | - return s.endsWith("target/classes") || s.contains("spring-data-commons"); |
63 | | - } |
64 | | - }) // exclude test code |
65 | | - .withSlicing("sub-modules", // sub-modules are defined by any of the following pattern. |
66 | | - "org.springframework.data.jdbc.(**).*", // |
67 | | - "org.springframework.data.relational.(**).*", // |
68 | | - new NamedPattern("org.springframework.data.r2dbc.**", "repository.reactive"), // |
69 | | - "org.springframework.data.(**).*") // |
70 | | - .printTo("degraph-across-modules.graphml"), // writes a graphml to this location |
71 | | - JCheck.violationFree()); |
| 59 | + void acrossModules() { |
| 60 | + |
| 61 | + JavaClasses importedClasses = new ClassFileImporter().withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) |
| 62 | + .importPackages( // |
| 63 | + "org.springframework.data.r2dbc", // Spring Data Relational |
| 64 | + "org.springframework.data.relational", // Spring Data Relational |
| 65 | + "org.springframework.data" // Spring Data Commons |
| 66 | + ).that(onlySpringData()) // |
| 67 | + .that(ignorePackage("org.springframework.data.aot.hint")) // ignoring aot, since it causes cycles in commons |
| 68 | + .that(ignorePackage("org.springframework.data.aot")); // ignoring aot, since it causes cycles in commons |
| 69 | + |
| 70 | + ArchRule rule = SlicesRuleDefinition.slices() // |
| 71 | + .assignedFrom(subModuleSlicing()) // |
| 72 | + .should().beFreeOfCycles(); |
| 73 | + |
| 74 | + rule.check(importedClasses); |
| 75 | + } |
| 76 | + |
| 77 | + @Test // GH-1058 |
| 78 | + void testGetFirstPackagePart() { |
| 79 | + |
| 80 | + SoftAssertions.assertSoftly(softly -> { |
| 81 | + softly.assertThat(getFirstPackagePart("a.b.c")).isEqualTo("a"); |
| 82 | + softly.assertThat(getFirstPackagePart("a")).isEqualTo("a"); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private DescribedPredicate<JavaClass> onlySpringData() { |
| 87 | + |
| 88 | + return new DescribedPredicate<>("Spring Data Classes") { |
| 89 | + @Override |
| 90 | + public boolean test(JavaClass input) { |
| 91 | + return input.getPackageName().startsWith("org.springframework.data"); |
| 92 | + } |
| 93 | + }; |
| 94 | + } |
| 95 | + |
| 96 | + private DescribedPredicate<JavaClass> ignore(Class<?> type) { |
| 97 | + |
| 98 | + return new DescribedPredicate<>("ignored class " + type.getName()) { |
| 99 | + @Override |
| 100 | + public boolean test(JavaClass input) { |
| 101 | + return !input.getFullName().startsWith(type.getName()); |
| 102 | + } |
| 103 | + }; |
72 | 104 | } |
73 | 105 |
|
| 106 | + private DescribedPredicate<JavaClass> ignorePackage(String type) { |
| 107 | + |
| 108 | + return new DescribedPredicate<>("ignored class " + type) { |
| 109 | + @Override |
| 110 | + public boolean test(JavaClass input) { |
| 111 | + return !input.getPackageName().equals(type); |
| 112 | + } |
| 113 | + }; |
| 114 | + } |
| 115 | + |
| 116 | + private String getFirstPackagePart(String subpackage) { |
| 117 | + |
| 118 | + int index = subpackage.indexOf("."); |
| 119 | + if (index < 0) { |
| 120 | + return subpackage; |
| 121 | + } |
| 122 | + return subpackage.substring(0, index); |
| 123 | + } |
| 124 | + |
| 125 | + private String subModule(String basePackage, String packageName) { |
| 126 | + |
| 127 | + if (packageName.startsWith(basePackage) && packageName.length() > basePackage.length()) { |
| 128 | + |
| 129 | + final int index = basePackage.length() + 1; |
| 130 | + String subpackage = packageName.substring(index); |
| 131 | + return getFirstPackagePart(subpackage); |
| 132 | + } |
| 133 | + return ""; |
| 134 | + } |
| 135 | + |
| 136 | + private SliceAssignment subModuleSlicing() { |
| 137 | + return new SliceAssignment() { |
| 138 | + |
| 139 | + @Override |
| 140 | + public SliceIdentifier getIdentifierOf(JavaClass javaClass) { |
| 141 | + |
| 142 | + String packageName = javaClass.getPackageName(); |
| 143 | + |
| 144 | + String subModule = subModule("org.springframework.data.jdbc", packageName); |
| 145 | + if (!subModule.isEmpty()) { |
| 146 | + return SliceIdentifier.of(subModule); |
| 147 | + } |
| 148 | + |
| 149 | + subModule = subModule("org.springframework.data.relational", packageName); |
| 150 | + if (!subModule.isEmpty()) { |
| 151 | + return SliceIdentifier.of(subModule); |
| 152 | + } |
| 153 | + |
| 154 | + subModule = subModule("org.springframework.data", packageName); |
| 155 | + if (!subModule.isEmpty()) { |
| 156 | + return SliceIdentifier.of(subModule); |
| 157 | + } |
| 158 | + |
| 159 | + return SliceIdentifier.ignore(); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public String getDescription() { |
| 164 | + return "Submodule"; |
| 165 | + } |
| 166 | + }; |
| 167 | + } |
74 | 168 | } |
0 commit comments