GH-668 - Dependency checks now explicitly skip module-internal dependencies.

As we process a type's entire type hierarchy for dependencies we might discover a foreign module's internal dependencies for modules that declare allowed dependencies explicitly. Explicitly declared dependencies so far solely verified the target being explicitly listed, which, for internal dependencies does not make sense. We now immediately start checking the source and target modules to be equivalent, in which case we can skip any further processing.
This commit is contained in:
Oliver Drotbohm
2024-06-18 12:29:04 +02:00
parent d763ee0b7a
commit c82e0ffc3b
8 changed files with 115 additions and 3 deletions

View File

@@ -968,9 +968,13 @@ public class ApplicationModule {
var originModule = getExistingModuleOf(source, modules);
var targetModule = getExistingModuleOf(target, modules);
var violations = Violations.NONE;
DeclaredDependencies declaredDependencies = originModule.getDeclaredDependencies(modules);
Violations violations = Violations.NONE;
if (originModule.equals(targetModule)) {
return violations;
}
var declaredDependencies = originModule.getDeclaredDependencies(modules);
// Check explicitly defined allowed targets
if (!declaredDependencies.isAllowedDependency(target)) {

View File

@@ -17,6 +17,7 @@ package org.springframework.modulith.core;
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.*;
import java.util.List;
import java.util.function.Supplier;
import org.jmolecules.ddd.annotation.AggregateRoot;
@@ -28,13 +29,14 @@ 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;
/**
* Utilities for testing.
*
* @author Oliver Drotbohm
*/
class TestUtils {
public class TestUtils {
private static Supplier<JavaClasses> imported = SingletonSupplier.of(() -> new ClassFileImporter() //
.importPackagesOf(ApplicationModules.class, Repository.class, AggregateRoot.class));
@@ -92,4 +94,9 @@ class TestUtils {
return Classes.of(new ClassFileImporter()
.importPackages(packageName));
}
public static ApplicationModules of(String basePackage, String... ignoredPackages) {
return new ApplicationModules(ModulithMetadata.of(basePackage), List.of(basePackage),
JavaClass.Predicates.resideInAnyPackage(ignoredPackages), false, new ImportOption.OnlyIncludeTests());
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2024 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 reproducers.gh660.first;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.TestUtils;
/**
* @author Oliver Drotbohm
*/
class Gh660Tests {
@Test // GH-660
void doesNotRejectInternalDependencies() {
TestUtils.of("reproducers.gh660").verify();
}
}

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.ApplicationModule(allowedDependencies = {})
package reproducers.gh660.first;

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2024 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 reproducers.gh660.first.repository;
/**
* @author Oliver Drotbohm
*/
public class Product {}

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2024 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 reproducers.gh660.first.repository;
/**
* @author Oliver Drotbohm
*/
public interface ProductRepository {
Product save(Product product);
}

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.NamedInterface
package reproducers.gh660.first.repository;

View File

@@ -0,0 +1,23 @@
/*
* Copyright 2024 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 reproducers.gh660.second;
import reproducers.gh660.first.repository.ProductRepository;
/**
* @author Oliver Drotbohm
*/
public interface ExtendingRepository extends ProductRepository {}