Skip to content

Commit

Permalink
GH-520 - Early reject invalid ApplicationModules bootstraps.
Browse files Browse the repository at this point in the history
We now immediately reject the ApplicationModules bootstrap in case the initial scanning of the root packages yield no classes at all.
  • Loading branch information
odrotbohm committed Mar 5, 2024
1 parent c45a0fc commit e41d7ec
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ protected ApplicationModules(ModulithMetadata metadata, Collection<String> packa
.importPackages(packages) //
.that(not(ignored.or(IS_AOT_TYPE).or(IS_SPRING_CGLIB_PROXY)));

Assert.notEmpty(allClasses, () -> "No classes found in packages %s!".formatted(packages));

Classes classes = Classes.of(allClasses);

this.modules = packages.stream() //
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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 org.springframework.modulith.core;

/**
* Factory interface to create {@link ApplicationModules} instances for application classes. The default version will
* simply delegate to {@link ApplicationModules#of(Class)} which will only look at production classes. Our test support
* provides an alternative implementation to bootstrap an {@link ApplicationModules} instance from test types as well,
* primarily for our very own integration test purposes.
*
* @author Oliver Drotbohm
* @since 1.2
*/
public interface ApplicationModulesFactory {

/**
* Returns the {@link ApplicationModules} instance for the given application class.
*
* @param applicationClass must not be {@literal null}.
* @return will never be {@literal null}.
*/
ApplicationModules of(Class<?> applicationClass);

/**
* Creates the default {@link ApplicationModulesFactory} delegating to {@link ApplicationModules#of(Class)}
*
* @return will never be {@literal null}.
*/
public static ApplicationModulesFactory defaultFactory() {
return ApplicationModules::of;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 example.empty;

import org.springframework.modulith.Modulithic;

/**
* @author Oliver Drotbohm
*/
@Modulithic
public class EmptyApplication {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 example.empty;

import org.springframework.modulith.Modulithic;

/**
* @author Oliver Drotbohm
*/
@Modulithic
public class EmptyApplication {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import example.declared.fourth.Fourth;
import example.declared.second.Second;
import example.declared.third.Third;
import example.empty.EmptyApplication;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -234,6 +235,13 @@ void detectsOpenModule() {
.noneMatch(it -> it.contains("Cycle detected: Slice open"));
}

@Test // GH-520
void bootstrapsOnEmptyProject() {

assertThatNoException().isThrownBy(() -> ApplicationModules.of(EmptyApplication.class).verify());
assertThatIllegalArgumentException().isThrownBy(() -> ApplicationModules.of("non.existant"));
}

private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class<?>... types) {

Stream.of(types).forEach(type -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.springframework.modulith.core.ApplicationModulesFactory=org.springframework.modulith.test.TestApplicationModules.Factory
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Role;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.modulith.ApplicationModuleInitializer;
import org.springframework.modulith.core.ApplicationModule;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.core.ApplicationModulesFactory;
import org.springframework.modulith.core.FormatableType;
import org.springframework.modulith.runtime.ApplicationModulesRuntime;
import org.springframework.modulith.runtime.ApplicationRuntime;
Expand Down Expand Up @@ -138,12 +140,21 @@ public void initialize() {
private static class ApplicationModulesBootstrap {

private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationModulesBootstrap.class);
private static final ApplicationModulesFactory BOOTSTRAP;

static {

var factories = SpringFactoriesLoader.loadFactories(ApplicationModulesFactory.class,
ApplicationModulesBootstrap.class.getClassLoader());

BOOTSTRAP = !factories.isEmpty() ? factories.get(0) : ApplicationModulesFactory.defaultFactory();
}

static ApplicationModules initializeApplicationModules(Class<?> applicationMainClass) {

LOGGER.debug("Obtaining Spring Modulith application modules…");

var result = ApplicationModules.of(applicationMainClass);
var result = BOOTSTRAP.of(applicationMainClass);
var numberOfModules = result.stream().count();

if (numberOfModules == 0) {
Expand All @@ -153,7 +164,7 @@ static ApplicationModules initializeApplicationModules(Class<?> applicationMainC
} else {

LOGGER.debug("Detected {} application modules: {}", //
result.stream().count(), //
numberOfModules, //
result.stream().map(ApplicationModule::getName).toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.List;

import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.core.ApplicationModulesFactory;
import org.springframework.modulith.core.ModulithMetadata;

import com.tngtech.archunit.base.DescribedPredicate;
Expand All @@ -34,10 +35,44 @@ public class TestApplicationModules {
* Creates an {@link ApplicationModules} instance from the given package but only inspecting the test code.
*
* @param basePackage must not be {@literal null} or empty.
* @return
* @return will never be {@literal null}.
*/
public static ApplicationModules of(String basePackage) {
return new ApplicationModules(ModulithMetadata.of(basePackage), List.of(basePackage),
DescribedPredicate.alwaysFalse(), false, new ImportOption.OnlyIncludeTests()) {};
return of(ModulithMetadata.of(basePackage), basePackage);
}

/**
* Creates an {@link ApplicationModules} instance from the given application class but only inspecting the test code.
*
* @param applicationClass must not be {@literal null} or empty.
* @return will never be {@literal null}.
* @since 1.2
*/
public static ApplicationModules of(Class<?> applicationClass) {
return of(ModulithMetadata.of(applicationClass), applicationClass.getPackageName());
}

private static ApplicationModules of(ModulithMetadata metadata, String basePackage) {
return new ApplicationModules(metadata, List.of(basePackage), DescribedPredicate.alwaysFalse(), false,
new ImportOption.OnlyIncludeTests()) {};
}

/**
* Custom {@link ApplicationModulesFactory} to bootstrap an {@link ApplicationModules} instance only considering test
* code.
*
* @author Oliver Drotbohm
* @since 1.2
*/
static class Factory implements ApplicationModulesFactory {

/*
* (non-Javadoc)
* @see org.springframework.modulith.core.util.ApplicationModulesFactory#of(java.lang.Class)
*/
@Override
public ApplicationModules of(Class<?> applicationClass) {
return TestApplicationModules.of(applicationClass);
}
}
}

0 comments on commit e41d7ec

Please sign in to comment.