commit
1f49cd5f6d
|
@ -24,6 +24,10 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
|
@ -44,6 +48,11 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWarDeployment;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnWarDeployment
|
||||
public class AdditionalWebConfiguration {
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
|
||||
public class ConditionalUtils {
|
||||
|
||||
public static boolean isWindows() {
|
||||
return SystemUtils.IS_OS_WINDOWS;
|
||||
}
|
||||
|
||||
public static boolean isJava8() {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.EIGHT);
|
||||
}
|
||||
|
||||
public static boolean isJava9() {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.NINE);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@Conditional(IsDevEnvCondition.class)
|
||||
public class DevEnvLoggingConfiguration {
|
||||
|
||||
@Bean
|
||||
@Conditional(IsDevEnvCondition.class)
|
||||
LoggingService loggingService() {
|
||||
return new LoggingService();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class IsDevEnvCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return "dev".equals(System.getProperty("env"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class IsWindowsCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return ConditionalUtils.isWindows();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class Java8Condition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return ConditionalUtils.isJava8();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Conditional(Java8Condition.class)
|
||||
public class Java8DependedService {
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
public class Java8OrJava9 extends AnyNestedCondition {
|
||||
Java8OrJava9() {
|
||||
super(ConfigurationPhase.REGISTER_BEAN);
|
||||
}
|
||||
|
||||
@Conditional(Java8Condition.class)
|
||||
static class Java8 { }
|
||||
|
||||
@Conditional(Java9Condition.class)
|
||||
static class Java9 { }
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class Java9Condition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return ConditionalUtils.isJava9();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnJava;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@Conditional({IsDevEnvCondition.class, IsWindowsCondition.class, Java8Condition.class})
|
||||
@ConditionalOnProperty(
|
||||
value = "logging.enabled",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
@ConditionalOnExpression("${logging.enabled:true} and '${logging.level}'.equals('DEBUG')")
|
||||
@ConditionalOnJava(JavaVersion.EIGHT)
|
||||
public class LoggingService {
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@TestConfiguration
|
||||
@ComponentScan("com.baeldung.annotations.conditional")
|
||||
public class ConditionalTestConfiguration {
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
public class DevEnvLoggingConfigurationUnitTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner();
|
||||
|
||||
@Test
|
||||
public void whenDevEnvEnabled_thenDevEnvLoggingConfigurationAndLoggingServiceShouldBeCreated() {
|
||||
System.setProperty("env", "dev");
|
||||
|
||||
contextRunner
|
||||
.withUserConfiguration(ConditionalTestConfiguration.class)
|
||||
.run(context ->
|
||||
Assertions.assertNotNull(
|
||||
context.getBean(DevEnvLoggingConfiguration.class)
|
||||
)
|
||||
);
|
||||
contextRunner
|
||||
.withUserConfiguration(ConditionalTestConfiguration.class)
|
||||
.run(context ->
|
||||
Assertions.assertNotNull(
|
||||
context.getBean(LoggingService.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDevEnvNotEnabled_thenDevEnvLoggingConfigurationAndLoggingServiceShouldNotBeCreated() {
|
||||
System.setProperty("env", "not-dev");
|
||||
|
||||
contextRunner
|
||||
.withUserConfiguration(ConditionalTestConfiguration.class)
|
||||
.run(context ->
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () ->
|
||||
context.getBean(DevEnvLoggingConfiguration.class)
|
||||
)
|
||||
);
|
||||
contextRunner
|
||||
.withUserConfiguration(ConditionalTestConfiguration.class)
|
||||
.run(context ->
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () ->
|
||||
context.getBean(LoggingService.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class IsDevEnvConditionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenDevEnvEnabled_thenDevEnvConditionShouldPass() {
|
||||
System.setProperty("env", "dev");
|
||||
|
||||
Assertions.assertTrue(
|
||||
new IsDevEnvCondition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDevEnvNotEnabled_thenDevEnvConditionShouldNotPass() {
|
||||
System.setProperty("env", "not-dev");
|
||||
|
||||
Assertions.assertFalse(
|
||||
new IsDevEnvCondition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class IsWindowsConditionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenOnWindows_thenIsWindowsConditionShouldPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isWindows)
|
||||
.thenReturn(true);
|
||||
Assertions.assertTrue(
|
||||
new IsWindowsCondition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotOnWindows_thenIsWindowsConditionShouldNotPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isWindows)
|
||||
.thenReturn(false);
|
||||
Assertions.assertFalse(
|
||||
new IsWindowsCondition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class Java8ConditionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenOnJava8_thenJava8ConditionShouldPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isJava8)
|
||||
.thenReturn(true);
|
||||
Assertions.assertTrue(
|
||||
new Java8Condition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotOnJava8_thenJava8ConditionShouldNotPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isJava8)
|
||||
.thenReturn(false);
|
||||
Assertions.assertFalse(
|
||||
new Java8Condition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
||||
public class Java9ConditionUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenOnJava9_thenJava9ConditionShouldPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isJava9)
|
||||
.thenReturn(true);
|
||||
Assertions.assertTrue(
|
||||
new Java9Condition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotOnJava9_thenJava9ConditionShouldNotPass() {
|
||||
try (MockedStatic<ConditionalUtils> theMock = Mockito.mockStatic(ConditionalUtils.class)) {
|
||||
theMock.when(ConditionalUtils::isJava9)
|
||||
.thenReturn(false);
|
||||
Assertions.assertFalse(
|
||||
new Java9Condition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue