GH-177 - Named interfaces names now default to the local package name.
Both package- and type-level declarations now use the local package name as the named interface's name. This allows to, at the same time, easily declared named interfaces based on packages but also a nice decoupling of the interface definition and the package layout as individual types can be assigned to such interfaces no matter where they are actually declared.
This commit is contained in:
21
spring-modulith-core/src/test/java/example/ni/RootType.java
Normal file
21
spring-modulith-core/src/test/java/example/ni/RootType.java
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface RootType {}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.api;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface ApiType {}
|
||||
@@ -0,0 +1,2 @@
|
||||
@org.springframework.modulith.NamedInterface
|
||||
package example.ni.api;
|
||||
@@ -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.ni.internal;
|
||||
|
||||
import org.springframework.modulith.NamedInterface;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@NamedInterface({ "spi", "kpi" })
|
||||
public interface AdditionalSpiType {}
|
||||
@@ -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.internal;
|
||||
|
||||
import org.springframework.modulith.NamedInterface;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@NamedInterface
|
||||
public interface DefaultedNamedInterfaceType {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* 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.spi;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
public interface SpiType {}
|
||||
@@ -0,0 +1,2 @@
|
||||
@org.springframework.modulith.NamedInterface
|
||||
package example.ni.spi;
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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 org.springframework.modulith.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
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 java.util.Arrays;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.tngtech.archunit.core.domain.JavaClass;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link NamedInterfaces}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
class NamedInterfacesUnitTests {
|
||||
|
||||
@Test
|
||||
void discoversNamedInterfaces() {
|
||||
|
||||
var classes = TestUtils.getClasses(RootType.class);
|
||||
var javaPackage = JavaPackage.of(classes, RootType.class.getPackageName());
|
||||
|
||||
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, "spi", SpiType.class, AdditionalSpiType.class);
|
||||
assertInterfaceContains(interfaces, "kpi", AdditionalSpiType.class);
|
||||
assertInterfaceContains(interfaces, "internal", DefaultedNamedInterfaceType.class);
|
||||
}
|
||||
|
||||
private static void assertInterfaceContains(NamedInterfaces interfaces, String name, Class<?>... types) {
|
||||
|
||||
var classNames = Arrays.stream(types).map(Class::getName).toArray(String[]::new);
|
||||
|
||||
assertThat(interfaces.getByName(name)).hasValueSatisfying(it -> {
|
||||
assertThat(it).map(JavaClass::getName).containsExactlyInAnyOrder(classNames);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,8 @@ class TestUtils {
|
||||
|
||||
Assert.notNull(packageType, "Package type must not be null!");
|
||||
|
||||
return getClasses().that(resideInAPackage(packageType.getPackage().getName()));
|
||||
return Classes.of(new ClassFileImporter()
|
||||
.importPackagesOf(packageType)
|
||||
.that(resideInAPackage(packageType.getPackage().getName() + "..")));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user