GH-183 - Improvements in named interface declarations.

Type based named interfaces on types declared in a module's API package still caused the type to be included in the unnamed interface. This is now fixed by explicitly removing named interface types from the unnamed interface.

We now also detect API package types assigned to a named interface without an explicit name as the package name defaulting doesn't work in this case.

Furthermore, named interfaces are now sorted alphabetically to make the unnamed one always appear first.
This commit is contained in:
Oliver Drotbohm
2023-04-23 23:21:00 +02:00
parent 0e6c41b57c
commit d9f298c96a
7 changed files with 140 additions and 22 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2023 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 example.ni;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface("api")
public interface AnnotatedNamedInterfaceType {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2023 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 example.ninvalid;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface
public interface InvalidDefaultNamedInterface {}

View File

@@ -17,11 +17,13 @@ package org.springframework.modulith.core;
import static org.assertj.core.api.Assertions.*;
import example.ni.AnnotatedNamedInterfaceType;
import example.ni.RootType;
import example.ni.api.ApiType;
import example.ni.internal.AdditionalSpiType;
import example.ni.internal.DefaultedNamedInterfaceType;
import example.ni.spi.SpiType;
import example.ninvalid.InvalidDefaultNamedInterface;
import java.util.Arrays;
@@ -39,21 +41,29 @@ class NamedInterfacesUnitTests {
@Test
void discoversNamedInterfaces() {
var classes = TestUtils.getClasses(RootType.class);
var javaPackage = JavaPackage.of(classes, RootType.class.getPackageName());
var javaPackage = TestUtils.getPackage(RootType.class);
var interfaces = NamedInterfaces.discoverNamedInterfaces(javaPackage);
assertThat(interfaces).map(NamedInterface::getName)
.containsExactlyInAnyOrder(NamedInterface.UNNAMED_NAME, "api", "spi", "kpi", "internal");
assertInterfaceContains(interfaces, NamedInterface.UNNAMED_NAME, RootType.class);
assertInterfaceContains(interfaces, "api", ApiType.class);
assertInterfaceContains(interfaces, "api", ApiType.class, AnnotatedNamedInterfaceType.class);
assertInterfaceContains(interfaces, "spi", SpiType.class, AdditionalSpiType.class);
assertInterfaceContains(interfaces, "kpi", AdditionalSpiType.class);
assertInterfaceContains(interfaces, "internal", DefaultedNamedInterfaceType.class);
}
@Test // GH-183
void rejectsDefaultingNamedInterfaceTypeInBasePackage() {
var javaPackage = TestUtils.getPackage(InvalidDefaultNamedInterface.class);
assertThatIllegalStateException().isThrownBy(() -> NamedInterfaces.discoverNamedInterfaces(javaPackage))
.withMessageContaining("named interface defaulting")
.withMessageContaining(InvalidDefaultNamedInterface.class.getSimpleName());
}
private static void assertInterfaceContains(NamedInterfaces interfaces, String name, Class<?>... types) {
var classNames = Arrays.stream(types).map(Class::getName).toArray(String[]::new);

View File

@@ -70,4 +70,8 @@ class TestUtils {
.importPackagesOf(packageType)
.that(resideInAPackage(packageType.getPackage().getName() + "..")));
}
public static JavaPackage getPackage(Class<?> packageType) {
return JavaPackage.of(TestUtils.getClasses(packageType), packageType.getPackageName());
}
}