Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

CoreExtensionRepositoryImplTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 com.tngtech.java.junit.dataprovider.DataProvider;
  23. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  24. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  25. import java.util.Set;
  26. import java.util.stream.Collectors;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.junit.runner.RunWith;
  31. import static java.util.Collections.emptySet;
  32. import static java.util.Collections.singleton;
  33. import static org.assertj.core.api.Assertions.assertThat;
  34. @RunWith(DataProviderRunner.class)
  35. public class CoreExtensionRepositoryImplTest {
  36. @Rule
  37. public ExpectedException expectedException = ExpectedException.none();
  38. private CoreExtensionRepositoryImpl underTest = new CoreExtensionRepositoryImpl();
  39. @Test
  40. public void loadedCoreExtensions_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
  41. expectRepositoryNotInitializedISE();
  42. underTest.loadedCoreExtensions();
  43. }
  44. @Test
  45. @UseDataProvider("coreExtensionsSets")
  46. public void loadedCoreExtensions_returns_CoreExtensions_from_setLoadedCoreExtensions(Set<CoreExtension> coreExtensions) {
  47. underTest.setLoadedCoreExtensions(coreExtensions);
  48. assertThat(underTest.loadedCoreExtensions().collect(Collectors.toSet()))
  49. .isEqualTo(coreExtensions);
  50. }
  51. @Test
  52. public void setLoadedCoreExtensions_fails_with_NPE_if_argument_is_null() {
  53. expectedException.expect(NullPointerException.class);
  54. underTest.setLoadedCoreExtensions(null);
  55. }
  56. @Test
  57. @UseDataProvider("coreExtensionsSets")
  58. public void setLoadedCoreExtensions_fails_with_ISE_if_called_twice(Set<CoreExtension> coreExtensions) {
  59. underTest.setLoadedCoreExtensions(coreExtensions);
  60. expectedException.expect(IllegalStateException.class);
  61. expectedException.expectMessage("Repository has already been initialized");
  62. underTest.setLoadedCoreExtensions(coreExtensions);
  63. }
  64. @Test
  65. public void installed_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
  66. CoreExtension coreExtension = newCoreExtension();
  67. expectRepositoryNotInitializedISE();
  68. underTest.installed(coreExtension);
  69. }
  70. @Test
  71. @UseDataProvider("coreExtensionsSets")
  72. public void installed_fails_with_IAE_if_CoreExtension_is_not_loaded(Set<CoreExtension> coreExtensions) {
  73. underTest.setLoadedCoreExtensions(coreExtensions);
  74. CoreExtension coreExtension = newCoreExtension();
  75. expectedException.expect(IllegalArgumentException.class);
  76. expectedException.expectMessage("Specified CoreExtension has not been loaded first");
  77. underTest.installed(coreExtension);
  78. }
  79. @Test
  80. @UseDataProvider("coreExtensionsSets")
  81. public void installed_fails_with_NPE_if_CoreExtension_is_null(Set<CoreExtension> coreExtensions) {
  82. underTest.setLoadedCoreExtensions(coreExtensions);
  83. expectedException.expect(NullPointerException.class);
  84. expectedException.expectMessage("coreExtension can't be null");
  85. underTest.installed(null);
  86. }
  87. @Test
  88. public void isInstalled_fails_with_ISE_if_called_before_setLoadedCoreExtensions() {
  89. expectRepositoryNotInitializedISE();
  90. underTest.isInstalled("foo");
  91. }
  92. @Test
  93. @UseDataProvider("coreExtensionsSets")
  94. public void isInstalled_returns_false_for_not_loaded_CoreExtension(Set<CoreExtension> coreExtensions) {
  95. underTest.setLoadedCoreExtensions(coreExtensions);
  96. assertThat(underTest.isInstalled("not loaded")).isFalse();
  97. }
  98. @Test
  99. public void isInstalled_returns_false_for_loaded_but_not_installed_CoreExtension() {
  100. CoreExtension coreExtension = newCoreExtension();
  101. underTest.setLoadedCoreExtensions(singleton(coreExtension));
  102. assertThat(underTest.isInstalled(coreExtension.getName())).isFalse();
  103. }
  104. @Test
  105. public void isInstalled_returns_true_for_loaded_and_installed_CoreExtension() {
  106. CoreExtension coreExtension = newCoreExtension();
  107. underTest.setLoadedCoreExtensions(singleton(coreExtension));
  108. underTest.installed(coreExtension);
  109. assertThat(underTest.isInstalled(coreExtension.getName())).isTrue();
  110. }
  111. private void expectRepositoryNotInitializedISE() {
  112. expectedException.expect(IllegalStateException.class);
  113. expectedException.expectMessage("Repository has not been initialized yet");
  114. }
  115. @DataProvider
  116. public static Object[][] coreExtensionsSets() {
  117. return new Object[][] {
  118. {emptySet()},
  119. {singleton(newCoreExtension())},
  120. {ImmutableSet.of(newCoreExtension(), newCoreExtension())},
  121. };
  122. }
  123. private static int nameCounter = 0;
  124. private static CoreExtension newCoreExtension() {
  125. String name = "name_" + nameCounter;
  126. nameCounter++;
  127. return newCoreExtension(name);
  128. }
  129. private static CoreExtension newCoreExtension(String name) {
  130. return new CoreExtension() {
  131. @Override
  132. public String getName() {
  133. return name;
  134. }
  135. @Override
  136. public void load(Context context) {
  137. throw new UnsupportedOperationException("load should not be called");
  138. }
  139. };
  140. }
  141. }