Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PriorityBeanFactoryTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.bootstrap;
  21. import javax.annotation.Priority;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
  25. import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
  26. import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  27. import org.springframework.beans.factory.support.RootBeanDefinition;
  28. import org.springframework.core.annotation.AnnotationAwareOrderComparator;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  31. public class PriorityBeanFactoryTest {
  32. private final DefaultListableBeanFactory parentBeanFactory = new PriorityBeanFactory();
  33. private final DefaultListableBeanFactory beanFactory = new PriorityBeanFactory();
  34. @Before
  35. public void setUp() {
  36. // needed to support autowiring with @Inject
  37. beanFactory.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
  38. //needed to read @Priority
  39. beanFactory.setDependencyComparator(new AnnotationAwareOrderComparator());
  40. beanFactory.setParentBeanFactory(parentBeanFactory);
  41. }
  42. @Test
  43. public void give_priority_to_child_container() {
  44. parentBeanFactory.registerBeanDefinition("A1", new RootBeanDefinition(A1.class));
  45. beanFactory.registerBeanDefinition("A2", new RootBeanDefinition(A2.class));
  46. beanFactory.registerBeanDefinition("B", new RootBeanDefinition(B.class));
  47. assertThat(beanFactory.getBean(B.class).dep.getClass()).isEqualTo(A2.class);
  48. }
  49. @Test
  50. public void follow_priority_annotations() {
  51. parentBeanFactory.registerBeanDefinition("A3", new RootBeanDefinition(A3.class));
  52. beanFactory.registerBeanDefinition("A1", new RootBeanDefinition(A1.class));
  53. beanFactory.registerBeanDefinition("A2", new RootBeanDefinition(A2.class));
  54. beanFactory.registerBeanDefinition("B", new RootBeanDefinition(B.class));
  55. assertThat(beanFactory.getBean(B.class).dep.getClass()).isEqualTo(A3.class);
  56. }
  57. @Test
  58. public void throw_NoUniqueBeanDefinitionException_if_cant_find_single_bean_with_higher_priority() {
  59. beanFactory.registerBeanDefinition("A1", new RootBeanDefinition(A1.class));
  60. beanFactory.registerBeanDefinition("A2", new RootBeanDefinition(A2.class));
  61. beanFactory.registerBeanDefinition("B", new RootBeanDefinition(B.class));
  62. assertThatThrownBy(() -> beanFactory.getBean(B.class))
  63. .hasRootCauseInstanceOf(NoUniqueBeanDefinitionException.class);
  64. }
  65. private static class B {
  66. private final A dep;
  67. public B(A dep) {
  68. this.dep = dep;
  69. }
  70. }
  71. private interface A {
  72. }
  73. private static class A1 implements A {
  74. }
  75. private static class A2 implements A {
  76. }
  77. @Priority(1)
  78. private static class A3 implements A {
  79. }
  80. }