From cdff28199aae72cc226bdaf5332aafb5e205078c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9E=D1=80=D1=83=D0=B4=D0=B6=D0=B5=D0=B2?= Date: Fri, 11 Dec 2020 00:56:45 +0400 Subject: [PATCH] 2020-11 adv config examples added and testing examples updatetd --- 2020-11/spring-05-adv-conf/.gitignore | 4 + .../application-events-demo/.gitignore | 32 +++++++ .../application-events-demo/README.md | 2 + .../application-events-demo/pom.xml | 86 +++++++++++++++++++ .../ApplicationEventsDemoApplication.java | 26 ++++++ .../events/EventsPublisher.java | 5 ++ .../events/HalfAGlassOfWaterEvent.java | 15 ++++ .../HalfAGlassOfWaterEventPublisher.java | 17 ++++ .../events/NegativeRespondent.java | 18 ++++ .../events/PositiveRespondent.java | 18 ++++ .../shell/ApplicationEventsCommands.java | 35 ++++++++ .../src/main/resources/application.yml | 3 + .../shell/ApplicationEventsCommandsTest.java | 67 +++++++++++++++ .../src/test/resources/application.yml | 4 + .../beans-lifecycle-exercise/.gitignore | 32 +++++++ .../beans-lifecycle-exercise/README.md | 19 ++++ .../beans-lifecycle-exercise/pom.xml | 42 +++++++++ .../BeansLifecycleDemoApplication.java | 20 +++++ .../domain/FriendPhoneNumber.java | 8 ++ .../domain/GirlfiendPhoneNumber.java | 11 +++ .../beanslifecycledemo/domain/Phone.java | 18 ++++ .../domain/PhoneNumber.java | 7 ++ .../CustomBeanFactoryPostProcessor.java | 32 +++++++ .../lifecycle/CustomBeanPostProcessor.java | 39 +++++++++ .../lifecycle/CustomLifeCycleBean.java | 55 ++++++++++++ .../lifecycle/LifeCycleConfig.java | 27 ++++++ .../src/main/resources/application.yml | 7 ++ .../beans-scopes-exercise/.gitignore | 32 +++++++ .../beans-scopes-exercise/README.md | 9 ++ .../beans-scopes-exercise/pom.xml | 48 +++++++++++ .../BeansScopesDemoApplication.java | 13 +++ .../controllers/GreetingController.java | 40 +++++++++ .../services/AbstractGreetingServiceImpl.java | 35 ++++++++ .../services/GreetingService.java | 6 ++ .../PrototypeGreetingServiceImpl.java | 7 ++ .../services/RequestGreetingServiceImpl.java | 7 ++ .../services/SessionGreetingServiceImpl.java | 7 ++ .../SingletonGreetingServiceImpl.java | 7 ++ .../src/main/resources/application.yml | 8 ++ .../src/main/resources/templates/index.html | 45 ++++++++++ .../beans-scopes-solution/.gitignore | 32 +++++++ .../beans-scopes-solution/README.md | 15 ++++ .../beans-scopes-solution/pom.xml | 48 +++++++++++ .../BeansScopesDemoApplication.java | 13 +++ .../controllers/GreetingController.java | 40 +++++++++ .../services/AbstractGreetingServiceImpl.java | 35 ++++++++ .../services/GreetingService.java | 6 ++ .../PrototypeGreetingServiceImpl.java | 9 ++ .../services/RequestGreetingServiceImpl.java | 11 +++ .../services/SessionGreetingServiceImpl.java | 11 +++ .../SingletonGreetingServiceImpl.java | 9 ++ .../src/main/resources/application.yml | 8 ++ .../src/main/resources/templates/index.html | 45 ++++++++++ .../.gitignore | 32 +++++++ .../README.md | 11 +++ .../conditional-and-profiles-exercise/pom.xml | 73 ++++++++++++++++ ...ConditionalAndProfilesDemoApplication.java | 17 ++++ .../model/Alexey.java | 14 +++ .../model/Anna.java | 14 +++ .../model/Oleg.java | 14 +++ .../model/Party.java | 19 ++++ .../model/Peter.java | 15 ++++ .../model/Yana.java | 16 ++++ .../model/Yanis.java | 14 +++ .../model/base/Friend.java | 5 ++ .../model/conditions/YanaConditions.java | 20 +++++ .../src/main/resources/application-Peter.yml | 2 + .../src/main/resources/application.yml | 20 +++++ 2020-11/spring-05-adv-conf/pom.xml | 26 ++++++ 2020-11/spring-05-adv-conf/spring-shell.log | 9 ++ .../test-configuration-exercise-1/.gitignore | 32 +++++++ .../test-configuration-exercise-1/README.md | 9 ++ .../test-configuration-exercise-1/pom.xml | 74 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ .../demo/NestedConfigurationDemoTest.java | 26 ++++++ .../demo/PlainSpringBootTestDemoTest.java | 26 ++++++ .../test-configuration-exercise-2/.gitignore | 32 +++++++ .../test-configuration-exercise-2/README.md | 9 ++ .../test-configuration-exercise-2/pom.xml | 74 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ .../demo/NestedTestConfigurationDemoTest.java | 26 ++++++ .../test-configuration-exercise-3/.gitignore | 32 +++++++ .../test-configuration-exercise-3/README.md | 8 ++ .../test-configuration-exercise-3/pom.xml | 76 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ ...ootTestWithExternalLimitationDemoTest.java | 26 ++++++ .../demo/TestSpringBootConfiguration.java | 4 + .../test-configuration-solution-1/.gitignore | 32 +++++++ .../test-configuration-solution-1/README.md | 7 ++ .../test-configuration-solution-1/pom.xml | 74 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ .../demo/NestedConfigurationDemoTest.java | 41 +++++++++ .../demo/PlainSpringBootTestDemoTest.java | 26 ++++++ .../test-configuration-solution-2/.gitignore | 32 +++++++ .../test-configuration-solution-2/README.md | 8 ++ .../test-configuration-solution-2/pom.xml | 74 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ .../demo/NestedTestConfigurationDemoTest.java | 37 ++++++++ .../test-configuration-solution-3/.gitignore | 32 +++++++ .../test-configuration-solution-3/README.md | 12 +++ .../test-configuration-solution-3/pom.xml | 74 ++++++++++++++++ .../TestConfigurationDemoApplication.java | 15 ++++ .../family/FamilyMember.java | 5 ++ .../family/childrens/Son.java | 12 +++ .../family/parents/Father.java | 10 +++ .../family/parents/Mother.java | 12 +++ .../family/pets/Dog.java | 13 +++ ...ootTestWithExternalLimitationDemoTest.java | 26 ++++++ .../demo/TestSpringBootConfiguration.java | 17 ++++ .../unit-testing-pure-spring-parallel/pom.xml | 8 +- examples/unit-testing-pure-spring/pom.xml | 8 +- examples/unit-testing-spring-boot/pom.xml | 2 +- .../services/GreetingServiceImplTest.java | 1 - 138 files changed, 2873 insertions(+), 10 deletions(-) create mode 100644 2020-11/spring-05-adv-conf/.gitignore create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/.gitignore create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/README.md create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/pom.xml create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/ApplicationEventsDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/EventsPublisher.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEvent.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEventPublisher.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/NegativeRespondent.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/PositiveRespondent.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommands.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/main/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/test/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommandsTest.java create mode 100644 2020-11/spring-05-adv-conf/application-events-demo/src/test/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/.gitignore create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/README.md create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/pom.xml create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/BeansLifecycleDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/FriendPhoneNumber.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/GirlfiendPhoneNumber.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/Phone.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/PhoneNumber.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanFactoryPostProcessor.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanPostProcessor.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomLifeCycleBean.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/LifeCycleConfig.java create mode 100644 2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/.gitignore create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/README.md create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/pom.xml create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/templates/index.html create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/.gitignore create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/README.md create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/pom.xml create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/templates/index.html create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/.gitignore create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/README.md create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/pom.xml create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/ConditionalAndProfilesDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Alexey.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Anna.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Oleg.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Party.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Peter.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yana.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yanis.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/base/Friend.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/conditions/YanaConditions.java create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application-Peter.yml create mode 100644 2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application.yml create mode 100644 2020-11/spring-05-adv-conf/pom.xml create mode 100644 2020-11/spring-05-adv-conf/spring-shell.log create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/.gitignore create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/README.md create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/pom.xml create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java create mode 100644 2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java diff --git a/2020-11/spring-05-adv-conf/.gitignore b/2020-11/spring-05-adv-conf/.gitignore new file mode 100644 index 00000000..e62c33c2 --- /dev/null +++ b/2020-11/spring-05-adv-conf/.gitignore @@ -0,0 +1,4 @@ +.idea/ +*.iml + +target/ diff --git a/2020-11/spring-05-adv-conf/application-events-demo/.gitignore b/2020-11/spring-05-adv-conf/application-events-demo/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/application-events-demo/README.md b/2020-11/spring-05-adv-conf/application-events-demo/README.md new file mode 100644 index 00000000..63e68847 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/README.md @@ -0,0 +1,2 @@ +# application-events-demo +Пример работы с событиями \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/application-events-demo/pom.xml b/2020-11/spring-05-adv-conf/application-events-demo/pom.xml new file mode 100644 index 00000000..5dbc4e95 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/pom.xml @@ -0,0 +1,86 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + application-events-demo + 0.0.1-SNAPSHOT + application-events-demo + Application events demo + + + 11 + 11 + 11 + 2.0.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + + org.springframework.shell + spring-shell-starter + ${spring.shell.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/ApplicationEventsDemoApplication.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/ApplicationEventsDemoApplication.java new file mode 100644 index 00000000..5b8d7ef3 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/ApplicationEventsDemoApplication.java @@ -0,0 +1,26 @@ +package ru.otus.example.applicationeventsdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; +import org.springframework.context.event.ApplicationEventMulticaster; +import org.springframework.context.event.SimpleApplicationEventMulticaster; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +@SpringBootApplication +public class ApplicationEventsDemoApplication { + + //@Bean @Primary + //@Bean(name = "applicationEventMulticaster") + public ApplicationEventMulticaster applicationEventMulticaster() { + SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); + eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); + return eventMulticaster; + } + + public static void main(String[] args) { + SpringApplication.run(ApplicationEventsDemoApplication.class, args); + } + +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/EventsPublisher.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/EventsPublisher.java new file mode 100644 index 00000000..2274570a --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/EventsPublisher.java @@ -0,0 +1,5 @@ +package ru.otus.example.applicationeventsdemo.events; + +public interface EventsPublisher { + void publish(); +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEvent.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEvent.java new file mode 100644 index 00000000..22bab552 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEvent.java @@ -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 = "Осталось половина стакана воды!!!"; + } +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEventPublisher.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEventPublisher.java new file mode 100644 index 00000000..a1186fcc --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/HalfAGlassOfWaterEventPublisher.java @@ -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)); + } +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/NegativeRespondent.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/NegativeRespondent.java new file mode 100644 index 00000000..ce6115c9 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/NegativeRespondent.java @@ -0,0 +1,18 @@ +package ru.otus.example.applicationeventsdemo.events; + +import lombok.SneakyThrows; +import org.springframework.context.ApplicationListener; +import org.springframework.stereotype.Component; + +@Component +public class NegativeRespondent implements ApplicationListener { + + @SneakyThrows + @Override + public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) { + Thread.sleep(100); + System.out.println("Негативно настроенный слушатель"); + System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload())); + System.out.println("- Какой ужас. Теперь он наполовину пуст!!!\n\n"); + } +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/PositiveRespondent.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/PositiveRespondent.java new file mode 100644 index 00000000..fb930f9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/events/PositiveRespondent.java @@ -0,0 +1,18 @@ +package ru.otus.example.applicationeventsdemo.events; + +import lombok.SneakyThrows; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class PositiveRespondent { + + @SneakyThrows + @EventListener + public void onApplicationEvent(HalfAGlassOfWaterEvent halfAGlassOfWaterEvent) { + Thread.sleep(100); + System.out.println("Позитивно настроенный слушатель"); + System.out.println(String.format("- %s", halfAGlassOfWaterEvent.getPayload())); + System.out.println("- Ничего. Главное, что он наполовину полон!!!\n\n"); + } +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommands.java b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommands.java new file mode 100644 index 00000000..153a61ae --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommands.java @@ -0,0 +1,35 @@ +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; + +@ShellComponent +@RequiredArgsConstructor +public class ApplicationEventsCommands { + + private final EventsPublisher eventsPublisher; + + private String userName; + + @ShellMethod(value = "Login command", key = {"l", "login"}) + public String login(@ShellOption(defaultValue = "AnyUser") String userName) { + this.userName = 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 userName == null? Availability.unavailable("Сначала залогиньтесь"): Availability.available(); + } +} diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/main/resources/application.yml b/2020-11/spring-05-adv-conf/application-events-demo/src/main/resources/application.yml new file mode 100644 index 00000000..f78a9f36 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/main/resources/application.yml @@ -0,0 +1,3 @@ +logging: + level: + root: ERROR \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/test/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommandsTest.java b/2020-11/spring-05-adv-conf/application-events-demo/src/test/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommandsTest.java new file mode 100644 index 00000000..be44d813 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/test/java/ru/otus/example/applicationeventsdemo/shell/ApplicationEventsCommandsTest.java @@ -0,0 +1,67 @@ +package ru.otus.example.applicationeventsdemo.shell; + +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.mock.mockito.MockBean; +import org.springframework.shell.Availability; +import org.springframework.shell.CommandNotCurrentlyAvailable; +import org.springframework.shell.Shell; +import org.springframework.test.annotation.DirtiesContext; +import ru.otus.example.applicationeventsdemo.events.EventsPublisher; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@DisplayName("Тест команд shell ") +@SpringBootTest +class ApplicationEventsCommandsTest { + + @MockBean + private EventsPublisher eventsPublisher; + + @Autowired + private Shell shell; + + 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"; + + @DisplayName(" должен возвращать приветствие для всех форм команды логина") + @Test + void shouldReturnExpectedGreetingAfterLoginCommandEvaluated() { + String res = (String) shell.evaluate(() -> COMMAND_LOGIN); + assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN)); + + res = (String) shell.evaluate(() -> COMMAND_LOGIN_SHORT); + assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN)); + + res = (String) shell.evaluate(() -> String.format(COMMAND_LOGIN_PATTERN, COMMAND_LOGIN_SHORT, CUSTOM_LOGIN)); + assertThat(res).isEqualTo(String.format(GREETING_PATTERN, CUSTOM_LOGIN)); + } + + @DisplayName(" должен возвращать CommandNotCurrentlyAvailable если при попытке выполнения команды publish пользователь выполнил вход") + @DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD) + @Test + void shouldReturnCommandNotCurrentlyAvailableObjectWhenUserDoesNotLoginAfterPublishCommandEvaluated() { + Object res = shell.evaluate(() -> COMMAND_PUBLISH); + assertThat(res).isInstanceOf(CommandNotCurrentlyAvailable.class); + } + + @DisplayName(" должен возвращать статус выполнения команды publish и вызвать соответствующий метод сервиса есл икоманда выполнена после входа") + @DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD) + @Test + void shouldReturnExpectedMessageAndFirePublishMethodAfterPublishCommandEvaluated() { + shell.evaluate(() -> COMMAND_LOGIN); + String res = (String) shell.evaluate(() -> COMMAND_PUBLISH); + assertThat(res).isEqualTo(COMMAND_PUBLISH_EXPECTED_RESULT); + verify(eventsPublisher, times(1)).publish(); + } +} \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/application-events-demo/src/test/resources/application.yml b/2020-11/spring-05-adv-conf/application-events-demo/src/test/resources/application.yml new file mode 100644 index 00000000..b4921681 --- /dev/null +++ b/2020-11/spring-05-adv-conf/application-events-demo/src/test/resources/application.yml @@ -0,0 +1,4 @@ +spring: + shell: + interactive: + enabled: false diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/.gitignore b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/README.md b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/README.md new file mode 100644 index 00000000..75a9cff8 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/README.md @@ -0,0 +1,19 @@ +#### Упражнение №2 +- Изучить классы ничего в них не меняя: + - CustomBeanFactoryPostProcessor + - CustomBeanPostProcessor + - CustomLifeCycleBean +- Запуститить приложение +- Изучить распечатанный жизненный цикл + + +#### Упражнение №3 +- В application.yml выставить spring.shell.interactive.enabled в true +- Запуститить приложение командой "cfn" или "call-favorite-number" +- Запомнить результат +- В CustomBeanFactoryPostProcessor раскомментировать блок кода +- Запуститить приложение командой "cfn" или "call-favorite-number" +- Что изменилось? +- В CustomBeanPostProcessor раскомментировать строку +- Запуститить приложение командой "cfn" или "call-favorite-number" +- Что изменилось теперь? Круто, да? \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/pom.xml b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/pom.xml new file mode 100644 index 00000000..da5a91a9 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/pom.xml @@ -0,0 +1,42 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + ru.otus.example + beans-lifecycle-exercise + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/BeansLifecycleDemoApplication.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/BeansLifecycleDemoApplication.java new file mode 100644 index 00000000..337afceb --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/BeansLifecycleDemoApplication.java @@ -0,0 +1,20 @@ +package ru.otus.example.beanslifecycledemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import ru.otus.example.beanslifecycledemo.domain.Phone; + +@SpringBootApplication +public class BeansLifecycleDemoApplication { + + public static void main(String[] args) { + ApplicationContext ctx = SpringApplication.run(BeansLifecycleDemoApplication.class, args); + try { + Phone phone = ctx.getBean(Phone.class); + phone.callFavoriteNumber(); + }catch (Exception e) { + } + } + +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/FriendPhoneNumber.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/FriendPhoneNumber.java new file mode 100644 index 00000000..913d7db0 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/FriendPhoneNumber.java @@ -0,0 +1,8 @@ +package ru.otus.example.beanslifecycledemo.domain; + +public class FriendPhoneNumber extends PhoneNumber { + @Override + public String getOwnerName() { + return "Друг"; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/GirlfiendPhoneNumber.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/GirlfiendPhoneNumber.java new file mode 100644 index 00000000..54e4e173 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/GirlfiendPhoneNumber.java @@ -0,0 +1,11 @@ +package ru.otus.example.beanslifecycledemo.domain; + +import org.springframework.stereotype.Component; + +@Component +public class GirlfiendPhoneNumber extends PhoneNumber { + @Override + public String getOwnerName() { + return "Подруга"; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/Phone.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/Phone.java new file mode 100644 index 00000000..81170317 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/Phone.java @@ -0,0 +1,18 @@ +package ru.otus.example.beanslifecycledemo.domain; + +import lombok.RequiredArgsConstructor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +@Component +@ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "false") +@RequiredArgsConstructor +public class Phone { + private String greeting = "Погнали к родителям"; + + private final PhoneNumber favoriteNumber; + + public void callFavoriteNumber() { + System.out.println(favoriteNumber.getOwnerName() + " " + greeting); + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/PhoneNumber.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/PhoneNumber.java new file mode 100644 index 00000000..215bd09f --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/domain/PhoneNumber.java @@ -0,0 +1,7 @@ +package ru.otus.example.beanslifecycledemo.domain; + +public abstract class PhoneNumber { + public String getOwnerName() { + return "Спорт-лото"; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanFactoryPostProcessor.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanFactoryPostProcessor.java new file mode 100644 index 00000000..5f82e6e5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanFactoryPostProcessor.java @@ -0,0 +1,32 @@ +package ru.otus.example.beanslifecycledemo.lifecycle; + +import org.springframework.beans.BeanMetadataAttribute; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.context.annotation.ScannedGenericBeanDefinition; +import ru.otus.example.beanslifecycledemo.domain.FriendPhoneNumber; +import ru.otus.example.beanslifecycledemo.domain.GirlfiendPhoneNumber; + +public class CustomBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + + if (beanFactory.containsBean("customLifeCycleBean")) { + System.out.println("Шаг #1: BeanFactoryPostProcessor.postProcessBeanFactory\n"); + } + +/* + for (String beanName : beanFactory.getBeanDefinitionNames()) { + BeanDefinition d = beanFactory.getBeanDefinition(beanName); + + if (GirlfiendPhoneNumber.class.getName().equalsIgnoreCase(d.getBeanClassName())) { + d.setBeanClassName(FriendPhoneNumber.class.getName()); + ((ScannedGenericBeanDefinition) d).addMetadataAttribute(new BeanMetadataAttribute("className", FriendPhoneNumber.class.getName())); + d.setAutowireCandidate(true); + } + } +*/ + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanPostProcessor.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanPostProcessor.java new file mode 100644 index 00000000..2e7ba3b8 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomBeanPostProcessor.java @@ -0,0 +1,39 @@ +package ru.otus.example.beanslifecycledemo.lifecycle; + +import lombok.SneakyThrows; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import ru.otus.example.beanslifecycledemo.domain.Phone; + +import java.lang.reflect.Field; + +public class CustomBeanPostProcessor implements BeanPostProcessor { + @Override + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { + if (bean.getClass().equals(CustomLifeCycleBean.class)) { + System.out.println("Шаг #5: BeanPostProcessor.postProcessBeforeInitialization\n"); + } + return bean; + } + + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean.getClass().equals(CustomLifeCycleBean.class)) { + System.out.println("Шаг #9: BeanPostProcessor.postProcessAfterInitialization\n"); + } + + if (bean.getClass().isAssignableFrom(Phone.class)) { + //updateGreeting(bean); + } + return bean; + } + + @SneakyThrows + private void updateGreeting(Object bean) { + Class aClass = Phone.class; + Field greetingField = aClass.getDeclaredField("greeting"); + + greetingField.setAccessible(true); + greetingField.set(bean, "Ай-да в гараж. Стихи читать!"); + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomLifeCycleBean.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomLifeCycleBean.java new file mode 100644 index 00000000..b94d3e99 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/CustomLifeCycleBean.java @@ -0,0 +1,55 @@ +package ru.otus.example.beanslifecycledemo.lifecycle; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.*; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; + +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; + +public class CustomLifeCycleBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, ApplicationContextAware { + + @Override + public void setBeanName(String s) { + System.out.println("Шаг #2: BeanNameAware\n"); + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + System.out.println("Шаг #3: BeanFactoryAware\n"); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + System.out.println("Шаг #4: ApplicationContextAware\n"); + } + + @PostConstruct + public void postConstruct() { + System.out.println("Шаг #6: @PostConstruct\n"); + } + + @Override + public void afterPropertiesSet() throws Exception { + System.out.println("Шаг #7: InitializingBean.afterPropertiesSet\n"); + } + + public void customInitMethod() { + System.out.println("Шаг #8: CustomLifeCycleBean.customInitMethod\n"); + } + + @PreDestroy + public void preDestroy() { + System.out.println("Шаг #10: @PreDestroy\n"); + } + + @Override + public void destroy() throws Exception { + System.out.println("Шаг #11: DisposableBean.destroy\n"); + } + + public void customDestroyMethod() { + System.out.println("Шаг #12: CustomLifeCycleBean.customDestroyMethod\n"); + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/LifeCycleConfig.java b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/LifeCycleConfig.java new file mode 100644 index 00000000..afb3b55f --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/java/ru/otus/example/beanslifecycledemo/lifecycle/LifeCycleConfig.java @@ -0,0 +1,27 @@ +package ru.otus.example.beanslifecycledemo.lifecycle; + +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class LifeCycleConfig { + + @Bean + public BeanFactoryPostProcessor customBeanFactoryPostProcessor() { + return new CustomBeanFactoryPostProcessor(); + } + + @Bean + public BeanPostProcessor customBeanPostProcessor() { + return new CustomBeanPostProcessor(); + } + + @ConditionalOnProperty(name = "lifecycle.print.enabled", havingValue = "true") + @Bean(initMethod = "customInitMethod", destroyMethod = "customDestroyMethod") + public CustomLifeCycleBean customLifeCycleBean() { + return new CustomLifeCycleBean(); + } +} diff --git a/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/resources/application.yml b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/resources/application.yml new file mode 100644 index 00000000..54b14aef --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-lifecycle-exercise/src/main/resources/application.yml @@ -0,0 +1,7 @@ +logging: + level: + root: ERROR + +lifecycle: + print: + enabled: true diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/.gitignore b/2020-11/spring-05-adv-conf/beans-scopes-exercise/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/README.md b/2020-11/spring-05-adv-conf/beans-scopes-exercise/README.md new file mode 100644 index 00000000..2a7c2a04 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/README.md @@ -0,0 +1,9 @@ +#### Упражнение №1 +- Расставить скоупы над сервисами так, чтобы приложение запустилось и заработало, как указано на странице http://localhost:8080 +- Подсказки в названии сервисов +- Не забываем про proxyMode для некоторых скоупов + +``` +Задать скоуп можно с помощью аннотации @Scope +Возможные значения: singleton, prototype, session, request +``` \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/pom.xml b/2020-11/spring-05-adv-conf/beans-scopes-exercise/pom.xml new file mode 100644 index 00000000..14a587e1 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + beans-scopes-exercise + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java new file mode 100644 index 00000000..a7ce4369 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java @@ -0,0 +1,13 @@ +package ru.otus.example.beansscopesdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BeansScopesDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(BeansScopesDemoApplication.class, args); + } + +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java new file mode 100644 index 00000000..1a309a90 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java @@ -0,0 +1,40 @@ +package ru.otus.example.beansscopesdemo.controllers; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import ru.otus.example.beansscopesdemo.services.GreetingService; + +@Controller +public class GreetingController { + private final GreetingService singletonGreetingService; + private final GreetingService prototypeGreetingService1; + private final GreetingService prototypeGreetingService2; + private final GreetingService sessionGreetingService; + private final GreetingService requestGreetingService; + + public GreetingController(@Qualifier("SingletonGreetingService") GreetingService singletonGreetingService, + @Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService1, + @Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService2, + @Qualifier("SessionGreetingService")GreetingService sessionGreetingService, + @Qualifier("RequestGreetingService")GreetingService requestGreetingService + ) { + this.singletonGreetingService = singletonGreetingService; + this.prototypeGreetingService1 = prototypeGreetingService1; + this.prototypeGreetingService2 = prototypeGreetingService2; + this.sessionGreetingService = sessionGreetingService; + this.requestGreetingService = requestGreetingService; + } + + @GetMapping("/") + public String greetingPage(Model model) { + boolean isFirstGreetingSuccess = prototypeGreetingService1.isFirstGreetingSuccess(); + model.addAttribute("singletonGreeting", singletonGreetingService.greeting()); + model.addAttribute("sessionGreeting", sessionGreetingService.greeting()); + model.addAttribute("requestGreeting", requestGreetingService.greeting()); + model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting()); + model.addAttribute("prototype2Greeting", isFirstGreetingSuccess? prototypeGreetingService2.greeting(): "Пока жду"); + return "index"; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java new file mode 100644 index 00000000..3fff2235 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java @@ -0,0 +1,35 @@ +package ru.otus.example.beansscopesdemo.services; + + +import org.springframework.beans.factory.annotation.Value; + +public abstract class AbstractGreetingServiceImpl implements GreetingService { + + @Value("${greetings.first-greeting}") + private String firstGreeting; + + @Value("${greetings.re-greeting}") + private String reGreeting; + + private boolean isFirstGreetingSuccess; + + public AbstractGreetingServiceImpl() { + this.isFirstGreetingSuccess = false; + } + + @Override + public boolean isFirstGreetingSuccess() { + return isFirstGreetingSuccess; + } + + @Override + public String greeting() { + return currentGreeting(); + } + + private synchronized String currentGreeting() { + String greeting = isFirstGreetingSuccess ? reGreeting : firstGreeting; + isFirstGreetingSuccess = true; + return greeting; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java new file mode 100644 index 00000000..1347ff39 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java @@ -0,0 +1,6 @@ +package ru.otus.example.beansscopesdemo.services; + +public interface GreetingService { + boolean isFirstGreetingSuccess(); + String greeting(); +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java new file mode 100644 index 00000000..73ebdcd6 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java @@ -0,0 +1,7 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.stereotype.Service; + +@Service("PrototypeGreetingService") +public class PrototypeGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java new file mode 100644 index 00000000..4340188e --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java @@ -0,0 +1,7 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.stereotype.Service; + +@Service("RequestGreetingService") +public class RequestGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java new file mode 100644 index 00000000..045b0b5a --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java @@ -0,0 +1,7 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.stereotype.Service; + +@Service("SessionGreetingService") +public class SessionGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java new file mode 100644 index 00000000..b2f792a1 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java @@ -0,0 +1,7 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.stereotype.Service; + +@Service("SingletonGreetingService") +public class SingletonGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/application.yml b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/application.yml new file mode 100644 index 00000000..a18bec79 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/application.yml @@ -0,0 +1,8 @@ +greetings: + first-greeting: Привет + re-greeting: Рад снова тебя видеть + + +logging: + level: + root: ERROR \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/templates/index.html b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/templates/index.html new file mode 100644 index 00000000..3719f228 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-exercise/src/main/resources/templates/index.html @@ -0,0 +1,45 @@ + + + + + Advanced configuration demo + + + +

+

Демонстрация @Scope

+ +

+ +
+ +

+

+

+

+

+ + \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/.gitignore b/2020-11/spring-05-adv-conf/beans-scopes-solution/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/README.md b/2020-11/spring-05-adv-conf/beans-scopes-solution/README.md new file mode 100644 index 00000000..b02345e6 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/README.md @@ -0,0 +1,15 @@ +#### Решение к упражнению №1 + +@Scope("singleton") +SingletonGreetingServiceImpl + +@Scope("prototype") +PrototypeGreetingServiceImpl + +@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) +RequestGreetingServiceImpl + +@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) +SessionGreetingServiceImpl + + diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/pom.xml b/2020-11/spring-05-adv-conf/beans-scopes-solution/pom.xml new file mode 100644 index 00000000..277f5dee --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/pom.xml @@ -0,0 +1,48 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + beans-scopes-solution + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.projectlombok + lombok + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java new file mode 100644 index 00000000..a7ce4369 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/BeansScopesDemoApplication.java @@ -0,0 +1,13 @@ +package ru.otus.example.beansscopesdemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BeansScopesDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(BeansScopesDemoApplication.class, args); + } + +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java new file mode 100644 index 00000000..1a309a90 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/controllers/GreetingController.java @@ -0,0 +1,40 @@ +package ru.otus.example.beansscopesdemo.controllers; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import ru.otus.example.beansscopesdemo.services.GreetingService; + +@Controller +public class GreetingController { + private final GreetingService singletonGreetingService; + private final GreetingService prototypeGreetingService1; + private final GreetingService prototypeGreetingService2; + private final GreetingService sessionGreetingService; + private final GreetingService requestGreetingService; + + public GreetingController(@Qualifier("SingletonGreetingService") GreetingService singletonGreetingService, + @Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService1, + @Qualifier("PrototypeGreetingService")GreetingService prototypeGreetingService2, + @Qualifier("SessionGreetingService")GreetingService sessionGreetingService, + @Qualifier("RequestGreetingService")GreetingService requestGreetingService + ) { + this.singletonGreetingService = singletonGreetingService; + this.prototypeGreetingService1 = prototypeGreetingService1; + this.prototypeGreetingService2 = prototypeGreetingService2; + this.sessionGreetingService = sessionGreetingService; + this.requestGreetingService = requestGreetingService; + } + + @GetMapping("/") + public String greetingPage(Model model) { + boolean isFirstGreetingSuccess = prototypeGreetingService1.isFirstGreetingSuccess(); + model.addAttribute("singletonGreeting", singletonGreetingService.greeting()); + model.addAttribute("sessionGreeting", sessionGreetingService.greeting()); + model.addAttribute("requestGreeting", requestGreetingService.greeting()); + model.addAttribute("prototype1Greeting", prototypeGreetingService1.greeting()); + model.addAttribute("prototype2Greeting", isFirstGreetingSuccess? prototypeGreetingService2.greeting(): "Пока жду"); + return "index"; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java new file mode 100644 index 00000000..3fff2235 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/AbstractGreetingServiceImpl.java @@ -0,0 +1,35 @@ +package ru.otus.example.beansscopesdemo.services; + + +import org.springframework.beans.factory.annotation.Value; + +public abstract class AbstractGreetingServiceImpl implements GreetingService { + + @Value("${greetings.first-greeting}") + private String firstGreeting; + + @Value("${greetings.re-greeting}") + private String reGreeting; + + private boolean isFirstGreetingSuccess; + + public AbstractGreetingServiceImpl() { + this.isFirstGreetingSuccess = false; + } + + @Override + public boolean isFirstGreetingSuccess() { + return isFirstGreetingSuccess; + } + + @Override + public String greeting() { + return currentGreeting(); + } + + private synchronized String currentGreeting() { + String greeting = isFirstGreetingSuccess ? reGreeting : firstGreeting; + isFirstGreetingSuccess = true; + return greeting; + } +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java new file mode 100644 index 00000000..1347ff39 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/GreetingService.java @@ -0,0 +1,6 @@ +package ru.otus.example.beansscopesdemo.services; + +public interface GreetingService { + boolean isFirstGreetingSuccess(); + String greeting(); +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java new file mode 100644 index 00000000..0932a66c --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/PrototypeGreetingServiceImpl.java @@ -0,0 +1,9 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +@Scope("prototype") +@Service("PrototypeGreetingService") +public class PrototypeGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java new file mode 100644 index 00000000..a1559ea3 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/RequestGreetingServiceImpl.java @@ -0,0 +1,11 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.stereotype.Service; +import org.springframework.web.context.WebApplicationContext; + +@Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) +@Service("RequestGreetingService") +public class RequestGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java new file mode 100644 index 00000000..e9ad2d8e --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SessionGreetingServiceImpl.java @@ -0,0 +1,11 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.stereotype.Service; +import org.springframework.web.context.WebApplicationContext; + +@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES) +@Service("SessionGreetingService") +public class SessionGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java new file mode 100644 index 00000000..fb8406a5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/java/ru/otus/example/beansscopesdemo/services/SingletonGreetingServiceImpl.java @@ -0,0 +1,9 @@ +package ru.otus.example.beansscopesdemo.services; + +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +@Scope("singleton") +@Service("SingletonGreetingService") +public class SingletonGreetingServiceImpl extends AbstractGreetingServiceImpl { +} diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/application.yml b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/application.yml new file mode 100644 index 00000000..a18bec79 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/application.yml @@ -0,0 +1,8 @@ +greetings: + first-greeting: Привет + re-greeting: Рад снова тебя видеть + + +logging: + level: + root: ERROR \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/templates/index.html b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/templates/index.html new file mode 100644 index 00000000..3719f228 --- /dev/null +++ b/2020-11/spring-05-adv-conf/beans-scopes-solution/src/main/resources/templates/index.html @@ -0,0 +1,45 @@ + + + + + Advanced configuration demo + + + +

+

Демонстрация @Scope

+ +

+ +
+ +

+

+

+

+

+ + \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/.gitignore b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/README.md b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/README.md new file mode 100644 index 00000000..3b88aa94 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/README.md @@ -0,0 +1,11 @@ +#### Упражнение №4 +Условия вечеринки: +- Алексей придет если condition.alexey-exists=true +- Анна придет если будет Алексей +- Олег придет если включен профиль Oleg +- Петр придет если включен профиль Peter +- Янис придет если condition.yanis-exists=true +- Яна придет если нет Алексея, но есть Янис + +Измените только одну настройку в application.yml, чтобы на вечеринку пришло максимальное количество людей +Напечатать имена пришедших на вечеринку можно с помощью команды "ppm" или "print-party-members" \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/pom.xml b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/pom.xml new file mode 100644 index 00000000..12724ce8 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/pom.xml @@ -0,0 +1,73 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + conditional-and-profiles-exercise + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/ConditionalAndProfilesDemoApplication.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/ConditionalAndProfilesDemoApplication.java new file mode 100644 index 00000000..2aa5c3a4 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/ConditionalAndProfilesDemoApplication.java @@ -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(); + } + +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Alexey.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Alexey.java new file mode 100644 index 00000000..66091903 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Alexey.java @@ -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 "Алексей"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Anna.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Anna.java new file mode 100644 index 00000000..be26e16d --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Anna.java @@ -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 "Аня"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Oleg.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Oleg.java new file mode 100644 index 00000000..2a11f476 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Oleg.java @@ -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 "Олег"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Party.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Party.java new file mode 100644 index 00000000..18a53b5d --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Party.java @@ -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 partyMembers; + + public void printPartyMembers() { + System.out.println("Участники вечеринки:"); + System.out.println(partyMembers.stream().map(Friend::getName).collect(Collectors.joining("\n"))); + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Peter.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Peter.java new file mode 100644 index 00000000..435891a8 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Peter.java @@ -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 "Петр"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yana.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yana.java new file mode 100644 index 00000000..76b0fca3 --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yana.java @@ -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 "Яна"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yanis.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yanis.java new file mode 100644 index 00000000..6dd2c14d --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/Yanis.java @@ -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 "Янис"; + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/base/Friend.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/base/Friend.java new file mode 100644 index 00000000..2d2eb67d --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/base/Friend.java @@ -0,0 +1,5 @@ +package ru.otus.example.conditionalandprofilesdemo.model.base; + +public abstract class Friend { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/conditions/YanaConditions.java b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/conditions/YanaConditions.java new file mode 100644 index 00000000..cf130ebb --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/java/ru/otus/example/conditionalandprofilesdemo/model/conditions/YanaConditions.java @@ -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 { + } +} diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application-Peter.yml b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application-Peter.yml new file mode 100644 index 00000000..aa28b2ce --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application-Peter.yml @@ -0,0 +1,2 @@ +condition: + #yanis-exists: true diff --git a/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application.yml b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application.yml new file mode 100644 index 00000000..50db026d --- /dev/null +++ b/2020-11/spring-05-adv-conf/conditional-and-profiles-exercise/src/main/resources/application.yml @@ -0,0 +1,20 @@ +condition: + alexey-exists: true + yanis-exists: false + +spring: + profiles: + #Доступные профили: Oleg и Peter + active: + +logging: + level: + root: ERROR + + +--- +spring: + profiles: Peter + +condition: + yanis-exists: true diff --git a/2020-11/spring-05-adv-conf/pom.xml b/2020-11/spring-05-adv-conf/pom.xml new file mode 100644 index 00000000..6eb42cdf --- /dev/null +++ b/2020-11/spring-05-adv-conf/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + ru.otus + advanced-config-class-work + 1.0 + + pom + + + application-events-demo + beans-lifecycle-exercise + beans-scopes-exercise + beans-scopes-solution + conditional-and-profiles-exercise + test-configuration-exercise-1 + test-configuration-exercise-2 + test-configuration-exercise-3 + test-configuration-solution-1 + test-configuration-solution-2 + test-configuration-solution-3 + + diff --git a/2020-11/spring-05-adv-conf/spring-shell.log b/2020-11/spring-05-adv-conf/spring-shell.log new file mode 100644 index 00000000..7bc7afeb --- /dev/null +++ b/2020-11/spring-05-adv-conf/spring-shell.log @@ -0,0 +1,9 @@ +1583937635277:p +1583937640796:l +1583937643194:p +1592323788784:l +1592323792080:p +1607631288271:l +1607631289245:p +1607631305842:l +1607631307404:p diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/README.md b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/README.md new file mode 100644 index 00000000..7f84475f --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/README.md @@ -0,0 +1,9 @@ +#### Упражнение №5. Должен остаться только один + +С помощью вложенных конфигураций сделать так, +чтобы прошел NestedConfigurationDemoTest +(family должно содержать только собаку) + +- Сделать вложенный класс конфигурации. Не забываем про static +- Выбрать тип конфигурации (@Configuration/@TestConfiguration) +- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы) \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/pom.xml new file mode 100644 index 00000000..45642bbd --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-exercise-1 + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java new file mode 100644 index 00000000..6f1795d0 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java @@ -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 family; + + @DisplayName(" содержать только собаку ") + @Test + void shouldContainOnlyDog() { + assertThat(family).containsOnlyKeys("dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java new file mode 100644 index 00000000..66932497 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java @@ -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 family; + + @DisplayName(" содержать маму, сына и собаку ") + @Test + void shouldContainAllFamilyExceptFather() { + assertThat(family).containsOnlyKeys("mother", "son", "dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/README.md b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/README.md new file mode 100644 index 00000000..1d6f9395 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/README.md @@ -0,0 +1,9 @@ +#### Упражнение №6. Вернуть отца в семью + +С помощью вложенных конфигураций сделать так, +чтобы прошел NestedTestConfigurationDemoTest +(family должно содержать маму, папу, сына и собаку) + +- Сделать вложенный класс конфигурации. Не забываем про static +- Выбрать тип конфигурации (@Configuration/@TestConfiguration) +- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы) \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/pom.xml new file mode 100644 index 00000000..e37b92a7 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-exercise-2 + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java new file mode 100644 index 00000000..a4b13be6 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java @@ -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 family; + + @DisplayName(" содержать маму, папу, сына и собаку ") + @Test + void shouldContainAllFamilyWithFather() { + assertThat(family).containsOnlyKeys("mother", "father", "son", "dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/README.md b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/README.md new file mode 100644 index 00000000..ca65c8aa --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/README.md @@ -0,0 +1,8 @@ +#### Упражнение №7. Вся семья, но без собаки + +С помощью внешней конфигурации сделать так, +чтобы прошел SpringBootTestWithExternalLimitationDemoTest +(family должно содержать маму, папу и сына) + +- Над TestSpringBootConfiguration повесить @SpringBootConfiguration +- С помощью @Bean/@ComponentScan добавить в контекст нужный бин(ы) \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/pom.xml new file mode 100644 index 00000000..96d6f819 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-exercise-3 + 0.0.1-SNAPSHOT + test-configuration-demo + Test configuration demo + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java new file mode 100644 index 00000000..2091efeb --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java @@ -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 family; + + @DisplayName(" содержать маму, папу и сына") + @Test + void shouldContainAllFamilyExceptFather() { + assertThat(family).containsOnlyKeys("mother", "father", "son"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java new file mode 100644 index 00000000..526a3c5f --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-exercise-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java @@ -0,0 +1,4 @@ +package ru.otus.example.testconfigurationdemo.demo; + +public class TestSpringBootConfiguration { +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-solution-1/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/README.md b/2020-11/spring-05-adv-conf/test-configuration-solution-1/README.md new file mode 100644 index 00000000..68ebd53c --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/README.md @@ -0,0 +1,7 @@ +#### Решение упражнения №5 +По заданию нужно ограничить контекст одним бином с помощью вложенной конфигурации. +@TestConfiguration не ограничивает, а дополняет контекст новыми бинами или подменяет те, что уже там. +Соответственно выбираем @Configuration. Вешаем его над вложенным статическим классом. +Т.к. Dog является @Component, то для его размещения в контексте подойдет любое из: +- @ComponentScan("ru.otus.example.testconfigurationdemo.family.pets") над конфигурацией +- Создание бина, через метод с аннотацией @Bean внутри конфигурации \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-solution-1/pom.xml new file mode 100644 index 00000000..738e32f9 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-solution-1 + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java new file mode 100644 index 00000000..7d496500 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedConfigurationDemoTest.java @@ -0,0 +1,41 @@ +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 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 +public class NestedConfigurationDemoTest { + + @ComponentScan("ru.otus.example.testconfigurationdemo.family.pets") + @Configuration + static class NestedConfiguration { +/* + @Bean + FamilyMember dog() { + return new Dog(); + } +*/ + } + + @Autowired + private Map family; + + @DisplayName(" содержать только собаку ") + @Test + void shouldContainOnlyDog() { + assertThat(family).containsOnlyKeys("dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java new file mode 100644 index 00000000..66932497 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-1/src/test/java/ru/otus/example/testconfigurationdemo/demo/PlainSpringBootTestDemoTest.java @@ -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 family; + + @DisplayName(" содержать маму, сына и собаку ") + @Test + void shouldContainAllFamilyExceptFather() { + assertThat(family).containsOnlyKeys("mother", "son", "dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-solution-2/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/README.md b/2020-11/spring-05-adv-conf/test-configuration-solution-2/README.md new file mode 100644 index 00000000..988e8eab --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/README.md @@ -0,0 +1,8 @@ +#### Решение упражнения №6 +По заданию нужно добавить в контекст бин, которого там нет, с помощью вложенной конфигурации. + +Эту проблему можно решить с помощью @Configuration, фактически заново сформировав контекст. +При этом @TestConfiguration может дополнить существующий. Соответственно выбираем его и вешаем над вложенным статическим классом. + +Т.к. Father НЕ является @Component, то для его размещения в контексте нужно его создать, +через метод с аннотацией @Bean внутри тестовой конфигурации \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-solution-2/pom.xml new file mode 100644 index 00000000..6231aa82 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-solution-2 + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java new file mode 100644 index 00000000..33bfe11e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-2/src/test/java/ru/otus/example/testconfigurationdemo/demo/NestedTestConfigurationDemoTest.java @@ -0,0 +1,37 @@ +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 ru.otus.example.testconfigurationdemo.family.FamilyMember; +import ru.otus.example.testconfigurationdemo.family.parents.Father; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +@DisplayName("В NestedTestConfigurationDemoTest семья должна ") +@SpringBootTest +public class NestedTestConfigurationDemoTest { + + @TestConfiguration + static class NestedTestConfiguration { + @Bean + FamilyMember father() { + return new Father(); + } + } + + @Autowired + private Map family; + + @DisplayName(" содержать маму, папу, сына и собаку ") + @Test + void shouldContainAllFamilyWithFather() { + assertThat(family).containsOnlyKeys("mother", "father", "son", "dog"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/.gitignore b/2020-11/spring-05-adv-conf/test-configuration-solution-3/.gitignore new file mode 100644 index 00000000..789ddc9e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/.gitignore @@ -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 \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/README.md b/2020-11/spring-05-adv-conf/test-configuration-solution-3/README.md new file mode 100644 index 00000000..c53d9710 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/README.md @@ -0,0 +1,12 @@ +#### Решение упражнения №7 +По заданию нужно ограничить контекст заданными бинами, с помощью внешней конфигурации. + +Вешаем @SpringBootConfiguration над внешним классом TestSpringBootConfiguration. +Теперь сканирование конфигураций не уйдет далше него. Ни одного бина не найдется. +Значит нужно формировать контекст самим. + +По заданию в нем должны быть бины типов Mother, Son и Father. Первые два являются @Component. +Их можно разместить в контексте просканировав соответствующие пакеты с помощью @ComponentScan + +Т.к. Father НЕ является @Component, то для его размещения в контексте нужно его создать, +через метод с аннотацией @Bean внутри тестовой конфигурации \ No newline at end of file diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/pom.xml b/2020-11/spring-05-adv-conf/test-configuration-solution-3/pom.xml new file mode 100644 index 00000000..30e1372e --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/pom.xml @@ -0,0 +1,74 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.4.0 + + + + ru.otus.example + test-configuration-solution-3 + 0.0.1-SNAPSHOT + + + 11 + 11 + 11 + + + + + org.springframework.boot + spring-boot-starter + + + org.projectlombok + lombok + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + junit + junit + + + + + + org.junit.jupiter + junit-jupiter-api + ${junit-jupiter.version} + test + + + + org.junit.jupiter + junit-jupiter-engine + ${junit-jupiter.version} + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java new file mode 100644 index 00000000..e5dd8341 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/TestConfigurationDemoApplication.java @@ -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); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java new file mode 100644 index 00000000..1685a4cc --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/FamilyMember.java @@ -0,0 +1,5 @@ +package ru.otus.example.testconfigurationdemo.family; + +public abstract class FamilyMember { + public abstract String getName(); +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java new file mode 100644 index 00000000..f3da3289 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/childrens/Son.java @@ -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 "Сын"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java new file mode 100644 index 00000000..d531bda5 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Father.java @@ -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 "Папа"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java new file mode 100644 index 00000000..d9b97b75 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/parents/Mother.java @@ -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 "Мама"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java new file mode 100644 index 00000000..3e436c12 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/main/java/ru/otus/example/testconfigurationdemo/family/pets/Dog.java @@ -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 "Собака"; + } +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java new file mode 100644 index 00000000..2091efeb --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/SpringBootTestWithExternalLimitationDemoTest.java @@ -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 family; + + @DisplayName(" содержать маму, папу и сына") + @Test + void shouldContainAllFamilyExceptFather() { + assertThat(family).containsOnlyKeys("mother", "father", "son"); + } + +} diff --git a/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java new file mode 100644 index 00000000..a92ce439 --- /dev/null +++ b/2020-11/spring-05-adv-conf/test-configuration-solution-3/src/test/java/ru/otus/example/testconfigurationdemo/demo/TestSpringBootConfiguration.java @@ -0,0 +1,17 @@ +package ru.otus.example.testconfigurationdemo.demo; + +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import ru.otus.example.testconfigurationdemo.family.FamilyMember; +import ru.otus.example.testconfigurationdemo.family.parents.Father; + +@ComponentScan({"ru.otus.example.testconfigurationdemo.family.parents", + "ru.otus.example.testconfigurationdemo.family.childrens"}) +@SpringBootConfiguration +public class TestSpringBootConfiguration { + @Bean + FamilyMember father() { + return new Father(); + } +} diff --git a/examples/unit-testing-pure-spring-parallel/pom.xml b/examples/unit-testing-pure-spring-parallel/pom.xml index 7580071d..836c52c1 100644 --- a/examples/unit-testing-pure-spring-parallel/pom.xml +++ b/examples/unit-testing-pure-spring-parallel/pom.xml @@ -13,11 +13,11 @@ 11 11 11 - 1.18.10 - 5.2.9.RELEASE + 1.18.16 + 5.3.1 5.7.0 - 2.28.2 - 3.13.2 + 3.6.28 + 3.18.1 diff --git a/examples/unit-testing-pure-spring/pom.xml b/examples/unit-testing-pure-spring/pom.xml index 43bd68de..13b5c158 100644 --- a/examples/unit-testing-pure-spring/pom.xml +++ b/examples/unit-testing-pure-spring/pom.xml @@ -14,10 +14,10 @@ 11 11 1.18.10 - 5.2.1.RELEASE - 5.5.2 - 2.28.2 - 3.13.2 + 5.3.1 + 5.7.0 + 3.6.28 + 3.18.1 diff --git a/examples/unit-testing-spring-boot/pom.xml b/examples/unit-testing-spring-boot/pom.xml index 129d75a3..0d6f8151 100644 --- a/examples/unit-testing-spring-boot/pom.xml +++ b/examples/unit-testing-spring-boot/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.2.1.RELEASE + 2.4.0 diff --git a/examples/unit-testing-spring-boot/src/test/java/ru/otus/testingExample/springboot/services/GreetingServiceImplTest.java b/examples/unit-testing-spring-boot/src/test/java/ru/otus/testingExample/springboot/services/GreetingServiceImplTest.java index 0423c382..f7dd5190 100644 --- a/examples/unit-testing-spring-boot/src/test/java/ru/otus/testingExample/springboot/services/GreetingServiceImplTest.java +++ b/examples/unit-testing-spring-boot/src/test/java/ru/otus/testingExample/springboot/services/GreetingServiceImplTest.java @@ -3,7 +3,6 @@ package ru.otus.testingExample.springboot.services; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean;