Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CoreExtensionsLoaderTest.java 3.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.core.extension;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Collections;
  23. import java.util.Random;
  24. import java.util.Set;
  25. import java.util.stream.Collectors;
  26. import java.util.stream.IntStream;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import static org.mockito.ArgumentMatchers.any;
  31. import static org.mockito.Mockito.mock;
  32. import static org.mockito.Mockito.verify;
  33. import static org.mockito.Mockito.verifyNoMoreInteractions;
  34. import static org.mockito.Mockito.when;
  35. public class CoreExtensionsLoaderTest {
  36. @Rule
  37. public ExpectedException expectedException = ExpectedException.none();
  38. private CoreExtensionRepository coreExtensionRepository = mock(CoreExtensionRepository.class);
  39. private CoreExtensionsLoader.ServiceLoaderWrapper serviceLoaderWrapper = mock(CoreExtensionsLoader.ServiceLoaderWrapper.class);
  40. private CoreExtensionsLoader underTest = new CoreExtensionsLoader(coreExtensionRepository, serviceLoaderWrapper);
  41. @Test
  42. public void load_has_no_effect_if_there_is_no_ServiceLoader_for_CoreExtension_class() {
  43. when(serviceLoaderWrapper.load(any())).thenReturn(Collections.emptySet());
  44. underTest.load();
  45. verify(serviceLoaderWrapper).load(CoreExtensionsLoader.class.getClassLoader());
  46. verify(coreExtensionRepository).setLoadedCoreExtensions(Collections.emptySet());
  47. verifyNoMoreInteractions(serviceLoaderWrapper, coreExtensionRepository);
  48. }
  49. @Test
  50. public void load_sets_loaded_core_extensions_into_repository() {
  51. Set<CoreExtension> coreExtensions = IntStream.range(0, 1 + new Random().nextInt(5))
  52. .mapToObj(i -> newCoreExtension("core_ext_" + i))
  53. .collect(Collectors.toSet());
  54. when(serviceLoaderWrapper.load(any())).thenReturn(coreExtensions);
  55. underTest.load();
  56. verify(serviceLoaderWrapper).load(CoreExtensionsLoader.class.getClassLoader());
  57. verify(coreExtensionRepository).setLoadedCoreExtensions(coreExtensions);
  58. verifyNoMoreInteractions(serviceLoaderWrapper, coreExtensionRepository);
  59. }
  60. @Test
  61. public void load_fails_with_ISE_if_multiple_core_extensions_declare_same_name() {
  62. Set<CoreExtension> coreExtensions = ImmutableSet.of(newCoreExtension("a"), newCoreExtension("a"));
  63. when(serviceLoaderWrapper.load(any())).thenReturn(coreExtensions);
  64. expectedException.expect(IllegalStateException.class);
  65. expectedException.expectMessage("Multiple core extensions declare the following names: a");
  66. underTest.load();
  67. }
  68. @Test
  69. public void load_fails_with_ISE_if_multiple_core_extensions_declare_same_names() {
  70. Set<CoreExtension> coreExtensions = ImmutableSet.of(newCoreExtension("a"), newCoreExtension("a"), newCoreExtension("b"), newCoreExtension("b"));
  71. when(serviceLoaderWrapper.load(any())).thenReturn(coreExtensions);
  72. expectedException.expect(IllegalStateException.class);
  73. expectedException.expectMessage("Multiple core extensions declare the following names: a, b");
  74. underTest.load();
  75. }
  76. private static CoreExtension newCoreExtension(String name) {
  77. CoreExtension res = mock(CoreExtension.class);
  78. when(res.getName()).thenReturn(name);
  79. return res;
  80. }
  81. }