GH-1 - Update Maven artifact coordinates, package and directory structure.

Original pull request: GH-7.
This commit is contained in:
Björn Kieling
2022-07-07 12:16:07 +02:00
committed by Oliver Drotbohm
parent 6c444769d7
commit 417e215993
255 changed files with 764 additions and 759 deletions

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject;
import org.springframework.modulith.Modulith;
/**
* @author Oliver Gierke
*/
@Modulith
public class Application {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.complex.api;
/**
*
* @author Oliver Gierke
*/
public class ComplexApiComponent {
}

View File

@@ -0,0 +1,4 @@
@NamedInterface("API")
package com.acme.myproject.complex.api;
import org.springframework.modulith.NamedInterface;

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.complex.internal;
import org.springframework.stereotype.Component;
/**
* @author Oliver Gierke
*/
@Component
class ComplextInternalComponent {
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2019 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 com.acme.myproject.complex.internal;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface({ "Port 1", "Port 2" })
public class FirstTypeBasedPort {
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2019 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 com.acme.myproject.complex.internal;
import org.springframework.modulith.NamedInterface;
/**
* @author Oliver Drotbohm
*/
@NamedInterface({ "Port 2", "Port 3" })
public class SecondTypeBasePort {
}

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.complex.spi;
/**
*
* @author Oliver Gierke
*/
public class ComplexSpiComponent {
}

View File

@@ -0,0 +1,4 @@
@NamedInterface("SPI")
package com.acme.myproject.complex.spi;
import org.springframework.modulith.NamedInterface;

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.cycleA;
import com.acme.myproject.cycleB.CycleB;
/**
* @author Oliver Gierke
*/
public class CycleA {
CycleB cycleB;
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.cycleB;
import com.acme.myproject.cycleA.CycleA;
/**
* @author Oliver Gierke
*/
public class CycleB {
CycleA cycleA;
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2019 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
*
* http://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 com.acme.myproject.fieldinjected;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.acme.myproject.moduleA.ServiceComponentA;
/**
* @author Oliver Drotbohm
*/
@Component
public class WithFieldInjection {
@Autowired ServiceComponentA a;
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.invalid;
import com.acme.myproject.moduleB.internal.InternalComponentB;
/**
* @author Oliver Gierke
*/
public class InvalidComponent {
InvalidComponent(InternalComponentB invalidComponent) {}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.invalid2;
import com.acme.myproject.moduleA.ServiceComponentA;
/**
* @author Oliver Gierke
*/
public class InvalidModuleDependency {
/**
* The dependency is invalid as the module declaration only allows dependencies to Module B.
*
* @param dependency
*/
public InvalidModuleDependency(ServiceComponentA dependency) {
// TODO Auto-generated constructor stub
}
}

View File

@@ -0,0 +1,4 @@
@Module(allowedDependencies = "moduleB")
package com.acme.myproject.invalid2;
import org.springframework.modulith.Module;

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2018-2019 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
*
* http://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 com.acme.myproject.moduleA;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
/**
* @author Oliver Drotbohm
*/
@Component
@RequiredArgsConstructor
public class ServiceComponentA {
private final ApplicationEventPublisher publisher;
public void fireEvent() {
publisher.publishEvent(new SomeEventA("Message"));
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleA;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Oliver Gierke
*/
@Configuration
public class SomeConfigurationA {
@Bean
SomeAtBeanComponentA atBeanComponent() {
return null;
}
public static class SomeAtBeanComponentA {}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright 2018-2019 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
*
* http://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 com.acme.myproject.moduleA;
import lombok.Value;
import org.jmolecules.event.annotation.DomainEvent;
/**
* @author Oliver Drotbohm
*/
@Value
@DomainEvent
public class SomeEventA {
String message;
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleB;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.acme.myproject.moduleB.internal.InternalComponentB;
/**
* @author Oliver Gierke
*/
@Component
@RequiredArgsConstructor
public class ServiceComponentB {
private final ServiceComponentA serviceComponentA;
private final InternalComponentB internalComponentB;
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleB;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import com.acme.myproject.moduleA.SomeEventA;
/**
* @author Oliver Gierke
*/
@Component
class SomeEventListenerB {
@EventListener
void on(SomeEventA event) {}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleB.internal;
import org.springframework.stereotype.Component;
/**
* @author Oliver Gierke
*/
@Component
public class InternalComponentB {
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleB.internal;
import org.springframework.stereotype.Component;
/**
* @author Oliver Gierke
*/
@Component
class SupportingComponentB {
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleC;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import com.acme.myproject.moduleB.ServiceComponentB;
/**
* @author Oliver Gierke
*/
@Component
@RequiredArgsConstructor
class ServiceComponentC {
private final ServiceComponentB serviceComponentB;
}

View File

@@ -0,0 +1,4 @@
@Module(displayName = "MyModule C")
package com.acme.myproject.moduleC;
import org.springframework.modulith.Module;

View File

@@ -0,0 +1,83 @@
/*
* Copyright 2020 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 com.acme.myproject.stereotypes;
import lombok.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.event.TransactionalEventListener;
/**
* @author Oliver Drotbohm
*/
public class Stereotypes {
@Component
static class SomeComponent {}
@Controller
static class SomeController {}
@Service
static class SomeService {}
@Repository
static class SomeRepository {}
@Component
static class SomeRepresentations {}
@Component
static class SomeEventListener {
@EventListener
void someEventListener(Object event) {}
}
@Component
static class SomeTxEventListener {
@TransactionalEventListener
void someTxEventListener(Object event) {}
}
@Component // Used for documentation purposes
public static interface SomeAppInterface {};
@Component
static class SomeAppInterfaceImplementation implements SomeAppInterface {}
@Value
@ConstructorBinding
@ConfigurationProperties("org.springframework.modulith.sample")
static class SomeConfigurationProperties {
/**
* Some test property.
*/
String test;
public SomeConfigurationProperties(String test) {
this.test = test;
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2020 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 com.acme.myproject.stereotypes.web;
import org.springframework.stereotype.Component;
/**
* @author Oliver Drotbohm
*/
@Component
public class WebRepresentations {
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.model.Modules;
import org.springframework.modulith.model.Modules.Filters;
import org.springframework.modulith.model.Violations;
import com.acme.myproject.invalid.InvalidComponent;
import com.acme.myproject.moduleB.internal.InternalComponentB;
import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
/**
* Test cases to verify the validity of the overall modulith rules
*
* @author Oliver Gierke
* @author Peter Gafert
*/
class ModulithTest {
static final DescribedPredicate<JavaClass> DEFAULT_EXCLUSIONS = Filters.withoutModules("cycleA", "cycleB", "invalid2",
"fieldinjected");
@Test
void verifyModules() {
String componentName = InternalComponentB.class.getSimpleName();
assertThatExceptionOfType(Violations.class) //
.isThrownBy(() -> Modules.of(Application.class, DEFAULT_EXCLUSIONS).verify()) //
.withMessageContaining(String.format("Module '%s' depends on non-exposed type %s within module 'moduleB'",
"invalid", InternalComponentB.class.getName()))
.withMessageContaining(String.format("%s declares constructor %s(%s)", InvalidComponent.class.getSimpleName(),
InvalidComponent.class.getSimpleName(), componentName));
}
@Test
void verifyModulesWithoutInvalid() {
Modules.of(Application.class, DEFAULT_EXCLUSIONS.or(Filters.withoutModule("invalid"))).verify();
}
@Test
void detectsCycleBetweenModules() {
assertThatExceptionOfType(Violations.class) //
.isThrownBy(() -> Modules.of(Application.class, Filters.withoutModules("invalid", "invalid2")).verify()) //
// mentions modules
.withMessageContaining("cycleA") //
.withMessageContaining("cycleB") //
// mentions offending types
.withMessageContaining("CycleA") //
.withMessageContaining("CycleB");
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.modulith.test.ModuleTest;
import org.springframework.modulith.test.ModuleTest.BootstrapMode;
import org.springframework.core.annotation.AliasFor;
/**
* @author Oliver Gierke
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@ModuleTest(verifyAutomatically = false)
public @interface NonVerifyingModuleTest {
@AliasFor(annotation = ModuleTest.class, attribute = "mode")
BootstrapMode value() default BootstrapMode.STANDALONE;
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.complex;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.model.NamedInterface;
import org.springframework.modulith.model.NamedInterfaces;
import org.springframework.modulith.test.ModuleTestExecution;
import org.springframework.beans.factory.annotation.Autowired;
import com.acme.myproject.NonVerifyingModuleTest;
/**
* @author Oliver Gierke
*/
@NonVerifyingModuleTest
class ComplexTest {
@Autowired ModuleTestExecution moduleTest;
@Test
void exposesNamedInterfaces() {
NamedInterfaces interfaces = moduleTest.getModule().getNamedInterfaces();
assertThat(interfaces.stream().map(NamedInterface::getName)) //
.containsExactlyInAnyOrder("API", "SPI", "Port 1", "Port 2", "Port 3");
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2019 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
*
* http://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 com.acme.myproject.fieldinjected;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.model.Modules;
import org.springframework.modulith.test.ModuleTestExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
/**
* Integration tests to verify field injection is rejected.
*
* @author Oliver Drotbohm
*/
@NonVerifyingModuleTest
class FieldInjectedIntegrationTest {
@Autowired ModuleTestExecution execution;
@MockBean ServiceComponentA dependency;
@Test
void rejectsFieldInjection() {
Modules modules = execution.getModules();
assertThat(execution.getModule().detectDependencies(modules)) //
.hasMessageContaining("field injection") //
.hasMessageContaining("WithFieldInjection.a"); // offending field
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleA;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.test.PublishedEvents;
import org.springframework.modulith.test.PublishedEvents.TypedPublishedEvents;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleB.ServiceComponentB;
/**
* @author Oliver Drotbohm
*/
@NonVerifyingModuleTest
class ModuleATest {
@Autowired ApplicationContext context;
@Autowired PublishedEvents events;
@Test
void bootstrapsModuleAOnly() {
context.getBean(ServiceComponentA.class);
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
.isThrownBy(() -> context.getBean(ServiceComponentB.class));
}
@Test
void assertEventsFired() {
context.getBean(ServiceComponentA.class).fireEvent();
TypedPublishedEvents<SomeEventA> matching = events.ofType(SomeEventA.class) //
.matching(it -> it.getMessage().equals("Message"));
assertThat(matching).hasSize(1);
}
@Test
void injectsPublishedEventsIntoMethod(PublishedEvents events) {
assertThat(events).isNotNull();
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleB;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.internal.creation.bytebuddy.MockAccess;
import org.springframework.modulith.test.ModuleTest.BootstrapMode;
import org.springframework.modulith.test.TestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.ApplicationContext;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.acme.myproject.moduleB.internal.InternalComponentB;
/**
* @author Oliver Gierke
*/
class ModuleBTest {
@Nested
static class WithoutMocksTest {
@Autowired ServiceComponentB serviceComponentB;
@NonVerifyingModuleTest
static class Config {}
@Test
void failsToStartBecauseServiceComponentAIsMissing() throws Exception {
TestUtils.assertDependencyMissing(WithoutMocksTest.Config.class, ServiceComponentA.class);
}
}
@Nested
@NonVerifyingModuleTest
static class WithMocksTest {
@Autowired ApplicationContext context;
@MockBean ServiceComponentA serviceComponentA;
@Test
void bootstrapsModuleB() {
context.getBean(ServiceComponentB.class);
assertThat(context.getBean(ServiceComponentA.class)).isInstanceOf(MockAccess.class);
}
@Test
void considersNestedPackagePartOfTheModuleByDefault() {
context.getBean(InternalComponentB.class);
}
@Test
void tweaksAutoConfigurationPackageToModulePackage() {
assertThat(AutoConfigurationPackages.get(context)) //
.containsExactly(getClass().getPackage().getName());
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class WithUpstreamModuleTest {
@Autowired ServiceComponentA componentA;
@Autowired ServiceComponentB componentB;
@Test
void bootstrapsContext() {}
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2018 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
*
* http://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 com.acme.myproject.moduleC;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.test.ModuleTest.BootstrapMode;
import org.springframework.modulith.test.TestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import com.acme.myproject.NonVerifyingModuleTest;
import com.acme.myproject.moduleA.ServiceComponentA;
import com.acme.myproject.moduleB.ServiceComponentB;
/**
* @author Oliver Gierke
*/
class ModuleCTest {
@Nested
public static class FailsStandaloneTest {
@NonVerifyingModuleTest
static class Config {}
@Test
void failsStandalone() {
TestUtils.assertDependencyMissing(FailsStandaloneTest.Config.class, ServiceComponentB.class);
}
}
@Nested
static class FailsWithDirectDependencyTest {
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class Config {}
@Test
void failsWithDirectDependency() {
TestUtils.assertDependencyMissing(FailsWithDirectDependencyTest.Config.class, ServiceComponentA.class);
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.DIRECT_DEPENDENCIES)
static class SucceedsWithDirectDependencyPlusItsDependenciesMocksTest {
@MockBean ServiceComponentA serviceComponentA;
@Test
void bootstrapsContext() {
assertThat(serviceComponentA).isNotNull();
}
}
@Nested
@NonVerifyingModuleTest(BootstrapMode.ALL_DEPENDENCIES)
static class SucceedsWithAllDependenciesTest {
@Autowired ServiceComponentA serviceComponentA;
@Autowired ServiceComponentB serviceComponentB;
@Test
void bootstrapsContext() {
assertThat(serviceComponentA).isNotNull();
assertThat(serviceComponentB).isNotNull();
}
}
}

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>
<root level="OFF">
<appender-ref ref="console" />
</root>
</configuration>