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:
Oliver Drotbohm
2023-03-31 13:34:21 +02:00
parent 6d2fdd6fdc
commit 2454ecbc4b
13 changed files with 266 additions and 155 deletions

View 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 {}

View 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.api;
/**
* @author Oliver Drotbohm
*/
public interface ApiType {}

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.NamedInterface
package example.ni.api;

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.ni.internal;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface({ "spi", "kpi" })
public interface AdditionalSpiType {}

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.internal;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface
public interface DefaultedNamedInterfaceType {
}

View 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.spi;
/**
* @author Oliver Drotbohm
*/
public interface SpiType {}

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.NamedInterface
package example.ni.spi;

View File

@@ -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);
});
}
}

View File

@@ -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() + "..")));
}
}