mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2024-03 spring-07-advanced-config updated
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,2 @@
|
||||
# application-events-demo
|
||||
Пример работы с событиями
|
||||
@@ -0,0 +1,63 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>application-events-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>application-events-demo</name>
|
||||
<description>Application events demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<spring.shell.version>3.2.1</spring.shell.version>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.shell</groupId>
|
||||
<artifactId>spring-shell-starter</artifactId>
|
||||
<version>${spring.shell.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.applicationeventsdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.shell.command.annotation.CommandScan;
|
||||
|
||||
@CommandScan
|
||||
@SpringBootApplication
|
||||
public class ApplicationEventsDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ApplicationEventsDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
public class AbstractRespondent {
|
||||
public void delay(long mills) {
|
||||
try {
|
||||
Thread.sleep(mills);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
//@Configuration
|
||||
public class EventsConfig {
|
||||
|
||||
// Имя бина важно
|
||||
@Bean
|
||||
public ApplicationEventMulticaster applicationEventMulticaster() {
|
||||
SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
|
||||
eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
return eventMulticaster;
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
public interface EventsPublisher {
|
||||
void publish();
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.Getter;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class HalfAGlassOfWaterEvent extends ApplicationEvent {
|
||||
|
||||
@Getter
|
||||
private final String payload;
|
||||
|
||||
public HalfAGlassOfWaterEvent(Object source) {
|
||||
super(source);
|
||||
payload = "Осталось половина стакана воды!!!";
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class HalfAGlassOfWaterEventPublisher implements EventsPublisher {
|
||||
|
||||
private final ApplicationEventPublisher publisher;
|
||||
|
||||
@Override
|
||||
public void publish() {
|
||||
publisher.publishEvent(new HalfAGlassOfWaterEvent(this));
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class NegativeRespondent extends AbstractRespondent implements ApplicationListener<HalfAGlassOfWaterEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
delay(100);
|
||||
System.out.println("Негативно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Какой ужас. Теперь он наполовину пуст!!!\n\n");
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.applicationeventsdemo.events;
|
||||
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PositiveRespondent extends AbstractRespondent {
|
||||
|
||||
@EventListener
|
||||
public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) {
|
||||
delay(100);
|
||||
System.out.println("Позитивно настроенный слушатель");
|
||||
System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload()));
|
||||
System.out.println("- Ничего. Главное, что он наполовину полон!!!\n\n");
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.example.applicationeventsdemo.security;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static java.util.Objects.nonNull;
|
||||
|
||||
@Component
|
||||
public class InMemoryLoginContext implements LoginContext {
|
||||
private String userName;
|
||||
|
||||
@Override
|
||||
public void login(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isUserLoggedIn() {
|
||||
return nonNull(userName);
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package ru.otus.example.applicationeventsdemo.security;
|
||||
|
||||
public interface LoginContext {
|
||||
void login(String userName);
|
||||
boolean isUserLoggedIn();
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.standard.ShellComponent;
|
||||
import org.springframework.shell.standard.ShellMethod;
|
||||
import org.springframework.shell.standard.ShellMethodAvailability;
|
||||
import org.springframework.shell.standard.ShellOption;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@ShellComponent(value = "Application Events Commands")
|
||||
@RequiredArgsConstructor
|
||||
public class ApplicationEventsCommands {
|
||||
|
||||
private final EventsPublisher eventsPublisher;
|
||||
|
||||
private final LoginContext loginContext;
|
||||
|
||||
@ShellMethod(value = "Login command", key = {"l", "login"})
|
||||
public String login(@ShellOption(defaultValue = "AnyUser") String userName) {
|
||||
loginContext.login(userName);
|
||||
return String.format("Добро пожаловать: %s", userName);
|
||||
}
|
||||
|
||||
@ShellMethod(value = "Publish event command", key = {"p", "pub", "publish"})
|
||||
@ShellMethodAvailability(value = "isPublishEventCommandAvailable")
|
||||
public String publishEvent() {
|
||||
eventsPublisher.publish();
|
||||
return "Событие опубликовано";
|
||||
}
|
||||
|
||||
private Availability isPublishEventCommandAvailable() {
|
||||
return loginContext.isUserLoggedIn()
|
||||
? Availability.available()
|
||||
: Availability.unavailable("Сначала залогиньтесь");
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.example.applicationeventsdemo.shellnew;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.shell.command.annotation.Command;
|
||||
import org.springframework.shell.command.annotation.CommandAvailability;
|
||||
import org.springframework.shell.command.annotation.Option;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@Command(group = "Application Events Commands New Way")
|
||||
@RequiredArgsConstructor
|
||||
public class ApplicationEventsCommandsNewWay {
|
||||
|
||||
private final EventsPublisher eventsPublisher;
|
||||
|
||||
private final LoginContext loginContext;
|
||||
|
||||
@Command(description = "Login command new way", command = "login2", alias = "l2")
|
||||
public String login(@Option(defaultValue = "AnyUser") String userName) {
|
||||
loginContext.login(userName);
|
||||
return String.format("Добро пожаловать: %s", userName);
|
||||
}
|
||||
|
||||
@Command(description = "Publish event command new way", command = "publish2", alias = {"p2", "pub2"})
|
||||
@CommandAvailability(provider = "publishEventCommandAvailabilityProvider")
|
||||
public String publishEvent() {
|
||||
eventsPublisher.publish();
|
||||
return "Событие опубликовано";
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package ru.otus.example.applicationeventsdemo.shellnew;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.shell.Availability;
|
||||
import org.springframework.shell.AvailabilityProvider;
|
||||
import ru.otus.example.applicationeventsdemo.security.LoginContext;
|
||||
|
||||
@Configuration
|
||||
public class ShellConfig {
|
||||
|
||||
@Bean
|
||||
public AvailabilityProvider publishEventCommandAvailabilityProvider(LoginContext loginContext) {
|
||||
return () -> loginContext.isUserLoggedIn()
|
||||
? Availability.available()
|
||||
: Availability.unavailable("Сначала залогиньтесь");
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
|
||||
spring:
|
||||
main:
|
||||
allow-circular-references: true
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.InputProvider;
|
||||
import org.springframework.shell.ResultHandlerService;
|
||||
import org.springframework.shell.Shell;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@DisplayName("Тест команд shell ")
|
||||
@SpringBootTest
|
||||
class ApplicationEventsCommandsTest {
|
||||
|
||||
private static final String GREETING_PATTERN = "Добро пожаловать: %s";
|
||||
private static final String DEFAULT_LOGIN = "AnyUser";
|
||||
private static final String CUSTOM_LOGIN = "Вася";
|
||||
private static final String COMMAND_LOGIN = "login";
|
||||
private static final String COMMAND_LOGIN_SHORT = "l";
|
||||
private static final String COMMAND_PUBLISH = "publish";
|
||||
private static final String COMMAND_PUBLISH_EXPECTED_RESULT = "Событие опубликовано";
|
||||
private static final String COMMAND_LOGIN_PATTERN = "%s %s";
|
||||
|
||||
private InputProvider inputProvider;
|
||||
|
||||
private ArgumentCaptor<Object> argumentCaptor;
|
||||
|
||||
@MockBean
|
||||
private EventsPublisher eventsPublisher;
|
||||
|
||||
|
||||
@SpyBean
|
||||
private ResultHandlerService resultHandlerService;
|
||||
|
||||
@Autowired
|
||||
private Shell shell;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
inputProvider = mock(InputProvider.class);
|
||||
argumentCaptor = ArgumentCaptor.forClass(Object.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать приветствие для всех форм команды логина")
|
||||
@Test
|
||||
void shouldReturnExpectedGreetingAfterLoginCommandEvaluated() throws Exception {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_LOGIN)
|
||||
.thenReturn(() -> COMMAND_LOGIN_SHORT)
|
||||
.thenReturn(() -> String.format(COMMAND_LOGIN_PATTERN, COMMAND_LOGIN_SHORT, CUSTOM_LOGIN))
|
||||
.thenReturn(null);
|
||||
|
||||
shell.run(inputProvider);
|
||||
verify(resultHandlerService, times(3)).handle(argumentCaptor.capture());
|
||||
List<Object> results = argumentCaptor.getAllValues();
|
||||
assertThat(results).containsExactlyInAnyOrder(String.format(GREETING_PATTERN, DEFAULT_LOGIN),
|
||||
String.format(GREETING_PATTERN, DEFAULT_LOGIN),
|
||||
String.format(GREETING_PATTERN, CUSTOM_LOGIN));
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать CommandNotCurrentlyAvailable если при попытке выполнения команды publish пользователь выполнил вход")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnCommandNotCurrentlyAvailableObjectWhenUserDoesNotLoginAfterPublishCommandEvaluated() {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_PUBLISH)
|
||||
.thenReturn(null);
|
||||
|
||||
assertThatCode(() -> shell.run(inputProvider)).isInstanceOf(CommandNotCurrentlyAvailable.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать статус выполнения команды publish и вызвать соответствующий метод сервиса есл и команда выполнена после входа")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnExpectedMessageAndFirePublishMethodAfterPublishCommandEvaluated() throws Exception {
|
||||
when(inputProvider.readInput())
|
||||
.thenReturn(() -> COMMAND_LOGIN)
|
||||
.thenReturn(() -> COMMAND_PUBLISH)
|
||||
.thenReturn(null);
|
||||
|
||||
shell.run(inputProvider);
|
||||
verify(resultHandlerService, times(2)).handle(argumentCaptor.capture());
|
||||
assertThat(argumentCaptor.getValue()).isEqualTo(COMMAND_PUBLISH_EXPECTED_RESULT);
|
||||
verify(eventsPublisher, times(1)).publish();
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
main:
|
||||
allow-circular-references: true
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,11 @@
|
||||
#### Упражнение №1
|
||||
Условия вечеринки:
|
||||
- Алексей придет если condition.alexey-exists=true
|
||||
- Анна придет если будет Алексей
|
||||
- Олег придет если включен профиль Oleg
|
||||
- Петр придет если включен профиль Peter
|
||||
- Янис придет если condition.yanis-exists=true
|
||||
- Яна придет если нет Алексея, но есть Янис
|
||||
|
||||
Измените только одну настройку в application.yml, чтобы на вечеринку пришло максимальное количество людей
|
||||
Напечатать имена пришедших на вечеринку можно с помощью команды "ppm" или "print-party-members"
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>conditional-and-profiles-exercise</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.Party;
|
||||
|
||||
@SpringBootApplication
|
||||
public class ConditionalAndProfilesDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext ctx = SpringApplication.run(ConditionalAndProfilesDemoApplication.class, args);
|
||||
Party party = ctx.getBean(Party.class);
|
||||
party.printPartyMembers();
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnProperty(name = "condition.alexey-exists", havingValue = "true")
|
||||
@Component
|
||||
public class Alexey extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Алексей";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnBean(Alexey.class)
|
||||
@Component
|
||||
public class Anna extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Аня";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@Profile("Oleg")
|
||||
@Component
|
||||
public class Oleg extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Олег";
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class Party {
|
||||
private final List<Friend> partyMembers;
|
||||
|
||||
public void printPartyMembers() {
|
||||
System.out.println("Участники вечеринки:");
|
||||
System.out.println(partyMembers.stream().map(Friend::getName).collect(Collectors.joining("\n")));
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@Profile("Peter")
|
||||
@Component
|
||||
public class Peter extends Friend {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Петр";
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.conditions.YanaConditions;
|
||||
|
||||
|
||||
@Conditional(YanaConditions.class)
|
||||
@Component
|
||||
public class Yana extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Яна";
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.conditionalandprofilesdemo.model.base.Friend;
|
||||
|
||||
@ConditionalOnProperty(name = "condition.yanis-exists", havingValue = "true")
|
||||
@Component
|
||||
public class Yanis extends Friend {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Янис";
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model.base;
|
||||
|
||||
public abstract class Friend {
|
||||
public abstract String getName();
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.example.conditionalandprofilesdemo.model.conditions;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
|
||||
public class YanaConditions extends AllNestedConditions {
|
||||
|
||||
public YanaConditions() {
|
||||
super(ConfigurationPhase.PARSE_CONFIGURATION);
|
||||
}
|
||||
|
||||
|
||||
@ConditionalOnProperty(name = "condition.alexey-exists", havingValue = "false")
|
||||
static class AlexeyDoesNotExistsCondition {
|
||||
}
|
||||
|
||||
@ConditionalOnProperty(name = "condition.yanis-exists", havingValue = "true")
|
||||
static class YanisExistsCondition {
|
||||
}
|
||||
}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
condition:
|
||||
#yanis-exists: true
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
condition:
|
||||
alexey-exists: true
|
||||
yanis-exists: false
|
||||
|
||||
#Доступные профили: Oleg и Peter
|
||||
spring:
|
||||
profiles:
|
||||
active:
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: ERROR
|
||||
|
||||
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: Peter
|
||||
# Старый вариант включения профилей
|
||||
#spring:
|
||||
# profiles: Peter
|
||||
|
||||
condition:
|
||||
yanis-exists: true
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>ru.otus</groupId>
|
||||
<artifactId>advanced-config-class-work</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>conditional-and-profiles-exercise</module>
|
||||
<module>application-events-demo</module>
|
||||
<module>test-configuration-exercise-1</module>
|
||||
<module>test-configuration-exercise-2</module>
|
||||
<module>test-configuration-exercise-3</module>
|
||||
<module>test-configuration-solution-1</module>
|
||||
<module>test-configuration-solution-2</module>
|
||||
<module>test-configuration-solution-3</module>
|
||||
<module>test-caching-demo</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-caching-demo</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class Service1 {
|
||||
@Getter
|
||||
private final String name = "Service1";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String state = "State1";
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class Service2 {
|
||||
@Getter
|
||||
private final String name = "Service2";
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String state = "State2";
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
//@SpringBootTest(classes = {Service1.class, Service2.class})
|
||||
//@TestPropertySource("classpath:test.properties")
|
||||
//@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
|
||||
@SpringBootTest
|
||||
class IntegrationTest1 {
|
||||
|
||||
@Autowired
|
||||
private Service1 service1;
|
||||
|
||||
//@MockBean
|
||||
@Autowired
|
||||
private Service2 service2;
|
||||
|
||||
@Test
|
||||
void test1() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State7");
|
||||
service2.setState("State8");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test2() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State9");
|
||||
service2.setState("State10");
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package ru.otus.example.testconfigurationdemo.statefulservices;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class IntegrationTest2 {
|
||||
|
||||
@Autowired
|
||||
private Service1 service1;
|
||||
|
||||
@Autowired
|
||||
private Service2 service2;
|
||||
|
||||
@Test
|
||||
void test3() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State3");
|
||||
service2.setState("State4");
|
||||
}
|
||||
|
||||
@Test
|
||||
void test4() {
|
||||
System.out.println(service1.getName() + ": " + service1.getState());
|
||||
System.out.println(service2.getName() + ": " + service2.getState());
|
||||
|
||||
service1.setState("State5");
|
||||
service2.setState("State6");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
any.prop=10
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,9 @@
|
||||
#### Упражнение №2. Должен остаться только один
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedConfigurationDemoTest
|
||||
(family должно содержать только собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedConfigurationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать только собаку ")
|
||||
@Test
|
||||
void shouldContainOnlyDog() {
|
||||
assertThat(family).containsOnlyKeys("dog");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В PlainSpringBootTestDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class PlainSpringBootTestDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,9 @@
|
||||
#### Упражнение №3. Вернуть отца в семью
|
||||
|
||||
С помощью вложенных конфигураций сделать так,
|
||||
чтобы прошел NestedTestConfigurationDemoTest
|
||||
(family должно содержать маму, папу, сына и собаку)
|
||||
|
||||
- Сделать вложенный класс конфигурации. Не забываем про static
|
||||
- Выбрать тип конфигурации (@Configuration/@TestConfiguration)
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedTestConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class NestedTestConfigurationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, папу, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyWithFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "father", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,8 @@
|
||||
#### Упражнение №4. Вся семья, но без собаки
|
||||
|
||||
С помощью внешней конфигурации сделать так,
|
||||
чтобы прошел SpringBootTestWithExternalLimitationDemoTest
|
||||
(family должно содержать маму, папу и сына)
|
||||
|
||||
- Над TestSpringBootConfiguration повесить @SpringBootConfiguration
|
||||
- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы)
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-exercise-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>test-configuration-demo</name>
|
||||
<description>Test configuration demo</description>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В SpringBootTestWithExternalLimitationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class SpringBootTestWithExternalLimitationDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, папу и сына")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "father", "son");
|
||||
}
|
||||
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
public class TestSpringBootConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,7 @@
|
||||
#### Решение упражнения №2
|
||||
По заданию нужно ограничить контекст одним бином с помощью вложенной конфигурации.
|
||||
@TestConfiguration не ограничивает, а дополняет контекст новыми бинами или подменяет те, что уже там.
|
||||
Соответственно выбираем @Configuration. Вешаем его над вложенным статическим классом.
|
||||
Т.к. Dog является @Component, то для его размещения в контексте подойдет любое из:
|
||||
- @ComponentScan("ru.otus.example.testconfigurationdemo.family.pets") над конфигурацией
|
||||
- Создание бина, через метод с аннотацией @Bean внутри конфигурации
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-solution-1</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
import ru.otus.example.testconfigurationdemo.family.pets.Dog;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
//@SpringBootTest(classes = Dog.class)
|
||||
//@ContextConfiguration(classes = Dog.class)
|
||||
public class NestedConfigurationDemoTest {
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family.pets")
|
||||
@Configuration
|
||||
static class NestedConfiguration {
|
||||
/*
|
||||
@Bean
|
||||
FamilyMember dog() {
|
||||
return new Dog();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать только собаку ")
|
||||
@Test
|
||||
void shouldContainOnlyDog() {
|
||||
assertThat(family).containsOnlyKeys("dog");
|
||||
}
|
||||
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В PlainSpringBootTestDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
public class PlainSpringBootTestDemoTest {
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyExceptFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,8 @@
|
||||
#### Решение упражнения №3
|
||||
По заданию нужно добавить в контекст бин, которого там нет, с помощью вложенной конфигурации.
|
||||
|
||||
Эту проблему можно решить с помощью @Configuration, фактически заново сформировав контекст.
|
||||
При этом @TestConfiguration может дополнить существующий. Соответственно выбираем его и вешаем над вложенным статическим классом.
|
||||
|
||||
Т.к. Father НЕ является @Component, то для его размещения в контексте нужно его создать,
|
||||
через метод с аннотацией @Bean внутри тестовой конфигурации
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-solution-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.example.testconfigurationdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan("ru.otus.example.testconfigurationdemo.family")
|
||||
@SpringBootApplication
|
||||
public class TestConfigurationDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfigurationDemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
package ru.otus.example.testconfigurationdemo.family;
|
||||
|
||||
public abstract class FamilyMember {
|
||||
public abstract String getName();
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.childrens;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Son extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Сын";
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
public class Father extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Папа";
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.parents;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Mother extends FamilyMember {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Мама";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.example.testconfigurationdemo.family.pets;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
|
||||
@Component
|
||||
public class Dog extends FamilyMember {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Собака";
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package ru.otus.example.testconfigurationdemo.demo;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import ru.otus.example.testconfigurationdemo.family.FamilyMember;
|
||||
import ru.otus.example.testconfigurationdemo.family.parents.Father;
|
||||
import ru.otus.example.testconfigurationdemo.family.pets.Dog;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@DisplayName("В NestedTestConfigurationDemoTest семья должна ")
|
||||
@SpringBootTest
|
||||
//@SpringBootTest(properties = "spring.main.allow-bean-definition-overriding=true")
|
||||
//@TestPropertySource(properties = "spring.main.allow-bean-definition-overriding=true")
|
||||
public class NestedTestConfigurationDemoTest {
|
||||
|
||||
@TestConfiguration
|
||||
static class NestedTestConfiguration {
|
||||
@Bean
|
||||
FamilyMember father() {
|
||||
return new Father();
|
||||
}
|
||||
|
||||
/*
|
||||
@Bean
|
||||
FamilyMember dog() {
|
||||
return new Dog() {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Злая собака";
|
||||
}
|
||||
};
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Map<String, FamilyMember> family;
|
||||
|
||||
@DisplayName(" содержать маму, папу, сына и собаку ")
|
||||
@Test
|
||||
void shouldContainAllFamilyWithFather() {
|
||||
assertThat(family).containsOnlyKeys("mother", "father", "son", "dog");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
# Compiled class file
|
||||
*.class
|
||||
|
||||
# Log file
|
||||
*.log
|
||||
|
||||
# BlueJ files
|
||||
*.ctxt
|
||||
|
||||
# Mobile Tools for Java (J2ME)
|
||||
.mtj.tmp/
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
||||
*.zip
|
||||
*.tar.gz
|
||||
*.rar
|
||||
|
||||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
|
||||
hs_err_pid*
|
||||
|
||||
#other
|
||||
*.bat
|
||||
*/.idea
|
||||
*.iml
|
||||
*/target
|
||||
|
||||
.idea
|
||||
*.iml
|
||||
target
|
||||
@@ -0,0 +1,12 @@
|
||||
#### Решение упражнения №4
|
||||
По заданию нужно ограничить контекст заданными бинами, с помощью внешней конфигурации.
|
||||
|
||||
Вешаем @SpringBootConfiguration над внешним классом TestSpringBootConfiguration.
|
||||
Теперь сканирование конфигураций не уйдет далше него. Ни одного бина не найдется.
|
||||
Значит нужно формировать контекст самим.
|
||||
|
||||
По заданию в нем должны быть бины типов Mother, Son и Father. Первые два являются @Component.
|
||||
Их можно разместить в контексте просканировав соответствующие пакеты с помощью @ComponentScan
|
||||
|
||||
Т.к. Father НЕ является @Component, то для его размещения в контексте нужно его создать,
|
||||
через метод с аннотацией @Bean внутри тестовой конфигурации
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<groupId>ru.otus.example</groupId>
|
||||
<artifactId>test-configuration-solution-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<snakeyaml.version>2.0</snakeyaml.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.yaml</groupId>
|
||||
<artifactId>snakeyaml</artifactId>
|
||||
<version>${snakeyaml.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user