Compare commits

...

2 Commits

Author SHA1 Message Date
Moritz Halbritter fa77e2057c Finish! 2025-05-20 18:30:52 +02:00
Moritz Halbritter 7626d5ccb3 Exercise 8 2025-05-20 18:30:52 +02:00
5 changed files with 150 additions and 6 deletions

View File

@ -28,6 +28,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>

View File

@ -33,5 +33,32 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -22,7 +22,7 @@ public class GreetingAutoConfiguration {
@ConditionalOnClass(StdOutGreetingService.class)
@ConditionalOnProperty(name = "workshop.greeting.type", havingValue = "stdout", matchIfMissing = true)
GreetingService stdOutGreetingService(GreetingProperties properties) {
return new StdOutGreetingService(properties.getText());
return new StdOutGreetingService(properties.getPrefix());
}
@Bean
@ -30,7 +30,7 @@ public class GreetingAutoConfiguration {
@ConditionalOnClass(LoggerGreetingService.class)
@ConditionalOnProperty(name = "workshop.greeting.type", havingValue = "logger")
GreetingService slf4jGreetingService(GreetingProperties properties) {
return new LoggerGreetingService(properties.getText());
return new LoggerGreetingService(properties.getPrefix());
}
@Bean
@ -38,6 +38,6 @@ public class GreetingAutoConfiguration {
@MyCustomCondition
@ConditionalOnClass(BeepGreetingService.class)
GreetingService beepGreetingService(GreetingProperties properties) {
return new BeepGreetingService(properties.getText());
return new BeepGreetingService(properties.getPrefix());
}
}

View File

@ -1,18 +1,21 @@
package com.workshop.magic.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
@ConfigurationProperties(prefix = "workshop.greeting")
public class GreetingProperties {
private String text = "Hello";
private Type type = Type.STDOUT;
private String prefix = "Hello";
@DeprecatedConfigurationProperty(replacement = "workshop.greeting.prefix")
@Deprecated
public String getText() {
return this.text;
return this.prefix;
}
public void setText(String text) {
this.text = text;
this.prefix = text;
}
public Type getType() {
@ -23,6 +26,14 @@ public class GreetingProperties {
this.type = type;
}
public String getPrefix() {
return this.prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public enum Type {
STDOUT,
LOGGER,

View File

@ -0,0 +1,101 @@
package com.workshop.magic.config;
import com.workshop.magic.service.GreetingService;
import com.workshop.magic.service.slf4j.BeepGreetingService;
import com.workshop.magic.service.slf4j.LoggerGreetingService;
import com.workshop.magic.service.stdout.StdOutGreetingService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
class GreetingAutoConfigurationTest {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(GreetingAutoConfiguration.class));
@Test
void shouldProvideStdOutGreetingServiceByDefault() {
this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(StdOutGreetingService.class);
});
}
@Test
void shouldProvideStdOutGreetingServiceWhenPropertyIsSet() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=stdout")
.run(context -> {
assertThat(context).hasSingleBean(StdOutGreetingService.class);
});
}
@Test
void shouldProvideLoggerGreetingServiceWhenPropertyIsSet() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=logger")
.run(context -> {
assertThat(context).hasSingleBean(LoggerGreetingService.class);
});
}
@Test
void shouldBackOffIfGreetingServiceIsDefinedByUser() {
this.contextRunner
.withBean(GreetingService.class, UserGreetingService::new)
.run(context -> {
assertThat(context).hasSingleBean(GreetingService.class);
assertThat(context).hasSingleBean(UserGreetingService.class);
});
}
@Test
void shouldNotUseStdOutGreetingServiceIfNotOnClasspath() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=stdout")
.withClassLoader(new FilteredClassLoader(StdOutGreetingService.class))
.run(context -> {
assertThat(context).doesNotHaveBean(GreetingService.class);
});
}
@Test
void shouldNotUseLoggerGreetingServiceIfNotOnClasspath() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=logger")
.withClassLoader(new FilteredClassLoader(LoggerGreetingService.class))
.run(context -> {
assertThat(context).doesNotHaveBean(GreetingService.class);
});
}
@Test
void shouldProvideBeepGreetingServiceIfSystemPropertyIsSet() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=none")
.withSystemProperties("my.custom.condition=true")
.run(context -> {
assertThat(context).hasSingleBean(BeepGreetingService.class);
});
}
@Test
void shouldNotUseBeepGreetingServiceIfNotOnClasspath() {
this.contextRunner
.withPropertyValues("workshop.greeting.type=none")
.withSystemProperties("my.custom.condition=true")
.withClassLoader(new FilteredClassLoader(BeepGreetingService.class))
.run(context -> {
assertThat(context).doesNotHaveBean(GreetingService.class);
});
}
private static class UserGreetingService implements GreetingService {
@Override
public void greet(String name) {
System.out.println("UserGreetingService: Hello " + name);
}
}
}