|
| 1 | +/* |
| 2 | + * Copyright 2017-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jdbc; |
| 17 | + |
| 18 | +import org.assertj.core.api.SoftAssertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.data.auditing.config.AuditingHandlerBeanDefinitionParser; |
| 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 | + |
| 32 | +/** |
| 33 | + * Test package dependencies for violations. |
| 34 | + * |
| 35 | + * @author Jens Schauder |
| 36 | + */ |
| 37 | +public class DependencyTests { |
| 38 | + |
| 39 | + @Test |
| 40 | + void cycleFree() { |
| 41 | + |
| 42 | + JavaClasses importedClasses = new ClassFileImporter() // |
| 43 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) // |
| 44 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_JARS) // we just analyze the code of this module. |
| 45 | + .importPackages("org.springframework.data.jdbc") |
| 46 | + .that( // |
| 47 | + onlySpringData() // |
| 48 | + ); |
| 49 | + |
| 50 | + ArchRule rule = SlicesRuleDefinition.slices() // |
| 51 | + .matching("org.springframework.data.jdbc.(**)") // |
| 52 | + .should() // |
| 53 | + .beFreeOfCycles(); |
| 54 | + |
| 55 | + rule.check(importedClasses); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void acrossModules() { |
| 60 | + |
| 61 | + JavaClasses importedClasses = new ClassFileImporter() |
| 62 | + .withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS) |
| 63 | + .importPackages( // |
| 64 | + "org.springframework.data.jdbc", // Spring Data Relational |
| 65 | + "org.springframework.data.relational", // Spring Data Relational |
| 66 | + "org.springframework.data" // Spring Data Commons |
| 67 | + ).that(onlySpringData()) // |
| 68 | + .that(ignore(AuditingHandlerBeanDefinitionParser.class)); |
| 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 | + @Test // GH-1058 |
| 87 | + void testSubModule() { |
| 88 | + |
| 89 | + SoftAssertions.assertSoftly(softly -> { |
| 90 | + softly.assertThat(subModule("a.b", "a.b.c.d")).isEqualTo("c"); |
| 91 | + softly.assertThat(subModule("a.b", "a.b.c")).isEqualTo("c"); |
| 92 | + softly.assertThat(subModule("a.b", "a.b")).isEqualTo(""); |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + private DescribedPredicate<JavaClass> onlySpringData() { |
| 97 | + |
| 98 | + return new DescribedPredicate<>("Spring Data Classes") { |
| 99 | + @Override |
| 100 | + public boolean apply(JavaClass input) { |
| 101 | + return input.getPackageName().startsWith("org.springframework.data"); |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + private DescribedPredicate<JavaClass> ignore(Class<?> type) { |
| 107 | + |
| 108 | + return new DescribedPredicate<>("ignored class " + type.getName()) { |
| 109 | + @Override |
| 110 | + public boolean apply(JavaClass input) { |
| 111 | + return !input.getFullName().startsWith(type.getName()); |
| 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 | + } |
| 168 | + |
| 169 | +} |
0 commit comments