BAEL-4871 Spring Conditional Annotations
This commit is contained in:
parent
4d08dcace0
commit
2598e4fa4d
|
@ -42,6 +42,11 @@
|
|||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-inline</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!--Test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,6 +8,6 @@ public class IsDevEnvCondition implements Condition {
|
|||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return System.getProperty("env").equals("dev");
|
||||
return "dev".equals(System.getProperty("env"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.apache.commons.lang3.SystemUtils;
|
||||
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
@ -10,6 +8,6 @@ public class IsWindowsCondition implements Condition {
|
|||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return SystemUtils.IS_OS_WINDOWS;
|
||||
return ConditionalUtils.isWindows();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
@ -9,6 +8,6 @@ public class Java8Condition implements Condition {
|
|||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.EIGHT);
|
||||
return ConditionalUtils.isJava8();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.annotations.conditional;
|
||||
|
||||
import org.springframework.boot.system.JavaVersion;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
|
@ -9,6 +8,6 @@ public class Java9Condition implements Condition {
|
|||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
return JavaVersion.getJavaVersion().equals(JavaVersion.NINE);
|
||||
return ConditionalUtils.isJava9();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ import org.springframework.stereotype.Service;
|
|||
value = "logging.enabled",
|
||||
havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
@ConditionalOnExpression("${logging.enabled:true} and ${logging.level:DEBUG}")
|
||||
@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,50 @@
|
|||
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 DevEnvLoggingConfigurationTest {
|
||||
|
||||
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() {
|
||||
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,31 @@
|
|||
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() {
|
||||
Assertions.assertTrue(
|
||||
new IsDevEnvCondition().matches(
|
||||
Mockito.mock(ConditionContext.class), Mockito.mock(AnnotatedTypeMetadata.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
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,40 @@
|
|||
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,40 @@
|
|||
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